-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsample.go
More file actions
45 lines (40 loc) · 1.06 KB
/
sample.go
File metadata and controls
45 lines (40 loc) · 1.06 KB
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
43
44
45
package instrument
import (
"github.com/gotracker/playback/mixing/volume"
"github.com/gotracker/playback/player/feature"
"github.com/gotracker/playback/voice/pcm"
)
func NewSample(data []byte, length int, channels int, format pcm.SampleDataFormat, features []feature.Feature) (pcm.Sample, error) {
sf := format
for _, feat := range features {
switch f := feat.(type) {
case feature.PreConvertSamples:
if f.Enabled {
sf = f.DesiredFormat
}
}
}
if sf == format {
// original format
return pcm.NewSample(data, length, channels, format), nil
}
inSample := pcm.NewSample(data, length, channels, format)
if sf != pcm.SampleDataFormatNative {
// format conversion
outSample, err := pcm.ConvertTo(inSample, sf)
if err != nil {
return nil, err
}
return outSample, nil
}
// native conversion
nativeData := make([]volume.Matrix, 0, length)
for i := 0; i < length; i++ {
d, err := inSample.Read()
if err != nil {
return nil, err
}
nativeData = append(nativeData, d)
}
return pcm.NewSampleNative(nativeData, length, channels), nil
}