1 /* libOggFLAC++ - Free Lossless Audio Codec + Ogg library
2 * Copyright (C) 2002,2003,2004 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 )
43 SeekableStream::SeekableStream():
44 decoder_(::OggFLAC__seekable_stream_decoder_new())
47 SeekableStream::~SeekableStream()
50 (void) ::OggFLAC__seekable_stream_decoder_finish(decoder_);
51 ::OggFLAC__seekable_stream_decoder_delete(decoder_);
55 bool SeekableStream::is_valid() const
60 bool SeekableStream::set_serial_number(long value)
62 FLAC__ASSERT(is_valid());
63 return (bool)::OggFLAC__seekable_stream_decoder_set_serial_number(decoder_, value);
66 bool SeekableStream::set_md5_checking(bool value)
68 FLAC__ASSERT(is_valid());
69 return (bool)::OggFLAC__seekable_stream_decoder_set_md5_checking(decoder_, value);
72 bool SeekableStream::set_metadata_respond(::FLAC__MetadataType type)
74 FLAC__ASSERT(is_valid());
75 return (bool)::OggFLAC__seekable_stream_decoder_set_metadata_respond(decoder_, type);
78 bool SeekableStream::set_metadata_respond_application(const FLAC__byte id[4])
80 FLAC__ASSERT(is_valid());
81 return (bool)::OggFLAC__seekable_stream_decoder_set_metadata_respond_application(decoder_, id);
84 bool SeekableStream::set_metadata_respond_all()
86 FLAC__ASSERT(is_valid());
87 return (bool)::OggFLAC__seekable_stream_decoder_set_metadata_respond_all(decoder_);
90 bool SeekableStream::set_metadata_ignore(::FLAC__MetadataType type)
92 FLAC__ASSERT(is_valid());
93 return (bool)::OggFLAC__seekable_stream_decoder_set_metadata_ignore(decoder_, type);
96 bool SeekableStream::set_metadata_ignore_application(const FLAC__byte id[4])
98 FLAC__ASSERT(is_valid());
99 return (bool)::OggFLAC__seekable_stream_decoder_set_metadata_ignore_application(decoder_, id);
102 bool SeekableStream::set_metadata_ignore_all()
104 FLAC__ASSERT(is_valid());
105 return (bool)::OggFLAC__seekable_stream_decoder_set_metadata_ignore_all(decoder_);
108 SeekableStream::State SeekableStream::get_state() const
110 FLAC__ASSERT(is_valid());
111 return State(::OggFLAC__seekable_stream_decoder_get_state(decoder_));
114 FLAC::Decoder::SeekableStream::State SeekableStream::get_FLAC_seekable_stream_decoder_state() const
116 FLAC__ASSERT(is_valid());
117 return FLAC::Decoder::SeekableStream::State(::OggFLAC__seekable_stream_decoder_get_FLAC_seekable_stream_decoder_state(decoder_));
120 FLAC::Decoder::Stream::State SeekableStream::get_FLAC_stream_decoder_state() const
122 FLAC__ASSERT(is_valid());
123 return FLAC::Decoder::Stream::State(::OggFLAC__seekable_stream_decoder_get_FLAC_stream_decoder_state(decoder_));
126 bool SeekableStream::get_md5_checking() const
128 FLAC__ASSERT(is_valid());
129 return (bool)::OggFLAC__seekable_stream_decoder_get_md5_checking(decoder_);
132 unsigned SeekableStream::get_channels() const
134 FLAC__ASSERT(is_valid());
135 return ::OggFLAC__seekable_stream_decoder_get_channels(decoder_);
138 ::FLAC__ChannelAssignment SeekableStream::get_channel_assignment() const
140 FLAC__ASSERT(is_valid());
141 return ::OggFLAC__seekable_stream_decoder_get_channel_assignment(decoder_);
144 unsigned SeekableStream::get_bits_per_sample() const
146 FLAC__ASSERT(is_valid());
147 return ::OggFLAC__seekable_stream_decoder_get_bits_per_sample(decoder_);
150 unsigned SeekableStream::get_sample_rate() const
152 FLAC__ASSERT(is_valid());
153 return ::OggFLAC__seekable_stream_decoder_get_sample_rate(decoder_);
156 unsigned SeekableStream::get_blocksize() const
158 FLAC__ASSERT(is_valid());
159 return ::OggFLAC__seekable_stream_decoder_get_blocksize(decoder_);
162 SeekableStream::State SeekableStream::init()
164 FLAC__ASSERT(is_valid());
165 ::OggFLAC__seekable_stream_decoder_set_read_callback(decoder_, read_callback_);
166 ::OggFLAC__seekable_stream_decoder_set_seek_callback(decoder_, seek_callback_);
167 ::OggFLAC__seekable_stream_decoder_set_tell_callback(decoder_, tell_callback_);
168 ::OggFLAC__seekable_stream_decoder_set_length_callback(decoder_, length_callback_);
169 ::OggFLAC__seekable_stream_decoder_set_eof_callback(decoder_, eof_callback_);
170 ::OggFLAC__seekable_stream_decoder_set_write_callback(decoder_, write_callback_);
171 ::OggFLAC__seekable_stream_decoder_set_metadata_callback(decoder_, metadata_callback_);
172 ::OggFLAC__seekable_stream_decoder_set_error_callback(decoder_, error_callback_);
173 ::OggFLAC__seekable_stream_decoder_set_client_data(decoder_, (void*)this);
174 return State(::OggFLAC__seekable_stream_decoder_init(decoder_));
177 bool SeekableStream::finish()
179 FLAC__ASSERT(is_valid());
180 return (bool)::OggFLAC__seekable_stream_decoder_finish(decoder_);
183 bool SeekableStream::flush()
185 FLAC__ASSERT(is_valid());
186 return (bool)::OggFLAC__seekable_stream_decoder_flush(decoder_);
189 bool SeekableStream::reset()
191 FLAC__ASSERT(is_valid());
192 return (bool)::OggFLAC__seekable_stream_decoder_reset(decoder_);
195 bool SeekableStream::process_single()
197 FLAC__ASSERT(is_valid());
198 return (bool)::OggFLAC__seekable_stream_decoder_process_single(decoder_);
201 bool SeekableStream::process_until_end_of_metadata()
203 FLAC__ASSERT(is_valid());
204 return (bool)::OggFLAC__seekable_stream_decoder_process_until_end_of_metadata(decoder_);
207 bool SeekableStream::process_until_end_of_stream()
209 FLAC__ASSERT(is_valid());
210 return (bool)::OggFLAC__seekable_stream_decoder_process_until_end_of_stream(decoder_);
213 bool SeekableStream::seek_absolute(FLAC__uint64 sample)
215 FLAC__ASSERT(is_valid());
216 return (bool)::OggFLAC__seekable_stream_decoder_seek_absolute(decoder_, sample);
219 ::FLAC__SeekableStreamDecoderReadStatus SeekableStream::read_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
222 FLAC__ASSERT(0 != client_data);
223 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
224 FLAC__ASSERT(0 != instance);
225 return instance->read_callback(buffer, bytes);
228 ::FLAC__SeekableStreamDecoderSeekStatus SeekableStream::seek_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 absolute_byte_offset, void *client_data)
231 FLAC__ASSERT(0 != client_data);
232 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
233 FLAC__ASSERT(0 != instance);
234 return instance->seek_callback(absolute_byte_offset);
237 ::FLAC__SeekableStreamDecoderTellStatus SeekableStream::tell_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset, void *client_data)
240 FLAC__ASSERT(0 != client_data);
241 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
242 FLAC__ASSERT(0 != instance);
243 return instance->tell_callback(absolute_byte_offset);
246 ::FLAC__SeekableStreamDecoderLengthStatus SeekableStream::length_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, FLAC__uint64 *stream_length, void *client_data)
249 FLAC__ASSERT(0 != client_data);
250 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
251 FLAC__ASSERT(0 != instance);
252 return instance->length_callback(stream_length);
255 FLAC__bool SeekableStream::eof_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, void *client_data)
258 FLAC__ASSERT(0 != client_data);
259 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
260 FLAC__ASSERT(0 != instance);
261 return instance->eof_callback();
264 ::FLAC__StreamDecoderWriteStatus SeekableStream::write_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
267 FLAC__ASSERT(0 != client_data);
268 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
269 FLAC__ASSERT(0 != instance);
270 return instance->write_callback(frame, buffer);
273 void SeekableStream::metadata_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
276 FLAC__ASSERT(0 != client_data);
277 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
278 FLAC__ASSERT(0 != instance);
279 instance->metadata_callback(metadata);
282 void SeekableStream::error_callback_(const ::OggFLAC__SeekableStreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
285 FLAC__ASSERT(0 != client_data);
286 SeekableStream *instance = reinterpret_cast<SeekableStream *>(client_data);
287 FLAC__ASSERT(0 != instance);
288 instance->error_callback(status);