rename (FLAC__format_is_valid_sample_rate() to FLAC__format_sample_rate_is_valid()
[flac.git] / src / libFLAC++ / stream_encoder.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++/encoder.h"
21 #include "FLAC/assert.h"
22
23 namespace FLAC {
24         namespace Encoder {
25
26                 Stream::Stream():
27                 encoder_(::FLAC__stream_encoder_new())
28                 { }
29
30                 Stream::~Stream()
31                 {
32                         if(0 != encoder_) {
33                                 ::FLAC__stream_encoder_finish(encoder_);
34                                 ::FLAC__stream_encoder_delete(encoder_);
35                         }
36                 }
37
38                 bool Stream::is_valid() const
39                 {
40                         return 0 != encoder_;
41                 }
42
43                 bool Stream::set_streamable_subset(bool value)
44                 {
45                         FLAC__ASSERT(is_valid());
46                         return (bool)::FLAC__stream_encoder_set_streamable_subset(encoder_, value);
47                 }
48
49                 bool Stream::set_do_mid_side_stereo(bool value)
50                 {
51                         FLAC__ASSERT(is_valid());
52                         return (bool)::FLAC__stream_encoder_set_do_mid_side_stereo(encoder_, value);
53                 }
54
55                 bool Stream::set_loose_mid_side_stereo(bool value)
56                 {
57                         FLAC__ASSERT(is_valid());
58                         return (bool)::FLAC__stream_encoder_set_loose_mid_side_stereo(encoder_, value);
59                 }
60
61                 bool Stream::set_channels(unsigned value)
62                 {
63                         FLAC__ASSERT(is_valid());
64                         return (bool)::FLAC__stream_encoder_set_channels(encoder_, value);
65                 }
66
67                 bool Stream::set_bits_per_sample(unsigned value)
68                 {
69                         FLAC__ASSERT(is_valid());
70                         return (bool)::FLAC__stream_encoder_set_bits_per_sample(encoder_, value);
71                 }
72
73                 bool Stream::set_sample_rate(unsigned value)
74                 {
75                         FLAC__ASSERT(is_valid());
76                         return (bool)::FLAC__stream_encoder_set_sample_rate(encoder_, value);
77                 }
78
79                 bool Stream::set_blocksize(unsigned value)
80                 {
81                         FLAC__ASSERT(is_valid());
82                         return (bool)::FLAC__stream_encoder_set_blocksize(encoder_, value);
83                 }
84
85                 bool Stream::set_max_lpc_order(unsigned value)
86                 {
87                         FLAC__ASSERT(is_valid());
88                         return (bool)::FLAC__stream_encoder_set_max_lpc_order(encoder_, value);
89                 }
90
91                 bool Stream::set_qlp_coeff_precision(unsigned value)
92                 {
93                         FLAC__ASSERT(is_valid());
94                         return (bool)::FLAC__stream_encoder_set_qlp_coeff_precision(encoder_, value);
95                 }
96
97                 bool Stream::set_do_qlp_coeff_prec_search(bool value)
98                 {
99                         FLAC__ASSERT(is_valid());
100                         return (bool)::FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder_, value);
101                 }
102
103                 bool Stream::set_do_escape_coding(bool value)
104                 {
105                         FLAC__ASSERT(is_valid());
106                         return (bool)::FLAC__stream_encoder_set_do_escape_coding(encoder_, value);
107                 }
108
109                 bool Stream::set_do_exhaustive_model_search(bool value)
110                 {
111                         FLAC__ASSERT(is_valid());
112                         return (bool)::FLAC__stream_encoder_set_do_exhaustive_model_search(encoder_, value);
113                 }
114
115                 bool Stream::set_min_residual_partition_order(unsigned value)
116                 {
117                         FLAC__ASSERT(is_valid());
118                         return (bool)::FLAC__stream_encoder_set_min_residual_partition_order(encoder_, value);
119                 }
120
121                 bool Stream::set_max_residual_partition_order(unsigned value)
122                 {
123                         FLAC__ASSERT(is_valid());
124                         return (bool)::FLAC__stream_encoder_set_max_residual_partition_order(encoder_, value);
125                 }
126
127                 bool Stream::set_rice_parameter_search_dist(unsigned value)
128                 {
129                         FLAC__ASSERT(is_valid());
130                         return (bool)::FLAC__stream_encoder_set_rice_parameter_search_dist(encoder_, value);
131                 }
132
133                 bool Stream::set_total_samples_estimate(FLAC__uint64 value)
134                 {
135                         FLAC__ASSERT(is_valid());
136                         return (bool)::FLAC__stream_encoder_set_total_samples_estimate(encoder_, value);
137                 }
138
139                 bool Stream::set_metadata(::FLAC__StreamMetadata **metadata, unsigned num_blocks)
140                 {
141                         FLAC__ASSERT(is_valid());
142                         return (bool)::FLAC__stream_encoder_set_metadata(encoder_, metadata, num_blocks);
143                 }
144
145                 Stream::State Stream::get_state() const
146                 {
147                         FLAC__ASSERT(is_valid());
148                         return State(::FLAC__stream_encoder_get_state(encoder_));
149                 }
150
151                 bool Stream::get_streamable_subset() const
152                 {
153                         FLAC__ASSERT(is_valid());
154                         return (bool)::FLAC__stream_encoder_get_streamable_subset(encoder_);
155                 }
156
157                 bool Stream::get_do_mid_side_stereo() const
158                 {
159                         FLAC__ASSERT(is_valid());
160                         return (bool)::FLAC__stream_encoder_get_do_mid_side_stereo(encoder_);
161                 }
162
163                 bool Stream::get_loose_mid_side_stereo() const
164                 {
165                         FLAC__ASSERT(is_valid());
166                         return (bool)::FLAC__stream_encoder_get_loose_mid_side_stereo(encoder_);
167                 }
168
169                 unsigned Stream::get_channels() const
170                 {
171                         FLAC__ASSERT(is_valid());
172                         return ::FLAC__stream_encoder_get_channels(encoder_);
173                 }
174
175                 unsigned Stream::get_bits_per_sample() const
176                 {
177                         FLAC__ASSERT(is_valid());
178                         return ::FLAC__stream_encoder_get_bits_per_sample(encoder_);
179                 }
180
181                 unsigned Stream::get_sample_rate() const
182                 {
183                         FLAC__ASSERT(is_valid());
184                         return ::FLAC__stream_encoder_get_sample_rate(encoder_);
185                 }
186
187                 unsigned Stream::get_blocksize() const
188                 {
189                         FLAC__ASSERT(is_valid());
190                         return ::FLAC__stream_encoder_get_blocksize(encoder_);
191                 }
192
193                 unsigned Stream::get_max_lpc_order() const
194                 {
195                         FLAC__ASSERT(is_valid());
196                         return ::FLAC__stream_encoder_get_max_lpc_order(encoder_);
197                 }
198
199                 unsigned Stream::get_qlp_coeff_precision() const
200                 {
201                         FLAC__ASSERT(is_valid());
202                         return ::FLAC__stream_encoder_get_qlp_coeff_precision(encoder_);
203                 }
204
205                 bool Stream::get_do_qlp_coeff_prec_search() const
206                 {
207                         FLAC__ASSERT(is_valid());
208                         return (bool)::FLAC__stream_encoder_get_do_qlp_coeff_prec_search(encoder_);
209                 }
210
211                 bool Stream::get_do_escape_coding() const
212                 {
213                         FLAC__ASSERT(is_valid());
214                         return (bool)::FLAC__stream_encoder_get_do_escape_coding(encoder_);
215                 }
216
217                 bool Stream::get_do_exhaustive_model_search() const
218                 {
219                         FLAC__ASSERT(is_valid());
220                         return (bool)::FLAC__stream_encoder_get_do_exhaustive_model_search(encoder_);
221                 }
222
223                 unsigned Stream::get_min_residual_partition_order() const
224                 {
225                         FLAC__ASSERT(is_valid());
226                         return ::FLAC__stream_encoder_get_min_residual_partition_order(encoder_);
227                 }
228
229                 unsigned Stream::get_max_residual_partition_order() const
230                 {
231                         FLAC__ASSERT(is_valid());
232                         return ::FLAC__stream_encoder_get_max_residual_partition_order(encoder_);
233                 }
234
235                 unsigned Stream::get_rice_parameter_search_dist() const
236                 {
237                         FLAC__ASSERT(is_valid());
238                         return ::FLAC__stream_encoder_get_rice_parameter_search_dist(encoder_);
239                 }
240
241                 Stream::State Stream::init()
242                 {
243                         FLAC__ASSERT(is_valid());
244                         ::FLAC__stream_encoder_set_write_callback(encoder_, write_callback_);
245                         ::FLAC__stream_encoder_set_metadata_callback(encoder_, metadata_callback_);
246                         ::FLAC__stream_encoder_set_client_data(encoder_, (void*)this);
247                         return State(::FLAC__stream_encoder_init(encoder_));
248                 }
249
250                 void Stream::finish()
251                 {
252                         FLAC__ASSERT(is_valid());
253                         ::FLAC__stream_encoder_finish(encoder_);
254                 }
255
256                 bool Stream::process(const FLAC__int32 * const buffer[], unsigned samples)
257                 {
258                         FLAC__ASSERT(is_valid());
259                         return (bool)::FLAC__stream_encoder_process(encoder_, buffer, samples);
260                 }
261
262                 bool Stream::process_interleaved(const FLAC__int32 buffer[], unsigned samples)
263                 {
264                         FLAC__ASSERT(is_valid());
265                         return (bool)::FLAC__stream_encoder_process_interleaved(encoder_, buffer, samples);
266                 }
267
268                 ::FLAC__StreamEncoderWriteStatus Stream::write_callback_(const ::FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
269                 {
270                         (void)encoder;
271                         FLAC__ASSERT(0 != client_data);
272                         Stream *instance = reinterpret_cast<Stream *>(client_data);
273                         FLAC__ASSERT(0 != instance);
274                         return instance->write_callback(buffer, bytes, samples, current_frame);
275                 }
276
277                 void Stream::metadata_callback_(const ::FLAC__StreamEncoder *encoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
278                 {
279                         (void)encoder;
280                         FLAC__ASSERT(0 != client_data);
281                         Stream *instance = reinterpret_cast<Stream *>(client_data);
282                         FLAC__ASSERT(0 != instance);
283                         instance->metadata_callback(metadata);
284                 }
285
286         };
287 };