99 "github.com/gotracker/gomixing/volume"
1010)
1111
12- // ChannelMixBuffer is a single channel's premixed volume data
13- type ChannelMixBuffer volume.Matrix
14-
1512// SampleMixIn is the parameters for mixing in a sample into a MixBuffer
1613type SampleMixIn struct {
1714 Sample sampling.Sampler
@@ -24,7 +21,7 @@ type SampleMixIn struct {
2421// MixBuffer is a buffer of premixed volume data intended to
2522// be eventually sent out to the sound output device after
2623// conversion to the output format
27- type MixBuffer []ChannelMixBuffer
24+ type MixBuffer []volume. Matrix
2825
2926// C returns a channel and a function that flushes any outstanding mix-ins and closes the channel
3027func (m * MixBuffer ) C () (chan <- SampleMixIn , func ()) {
@@ -46,39 +43,35 @@ func (m *MixBuffer) C() (chan<- SampleMixIn, func()) {
4643func (m * MixBuffer ) MixInSample (d SampleMixIn ) {
4744 pos := d .MixPos
4845 for i := 0 ; i < d .MixLen ; i ++ {
49- sdata := d .Sample .GetSample ()
50- samp := sdata .ApplyInSitu (d .StaticVol )
51- mixed := d .VolMatrix .Apply (samp ... )
52- for c , s := range mixed {
53- (* m )[c ][pos ] += s
54- }
46+ dry := d .Sample .GetSample ()
47+ samp := dry .Apply (d .StaticVol )
48+ mixed := d .VolMatrix .ApplyToMatrix (samp )
49+ (* m )[pos ].Accumulate (mixed )
5550 pos ++
5651 d .Sample .Advance ()
5752 }
5853}
5954
6055// Add will mix in another MixBuffer's data
61- func (m * MixBuffer ) Add (pos int , rhs MixBuffer , volMtx volume.Matrix ) {
62- sdata := make (volume.Matrix , len (* m ))
63- for i := 0 ; i < len (rhs [0 ]); i ++ {
64- for c := 0 ; c < len (rhs ); c ++ {
65- sdata [c ] = rhs [c ][i ]
66- }
67- sd := volMtx .Apply (sdata ... )
68- for c , s := range sd {
69- (* m )[c ][pos + i ] += s
70- }
56+ func (m * MixBuffer ) Add (pos int , rhs * MixBuffer , volMtx volume.Matrix ) {
57+ maxLen := len (* rhs )
58+ for i := 0 ; i < maxLen ; i ++ {
59+ out := volMtx .ApplyToMatrix (volume .Matrix ((* rhs )[i ]))
60+ (* m )[pos + i ].Accumulate (out )
7161 }
7262}
7363
7464// ToRenderData converts a mixbuffer into a byte stream intended to be
7565// output to the output sound device
76- func (m * MixBuffer ) ToRenderData (samples int , bitsPerSample int , mixerVolume volume.Volume ) []byte {
66+ func (m * MixBuffer ) ToRenderData (samples int , bitsPerSample int , channels int , mixerVolume volume.Volume ) []byte {
7767 writer := & bytes.Buffer {}
78- for i := 0 ; i < samples ; i ++ {
79- for _ , buf := range * m {
80- v := buf [i ] * mixerVolume
81- val := v .ToSample (bitsPerSample )
68+ writer .Grow (samples * ((bitsPerSample + 7 ) / 8 ) * channels )
69+ for _ , samp := range * m {
70+ buf := samp .Apply (mixerVolume )
71+ var d volume.StaticMatrix
72+ buf .ToChannels (channels , d [:])
73+ for i := 0 ; i < channels ; i ++ {
74+ val := d [i ].ToSample (bitsPerSample )
8275 _ = binary .Write (writer , binary .LittleEndian , val ) // lint
8376 }
8477 }
@@ -92,10 +85,12 @@ func (m *MixBuffer) ToIntStream(outputChannels int, samples int, bitsPerSample i
9285 for c := range data {
9386 data [c ] = make ([]int32 , samples )
9487 }
95- for i := 0 ; i < samples ; i ++ {
96- for c , buf := range * m {
97- v := buf [i ] * mixerVolume
98- data [c ][i ] = v .ToIntSample (bitsPerSample )
88+ for i , samp := range * m {
89+ buf := samp .Apply (mixerVolume )
90+ var d volume.StaticMatrix
91+ buf .ToChannels (outputChannels , d [:])
92+ for c := 0 ; c < outputChannels ; c ++ {
93+ data [c ][i ] = d [c ].ToIntSample (bitsPerSample )
9994 }
10095 }
10196 return data
@@ -107,8 +102,9 @@ func (m *MixBuffer) ToRenderDataWithBufs(outBuffers [][]byte, samples int, bitsP
107102 pos := 0
108103 onum := 0
109104 out := outBuffers [onum ]
110- for i := 0 ; i < samples ; i ++ {
111- for _ , buf := range * m {
105+ for _ , samp := range * m {
106+ buf := samp .Apply (mixerVolume )
107+ for c := 0 ; c < buf .Channels ; c ++ {
112108 for pos >= len (out ) {
113109 onum ++
114110 if onum > len (outBuffers ) {
@@ -117,8 +113,7 @@ func (m *MixBuffer) ToRenderDataWithBufs(outBuffers [][]byte, samples int, bitsP
117113 out = outBuffers [onum ]
118114 pos = 0
119115 }
120- v := buf [i ] * mixerVolume
121- val := v .ToSample (bitsPerSample )
116+ val := buf .StaticMatrix [c ].ToSample (bitsPerSample )
122117 switch d := val .(type ) {
123118 case int8 :
124119 out [pos ] = uint8 (d )
0 commit comments