Skip to content

Commit

Permalink
Convert underlying wave buffer to be uint8
Browse files Browse the repository at this point in the history
This follows image package from the standard library.
By having a homogenous data type for storing the samples,
it makes easier to manipulate the raw data in a generic way.
  • Loading branch information
lherman-cs committed Sep 25, 2020
1 parent a44240b commit 444a466
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 249 deletions.
8 changes: 4 additions & 4 deletions pkg/io/audio/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ func NewBuffer(nSamples int) TransformFunc {
case *wave.Int16Interleaved:
ibCopy := *ib
ibCopy.Size.Len = nSamples
n := nSamples * ib.Size.Channels
ibCopy.Data = make([]int16, n)
n := nSamples * ib.Size.Channels * 2
ibCopy.Data = make([]uint8, n)
copy(ibCopy.Data, ib.Data)
ib.Data = ib.Data[n:]
ib.Size.Len -= nSamples
Expand All @@ -76,8 +76,8 @@ func NewBuffer(nSamples int) TransformFunc {
case *wave.Float32Interleaved:
ibCopy := *ib
ibCopy.Size.Len = nSamples
n := nSamples * ib.Size.Channels
ibCopy.Data = make([]float32, n)
n := nSamples * ib.Size.Channels * 4
ibCopy.Data = make([]uint8, n)
copy(ibCopy.Data, ib.Data)
ib.Data = ib.Data[n:]
ib.Size.Len -= nSamples
Expand Down
106 changes: 74 additions & 32 deletions pkg/io/audio/buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,83 @@ import (
)

func TestBuffer(t *testing.T) {
input1 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 1, Channels: 2, SamplingRate: 1234})
input1.SetInt16(0, 0, 1)
input1.SetInt16(0, 1, 2)

input2 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234})
input2.SetInt16(0, 0, 3)
input2.SetInt16(0, 1, 4)
input2.SetInt16(1, 0, 5)
input2.SetInt16(1, 1, 6)
input2.SetInt16(2, 0, 7)
input2.SetInt16(2, 1, 8)

input3 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 2, Channels: 2, SamplingRate: 1234})
input3.SetInt16(0, 0, 9)
input3.SetInt16(0, 1, 10)
input3.SetInt16(1, 0, 11)
input3.SetInt16(1, 1, 12)

input4 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 7, Channels: 2, SamplingRate: 1234})
input4.SetInt16(0, 0, 13)
input4.SetInt16(0, 1, 14)
input4.SetInt16(1, 0, 15)
input4.SetInt16(1, 1, 16)
input4.SetInt16(2, 0, 17)
input4.SetInt16(2, 1, 18)
input4.SetInt16(3, 0, 19)
input4.SetInt16(3, 1, 20)
input4.SetInt16(4, 0, 21)
input4.SetInt16(4, 1, 22)
input4.SetInt16(5, 0, 23)
input4.SetInt16(5, 1, 24)
input4.SetInt16(6, 0, 25)
input4.SetInt16(6, 1, 26)

expected1 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234})
expected1.SetInt16(0, 0, 1)
expected1.SetInt16(0, 1, 2)
expected1.SetInt16(1, 0, 3)
expected1.SetInt16(1, 1, 4)
expected1.SetInt16(2, 0, 5)
expected1.SetInt16(2, 1, 6)

expected2 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234})
expected2.SetInt16(0, 0, 7)
expected2.SetInt16(0, 1, 8)
expected2.SetInt16(1, 0, 9)
expected2.SetInt16(1, 1, 10)
expected2.SetInt16(2, 0, 11)
expected2.SetInt16(2, 1, 12)

expected3 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234})
expected3.SetInt16(0, 0, 13)
expected3.SetInt16(0, 1, 14)
expected3.SetInt16(1, 0, 15)
expected3.SetInt16(1, 1, 16)
expected3.SetInt16(2, 0, 17)
expected3.SetInt16(2, 1, 18)

expected4 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234})
expected4.SetInt16(0, 0, 19)
expected4.SetInt16(0, 1, 20)
expected4.SetInt16(1, 0, 21)
expected4.SetInt16(1, 1, 22)
expected4.SetInt16(2, 0, 23)
expected4.SetInt16(2, 1, 24)

input := []wave.Audio{
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 1, Channels: 2, SamplingRate: 1234},
Data: []int16{1, 2},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234},
Data: []int16{3, 4, 5, 6, 7, 8},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 2, Channels: 2, SamplingRate: 1234},
Data: []int16{9, 10, 11, 12},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 7, Channels: 2, SamplingRate: 1234},
Data: []int16{13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
},
input1,
input2,
input3,
input4,
}
expected := []wave.Audio{
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234},
Data: []int16{1, 2, 3, 4, 5, 6},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234},
Data: []int16{7, 8, 9, 10, 11, 12},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234},
Data: []int16{13, 14, 15, 16, 17, 18},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234},
Data: []int16{19, 20, 21, 22, 23, 24},
},
expected1,
expected2,
expected3,
expected4,
}

trans := NewBuffer(3)
Expand Down
40 changes: 24 additions & 16 deletions pkg/io/audio/mixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,33 @@ import (
)

func TestMixer(t *testing.T) {
input1 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 1, Channels: 2, SamplingRate: 1234})
input1.SetInt16(0, 0, 1)
input1.SetInt16(0, 1, 3)

input2 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234})
input2.SetInt16(0, 0, 2)
input2.SetInt16(0, 1, 4)
input2.SetInt16(1, 0, 3)
input2.SetInt16(1, 1, 5)
input2.SetInt16(2, 0, 4)
input2.SetInt16(2, 1, 6)

expected1 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 1, Channels: 1, SamplingRate: 1234})
expected1.SetInt16(0, 0, 2)

expected2 := wave.NewInt16Interleaved(wave.ChunkInfo{Len: 3, Channels: 1, SamplingRate: 1234})
expected2.SetInt16(0, 0, 3)
expected2.SetInt16(1, 0, 4)
expected2.SetInt16(2, 0, 5)

input := []wave.Audio{
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 1, Channels: 2, SamplingRate: 1234},
Data: []int16{1, 3},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 3, Channels: 2, SamplingRate: 1234},
Data: []int16{2, 4, 3, 5, 4, 6},
},
input1,
input2,
}
expected := []wave.Audio{
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 1, Channels: 1, SamplingRate: 1234},
Data: []int16{2},
},
&wave.Int16Interleaved{
Size: wave.ChunkInfo{Len: 3, Channels: 1, SamplingRate: 1234},
Data: []int16{3, 4, 5},
},
expected1,
expected2,
}

trans := NewChannelMixer(1, &mixer.MonoMixer{})
Expand Down
Loading

0 comments on commit 444a466

Please sign in to comment.