rename (FLAC__format_is_valid_sample_rate() to FLAC__format_sample_rate_is_valid()
[flac.git] / src / libFLAC++ / stream_decoder.cc
1 /* libFLAC++ - Free Lossless Audio Codec library
2  * Copyright (C) 2002  Josh Coalson
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA  02111-1307, USA.
18  */
19
20 #include "FLAC++/decoder.h"
21 #include "FLAC/assert.h"
22
23 namespace FLAC {
24         namespace Decoder {
25
26                 Stream::Stream():
27                 decoder_(::FLAC__stream_decoder_new())
28                 { }
29
30                 Stream::~Stream()
31                 {
32                         if(0 != decoder_) {
33                                 ::FLAC__stream_decoder_finish(decoder_);
34                                 ::FLAC__stream_decoder_delete(decoder_);
35                         }
36                 }
37
38                 bool Stream::is_valid() const
39                 {
40                         return 0 != decoder_;
41                 }
42
43                 bool Stream::set_metadata_respond(::FLAC__MetadataType type)
44                 {
45                         FLAC__ASSERT(is_valid());
46                         return (bool)::FLAC__stream_decoder_set_metadata_respond(decoder_, type);
47                 }
48
49                 bool Stream::set_metadata_respond_application(const FLAC__byte id[4])
50                 {
51                         FLAC__ASSERT(is_valid());
52                         return (bool)::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id);
53                 }
54
55                 bool Stream::set_metadata_respond_all()
56                 {
57                         FLAC__ASSERT(is_valid());
58                         return (bool)::FLAC__stream_decoder_set_metadata_respond_all(decoder_);
59                 }
60
61                 bool Stream::set_metadata_ignore(::FLAC__MetadataType type)
62                 {
63                         FLAC__ASSERT(is_valid());
64                         return (bool)::FLAC__stream_decoder_set_metadata_ignore(decoder_, type);
65                 }
66
67                 bool Stream::set_metadata_ignore_application(const FLAC__byte id[4])
68                 {
69                         FLAC__ASSERT(is_valid());
70                         return (bool)::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id);
71                 }
72
73                 bool Stream::set_metadata_ignore_all()
74                 {
75                         FLAC__ASSERT(is_valid());
76                         return (bool)::FLAC__stream_decoder_set_metadata_ignore_all(decoder_);
77                 }
78
79                 Stream::State Stream::get_state() const
80                 {
81                         FLAC__ASSERT(is_valid());
82                         return State(::FLAC__stream_decoder_get_state(decoder_));
83                 }
84
85                 unsigned Stream::get_channels() const
86                 {
87                         FLAC__ASSERT(is_valid());
88                         return ::FLAC__stream_decoder_get_channels(decoder_);
89                 }
90
91                 ::FLAC__ChannelAssignment Stream::get_channel_assignment() const
92                 {
93                         FLAC__ASSERT(is_valid());
94                         return ::FLAC__stream_decoder_get_channel_assignment(decoder_);
95                 }
96
97                 unsigned Stream::get_bits_per_sample() const
98                 {
99                         FLAC__ASSERT(is_valid());
100                         return ::FLAC__stream_decoder_get_bits_per_sample(decoder_);
101                 }
102
103                 unsigned Stream::get_sample_rate() const
104                 {
105                         FLAC__ASSERT(is_valid());
106                         return ::FLAC__stream_decoder_get_sample_rate(decoder_);
107                 }
108
109                 unsigned Stream::get_blocksize() const
110                 {
111                         FLAC__ASSERT(is_valid());
112                         return ::FLAC__stream_decoder_get_blocksize(decoder_);
113                 }
114
115                 Stream::State Stream::init()
116                 {
117                         FLAC__ASSERT(is_valid());
118                         ::FLAC__stream_decoder_set_read_callback(decoder_, read_callback_);
119                         ::FLAC__stream_decoder_set_write_callback(decoder_, write_callback_);
120                         ::FLAC__stream_decoder_set_metadata_callback(decoder_, metadata_callback_);
121                         ::FLAC__stream_decoder_set_error_callback(decoder_, error_callback_);
122                         ::FLAC__stream_decoder_set_client_data(decoder_, (void*)this);
123                         return State(::FLAC__stream_decoder_init(decoder_));
124                 }
125
126                 void Stream::finish()
127                 {
128                         FLAC__ASSERT(is_valid());
129                         ::FLAC__stream_decoder_finish(decoder_);
130                 }
131
132                 bool Stream::flush()
133                 {
134                         FLAC__ASSERT(is_valid());
135                         return (bool)::FLAC__stream_decoder_flush(decoder_);
136                 }
137
138                 bool Stream::reset()
139                 {
140                         FLAC__ASSERT(is_valid());
141                         return (bool)::FLAC__stream_decoder_reset(decoder_);
142                 }
143
144                 bool Stream::process_whole_stream()
145                 {
146                         FLAC__ASSERT(is_valid());
147                         return (bool)::FLAC__stream_decoder_process_whole_stream(decoder_);
148                 }
149
150                 bool Stream::process_metadata()
151                 {
152                         FLAC__ASSERT(is_valid());
153                         return (bool)::FLAC__stream_decoder_process_metadata(decoder_);
154                 }
155
156                 bool Stream::process_one_frame()
157                 {
158                         FLAC__ASSERT(is_valid());
159                         return (bool)::FLAC__stream_decoder_process_one_frame(decoder_);
160                 }
161
162                 bool Stream::process_remaining_frames()
163                 {
164                         FLAC__ASSERT(is_valid());
165                         return (bool)::FLAC__stream_decoder_process_remaining_frames(decoder_);
166                 }
167
168                 ::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
169                 {
170                         (void)decoder;
171                         FLAC__ASSERT(0 != client_data);
172                         Stream *instance = reinterpret_cast<Stream *>(client_data);
173                         FLAC__ASSERT(0 != instance);
174                         return instance->read_callback(buffer, bytes);
175                 }
176
177                 ::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
178                 {
179                         (void)decoder;
180                         FLAC__ASSERT(0 != client_data);
181                         Stream *instance = reinterpret_cast<Stream *>(client_data);
182                         FLAC__ASSERT(0 != instance);
183                         return instance->write_callback(frame, buffer);
184                 }
185
186                 void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
187                 {
188                         (void)decoder;
189                         FLAC__ASSERT(0 != client_data);
190                         Stream *instance = reinterpret_cast<Stream *>(client_data);
191                         FLAC__ASSERT(0 != instance);
192                         instance->metadata_callback(metadata);
193                 }
194
195                 void Stream::error_callback_(const ::FLAC__StreamDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
196                 {
197                         (void)decoder;
198                         FLAC__ASSERT(0 != client_data);
199                         Stream *instance = reinterpret_cast<Stream *>(client_data);
200                         FLAC__ASSERT(0 != instance);
201                         instance->error_callback(status);
202                 }
203
204         };
205 };