diff --git a/include/FLAC++/decoder.h b/include/FLAC++/decoder.h new file mode 100644 index 00000000..ecf545a3 --- /dev/null +++ b/include/FLAC++/decoder.h @@ -0,0 +1,251 @@ +/* libFLAC++ - Free Lossless Audio Codec library + * Copyright (C) 2002 Josh Coalson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef FLACPP__DECODER_H +#define FLACPP__DECODER_H + +#include "FLAC/file_decoder.h" +#include "FLAC/seekable_stream_decoder.h" +#include "FLAC/stream_decoder.h" + +// =============================================================== +// +// Full documentation for the decoder interfaces can be found +// in the C layer in include/FLAC/*_decoder.h +// +// =============================================================== + + +namespace FLAC { + namespace Decoder { + + // ============================================================ + // + // The only real difference here is that instead of passing + // in C function pointers for callbacks, you inherit from + // stream and provide implementations for the callbacks in + // the derived class; because of this there is no need for a + // 'client_data' property. + // + // ============================================================ + + // ============================================================ + // + // Equivalent: FLAC__StreamDecoder + // + // ============================================================ + + class Stream { + public: + class State { + public: + inline State(::FLAC__StreamDecoderState state): state_(state) { } + inline operator ::FLAC__StreamDecoderState() const { return state_; } + inline const char *as_cstring() const { return ::FLAC__StreamDecoderStateString[state_]; } + protected: + ::FLAC__StreamDecoderState state_; + }; + + Stream(); + virtual ~Stream(); + + bool is_valid() const; + inline operator bool() const { return is_valid(); } + + bool set_metadata_respond(::FLAC__MetaDataType type); + bool set_metadata_respond_application(const FLAC__byte id[4]); + bool set_metadata_respond_all(); + bool set_metadata_ignore(::FLAC__MetaDataType type); + bool set_metadata_ignore_application(const FLAC__byte id[4]); + bool set_metadata_ignore_all(); + + State get_state() const; + unsigned get_channels() const; + ::FLAC__ChannelAssignment get_channel_assignment() const; + unsigned get_bits_per_sample() const; + unsigned get_sample_rate() const; + unsigned get_blocksize() const; + + // Initialize the instance; as with the C interface, + // init() should be called after construction and 'set' + // calls but before any of the 'process' calls. + State init(); + + void finish(); + + bool flush(); + bool reset(); + + bool process_whole_stream(); + bool process_metadata(); + bool process_one_frame(); + bool process_remaining_frames(); + protected: + virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes) = 0; + virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 *buffer[]) = 0; + virtual void metadata_callback(const ::FLAC__StreamMetaData *metadata) = 0; + virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0; + + ::FLAC__StreamDecoder *decoder_; + private: + static ::FLAC__StreamDecoderReadStatus read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data); + static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data); + static void metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetaData *metadata, void *client_data); + static void error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data); + + // Private and undefined so you can't use them: + Stream(const Stream &); + void operator=(const Stream &); + }; + + // ============================================================ + // + // Equivalent: FLAC__SeekableStreamDecoder + // + // ============================================================ + + class SeekableStream { + public: + class State { + public: + inline State(::FLAC__SeekableStreamDecoderState state): state_(state) { } + inline operator ::FLAC__SeekableStreamDecoderState() const { return state_; } + inline const char *as_cstring() const { return ::FLAC__SeekableStreamDecoderStateString[state_]; } + protected: + ::FLAC__SeekableStreamDecoderState state_; + }; + + SeekableStream(); + virtual ~SeekableStream(); + + bool is_valid() const; + inline operator bool() const { return is_valid(); } + + bool set_md5_checking(bool value); + bool set_metadata_respond(::FLAC__MetaDataType type); + bool set_metadata_respond_application(const FLAC__byte id[4]); + bool set_metadata_respond_all(); + bool set_metadata_ignore(::FLAC__MetaDataType type); + bool set_metadata_ignore_application(const FLAC__byte id[4]); + bool set_metadata_ignore_all(); + + State get_state() const; + bool get_md5_checking() const; + + State init(); + + bool finish(); + + bool process_whole_stream(); + bool process_metadata(); + bool process_one_frame(); + bool process_remaining_frames(); + + bool seek_absolute(FLAC__uint64 sample); + protected: + virtual ::FLAC__SeekableStreamDecoderReadStatus read_callback(FLAC__byte buffer[], unsigned *bytes) = 0; + virtual ::FLAC__SeekableStreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset) = 0; + virtual ::FLAC__SeekableStreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset) = 0; + virtual ::FLAC__SeekableStreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length) = 0; + virtual bool eof_callback() = 0; + virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 *buffer[]) = 0; + virtual void metadata_callback(const ::FLAC__StreamMetaData *metadata) = 0; + virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0; + + ::FLAC__SeekableStreamDecoder *decoder_; + private: + static FLAC__SeekableStreamDecoderReadStatus read_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data); + static FLAC__SeekableStreamDecoderSeekStatus seek_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); + static FLAC__SeekableStreamDecoderTellStatus tell_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); + static FLAC__SeekableStreamDecoderLengthStatus length_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); + static FLAC__bool eof_callback_(const ::FLAC__SeekableStreamDecoder *decoder, void *client_data); + static FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data); + static void metadata_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__StreamMetaData *metadata, void *client_data); + static void error_callback_(const ::FLAC__SeekableStreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data); + + // Private and undefined so you can't use them: + SeekableStream(const SeekableStream &); + void operator=(const SeekableStream &); + }; + + // ============================================================ + // + // Equivalent: FLAC__FileDecoder + // + // ============================================================ + + class File { + public: + class State { + public: + inline State(::FLAC__FileDecoderState state): state_(state) { } + inline operator ::FLAC__FileDecoderState() const { return state_; } + inline const char *as_cstring() const { return ::FLAC__FileDecoderStateString[state_]; } + protected: + ::FLAC__FileDecoderState state_; + }; + + File(); + virtual ~File(); + + bool is_valid() const; + inline operator bool() const { return is_valid(); } + + bool set_md5_checking(bool value); + bool set_filename(const char *value); // 'value' may not be 0; use "-" for stdin + bool set_metadata_respond(::FLAC__MetaDataType type); + bool set_metadata_respond_application(const FLAC__byte id[4]); + bool set_metadata_respond_all(); + bool set_metadata_ignore(::FLAC__MetaDataType type); + bool set_metadata_ignore_application(const FLAC__byte id[4]); + bool set_metadata_ignore_all(); + + State get_state() const; + bool get_md5_checking() const; + + State init(); + + bool finish(); + + bool process_whole_file(); + bool process_metadata(); + bool process_one_frame(); + bool process_remaining_frames(); + + bool seek_absolute(FLAC__uint64 sample); + protected: + virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 *buffer[]) = 0; + virtual void metadata_callback(const ::FLAC__StreamMetaData *metadata) = 0; + virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0; + + ::FLAC__FileDecoder *decoder_; + private: + static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data); + static void metadata_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__StreamMetaData *metadata, void *client_data); + static void error_callback_(const ::FLAC__FileDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data); + + // Private and undefined so you can't use them: + File(const File &); + void operator=(const File &); + }; + + }; +}; + +#endif diff --git a/include/FLAC++/encoder.h b/include/FLAC++/encoder.h new file mode 100644 index 00000000..7ed2d52a --- /dev/null +++ b/include/FLAC++/encoder.h @@ -0,0 +1,130 @@ +/* libFLAC++ - Free Lossless Audio Codec library + * Copyright (C) 2002 Josh Coalson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef FLACPP__ENCODER_H +#define FLACPP__ENCODER_H + +#include "FLAC/stream_encoder.h" + +// =============================================================== +// +// Full documentation for the encoder interface can be found +// in the C layer in include/FLAC/stream_encoder.h +// +// =============================================================== + + +namespace FLAC { + namespace Encoder { + + // ============================================================ + // + // Equivalent: FLAC__StreamEncoder + // + // ---------------------------------------------------------- + // + // The only real difference here is that instead of passing + // in C function pointers for callbacks, you inherit from + // stream and provide implementations for the callbacks in + // the derived class; because of this there is no need for a + // 'client_data' property. + // + // ============================================================ + + class Stream { + public: + class State { + public: + inline State(::FLAC__StreamEncoderState state): state_(state) { } + inline operator ::FLAC__StreamEncoderState() const { return state_; } + inline const char *as_cstring() const { return ::FLAC__StreamEncoderStateString[state_]; } + protected: + ::FLAC__StreamEncoderState state_; + }; + + Stream(); + virtual ~Stream(); + + bool is_valid() const; + inline operator bool() const { return is_valid(); } + + bool set_streamable_subset(bool value); + bool set_do_mid_side_stereo(bool value); + bool set_loose_mid_side_stereo(bool value); + bool set_channels(unsigned value); + bool set_bits_per_sample(unsigned value); + bool set_sample_rate(unsigned value); + bool set_blocksize(unsigned value); + bool set_max_lpc_order(unsigned value); + bool set_qlp_coeff_precision(unsigned value); + bool set_do_qlp_coeff_prec_search(bool value); + bool set_do_escape_coding(bool value); + bool set_do_exhaustive_model_search(bool value); + bool set_min_residual_partition_order(unsigned value); + bool set_max_residual_partition_order(unsigned value); + bool set_rice_parameter_search_dist(unsigned value); + bool set_total_samples_estimate(FLAC__uint64 value); + bool set_seek_table(const FLAC__StreamMetaData_SeekTable *value); + bool set_padding(int value); + bool set_last_metadata_is_last(bool value); + + State get_state() const; + bool get_streamable_subset() const; + bool get_do_mid_side_stereo() const; + bool get_loose_mid_side_stereo() const; + unsigned get_channels() const; + unsigned get_bits_per_sample() const; + unsigned get_sample_rate() const; + unsigned get_blocksize() const; + unsigned get_max_lpc_order() const; + unsigned get_qlp_coeff_precision() const; + bool get_do_qlp_coeff_prec_search() const; + bool get_do_escape_coding() const; + bool get_do_exhaustive_model_search() const; + unsigned get_min_residual_partition_order() const; + unsigned get_max_residual_partition_order() const; + unsigned get_rice_parameter_search_dist() const; + + // Initialize the instance; as with the C interface, + // init() should be called after construction and 'set' + // calls but before any of the 'process' calls. + State init(); + + void finish(); + + bool process(const FLAC__int32 *buf[], unsigned samples); + bool process_interleaved(const FLAC__int32 buf[], unsigned samples); + protected: + virtual ::FLAC__StreamEncoderWriteStatus write_callback(const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame) = 0; + virtual void metadata_callback(const FLAC__StreamMetaData *metadata) = 0; + + ::FLAC__StreamEncoder *encoder_; + private: + static ::FLAC__StreamEncoderWriteStatus write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data); + static void metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetaData *metadata, void *client_data); + + // Private and undefined so you can't use them: + Stream(const Stream &); + void operator=(const Stream &); + }; + + }; +}; + +#endif diff --git a/src/libFLAC++/file_decoder.cc b/src/libFLAC++/file_decoder.cc new file mode 100644 index 00000000..5c22e1b0 --- /dev/null +++ b/src/libFLAC++/file_decoder.cc @@ -0,0 +1,177 @@ +/* libFLAC++ - Free Lossless Audio Codec library + * Copyright (C) 2002 Josh Coalson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "FLAC++/decoder.h" +#include "FLAC/assert.h" + +namespace FLAC { + namespace Decoder { + + File::File(): + decoder_(::FLAC__file_decoder_new()) + { } + + File::~File() + { + if(0 != decoder_) { + (void) ::FLAC__file_decoder_finish(decoder_); + ::FLAC__file_decoder_delete(decoder_); + } + } + + bool File::is_valid() const + { + return 0 != decoder_; + } + + bool File::set_md5_checking(bool value) + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_md5_checking(decoder_, value); + } + + bool File::set_filename(const char *value) + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_filename(decoder_, value); + } + + bool File::set_metadata_respond(::FLAC__MetaDataType type) + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_metadata_respond(decoder_, type); + } + + bool File::set_metadata_respond_application(const FLAC__byte id[4]) + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_metadata_respond_application(decoder_, id); + } + + bool File::set_metadata_respond_all() + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_metadata_respond_all(decoder_); + } + + bool File::set_metadata_ignore(::FLAC__MetaDataType type) + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_metadata_ignore(decoder_, type); + } + + bool File::set_metadata_ignore_application(const FLAC__byte id[4]) + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_metadata_ignore_application(decoder_, id); + } + + bool File::set_metadata_ignore_all() + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_set_metadata_ignore_all(decoder_); + } + + File::State File::get_state() const + { + FLAC__ASSERT(0 != decoder_); + return State(::FLAC__file_decoder_get_state(decoder_)); + } + + bool File::get_md5_checking() const + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_get_md5_checking(decoder_); + } + + File::State File::init() + { + FLAC__ASSERT(0 != decoder_); + ::FLAC__file_decoder_set_write_callback(decoder_, write_callback_); + ::FLAC__file_decoder_set_metadata_callback(decoder_, metadata_callback_); + ::FLAC__file_decoder_set_error_callback(decoder_, error_callback_); + ::FLAC__file_decoder_set_client_data(decoder_, (void*)this); + return State(::FLAC__file_decoder_init(decoder_)); + } + + bool File::finish() + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_finish(decoder_); + } + + bool File::process_whole_file() + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_process_whole_file(decoder_); + } + + bool File::process_metadata() + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_process_metadata(decoder_); + } + + bool File::process_one_frame() + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_process_one_frame(decoder_); + } + + bool File::process_remaining_frames() + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_process_remaining_frames(decoder_); + } + + bool File::seek_absolute(FLAC__uint64 sample) + { + FLAC__ASSERT(0 != decoder_); + return ::FLAC__file_decoder_seek_absolute(decoder_, sample); + } + + ::FLAC__StreamDecoderWriteStatus File::write_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + File *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->write_callback(frame, buffer); + } + + void File::metadata_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__StreamMetaData *metadata, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + File *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + instance->metadata_callback(metadata); + } + + void File::error_callback_(const ::FLAC__FileDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + File *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + instance->error_callback(status); + } + + }; +}; diff --git a/src/libFLAC++/seekable_stream_decoder.cc b/src/libFLAC++/seekable_stream_decoder.cc new file mode 100644 index 00000000..2c75f253 --- /dev/null +++ b/src/libFLAC++/seekable_stream_decoder.cc @@ -0,0 +1,221 @@ +/* libFLAC++ - Free Lossless Audio Codec library + * Copyright (C) 2002 Josh Coalson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "FLAC++/decoder.h" +#include "FLAC/assert.h" + +namespace FLAC { + namespace Decoder { + + SeekableStream::SeekableStream(): + decoder_(::FLAC__seekable_stream_decoder_new()) + { } + + SeekableStream::~SeekableStream() + { + if(0 != decoder_) { + (void) ::FLAC__seekable_stream_decoder_finish(decoder_); + ::FLAC__seekable_stream_decoder_delete(decoder_); + } + } + + bool SeekableStream::is_valid() const + { + return 0 != decoder_; + } + + bool SeekableStream::set_md5_checking(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_set_md5_checking(decoder_, value); + } + + bool SeekableStream::set_metadata_respond(::FLAC__MetaDataType type) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_set_metadata_respond(decoder_, type); + } + + bool SeekableStream::set_metadata_respond_application(const FLAC__byte id[4]) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_set_metadata_respond_application(decoder_, id); + } + + bool SeekableStream::set_metadata_respond_all() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_set_metadata_respond_all(decoder_); + } + + bool SeekableStream::set_metadata_ignore(::FLAC__MetaDataType type) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_set_metadata_ignore(decoder_, type); + } + + bool SeekableStream::set_metadata_ignore_application(const FLAC__byte id[4]) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder_, id); + } + + bool SeekableStream::set_metadata_ignore_all() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder_); + } + + SeekableStream::State SeekableStream::get_state() const + { + FLAC__ASSERT(is_valid()); + return State(::FLAC__seekable_stream_decoder_get_state(decoder_)); + } + + bool SeekableStream::get_md5_checking() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_get_md5_checking(decoder_); + } + + SeekableStream::State SeekableStream::init() + { + FLAC__ASSERT(is_valid()); + ::FLAC__seekable_stream_decoder_set_read_callback(decoder_, read_callback_); + ::FLAC__seekable_stream_decoder_set_seek_callback(decoder_, seek_callback_); + ::FLAC__seekable_stream_decoder_set_tell_callback(decoder_, tell_callback_); + ::FLAC__seekable_stream_decoder_set_length_callback(decoder_, length_callback_); + ::FLAC__seekable_stream_decoder_set_eof_callback(decoder_, eof_callback_); + ::FLAC__seekable_stream_decoder_set_write_callback(decoder_, write_callback_); + ::FLAC__seekable_stream_decoder_set_metadata_callback(decoder_, metadata_callback_); + ::FLAC__seekable_stream_decoder_set_error_callback(decoder_, error_callback_); + ::FLAC__seekable_stream_decoder_set_client_data(decoder_, (void*)this); + return State(::FLAC__seekable_stream_decoder_init(decoder_)); + } + + bool SeekableStream::finish() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_finish(decoder_); + } + + bool SeekableStream::process_whole_stream() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_process_whole_stream(decoder_); + } + + bool SeekableStream::process_metadata() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_process_metadata(decoder_); + } + + bool SeekableStream::process_one_frame() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_process_one_frame(decoder_); + } + + bool SeekableStream::process_remaining_frames() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_process_remaining_frames(decoder_); + } + + bool SeekableStream::seek_absolute(FLAC__uint64 sample) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__seekable_stream_decoder_seek_absolute(decoder_, sample); + } + + FLAC__SeekableStreamDecoderReadStatus SeekableStream::read_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->read_callback(buffer, bytes); + } + + FLAC__SeekableStreamDecoderSeekStatus SeekableStream::seek_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->seek_callback(absolute_byte_offset); + } + + FLAC__SeekableStreamDecoderTellStatus SeekableStream::tell_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->tell_callback(absolute_byte_offset); + } + + FLAC__SeekableStreamDecoderLengthStatus SeekableStream::length_callback_(const ::FLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->length_callback(stream_length); + } + + FLAC__bool SeekableStream::eof_callback_(const ::FLAC__SeekableStreamDecoder *decoder, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->eof_callback(); + } + + FLAC__StreamDecoderWriteStatus SeekableStream::write_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->write_callback(frame, buffer); + } + + void SeekableStream::metadata_callback_(const ::FLAC__SeekableStreamDecoder *decoder, const ::FLAC__StreamMetaData *metadata, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + instance->metadata_callback(metadata); + } + + void SeekableStream::error_callback_(const ::FLAC__SeekableStreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data) + { + (void) decoder; + FLAC__ASSERT(0 != client_data); + SeekableStream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + instance->error_callback(status); + } + + }; +}; diff --git a/src/libFLAC++/stream_decoder.cc b/src/libFLAC++/stream_decoder.cc new file mode 100644 index 00000000..ce420171 --- /dev/null +++ b/src/libFLAC++/stream_decoder.cc @@ -0,0 +1,205 @@ +/* libFLAC++ - Free Lossless Audio Codec library + * Copyright (C) 2002 Josh Coalson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "FLAC++/decoder.h" +#include "FLAC/assert.h" + +namespace FLAC { + namespace Decoder { + + Stream::Stream(): + decoder_(::FLAC__stream_decoder_new()) + { } + + Stream::~Stream() + { + if(0 != decoder_) { + ::FLAC__stream_decoder_finish(decoder_); + ::FLAC__stream_decoder_delete(decoder_); + } + } + + bool Stream::is_valid() const + { + return 0 != decoder_; + } + + bool Stream::set_metadata_respond(::FLAC__MetaDataType type) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_set_metadata_respond(decoder_, type); + } + + bool Stream::set_metadata_respond_application(const FLAC__byte id[4]) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id); + } + + bool Stream::set_metadata_respond_all() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_set_metadata_respond_all(decoder_); + } + + bool Stream::set_metadata_ignore(::FLAC__MetaDataType type) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_set_metadata_ignore(decoder_, type); + } + + bool Stream::set_metadata_ignore_application(const FLAC__byte id[4]) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id); + } + + bool Stream::set_metadata_ignore_all() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_set_metadata_ignore_all(decoder_); + } + + Stream::State Stream::get_state() const + { + FLAC__ASSERT(is_valid()); + return State(::FLAC__stream_decoder_get_state(decoder_)); + } + + unsigned Stream::get_channels() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_get_channels(decoder_); + } + + ::FLAC__ChannelAssignment Stream::get_channel_assignment() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_get_channel_assignment(decoder_); + } + + unsigned Stream::get_bits_per_sample() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_get_bits_per_sample(decoder_); + } + + unsigned Stream::get_sample_rate() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_get_sample_rate(decoder_); + } + + unsigned Stream::get_blocksize() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_get_blocksize(decoder_); + } + + Stream::State Stream::init() + { + FLAC__ASSERT(is_valid()); + ::FLAC__stream_decoder_set_read_callback(decoder_, read_callback_); + ::FLAC__stream_decoder_set_write_callback(decoder_, write_callback_); + ::FLAC__stream_decoder_set_metadata_callback(decoder_, metadata_callback_); + ::FLAC__stream_decoder_set_error_callback(decoder_, error_callback_); + ::FLAC__stream_decoder_set_client_data(decoder_, (void*)this); + return State(::FLAC__stream_decoder_init(decoder_)); + } + + void Stream::finish() + { + FLAC__ASSERT(is_valid()); + ::FLAC__stream_decoder_finish(decoder_); + } + + bool Stream::flush() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_flush(decoder_); + } + + bool Stream::reset() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_reset(decoder_); + } + + bool Stream::process_whole_stream() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_process_whole_stream(decoder_); + } + + bool Stream::process_metadata() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_process_metadata(decoder_); + } + + bool Stream::process_one_frame() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_process_one_frame(decoder_); + } + + bool Stream::process_remaining_frames() + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_decoder_process_remaining_frames(decoder_); + } + + ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) + { + (void)decoder; + FLAC__ASSERT(0 != client_data); + Stream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->read_callback(buffer, bytes); + } + + ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 *buffer[], void *client_data) + { + (void)decoder; + FLAC__ASSERT(0 != client_data); + Stream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->write_callback(frame, buffer); + } + + void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetaData *metadata, void *client_data) + { + (void)decoder; + FLAC__ASSERT(0 != client_data); + Stream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + instance->metadata_callback(metadata); + } + + void Stream::error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data) + { + (void)decoder; + FLAC__ASSERT(0 != client_data); + Stream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + instance->error_callback(status); + } + + }; +}; diff --git a/src/libFLAC++/stream_encoder.cc b/src/libFLAC++/stream_encoder.cc new file mode 100644 index 00000000..0f6aa854 --- /dev/null +++ b/src/libFLAC++/stream_encoder.cc @@ -0,0 +1,299 @@ +/* libFLAC++ - Free Lossless Audio Codec library + * Copyright (C) 2002 Josh Coalson + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "FLAC++/encoder.h" +#include "FLAC/assert.h" + +namespace FLAC { + namespace Encoder { + + Stream::Stream(): + encoder_(::FLAC__stream_encoder_new()) + { } + + Stream::~Stream() + { + if(0 != encoder_) { + ::FLAC__stream_encoder_finish(encoder_); + ::FLAC__stream_encoder_delete(encoder_); + } + } + + bool Stream::is_valid() const + { + return 0 != encoder_; + } + + bool Stream::set_streamable_subset(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_streamable_subset(encoder_, value); + } + + bool Stream::set_do_mid_side_stereo(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value); + } + + bool Stream::set_loose_mid_side_stereo(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value); + } + + bool Stream::set_channels(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_channels(encoder_, value); + } + + bool Stream::set_bits_per_sample(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_bits_per_sample(encoder_, value); + } + + bool Stream::set_sample_rate(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_sample_rate(encoder_, value); + } + + bool Stream::set_blocksize(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_blocksize(encoder_, value); + } + + bool Stream::set_max_lpc_order(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_max_lpc_order(encoder_, value); + } + + bool Stream::set_qlp_coeff_precision(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value); + } + + bool Stream::set_do_qlp_coeff_prec_search(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value); + } + + bool Stream::set_do_escape_coding(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_do_escape_coding(encoder_, value); + } + + bool Stream::set_do_exhaustive_model_search(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value); + } + + bool Stream::set_min_residual_partition_order(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_min_residual_partition_order(encoder_, value); + } + + bool Stream::set_max_residual_partition_order(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_max_residual_partition_order(encoder_, value); + } + + bool Stream::set_rice_parameter_search_dist(unsigned value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value); + } + + bool Stream::set_total_samples_estimate(FLAC__uint64 value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_total_samples_estimate(encoder_, value); + } + + bool Stream::set_seek_table(const FLAC__StreamMetaData_SeekTable *value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_seek_table(encoder_, value); + } + + bool Stream::set_padding(int value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_padding(encoder_, value); + } + + bool Stream::set_last_metadata_is_last(bool value) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_set_last_metadata_is_last(encoder_, value); + } + + Stream::State Stream::get_state() const + { + FLAC__ASSERT(is_valid()); + return State(::FLAC__stream_encoder_get_state(encoder_)); + } + + bool Stream::get_streamable_subset() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_streamable_subset(encoder_); + } + + bool Stream::get_do_mid_side_stereo() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_do_mid_side_stereo(encoder_); + } + + bool Stream::get_loose_mid_side_stereo() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_loose_mid_side_stereo(encoder_); + } + + unsigned Stream::get_channels() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_channels(encoder_); + } + + unsigned Stream::get_bits_per_sample() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_bits_per_sample(encoder_); + } + + unsigned Stream::get_sample_rate() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_sample_rate(encoder_); + } + + unsigned Stream::get_blocksize() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_blocksize(encoder_); + } + + unsigned Stream::get_max_lpc_order() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_max_lpc_order(encoder_); + } + + unsigned Stream::get_qlp_coeff_precision() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_qlp_coeff_precision(encoder_); + } + + bool Stream::get_do_qlp_coeff_prec_search() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_); + } + + bool Stream::get_do_escape_coding() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_do_escape_coding(encoder_); + } + + bool Stream::get_do_exhaustive_model_search() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_do_exhaustive_model_search(encoder_); + } + + unsigned Stream::get_min_residual_partition_order() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_min_residual_partition_order(encoder_); + } + + unsigned Stream::get_max_residual_partition_order() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_max_residual_partition_order(encoder_); + } + + unsigned Stream::get_rice_parameter_search_dist() const + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_get_rice_parameter_search_dist(encoder_); + } + + Stream::State Stream::init() + { + FLAC__ASSERT(is_valid()); + FLAC__stream_encoder_set_write_callback(encoder_, write_callback_); + FLAC__stream_encoder_set_metadata_callback(encoder_, metadata_callback_); + FLAC__stream_encoder_set_client_data(encoder_, (void*)this); + return State(::FLAC__stream_encoder_init(encoder_)); + } + + void Stream::finish() + { + FLAC__ASSERT(is_valid()); + ::FLAC__stream_encoder_finish(encoder_); + } + + bool Stream::process(const FLAC__int32 *buf[], unsigned samples) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_process(encoder_, buf, samples); + } + + bool Stream::process_interleaved(const FLAC__int32 buf[], unsigned samples) + { + FLAC__ASSERT(is_valid()); + return ::FLAC__stream_encoder_process_interleaved(encoder_, buf, samples); + } + + ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data) + { + (void)encoder; + FLAC__ASSERT(0 != client_data); + Stream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + return instance->write_callback(buffer, bytes, samples, current_frame); + } + + void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetaData *metadata, void *client_data) + { + (void)encoder; + FLAC__ASSERT(0 != client_data); + Stream *instance = reinterpret_cast(client_data); + FLAC__ASSERT(0 != instance); + instance->metadata_callback(metadata); + } + + }; +};