How to concatenate several wav files?
Closed this issue · 3 comments
olebedev commented
I do it in this way:
func concat(files [][]byte) ([]byte, error) {
if len(files) == 1 {
return files[0], nil
}
r, err := wav.NewReader(bytes.NewReader(files[0]), int64(len(files[0])))
if err != nil {
return nil, err
}
// create virtual fs
fs := memfs.Create()
tmp, err := fs.OpenFile("/tmp.wav", os.O_RDWR|os.O_CREATE, 0777)
n, err := tmp.Write(files[0]) // hack to expand the capacity, needs for memfs
if err != nil {
return nil, err
}
if n != len(files[0]) {
return nil, errors.New("writing failure")
}
if err != nil {
return nil, err
}
defer tmp.Close()
file := r.GetFile()
w, err := file.NewWriter(tmp)
if err != nil {
return nil, err
}
writer, _, err := w.GetDumbWriter()
if err != nil {
return nil, err
}
// copy first file
firstReader, err := r.GetDumbReader()
if err != nil {
return nil, err
}
io.Copy(writer, firstReader)
// only first file, for debugging
// for _, chunk := range files[1:] {
// copy others chunks
// }
w.Close()
tmp.Seek(0, os.SEEK_SET)
b, err := ioutil.ReadAll(tmp)
return b, err
}
But the headers of files are different. What I am doing wrong? Thanks.
Input file: - https://yadi.sk/d/PJE8hfxRrHnVB
Output file: - https://yadi.sk/d/B8q1PBrTrHnVK
cryptix commented
Sorry.. I somehow didn't get an email notification for this issue.
I'll look into it and reply once I have an idea.
cryptix commented
Sorry about this falling into oblivion.. Do you still need my insight here or did you solve it already?
olebedev commented
Oh, I solved the problem via operating over mp3 files instead. No need more. Thank you.