cannam@90: /* libFLAC++ - Free Lossless Audio Codec library cannam@90: * Copyright (C) 2002,2003,2004,2005,2006,2007 Josh Coalson cannam@90: * cannam@90: * Redistribution and use in source and binary forms, with or without cannam@90: * modification, are permitted provided that the following conditions cannam@90: * are met: cannam@90: * cannam@90: * - Redistributions of source code must retain the above copyright cannam@90: * notice, this list of conditions and the following disclaimer. cannam@90: * cannam@90: * - Redistributions in binary form must reproduce the above copyright cannam@90: * notice, this list of conditions and the following disclaimer in the cannam@90: * documentation and/or other materials provided with the distribution. cannam@90: * cannam@90: * - Neither the name of the Xiph.org Foundation nor the names of its cannam@90: * contributors may be used to endorse or promote products derived from cannam@90: * this software without specific prior written permission. cannam@90: * cannam@90: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@90: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@90: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@90: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR cannam@90: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@90: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@90: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@90: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@90: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@90: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@90: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@90: */ cannam@90: cannam@90: #ifndef FLACPP__DECODER_H cannam@90: #define FLACPP__DECODER_H cannam@90: cannam@90: #include "export.h" cannam@90: cannam@90: #include cannam@90: #include "FLAC/stream_decoder.h" cannam@90: cannam@90: cannam@90: /** \file include/FLAC++/decoder.h cannam@90: * cannam@90: * \brief cannam@90: * This module contains the classes which implement the various cannam@90: * decoders. cannam@90: * cannam@90: * See the detailed documentation in the cannam@90: * \link flacpp_decoder decoder \endlink module. cannam@90: */ cannam@90: cannam@90: /** \defgroup flacpp_decoder FLAC++/decoder.h: decoder classes cannam@90: * \ingroup flacpp cannam@90: * cannam@90: * \brief cannam@90: * This module describes the decoder layers provided by libFLAC++. cannam@90: * cannam@90: * The libFLAC++ decoder classes are object wrappers around their cannam@90: * counterparts in libFLAC. All decoding layers available in cannam@90: * libFLAC are also provided here. The interface is very similar; cannam@90: * make sure to read the \link flac_decoder libFLAC decoder module \endlink. cannam@90: * cannam@90: * There are only two significant differences here. First, instead of cannam@90: * passing in C function pointers for callbacks, you inherit from the cannam@90: * decoder class and provide implementations for the callbacks in your cannam@90: * derived class; because of this there is no need for a 'client_data' cannam@90: * property. cannam@90: * cannam@90: * Second, there are two stream decoder classes. FLAC::Decoder::Stream cannam@90: * is used for the same cases that FLAC__stream_decoder_init_stream() / cannam@90: * FLAC__stream_decoder_init_ogg_stream() are used, and FLAC::Decoder::File cannam@90: * is used for the same cases that cannam@90: * FLAC__stream_decoder_init_FILE() and FLAC__stream_decoder_init_file() / cannam@90: * FLAC__stream_decoder_init_ogg_FILE() and FLAC__stream_decoder_init_ogg_file() cannam@90: * are used. cannam@90: */ cannam@90: cannam@90: namespace FLAC { cannam@90: namespace Decoder { cannam@90: cannam@90: /** \ingroup flacpp_decoder cannam@90: * \brief cannam@90: * This class wraps the ::FLAC__StreamDecoder. If you are cannam@90: * decoding from a file, FLAC::Decoder::File may be more cannam@90: * convenient. cannam@90: * cannam@90: * The usage of this class is similar to FLAC__StreamDecoder, cannam@90: * except instead of providing callbacks to cannam@90: * FLAC__stream_decoder_init*_stream(), you will inherit from this cannam@90: * class and override the virtual callback functions with your cannam@90: * own implementations, then call init() or init_ogg(). The rest cannam@90: * of the calls work the same as in the C layer. cannam@90: * cannam@90: * Only the read, write, and error callbacks are mandatory. The cannam@90: * others are optional; this class provides default cannam@90: * implementations that do nothing. In order for seeking to work cannam@90: * you must overide seek_callback(), tell_callback(), cannam@90: * length_callback(), and eof_callback(). cannam@90: */ cannam@90: class FLACPP_API Stream { cannam@90: public: cannam@90: /** This class is a wrapper around FLAC__StreamDecoderState. cannam@90: */ cannam@90: class FLACPP_API State { cannam@90: public: cannam@90: inline State(::FLAC__StreamDecoderState state): state_(state) { } cannam@90: inline operator ::FLAC__StreamDecoderState() const { return state_; } cannam@90: inline const char *as_cstring() const { return ::FLAC__StreamDecoderStateString[state_]; } cannam@90: inline const char *resolved_as_cstring(const Stream &decoder) const { return ::FLAC__stream_decoder_get_resolved_state_string(decoder.decoder_); } cannam@90: protected: cannam@90: ::FLAC__StreamDecoderState state_; cannam@90: }; cannam@90: cannam@90: Stream(); cannam@90: virtual ~Stream(); cannam@90: cannam@90: //@{ cannam@90: /** Call after construction to check the that the object was created cannam@90: * successfully. If not, use get_state() to find out why not. cannam@90: */ cannam@90: virtual bool is_valid() const; cannam@90: inline operator bool() const { return is_valid(); } ///< See is_valid() cannam@90: //@} cannam@90: cannam@90: virtual bool set_ogg_serial_number(long value); ///< See FLAC__stream_decoder_set_ogg_serial_number() cannam@90: virtual bool set_md5_checking(bool value); ///< See FLAC__stream_decoder_set_md5_checking() cannam@90: virtual bool set_metadata_respond(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_respond() cannam@90: virtual bool set_metadata_respond_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_respond_application() cannam@90: virtual bool set_metadata_respond_all(); ///< See FLAC__stream_decoder_set_metadata_respond_all() cannam@90: virtual bool set_metadata_ignore(::FLAC__MetadataType type); ///< See FLAC__stream_decoder_set_metadata_ignore() cannam@90: virtual bool set_metadata_ignore_application(const FLAC__byte id[4]); ///< See FLAC__stream_decoder_set_metadata_ignore_application() cannam@90: virtual bool set_metadata_ignore_all(); ///< See FLAC__stream_decoder_set_metadata_ignore_all() cannam@90: cannam@90: /* get_state() is not virtual since we want subclasses to be able to return their own state */ cannam@90: State get_state() const; ///< See FLAC__stream_decoder_get_state() cannam@90: virtual bool get_md5_checking() const; ///< See FLAC__stream_decoder_get_md5_checking() cannam@90: virtual FLAC__uint64 get_total_samples() const; ///< See FLAC__stream_decoder_get_total_samples() cannam@90: virtual unsigned get_channels() const; ///< See FLAC__stream_decoder_get_channels() cannam@90: virtual ::FLAC__ChannelAssignment get_channel_assignment() const; ///< See FLAC__stream_decoder_get_channel_assignment() cannam@90: virtual unsigned get_bits_per_sample() const; ///< See FLAC__stream_decoder_get_bits_per_sample() cannam@90: virtual unsigned get_sample_rate() const; ///< See FLAC__stream_decoder_get_sample_rate() cannam@90: virtual unsigned get_blocksize() const; ///< See FLAC__stream_decoder_get_blocksize() cannam@90: virtual bool get_decode_position(FLAC__uint64 *position) const; ///< See FLAC__stream_decoder_get_decode_position() cannam@90: cannam@90: virtual ::FLAC__StreamDecoderInitStatus init(); ///< Seek FLAC__stream_decoder_init_stream() cannam@90: virtual ::FLAC__StreamDecoderInitStatus init_ogg(); ///< Seek FLAC__stream_decoder_init_ogg_stream() cannam@90: cannam@90: virtual bool finish(); ///< See FLAC__stream_decoder_finish() cannam@90: cannam@90: virtual bool flush(); ///< See FLAC__stream_decoder_flush() cannam@90: virtual bool reset(); ///< See FLAC__stream_decoder_reset() cannam@90: cannam@90: virtual bool process_single(); ///< See FLAC__stream_decoder_process_single() cannam@90: virtual bool process_until_end_of_metadata(); ///< See FLAC__stream_decoder_process_until_end_of_metadata() cannam@90: virtual bool process_until_end_of_stream(); ///< See FLAC__stream_decoder_process_until_end_of_stream() cannam@90: virtual bool skip_single_frame(); ///< See FLAC__stream_decoder_skip_single_frame() cannam@90: cannam@90: virtual bool seek_absolute(FLAC__uint64 sample); ///< See FLAC__stream_decoder_seek_absolute() cannam@90: protected: cannam@90: /// see FLAC__StreamDecoderReadCallback cannam@90: virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes) = 0; cannam@90: cannam@90: /// see FLAC__StreamDecoderSeekCallback cannam@90: virtual ::FLAC__StreamDecoderSeekStatus seek_callback(FLAC__uint64 absolute_byte_offset); cannam@90: cannam@90: /// see FLAC__StreamDecoderTellCallback cannam@90: virtual ::FLAC__StreamDecoderTellStatus tell_callback(FLAC__uint64 *absolute_byte_offset); cannam@90: cannam@90: /// see FLAC__StreamDecoderLengthCallback cannam@90: virtual ::FLAC__StreamDecoderLengthStatus length_callback(FLAC__uint64 *stream_length); cannam@90: cannam@90: /// see FLAC__StreamDecoderEofCallback cannam@90: virtual bool eof_callback(); cannam@90: cannam@90: /// see FLAC__StreamDecoderWriteCallback cannam@90: virtual ::FLAC__StreamDecoderWriteStatus write_callback(const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[]) = 0; cannam@90: cannam@90: /// see FLAC__StreamDecoderMetadataCallback cannam@90: virtual void metadata_callback(const ::FLAC__StreamMetadata *metadata); cannam@90: cannam@90: /// see FLAC__StreamDecoderErrorCallback cannam@90: virtual void error_callback(::FLAC__StreamDecoderErrorStatus status) = 0; cannam@90: cannam@90: #if (defined _MSC_VER) || (defined __BORLANDC__) || (defined __GNUG__ && (__GNUG__ < 2 || (__GNUG__ == 2 && __GNUC_MINOR__ < 96))) || (defined __SUNPRO_CC) cannam@90: // lame hack: some MSVC/GCC versions can't see a protected decoder_ from nested State::resolved_as_cstring() cannam@90: friend State; cannam@90: #endif cannam@90: ::FLAC__StreamDecoder *decoder_; cannam@90: cannam@90: static ::FLAC__StreamDecoderReadStatus read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data); cannam@90: static ::FLAC__StreamDecoderSeekStatus seek_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data); cannam@90: static ::FLAC__StreamDecoderTellStatus tell_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data); cannam@90: static ::FLAC__StreamDecoderLengthStatus length_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data); cannam@90: static FLAC__bool eof_callback_(const ::FLAC__StreamDecoder *decoder, void *client_data); cannam@90: static ::FLAC__StreamDecoderWriteStatus write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data); cannam@90: static void metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data); cannam@90: static void error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data); cannam@90: private: cannam@90: // Private and undefined so you can't use them: cannam@90: Stream(const Stream &); cannam@90: void operator=(const Stream &); cannam@90: }; cannam@90: cannam@90: /** \ingroup flacpp_decoder cannam@90: * \brief cannam@90: * This class wraps the ::FLAC__StreamDecoder. If you are cannam@90: * not decoding from a file, you may need to use cannam@90: * FLAC::Decoder::Stream. cannam@90: * cannam@90: * The usage of this class is similar to FLAC__StreamDecoder, cannam@90: * except instead of providing callbacks to cannam@90: * FLAC__stream_decoder_init*_FILE() or cannam@90: * FLAC__stream_decoder_init*_file(), you will inherit from this cannam@90: * class and override the virtual callback functions with your cannam@90: * own implementations, then call init() or init_off(). The rest cannam@90: * of the calls work the same as in the C layer. cannam@90: * cannam@90: * Only the write, and error callbacks from FLAC::Decoder::Stream cannam@90: * are mandatory. The others are optional; this class provides cannam@90: * full working implementations for all other callbacks and cannam@90: * supports seeking. cannam@90: */ cannam@90: class FLACPP_API File: public Stream { cannam@90: public: cannam@90: File(); cannam@90: virtual ~File(); cannam@90: cannam@90: virtual ::FLAC__StreamDecoderInitStatus init(FILE *file); ///< See FLAC__stream_decoder_init_FILE() cannam@90: virtual ::FLAC__StreamDecoderInitStatus init(const char *filename); ///< See FLAC__stream_decoder_init_file() cannam@90: virtual ::FLAC__StreamDecoderInitStatus init(const std::string &filename); ///< See FLAC__stream_decoder_init_file() cannam@90: virtual ::FLAC__StreamDecoderInitStatus init_ogg(FILE *file); ///< See FLAC__stream_decoder_init_ogg_FILE() cannam@90: virtual ::FLAC__StreamDecoderInitStatus init_ogg(const char *filename); ///< See FLAC__stream_decoder_init_ogg_file() cannam@90: virtual ::FLAC__StreamDecoderInitStatus init_ogg(const std::string &filename); ///< See FLAC__stream_decoder_init_ogg_file() cannam@90: protected: cannam@90: // this is a dummy implementation to satisfy the pure virtual in Stream that is actually supplied internally by the C layer cannam@90: virtual ::FLAC__StreamDecoderReadStatus read_callback(FLAC__byte buffer[], size_t *bytes); cannam@90: private: cannam@90: // Private and undefined so you can't use them: cannam@90: File(const File &); cannam@90: void operator=(const File &); cannam@90: }; cannam@90: cannam@90: } cannam@90: } cannam@90: cannam@90: #endif