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 25a1bf5
Show file tree
Hide file tree
Showing 12 changed files with 345 additions and 254 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.13
require (
github.com/blackjack/webcam v0.0.0-20200313125108-10ed912a8539
github.com/jfreymuth/pulse v0.0.0-20200817093420-a82ccdb5e8aa
github.com/lherman-cs/opus v0.0.0-20200223204610-6a4b98199ea4
github.com/lherman-cs/opus v0.0.0-20200925064139-8edf1852fd1f
github.com/pion/webrtc/v2 v2.2.26
github.com/satori/go.uuid v1.2.0
golang.org/x/image v0.0.0-20200801110659-972c09e46d76
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lherman-cs/opus v0.0.0-20200223204610-6a4b98199ea4 h1:2ydMA2KbxRkYmIw3R8Me8dn90bejxBR4MKYXJ5THK3I=
github.com/lherman-cs/opus v0.0.0-20200223204610-6a4b98199ea4/go.mod h1:v9KQvlDYMuvlwniumBVMlrB0VHQvyTgxNvaXjPmTmps=
github.com/lherman-cs/opus v0.0.0-20200925064139-8edf1852fd1f h1:xZKyjUoki95rRDQl3mDf20j2WZ+jZaFVzPZO72Jdi4A=
github.com/lherman-cs/opus v0.0.0-20200925064139-8edf1852fd1f/go.mod h1:v9KQvlDYMuvlwniumBVMlrB0VHQvyTgxNvaXjPmTmps=
github.com/lucas-clemente/quic-go v0.7.1-0.20190401152353-907071221cf9 h1:tbuodUh2vuhOVZAdW3NEUvosFHUMJwUNl7jk/VSEiwc=
github.com/lucas-clemente/quic-go v0.7.1-0.20190401152353-907071221cf9/go.mod h1:PpMmPfPKO9nKJ/psF49ESTAGQSdfXxlg1otPbEB2nOw=
github.com/marten-seemann/qtls v0.2.3 h1:0yWJ43C62LsZt08vuQJDK1uC1czUc3FJeCLPoNAI4vA=
Expand Down
4 changes: 2 additions & 2 deletions pkg/codec/opus/opus.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ func (e *encoder) Read(p []byte) (int, error) {

switch b := buff.(type) {
case *wave.Int16Interleaved:
n, err := e.engine.Encode(b.Data, p)
n, err := e.engine.EncodeBytes(b.Data, p, false)
if err != nil {
return n, err
}
return n, nil
case *wave.Float32Interleaved:
n, err := e.engine.EncodeFloat32(b.Data, p)
n, err := e.engine.EncodeBytes(b.Data, p, true)
if err != nil {
return n, err
}
Expand Down
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 25a1bf5

Please sign in to comment.