-
Notifications
You must be signed in to change notification settings - Fork 119
/
mixer.go
42 lines (35 loc) · 914 Bytes
/
mixer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package mixer
import (
"errors"
"github.com/pion/mediadevices/pkg/wave"
)
// ChannelMixer mixes audio into specifix channels.
type ChannelMixer interface {
Mix(dst wave.Audio, src wave.Audio) error
}
// MonoMixer mixes channels into monaural audio.
type MonoMixer struct {
}
func (m *MonoMixer) Mix(dst wave.Audio, src wave.Audio) error {
if dst.ChunkInfo().Len != src.ChunkInfo().Len {
return errors.New("buffer size mismatch")
}
dstSetter, ok := dst.(wave.EditableAudio)
if !ok {
return errors.New("destination buffer is not settable")
}
n := src.ChunkInfo().Len
channels := src.ChunkInfo().Channels
dstChannels := dst.ChunkInfo().Channels
for i := 0; i < n; i++ {
var mean int64
for ch := 0; ch < channels; ch++ {
mean += src.At(i, ch).Int()
}
mean /= int64(channels)
for ch := 0; ch < dstChannels; ch++ {
dstSetter.Set(i, ch, wave.Int64Sample(mean))
}
}
return nil
}