stream_decoder.cpp 5.8 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
		const char *Stream::State::resolved_as_cstring(const Stream &) const
		{
			return as_cstring();
		}

Josh Coalson's avatar
Josh Coalson committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
		Stream::Stream():
		decoder_(::FLAC__stream_decoder_new())
		{ }

		Stream::~Stream()
		{
			if(0 != decoder_) {
				::FLAC__stream_decoder_finish(decoder_);
				::FLAC__stream_decoder_delete(decoder_);
			}
		}

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

48
		bool Stream::set_metadata_respond(::FLAC__MetadataType type)
Josh Coalson's avatar
Josh Coalson committed
49
50
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
51
			return (bool)::FLAC__stream_decoder_set_metadata_respond(decoder_, type);
Josh Coalson's avatar
Josh Coalson committed
52
53
54
55
56
		}

		bool Stream::set_metadata_respond_application(const FLAC__byte id[4])
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
57
			return (bool)::FLAC__stream_decoder_set_metadata_respond_application(decoder_, id);
Josh Coalson's avatar
Josh Coalson committed
58
59
60
61
62
		}

		bool Stream::set_metadata_respond_all()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
63
			return (bool)::FLAC__stream_decoder_set_metadata_respond_all(decoder_);
Josh Coalson's avatar
Josh Coalson committed
64
65
		}

66
		bool Stream::set_metadata_ignore(::FLAC__MetadataType type)
Josh Coalson's avatar
Josh Coalson committed
67
68
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
69
			return (bool)::FLAC__stream_decoder_set_metadata_ignore(decoder_, type);
Josh Coalson's avatar
Josh Coalson committed
70
71
72
73
74
		}

		bool Stream::set_metadata_ignore_application(const FLAC__byte id[4])
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
75
			return (bool)::FLAC__stream_decoder_set_metadata_ignore_application(decoder_, id);
Josh Coalson's avatar
Josh Coalson committed
76
77
78
79
80
		}

		bool Stream::set_metadata_ignore_all()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
81
			return (bool)::FLAC__stream_decoder_set_metadata_ignore_all(decoder_);
Josh Coalson's avatar
Josh Coalson committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
		}

		Stream::State Stream::get_state() const
		{
			FLAC__ASSERT(is_valid());
			return State(::FLAC__stream_decoder_get_state(decoder_));
		}

		unsigned Stream::get_channels() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_decoder_get_channels(decoder_);
		}

		::FLAC__ChannelAssignment Stream::get_channel_assignment() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_decoder_get_channel_assignment(decoder_);
		}

		unsigned Stream::get_bits_per_sample() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_decoder_get_bits_per_sample(decoder_);
		}

		unsigned Stream::get_sample_rate() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_decoder_get_sample_rate(decoder_);
		}

		unsigned Stream::get_blocksize() const
		{
			FLAC__ASSERT(is_valid());
			return ::FLAC__stream_decoder_get_blocksize(decoder_);
		}

		Stream::State Stream::init()
		{
			FLAC__ASSERT(is_valid());
			::FLAC__stream_decoder_set_read_callback(decoder_, read_callback_);
			::FLAC__stream_decoder_set_write_callback(decoder_, write_callback_);
			::FLAC__stream_decoder_set_metadata_callback(decoder_, metadata_callback_);
			::FLAC__stream_decoder_set_error_callback(decoder_, error_callback_);
			::FLAC__stream_decoder_set_client_data(decoder_, (void*)this);
			return State(::FLAC__stream_decoder_init(decoder_));
		}

		void Stream::finish()
		{
			FLAC__ASSERT(is_valid());
			::FLAC__stream_decoder_finish(decoder_);
		}

		bool Stream::flush()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
140
			return (bool)::FLAC__stream_decoder_flush(decoder_);
Josh Coalson's avatar
Josh Coalson committed
141
142
143
144
145
		}

		bool Stream::reset()
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
146
			return (bool)::FLAC__stream_decoder_reset(decoder_);
Josh Coalson's avatar
Josh Coalson committed
147
148
		}

Josh Coalson's avatar
Josh Coalson committed
149
		bool Stream::process_single()
Josh Coalson's avatar
Josh Coalson committed
150
151
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
152
			return (bool)::FLAC__stream_decoder_process_single(decoder_);
Josh Coalson's avatar
Josh Coalson committed
153
154
		}

Josh Coalson's avatar
Josh Coalson committed
155
		bool Stream::process_until_end_of_metadata()
Josh Coalson's avatar
Josh Coalson committed
156
157
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
158
			return (bool)::FLAC__stream_decoder_process_until_end_of_metadata(decoder_);
Josh Coalson's avatar
Josh Coalson committed
159
160
		}

Josh Coalson's avatar
Josh Coalson committed
161
		bool Stream::process_until_end_of_stream()
Josh Coalson's avatar
Josh Coalson committed
162
163
		{
			FLAC__ASSERT(is_valid());
Josh Coalson's avatar
Josh Coalson committed
164
			return (bool)::FLAC__stream_decoder_process_until_end_of_stream(decoder_);
Josh Coalson's avatar
Josh Coalson committed
165
166
167
168
169
170
171
172
173
174
175
		}

		::FLAC__StreamDecoderReadStatus Stream::read_callback_(const ::FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
		{
			(void)decoder;
			FLAC__ASSERT(0 != client_data);
			Stream *instance = reinterpret_cast<Stream *>(client_data);
			FLAC__ASSERT(0 != instance);
			return instance->read_callback(buffer, bytes);
		}

176
		::FLAC__StreamDecoderWriteStatus Stream::write_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
Josh Coalson's avatar
Josh Coalson committed
177
178
179
180
181
182
183
184
		{
			(void)decoder;
			FLAC__ASSERT(0 != client_data);
			Stream *instance = reinterpret_cast<Stream *>(client_data);
			FLAC__ASSERT(0 != instance);
			return instance->write_callback(frame, buffer);
		}

185
		void Stream::metadata_callback_(const ::FLAC__StreamDecoder *decoder, const ::FLAC__StreamMetadata *metadata, void *client_data)
Josh Coalson's avatar
Josh Coalson committed
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
		{
			(void)decoder;
			FLAC__ASSERT(0 != client_data);
			Stream *instance = reinterpret_cast<Stream *>(client_data);
			FLAC__ASSERT(0 != instance);
			instance->metadata_callback(metadata);
		}

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

	};
};