Compare commits

...

2 commits

Author SHA1 Message Date
DataHoarder 3d5460c48a
Fix TestCodecFloat32
Some checks failed
continuous-integration/drone/push Build is failing
2022-07-21 15:06:34 +02:00
DataHoarder d0c3e3e655
Add Channels and Info methods on decoder 2022-07-21 15:06:17 +02:00
2 changed files with 65 additions and 5 deletions

View file

@ -86,6 +86,8 @@ func TestCodecFloat32(t *testing.T) {
}
addSineFloat32(pcm, SAMPLE_RATE, G4)
err = enc.EncodeFloat32(pcm)
enc.Close()
if err != nil {
t.Fatalf("Couldn't encode data: %v", err)
}
@ -95,11 +97,16 @@ func TestCodecFloat32(t *testing.T) {
}
// TODO: Uh-oh.. it looks like I forgot to put a data = data[:n] here, yet
// the test is not failing. Why?
n, err := dec.ReadFloat32(pcm)
if err != nil {
t.Fatalf("Couldn't decode data: %v", err)
var out []float32
buf := make([]float32, 120*SAMPLE_RATE)
for {
n, err := dec.ReadFloat32(pcm)
if err != nil {
break
}
out = append(out, buf[:n]...)
}
if len(pcm) != n {
t.Fatalf("Length mismatch: %d samples in, %d out", len(pcm), n)
if len(out) != len(pcm) {
t.Fatalf("Length mismatch: %d samples in, %d out", len(out), len(pcm))
}
}

View file

@ -115,6 +115,59 @@ func (s *Stream) Init(read io.Reader) error {
return nil
}
const ChannelCountMax = int(C.OPUS_CHANNEL_COUNT_MAX)
// Information a mapped OpusHead struct, see https://opus-codec.org/docs/opusfile_api-0.5/structOpusHead.html
type Information struct {
Version int
ChannelCount int
PreSkip uint
InputSampleRate uint
OutputGain int
MappingFamily int
StreamCount int
CoupledCount int
Mapping [ChannelCountMax]uint8
}
//Info Returns the information of the current link
func (s *Stream) Info() *Information {
if s.oggfile == nil {
return nil
}
head := C.op_head(s.oggfile, C.int(-1))
if head == nil {
return nil
}
info := &Information{
Version: int(head.version),
ChannelCount: int(head.channel_count),
PreSkip: uint(head.pre_skip),
InputSampleRate: uint(head.input_sample_rate),
OutputGain: int(head.output_gain),
MappingFamily: int(head.mapping_family),
StreamCount: int(head.stream_count),
CoupledCount: int(head.coupled_count),
}
copy(info.Mapping[:], unsafe.Slice((*uint8)(&head.mapping[0]), ChannelCountMax))
return info
}
//Channels Returns the channel count of the current link
func (s *Stream) Channels() int {
if s.oggfile == nil {
return 0
}
return int(C.op_channel_count(s.oggfile, C.int(-1)))
}
// Read a chunk of raw opus data from the stream and decode it. Returns the
// number of decoded samples per channel. This means that a dual channel
// (stereo) feed will have twice as many samples as the value returned.