Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62,462 changes: 62,462 additions & 0 deletions wave/input.txt

Large diffs are not rendered by default.

8,460 changes: 8,460 additions & 0 deletions wave/maybe-next-time-in.txt

Large diffs are not rendered by default.

62,446 changes: 62,446 additions & 0 deletions wave/newoutput.txt

Large diffs are not rendered by default.

62,447 changes: 62,447 additions & 0 deletions wave/output.txt

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions wave/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
maxValues = map[int]int{
8: math.MaxInt8,
16: math.MaxInt16,
24: math.MaxInt32 >> 8,
32: math.MaxInt32,
64: math.MaxInt64,
}
Expand Down Expand Up @@ -143,6 +144,18 @@ func readData(b []byte, wfmt WaveFmt) WaveData {

start := 36 + wfmt.ExtraParamSize
subchunk2ID := b[start : start+4]

if !bytes.Equal(subchunk2ID, []byte{0x64, 0x61, 0x74, 0x61}) {
// some files put "unknown" chunks between FMT and DATA, we can manually
// seek at this point to see if "data" does appear somewhere..
for i := 0; i < len(b)-4; i++ {
if bytes.Equal(b[i:i+4], []byte{0x64, 0x61, 0x74, 0x61}) {
subchunk2ID = b[i : i+4]
break
}
}

}
wd.Subchunk2ID = subchunk2ID

subsize := bits32ToInt(b[start+4 : start+8])
Expand Down Expand Up @@ -233,6 +246,7 @@ func readFmt(b []byte) WaveFmt {
if subchunksize != 16 {
// only for compressed files (non-PCM)
extraSize := bits16ToInt(b[36:38])
//wfmt.ExtraParamSize = extraSize
wfmt.ExtraParamSize = extraSize
wfmt.ExtraParams = b[38 : 38+extraSize]
}
Expand Down
11 changes: 10 additions & 1 deletion wave/reader_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wave

import (
"bytes"
"runtime/debug"
"testing"
)
Expand Down Expand Up @@ -95,7 +96,7 @@ func TestScaleFrames(t *testing.T) {
func TestJunkChunkFile(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Fatalf("Should not have panic'd reading JUNK files\n%v", string(debug.Stack()))
t.Fatalf("Should not have panic'd reading JUNK files\n%v\n%v", string(debug.Stack()), r)
}
}()

Expand All @@ -113,4 +114,12 @@ func TestJunkChunkFile(t *testing.T) {
if wav.NumChannels != 2 {
t.Fatalf("Expected 2 channels, got: %v", wav.NumChannels)
}

if !bytes.Equal(wav.WaveFmt.Subchunk1ID, []byte{0x66, 0x6d, 0x74, 0x20}) {
t.Fatalf("Expected Subchunk1ID to contain 'fmt'")
}

if !bytes.Equal(wav.WaveData.Subchunk2ID, []byte{0x64, 0x61, 0x74, 0x61}) {
t.Fatalf("Expected Subchunk2ID to contain 'data', but got %x", wav.WaveData.Subchunk2ID)
}
}
14 changes: 14 additions & 0 deletions wave/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
// intsToBytesFm to map X-bit int to byte functions
intsToBytesFm = map[int]intsToBytesFunc{
16: int16ToBytes,
24: int24ToBytes,
32: int32ToBytes,
}
)
Expand Down Expand Up @@ -71,6 +72,14 @@ func int16ToBytes(i int) []byte {
return b
}

func int24ToBytes(i int) []byte {
b := make([]byte, 3)
b[0] = byte(i & 0xff)
b[1] = byte((i >> 8) & 0xff)
b[2] = byte((i >> 16) & 0xff)
return b
}

func int32ToBytes(i int) []byte {
b := make([]byte, 4)
in := uint32(i)
Expand Down Expand Up @@ -143,6 +152,7 @@ func fmtToBytes(wfmt WaveFmt) []byte {
br := int32ToBytes(wfmt.ByteRate)
blockalign := int16ToBytes(wfmt.BlockAlign)
bitsPerSample := int16ToBytes(wfmt.BitsPerSample)
extraParamSize := int16ToBytes(wfmt.ExtraParamSize)

b = append(b, wfmt.Subchunk1ID...)
b = append(b, subchunksize...)
Expand All @@ -153,6 +163,10 @@ func fmtToBytes(wfmt WaveFmt) []byte {
b = append(b, blockalign...)
b = append(b, bitsPerSample...)

// extra params if present
b = append(b, extraParamSize...)
b = append(b, wfmt.ExtraParams...)

return b
}

Expand Down
13 changes: 13 additions & 0 deletions wave/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ func TestWriteWave(t *testing.T) {
t.Fatalf("Should be able to write file: %v", err)
}
}

// TestWriteWave reads wave file and writes it, ensuring nothing is different between the two
func TestWrite24BitWave(t *testing.T) {
goldenfile := "./golden/24bit.wav"
wav, err := ReadWaveFile(goldenfile)
if err != nil {
t.Fatalf("Should be able to read 24-bit wave file: %v", err)
}

if err := WriteFrames(wav.Frames, wav.WaveFmt, "output24.wav"); err != nil {
t.Fatalf("Should be able to write file: %v", err)
}
}