Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 1 | /* libFLAC++ - Free Lossless Audio Codec library |
Josh Coalson | 9564390 | 2004-01-17 04:14:43 +0000 | [diff] [blame] | 2 | * Copyright (C) 2002,2003,2004 Josh Coalson |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 3 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 7 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 8 | * - Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 10 | * |
Josh Coalson | afd8107 | 2003-01-31 23:34:56 +0000 | [diff] [blame] | 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. |
| 14 | * |
| 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. |
| 18 | * |
| 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. |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
| 32 | #include "FLAC++/decoder.h" |
| 33 | #include "FLAC/assert.h" |
| 34 | |
Josh Coalson | b7eeec1 | 2003-12-18 05:16:44 +0000 | [diff] [blame] | 35 | #ifdef _MSC_VER |
| 36 | // warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning) |
| 37 | #pragma warning ( disable : 4800 ) |
| 38 | #endif |
| 39 | |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 40 | namespace FLAC { |
| 41 | namespace Decoder { |
| 42 | |
| 43 | Stream::Stream(): |
| 44 | decoder_(::FLAC__stream_decoder_new()) |
| 45 | { } |
| 46 | |
| 47 | Stream::~Stream() |
| 48 | { |
| 49 | if(0 != decoder_) { |
| 50 | ::FLAC__stream_decoder_finish(decoder_); |
| 51 | ::FLAC__stream_decoder_delete(decoder_); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | bool Stream::is_valid() const |
| 56 | { |
| 57 | return 0 != decoder_; |
| 58 | } |
| 59 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 60 | bool Stream::set_metadata_respond(::FLAC__MetadataType type) |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 61 | { |
| 62 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 63 | return (bool)::FLAC__stream_decoder_set_metadata_respond(decoder_, type); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool Stream::set_metadata_respond_application(const FLAC__byte id[4]) |
| 67 | { |
| 68 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 69 | return (bool)::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | bool Stream::set_metadata_respond_all() |
| 73 | { |
| 74 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 75 | return (bool)::FLAC__stream_decoder_set_metadata_respond_all(decoder_); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 78 | bool Stream::set_metadata_ignore(::FLAC__MetadataType type) |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 79 | { |
| 80 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 81 | return (bool)::FLAC__stream_decoder_set_metadata_ignore(decoder_, type); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool Stream::set_metadata_ignore_application(const FLAC__byte id[4]) |
| 85 | { |
| 86 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 87 | return (bool)::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool Stream::set_metadata_ignore_all() |
| 91 | { |
| 92 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 93 | return (bool)::FLAC__stream_decoder_set_metadata_ignore_all(decoder_); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Stream::State Stream::get_state() const |
| 97 | { |
| 98 | FLAC__ASSERT(is_valid()); |
| 99 | return State(::FLAC__stream_decoder_get_state(decoder_)); |
| 100 | } |
| 101 | |
| 102 | unsigned Stream::get_channels() const |
| 103 | { |
| 104 | FLAC__ASSERT(is_valid()); |
| 105 | return ::FLAC__stream_decoder_get_channels(decoder_); |
| 106 | } |
| 107 | |
| 108 | ::FLAC__ChannelAssignment Stream::get_channel_assignment() const |
| 109 | { |
| 110 | FLAC__ASSERT(is_valid()); |
| 111 | return ::FLAC__stream_decoder_get_channel_assignment(decoder_); |
| 112 | } |
| 113 | |
| 114 | unsigned Stream::get_bits_per_sample() const |
| 115 | { |
| 116 | FLAC__ASSERT(is_valid()); |
| 117 | return ::FLAC__stream_decoder_get_bits_per_sample(decoder_); |
| 118 | } |
| 119 | |
| 120 | unsigned Stream::get_sample_rate() const |
| 121 | { |
| 122 | FLAC__ASSERT(is_valid()); |
| 123 | return ::FLAC__stream_decoder_get_sample_rate(decoder_); |
| 124 | } |
| 125 | |
| 126 | unsigned Stream::get_blocksize() const |
| 127 | { |
| 128 | FLAC__ASSERT(is_valid()); |
| 129 | return ::FLAC__stream_decoder_get_blocksize(decoder_); |
| 130 | } |
| 131 | |
| 132 | Stream::State Stream::init() |
| 133 | { |
| 134 | FLAC__ASSERT(is_valid()); |
| 135 | ::FLAC__stream_decoder_set_read_callback(decoder_, read_callback_); |
| 136 | ::FLAC__stream_decoder_set_write_callback(decoder_, write_callback_); |
| 137 | ::FLAC__stream_decoder_set_metadata_callback(decoder_, metadata_callback_); |
| 138 | ::FLAC__stream_decoder_set_error_callback(decoder_, error_callback_); |
| 139 | ::FLAC__stream_decoder_set_client_data(decoder_, (void*)this); |
| 140 | return State(::FLAC__stream_decoder_init(decoder_)); |
| 141 | } |
| 142 | |
| 143 | void Stream::finish() |
| 144 | { |
| 145 | FLAC__ASSERT(is_valid()); |
| 146 | ::FLAC__stream_decoder_finish(decoder_); |
| 147 | } |
| 148 | |
| 149 | bool Stream::flush() |
| 150 | { |
| 151 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 152 | return (bool)::FLAC__stream_decoder_flush(decoder_); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | bool Stream::reset() |
| 156 | { |
| 157 | FLAC__ASSERT(is_valid()); |
Josh Coalson | 7ee4d17 | 2002-06-01 06:31:27 +0000 | [diff] [blame] | 158 | return (bool)::FLAC__stream_decoder_reset(decoder_); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 161 | bool Stream::process_single() |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 162 | { |
| 163 | FLAC__ASSERT(is_valid()); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 164 | return (bool)::FLAC__stream_decoder_process_single(decoder_); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 167 | bool Stream::process_until_end_of_metadata() |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 168 | { |
| 169 | FLAC__ASSERT(is_valid()); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 170 | return (bool)::FLAC__stream_decoder_process_until_end_of_metadata(decoder_); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 173 | bool Stream::process_until_end_of_stream() |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 174 | { |
| 175 | FLAC__ASSERT(is_valid()); |
Josh Coalson | cfdfc82 | 2002-08-02 06:12:36 +0000 | [diff] [blame] | 176 | return (bool)::FLAC__stream_decoder_process_until_end_of_stream(decoder_); |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Josh Coalson | 47f51b1 | 2004-07-16 00:53:38 +0000 | [diff] [blame] | 179 | bool Stream::skip_single_frame() |
| 180 | { |
| 181 | FLAC__ASSERT(is_valid()); |
| 182 | return (bool)::FLAC__stream_decoder_skip_single_frame(decoder_); |
| 183 | } |
| 184 | |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 185 | ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data) |
| 186 | { |
| 187 | (void)decoder; |
| 188 | FLAC__ASSERT(0 != client_data); |
| 189 | Stream *instance = reinterpret_cast<Stream *>(client_data); |
| 190 | FLAC__ASSERT(0 != instance); |
| 191 | return instance->read_callback(buffer, bytes); |
| 192 | } |
| 193 | |
Josh Coalson | 57ba6f4 | 2002-06-07 05:27:37 +0000 | [diff] [blame] | 194 | ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data) |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 195 | { |
| 196 | (void)decoder; |
| 197 | FLAC__ASSERT(0 != client_data); |
| 198 | Stream *instance = reinterpret_cast<Stream *>(client_data); |
| 199 | FLAC__ASSERT(0 != instance); |
| 200 | return instance->write_callback(frame, buffer); |
| 201 | } |
| 202 | |
Josh Coalson | cc68251 | 2002-06-08 04:53:42 +0000 | [diff] [blame] | 203 | void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data) |
Josh Coalson | b04a16e | 2002-05-29 05:49:50 +0000 | [diff] [blame] | 204 | { |
| 205 | (void)decoder; |
| 206 | FLAC__ASSERT(0 != client_data); |
| 207 | Stream *instance = reinterpret_cast<Stream *>(client_data); |
| 208 | FLAC__ASSERT(0 != instance); |
| 209 | instance->metadata_callback(metadata); |
| 210 | } |
| 211 | |
| 212 | void Stream::error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data) |
| 213 | { |
| 214 | (void)decoder; |
| 215 | FLAC__ASSERT(0 != client_data); |
| 216 | Stream *instance = reinterpret_cast<Stream *>(client_data); |
| 217 | FLAC__ASSERT(0 != instance); |
| 218 | instance->error_callback(status); |
| 219 | } |
| 220 | |
| 221 | }; |
| 222 | }; |