Added Ogg encoding to alternate API methods
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
DataHoarder 2022-04-17 20:04:34 +02:00
parent f88fdcb740
commit 75c5c45579
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -512,6 +512,48 @@ func NewEncoderWriteSeeker(writer FlacWriter, channels int, depth int, rate int,
return
}
// NewEncoderWriteSeekerOgg creates a new Encoder object from a FlacWriter.
func NewEncoderWriteSeekerOgg(writer FlacWriter, channels int, depth int, rate int, compressionLevel int, streamable bool, blockSize 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)
e.writer = writer
e.writeSeeker = writer
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))
C.FLAC__stream_encoder_set_compression_level(e.e, C.uint(compressionLevel))
C.FLAC__stream_encoder_set_blocksize(e.e, C.uint(blockSize))
if streamable {
C.FLAC__stream_encoder_set_streamable_subset(e.e, C.FLAC__bool(1))
} else {
C.FLAC__stream_encoder_set_streamable_subset(e.e, C.FLAC__bool(0))
}
status := C.FLAC__stream_encoder_init_ogg_stream(e.e,
(C.FLAC__StreamEncoderWriteCallback)(unsafe.Pointer(C.encoderWriteCallback_cgo)),
(C.FLAC__StreamEncoderSeekCallback)(unsafe.Pointer(C.encoderSeekCallback_cgo)),
(C.FLAC__StreamEncoderTellCallback)(unsafe.Pointer(C.encoderTellCallback_cgo)),
nil, nil)
if status != C.FLAC__STREAM_ENCODER_INIT_STATUS_OK {
return nil, fmt.Errorf("failed to open file, status = %d, state = %d", int(status), int(C.FLAC__stream_encoder_get_state(e.e)))
}
e.Channels = channels
e.Depth = depth
e.Rate = rate
return
}
// NewEncoderWriter creates a new Encoder object from an io.WriteCloser.
func NewEncoderWriter(writer io.WriteCloser, channels int, depth int, rate int, compressionLevel int, streamable bool, blockSize int) (e *Encoder, err error) {
if channels == 0 {
@ -552,6 +594,46 @@ func NewEncoderWriter(writer io.WriteCloser, channels int, depth int, rate int,
return
}
// NewEncoderWriterOgg creates a new Encoder object from an io.WriteCloser.
func NewEncoderWriterOgg(writer io.WriteCloser, channels int, depth int, rate int, compressionLevel int, streamable bool, blockSize 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)
e.writer = writer
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))
C.FLAC__stream_encoder_set_compression_level(e.e, C.uint(compressionLevel))
C.FLAC__stream_encoder_set_blocksize(e.e, C.uint(blockSize))
if streamable {
C.FLAC__stream_encoder_set_streamable_subset(e.e, C.FLAC__bool(1))
} else {
C.FLAC__stream_encoder_set_streamable_subset(e.e, C.FLAC__bool(0))
}
status := C.FLAC__stream_encoder_init_ogg_stream(e.e,
(C.FLAC__StreamEncoderWriteCallback)(unsafe.Pointer(C.encoderWriteCallback_cgo)),
nil,
nil,
nil, nil)
if status != C.FLAC__STREAM_ENCODER_INIT_STATUS_OK {
return nil, fmt.Errorf("failed to open file, status = %d, state = %d", int(status), int(C.FLAC__stream_encoder_get_state(e.e)))
}
e.Channels = channels
e.Depth = depth
e.Rate = rate
return
}
// WriteFrame writes a frame of audio data to the encoder.
func (e *Encoder) WriteFrame(f Frame) (err error) {
if f.Channels != e.Channels || f.Depth != e.Depth || f.Rate != e.Rate {