1 /* libOggFLAC++ - Free Lossless Audio Codec + Ogg library
2 * Copyright (C) 2002,2003,2004,2005 Josh Coalson
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * - Neither the name of the Xiph.org Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "OggFLAC++/decoder.h"
33 #include "FLAC/assert.h"
36 // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
37 #pragma warning ( disable : 4800 )
44 decoder_(::OggFLAC__stream_decoder_new())
50 ::OggFLAC__stream_decoder_finish(decoder_);
51 ::OggFLAC__stream_decoder_delete(decoder_);
55 bool Stream::is_valid() const
60 bool Stream::set_serial_number(long value)
62 FLAC__ASSERT(is_valid());
63 return (bool)::OggFLAC__stream_decoder_set_serial_number(decoder_, value);
66 bool Stream::set_metadata_respond(::FLAC__MetadataType type)
68 FLAC__ASSERT(is_valid());
69 return (bool)::OggFLAC__stream_decoder_set_metadata_respond(decoder_, type);
72 bool Stream::set_metadata_respond_application(const FLAC__byte id[4])
74 FLAC__ASSERT(is_valid());
75 return (bool)::OggFLAC__stream_decoder_set_metadata_respond_application(decoder_, id);
78 bool Stream::set_metadata_respond_all()
80 FLAC__ASSERT(is_valid());
81 return (bool)::OggFLAC__stream_decoder_set_metadata_respond_all(decoder_);
84 bool Stream::set_metadata_ignore(::FLAC__MetadataType type)
86 FLAC__ASSERT(is_valid());
87 return (bool)::OggFLAC__stream_decoder_set_metadata_ignore(decoder_, type);
90 bool Stream::set_metadata_ignore_application(const FLAC__byte id[4])
92 FLAC__ASSERT(is_valid());
93 return (bool)::OggFLAC__stream_decoder_set_metadata_ignore_application(decoder_, id);
96 bool Stream::set_metadata_ignore_all()
98 FLAC__ASSERT(is_valid());
99 return (bool)::OggFLAC__stream_decoder_set_metadata_ignore_all(decoder_);
102 Stream::State Stream::get_state() const
104 FLAC__ASSERT(is_valid());
105 return State(::OggFLAC__stream_decoder_get_state(decoder_));
108 FLAC::Decoder::Stream::State Stream::get_FLAC_stream_decoder_state() const
110 FLAC__ASSERT(is_valid());
111 return FLAC::Decoder::Stream::State(::OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(decoder_));
114 unsigned Stream::get_channels() const
116 FLAC__ASSERT(is_valid());
117 return ::OggFLAC__stream_decoder_get_channels(decoder_);
120 ::FLAC__ChannelAssignment Stream::get_channel_assignment() const
122 FLAC__ASSERT(is_valid());
123 return ::OggFLAC__stream_decoder_get_channel_assignment(decoder_);
126 unsigned Stream::get_bits_per_sample() const
128 FLAC__ASSERT(is_valid());
129 return ::OggFLAC__stream_decoder_get_bits_per_sample(decoder_);
132 unsigned Stream::get_sample_rate() const
134 FLAC__ASSERT(is_valid());
135 return ::OggFLAC__stream_decoder_get_sample_rate(decoder_);
138 unsigned Stream::get_blocksize() const
140 FLAC__ASSERT(is_valid());
141 return ::OggFLAC__stream_decoder_get_blocksize(decoder_);
144 Stream::State Stream::init()
146 FLAC__ASSERT(is_valid());
147 ::OggFLAC__stream_decoder_set_read_callback(decoder_, read_callback_);
148 ::OggFLAC__stream_decoder_set_write_callback(decoder_, write_callback_);
149 ::OggFLAC__stream_decoder_set_metadata_callback(decoder_, metadata_callback_);
150 ::OggFLAC__stream_decoder_set_error_callback(decoder_, error_callback_);
151 ::OggFLAC__stream_decoder_set_client_data(decoder_, (void*)this);
152 return State(::OggFLAC__stream_decoder_init(decoder_));
155 void Stream::finish()
157 FLAC__ASSERT(is_valid());
158 ::OggFLAC__stream_decoder_finish(decoder_);
163 FLAC__ASSERT(is_valid());
164 return (bool)::OggFLAC__stream_decoder_flush(decoder_);
169 FLAC__ASSERT(is_valid());
170 return (bool)::OggFLAC__stream_decoder_reset(decoder_);
173 bool Stream::process_single()
175 FLAC__ASSERT(is_valid());
176 return (bool)::OggFLAC__stream_decoder_process_single(decoder_);
179 bool Stream::process_until_end_of_metadata()
181 FLAC__ASSERT(is_valid());
182 return (bool)::OggFLAC__stream_decoder_process_until_end_of_metadata(decoder_);
185 bool Stream::process_until_end_of_stream()
187 FLAC__ASSERT(is_valid());
188 return (bool)::OggFLAC__stream_decoder_process_until_end_of_stream(decoder_);
191 ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
194 FLAC__ASSERT(0 != client_data);
195 Stream *instance = reinterpret_cast<Stream *>(client_data);
196 FLAC__ASSERT(0 != instance);
197 return instance->read_callback(buffer, bytes);
200 ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::OggFLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
203 FLAC__ASSERT(0 != client_data);
204 Stream *instance = reinterpret_cast<Stream *>(client_data);
205 FLAC__ASSERT(0 != instance);
206 return instance->write_callback(frame, buffer);
209 void Stream::metadata_callback_(const ::OggFLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
212 FLAC__ASSERT(0 != client_data);
213 Stream *instance = reinterpret_cast<Stream *>(client_data);
214 FLAC__ASSERT(0 != instance);
215 instance->metadata_callback(metadata);
218 void Stream::error_callback_(const ::OggFLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
221 FLAC__ASSERT(0 != client_data);
222 Stream *instance = reinterpret_cast<Stream *>(client_data);
223 FLAC__ASSERT(0 != instance);
224 instance->error_callback(status);