file_decoder.cpp 6.42 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* libFLAC++ - Free Lossless Audio Codec library
 * Copyright (C) 2002  Josh Coalson
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 */

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

namespace FLAC {
	namespace Decoder {

26
27
28
29
30
31
32
33
34
35
36
37
38
		const char *File::State::resolved_as_cstring(const File &decoder) const
		{
			if(state_ == ::FLAC__FILE_DECODER_SEEKABLE_STREAM_DECODER_ERROR) {
				FLAC::Decoder::SeekableStream::State state__ = decoder.get_seekable_stream_decoder_state();
				if(state__ == ::FLAC__SEEKABLE_STREAM_DECODER_STREAM_DECODER_ERROR)
					return decoder.get_stream_decoder_state().as_cstring();
				else
					return state__.as_cstring();
			}
			else
				return as_cstring();
		}

Josh Coalson's avatar
Josh Coalson committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
		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
59
			return (bool)::FLAC__file_decoder_set_md5_checking(decoder_, value);
Josh Coalson's avatar
Josh Coalson committed
60
61
62
63
64
		}

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

68
		bool File::set_metadata_respond(::FLAC__MetadataType type)
Josh Coalson's avatar
Josh Coalson committed
69
70
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
71
			return (bool)::FLAC__file_decoder_set_metadata_respond(decoder_, type);
Josh Coalson's avatar
Josh Coalson committed
72
73
74
75
76
		}

		bool File::set_metadata_respond_application(const FLAC__byte id[4])
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
77
			return (bool)::FLAC__file_decoder_set_metadata_respond_application(decoder_, id);
Josh Coalson's avatar
Josh Coalson committed
78
79
80
81
82
		}

		bool File::set_metadata_respond_all()
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
83
			return (bool)::FLAC__file_decoder_set_metadata_respond_all(decoder_);
Josh Coalson's avatar
Josh Coalson committed
84
85
		}

86
		bool File::set_metadata_ignore(::FLAC__MetadataType type)
Josh Coalson's avatar
Josh Coalson committed
87
88
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
89
			return (bool)::FLAC__file_decoder_set_metadata_ignore(decoder_, type);
Josh Coalson's avatar
Josh Coalson committed
90
91
92
93
94
		}

		bool File::set_metadata_ignore_application(const FLAC__byte id[4])
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
95
			return (bool)::FLAC__file_decoder_set_metadata_ignore_application(decoder_, id);
Josh Coalson's avatar
Josh Coalson committed
96
97
98
99
100
		}

		bool File::set_metadata_ignore_all()
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
101
			return (bool)::FLAC__file_decoder_set_metadata_ignore_all(decoder_);
Josh Coalson's avatar
Josh Coalson committed
102
103
104
105
106
107
108
109
		}

		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
110
111
112
113
114
115
116
117
118
119
120
121
		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
122
123
124
		bool File::get_md5_checking() const
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
125
			return (bool)::FLAC__file_decoder_get_md5_checking(decoder_);
Josh Coalson's avatar
Josh Coalson committed
126
127
		}

128
129
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
		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
158
159
160
161
162
163
164
165
166
167
168
169
170
		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
171
			return (bool)::FLAC__file_decoder_finish(decoder_);
Josh Coalson's avatar
Josh Coalson committed
172
173
		}

Josh Coalson's avatar
Josh Coalson committed
174
		bool File::process_single()
Josh Coalson's avatar
Josh Coalson committed
175
176
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
177
			return (bool)::FLAC__file_decoder_process_single(decoder_);
Josh Coalson's avatar
Josh Coalson committed
178
179
		}

Josh Coalson's avatar
Josh Coalson committed
180
		bool File::process_until_end_of_metadata()
Josh Coalson's avatar
Josh Coalson committed
181
182
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
183
			return (bool)::FLAC__file_decoder_process_until_end_of_metadata(decoder_);
Josh Coalson's avatar
Josh Coalson committed
184
185
		}

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

		bool File::seek_absolute(FLAC__uint64 sample)
		{
			FLAC__ASSERT(0 != decoder_);
Josh Coalson's avatar
Josh Coalson committed
195
			return (bool)::FLAC__file_decoder_seek_absolute(decoder_, sample);
Josh Coalson's avatar
Josh Coalson committed
196
197
		}

198
		::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
199
200
201
202
203
204
205
206
		{
			(void) decoder;
			FLAC__ASSERT(0 != client_data);
			File *instance = reinterpret_cast<File *>(client_data);
			FLAC__ASSERT(0 != instance);
			return instance->write_callback(frame, buffer);
		}

207
		void File::metadata_callback_(const ::FLAC__FileDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
		{
			(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);
		}

	};
};