file_decoder.cpp 6.99 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* libFLAC++ - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2002,2003,2004,2005  Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
 *
4
5
6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Josh Coalson's avatar
Josh Coalson committed
7
 *
8
9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
Josh Coalson's avatar
Josh Coalson committed
10
 *
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Xiph.org Foundation nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson's avatar
Josh Coalson committed
30
31
32
33
34
 */

#include "FLAC++/decoder.h"
#include "FLAC/assert.h"

35
36
37
38
39
#ifdef _MSC_VER
// warning C4800: 'int' : forcing to bool 'true' or 'false' (performance warning)
#pragma warning ( disable : 4800 )
#endif

Josh Coalson's avatar
Josh Coalson committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace FLAC {
	namespace Decoder {

		File::File():
		decoder_(::FLAC__file_decoder_new())
		{ }

		File::~File()
		{
			if(0 != decoder_) {
				(void) ::FLAC__file_decoder_finish(decoder_);
				::FLAC__file_decoder_delete(decoder_);
			}
		}

		bool File::is_valid() const
		{
			return 0 != decoder_;
		}

		bool File::set_md5_checking(bool value)
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
63
			return (bool)::FLAC__file_decoder_set_md5_checking(decoder_, value);
Josh Coalson's avatar
Josh Coalson committed
64
65
66
67
68
		}

		bool File::set_filename(const char *value)
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
69
			return (bool)::FLAC__file_decoder_set_filename(decoder_, value);
Josh Coalson's avatar
Josh Coalson committed
70
71
		}

72
		bool File::set_metadata_respond(::FLAC__MetadataType type)
Josh Coalson's avatar
Josh Coalson committed
73
74
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
75
			return (bool)::FLAC__file_decoder_set_metadata_respond(decoder_, type);
Josh Coalson's avatar
Josh Coalson committed
76
77
78
79
80
		}

		bool File::set_metadata_respond_application(const FLAC__byte id[4])
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
81
			return (bool)::FLAC__file_decoder_set_metadata_respond_application(decoder_, id);
Josh Coalson's avatar
Josh Coalson committed
82
83
84
85
86
		}

		bool File::set_metadata_respond_all()
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
87
			return (bool)::FLAC__file_decoder_set_metadata_respond_all(decoder_);
Josh Coalson's avatar
Josh Coalson committed
88
89
		}

90
		bool File::set_metadata_ignore(::FLAC__MetadataType type)
Josh Coalson's avatar
Josh Coalson committed
91
92
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
93
			return (bool)::FLAC__file_decoder_set_metadata_ignore(decoder_, type);
Josh Coalson's avatar
Josh Coalson committed
94
95
96
97
98
		}

		bool File::set_metadata_ignore_application(const FLAC__byte id[4])
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
99
			return (bool)::FLAC__file_decoder_set_metadata_ignore_application(decoder_, id);
Josh Coalson's avatar
Josh Coalson committed
100
101
102
103
104
		}

		bool File::set_metadata_ignore_all()
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
105
			return (bool)::FLAC__file_decoder_set_metadata_ignore_all(decoder_);
Josh Coalson's avatar
Josh Coalson committed
106
107
108
109
110
111
112
113
		}

		File::State File::get_state() const
		{
			FLAC__ASSERT(0 != decoder_);
			return State(::FLAC__file_decoder_get_state(decoder_));
		}

Josh Coalson's avatar
Josh Coalson committed
114
115
116
117
118
119
120
121
122
123
124
125
		SeekableStream::State File::get_seekable_stream_decoder_state() const
		{
			FLAC__ASSERT(0 != decoder_);
			return SeekableStream::State(::FLAC__file_decoder_get_seekable_stream_decoder_state(decoder_));
		}

		Stream::State File::get_stream_decoder_state() const
		{
			FLAC__ASSERT(0 != decoder_);
			return Stream::State(::FLAC__file_decoder_get_stream_decoder_state(decoder_));
		}

Josh Coalson's avatar
Josh Coalson committed
126
127
128
		bool File::get_md5_checking() const
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
129
			return (bool)::FLAC__file_decoder_get_md5_checking(decoder_);
Josh Coalson's avatar
Josh Coalson committed
130
131
		}

132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
		unsigned File::get_channels() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__file_decoder_get_channels(decoder_);
		}

		::FLAC__ChannelAssignment File::get_channel_assignment() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__file_decoder_get_channel_assignment(decoder_);
		}

		unsigned File::get_bits_per_sample() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__file_decoder_get_bits_per_sample(decoder_);
		}

		unsigned File::get_sample_rate() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__file_decoder_get_sample_rate(decoder_);
		}

		unsigned File::get_blocksize() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__file_decoder_get_blocksize(decoder_);
		}

Josh Coalson's avatar
Josh Coalson committed
162
163
164
165
166
167
168
169
170
171
172
173
174
		File::State File::init()
		{
			FLAC__ASSERT(0 != decoder_);
			::FLAC__file_decoder_set_write_callback(decoder_, write_callback_);
			::FLAC__file_decoder_set_metadata_callback(decoder_, metadata_callback_);
			::FLAC__file_decoder_set_error_callback(decoder_, error_callback_);
			::FLAC__file_decoder_set_client_data(decoder_, (void*)this);
			return State(::FLAC__file_decoder_init(decoder_));
		}

		bool File::finish()
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
175
			return (bool)::FLAC__file_decoder_finish(decoder_);
Josh Coalson's avatar
Josh Coalson committed
176
177
		}

Josh Coalson's avatar
Josh Coalson committed
178
		bool File::process_single()
Josh Coalson's avatar
Josh Coalson committed
179
180
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
181
			return (bool)::FLAC__file_decoder_process_single(decoder_);
Josh Coalson's avatar
Josh Coalson committed
182
183
		}

Josh Coalson's avatar
Josh Coalson committed
184
		bool File::process_until_end_of_metadata()
Josh Coalson's avatar
Josh Coalson committed
185
186
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
187
			return (bool)::FLAC__file_decoder_process_until_end_of_metadata(decoder_);
Josh Coalson's avatar
Josh Coalson committed
188
189
		}

Josh Coalson's avatar
Josh Coalson committed
190
		bool File::process_until_end_of_file()
Josh Coalson's avatar
Josh Coalson committed
191
192
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
193
			return (bool)::FLAC__file_decoder_process_until_end_of_file(decoder_);
Josh Coalson's avatar
Josh Coalson committed
194
195
		}

196
197
198
199
200
201
		bool File::skip_single_frame()
		{
			FLAC__ASSERT(is_valid());
			return (bool)::FLAC__file_decoder_skip_single_frame(decoder_);
		}

Josh Coalson's avatar
Josh Coalson committed
202
203
204
		bool File::seek_absolute(FLAC__uint64 sample)
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
205
			return (bool)::FLAC__file_decoder_seek_absolute(decoder_, sample);
Josh Coalson's avatar
Josh Coalson committed
206
207
		}

208
		::FLAC__StreamDecoderWriteStatus File::write_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
Josh Coalson's avatar
Josh Coalson committed
209
210
211
212
213
214
215
216
		{
			(void) decoder;
			FLAC__ASSERT(0 != client_data);
			File *instance = reinterpret_cast<File *>(client_data);
			FLAC__ASSERT(0 != instance);
			return instance->write_callback(frame, buffer);
		}

217
		void File::metadata_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
		{
			(void) decoder;
			FLAC__ASSERT(0 != client_data);
			File *instance = reinterpret_cast<File *>(client_data);
			FLAC__ASSERT(0 != instance);
			instance->metadata_callback(metadata);
		}

		void File::error_callback_(const ::FLAC__FileDecoder *decoder, ::FLAC__StreamDecoderErrorStatus status, void *client_data)
		{
			(void) decoder;
			FLAC__ASSERT(0 != client_data);
			File *instance = reinterpret_cast<File *>(client_data);
			FLAC__ASSERT(0 != instance);
			instance->error_callback(status);
		}

235
236
	}
}