Added support for Ogg encoding
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
2fd5e32855
commit
f88fdcb740
30
libflac.go
30
libflac.go
|
@ -393,6 +393,36 @@ func NewEncoder(name string, channels int, depth int, rate int) (e *Encoder, err
|
|||
return
|
||||
}
|
||||
|
||||
// NewEncoderOgg creates a new Encoder object.
|
||||
func NewEncoderOgg(name string, channels int, depth int, rate int) (e *Encoder, err error) {
|
||||
if channels == 0 {
|
||||
return nil, errors.New("channels must be greater than 0")
|
||||
}
|
||||
if !(depth == 16 || depth == 24) {
|
||||
return nil, errors.New("depth must be 16 or 24")
|
||||
}
|
||||
e = new(Encoder)
|
||||
e.e = C.FLAC__stream_encoder_new()
|
||||
if e.e == nil {
|
||||
return nil, errors.New("failed to create decoder")
|
||||
}
|
||||
encoderPtrs.add(e)
|
||||
c := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(c))
|
||||
runtime.SetFinalizer(e, (*Encoder).Close)
|
||||
C.FLAC__stream_encoder_set_channels(e.e, C.uint(channels))
|
||||
C.FLAC__stream_encoder_set_bits_per_sample(e.e, C.uint(depth))
|
||||
C.FLAC__stream_encoder_set_sample_rate(e.e, C.uint(rate))
|
||||
status := C.FLAC__stream_encoder_init_ogg_file(e.e, c, nil, nil)
|
||||
if status != C.FLAC__STREAM_ENCODER_INIT_STATUS_OK {
|
||||
return nil, errors.New("failed to open file")
|
||||
}
|
||||
e.Channels = channels
|
||||
e.Depth = depth
|
||||
e.Rate = rate
|
||||
return
|
||||
}
|
||||
|
||||
//export encoderWriteCallback
|
||||
func encoderWriteCallback(e *C.FLAC__StreamEncoder, buffer *C.FLAC__byte, bytes C.size_t, samples, current_frame C.unsigned, data unsafe.Pointer) C.FLAC__StreamEncoderWriteStatus {
|
||||
encoder := encoderPtrs.get(e)
|
||||
|
|
|
@ -237,6 +237,37 @@ func TestEncode(t *testing.T) {
|
|||
os.Remove(fileName)
|
||||
}
|
||||
|
||||
func TestEncodeOgg(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
e, err := NewEncoder("not-existdir/foo.ogg", 2, 24, 48000)
|
||||
a.Equal(e, (*Encoder)(nil), "encoder is nil")
|
||||
a.NotNil(err, "err is not nil")
|
||||
|
||||
fileName := "testdata/test.ogg"
|
||||
|
||||
e, err = NewEncoder(fileName, 2, 24, 48000)
|
||||
|
||||
a.Equal(err, nil, "err is nil")
|
||||
|
||||
f := Frame{Channels: 1, Depth: 24, Rate: 48000}
|
||||
|
||||
err = e.WriteFrame(f)
|
||||
|
||||
a.Error(err, "channels mismatch")
|
||||
|
||||
f.Channels = 2
|
||||
f.Buffer = make([]int32, 2*100)
|
||||
|
||||
err = e.WriteFrame(f)
|
||||
|
||||
a.Equal(err, nil, "frame encoded")
|
||||
|
||||
e.Close()
|
||||
|
||||
os.Remove(fileName)
|
||||
}
|
||||
|
||||
func TestRoundTrip(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
|
|
Loading…
Reference in a new issue