easyflac.c 12.5 KB
Newer Older
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 26 27 28 29 30 31 32 33 34 35
/* EasyFLAC - A thin decoding wrapper around libFLAC and libOggFLAC to
 * make your code less ugly.  See easyflac.h for explanation.
 *
 * Copyright 2003 - Stan Seibert <volsung@xiph.org>
 * This code is licensed under a BSD style license:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - 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.
 *
 */

36 37 38 39
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

40 41 42 43 44 45
#include <stdlib.h>
#include "easyflac.h"


FLAC__bool EasyFLAC__is_oggflac(EasyFLAC__StreamDecoder *decoder)
{
46
    return decoder->is_oggflac;
47 48 49 50 51
}


EasyFLAC__StreamDecoder *EasyFLAC__stream_decoder_new(FLAC__bool is_oggflac)
{
52
    EasyFLAC__StreamDecoder *decoder = malloc(sizeof(EasyFLAC__StreamDecoder));
53

54 55 56 57 58 59 60 61
    if (decoder != NULL)
    {
        decoder->is_oggflac = is_oggflac;
        
        if (decoder->is_oggflac)
            decoder->oggflac = OggFLAC__stream_decoder_new();
        else
            decoder->flac = FLAC__stream_decoder_new();
62

63 64 65 66 67 68 69
        if (  (decoder->is_oggflac && decoder->oggflac == NULL)
            ||(!decoder->is_oggflac && decoder->flac == NULL)  )
        {
            free(decoder);
            decoder = NULL;
        }
    }
70

71
    return decoder;
72 73 74 75 76
}


void EasyFLAC__stream_decoder_delete(EasyFLAC__StreamDecoder *decoder)
{
77 78 79 80
    if (decoder->is_oggflac)
        OggFLAC__stream_decoder_delete(decoder->oggflac);
    else
        FLAC__stream_decoder_delete(decoder->flac);
81

82
    free(decoder);
83 84 85 86 87 88 89
}


/* Wrappers around the callbacks for OggFLAC */

FLAC__StreamDecoderReadStatus oggflac_read_callback(const OggFLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
{
90
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
91

92
    return (*e_decoder->callbacks.read)(e_decoder, buffer, bytes, e_decoder->callbacks.client_data);
93 94 95 96 97
}


FLAC__StreamDecoderWriteStatus oggflac_write_callback(const OggFLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
98
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
99

100
    return (*(e_decoder->callbacks.write))(e_decoder, frame, buffer, e_decoder->callbacks.client_data);
101 102 103 104 105
}


void oggflac_metadata_callback(const OggFLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
106
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
107

108
    (*e_decoder->callbacks.metadata)(e_decoder, metadata, e_decoder->callbacks.client_data);
109 110 111 112 113
}


void oggflac_error_callback(const OggFLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
114
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
115

116
    (*e_decoder->callbacks.error)(e_decoder, status, e_decoder->callbacks.client_data);
117 118 119 120 121 122 123
}


/* Wrappers around the callbacks for FLAC */

FLAC__StreamDecoderReadStatus flac_read_callback(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], unsigned *bytes, void *client_data)
{
124
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
125

126
    return (*e_decoder->callbacks.read)(e_decoder, buffer, bytes, e_decoder->callbacks.client_data);
127 128 129 130 131
}


FLAC__StreamDecoderWriteStatus flac_write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
132
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
133

134
    return (*e_decoder->callbacks.write)(e_decoder, frame, buffer, e_decoder->callbacks.client_data);
135 136 137 138 139
}


void flac_metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
140
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
141

142
    (*e_decoder->callbacks.metadata)(e_decoder, metadata, e_decoder->callbacks.client_data);
143 144 145 146 147
}


void flac_error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
148
    EasyFLAC__StreamDecoder *e_decoder = (EasyFLAC__StreamDecoder *) client_data;
149

150
    (*e_decoder->callbacks.error)(e_decoder, status, e_decoder->callbacks.client_data);
151 152 153 154 155
}


FLAC__bool EasyFLAC__set_read_callback(EasyFLAC__StreamDecoder *decoder, EasyFLAC__StreamDecoderReadCallback value)
{
156
    decoder->callbacks.read = value;
157

158 159 160 161
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_read_callback(decoder->oggflac, &oggflac_read_callback);
    else
        return FLAC__stream_decoder_set_read_callback(decoder->flac, &flac_read_callback);
162 163 164 165 166
}


FLAC__bool EasyFLAC__set_write_callback(EasyFLAC__StreamDecoder *decoder, EasyFLAC__StreamDecoderWriteCallback value)
{
167
    decoder->callbacks.write = value;
168

169 170 171 172
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_write_callback(decoder->oggflac, &oggflac_write_callback);
    else
        return FLAC__stream_decoder_set_write_callback(decoder->flac, &flac_write_callback);
173 174 175 176 177
}


FLAC__bool EasyFLAC__set_metadata_callback(EasyFLAC__StreamDecoder *decoder, EasyFLAC__StreamDecoderMetadataCallback value)
{
178
    decoder->callbacks.metadata = value;
179

180 181 182 183
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_metadata_callback(decoder->oggflac, &oggflac_metadata_callback);
    else
        return FLAC__stream_decoder_set_metadata_callback(decoder->flac, &flac_metadata_callback);
184 185 186 187 188
}


FLAC__bool EasyFLAC__set_error_callback(EasyFLAC__StreamDecoder *decoder, EasyFLAC__StreamDecoderErrorCallback value)
{
189
    decoder->callbacks.error = value;
190

191 192 193 194
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_error_callback(decoder->oggflac, &oggflac_error_callback);
    else
        return FLAC__stream_decoder_set_error_callback(decoder->flac, &flac_error_callback);
195 196 197 198 199
}


FLAC__bool EasyFLAC__set_client_data(EasyFLAC__StreamDecoder *decoder, void *value)
{
200 201 202 203 204 205
    decoder->callbacks.client_data = value;
    
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_client_data(decoder->oggflac, decoder);
    else
        return FLAC__stream_decoder_set_client_data(decoder->flac, decoder);
206 207 208 209 210
}


FLAC__bool EasyFLAC__set_metadata_respond(EasyFLAC__StreamDecoder *decoder, FLAC__MetadataType type)
{
211 212 213 214
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_metadata_respond(decoder->oggflac, type);
    else
        return FLAC__stream_decoder_set_metadata_respond(decoder->flac, type);
215 216 217 218 219
}


FLAC__bool EasyFLAC__set_metadata_respond_application(EasyFLAC__StreamDecoder *decoder, const FLAC__byte id[4])
{
220 221 222 223
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_metadata_respond_application(decoder->oggflac, id);
    else
        return FLAC__stream_decoder_set_metadata_respond_application(decoder->flac, id);
224 225 226 227 228
}


FLAC__bool EasyFLAC__set_metadata_respond_all(EasyFLAC__StreamDecoder *decoder)
{
229 230 231 232
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_metadata_respond_all(decoder->oggflac);
    else
        return FLAC__stream_decoder_set_metadata_respond_all(decoder->flac);
233 234 235 236 237
}


FLAC__bool EasyFLAC__set_metadata_ignore(EasyFLAC__StreamDecoder *decoder, FLAC__MetadataType type)
{
238 239 240 241
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_metadata_ignore(decoder->oggflac, type);
    else
        return FLAC__stream_decoder_set_metadata_ignore(decoder->flac, type);
242 243 244 245 246
}


FLAC__bool EasyFLAC__set_metadata_ignore_application(EasyFLAC__StreamDecoder *decoder, const FLAC__byte id[4])
{
247 248 249 250
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_metadata_ignore_application(decoder->oggflac, id);
    else
        return FLAC__stream_decoder_set_metadata_ignore_application(decoder->flac, id);
251 252 253 254
}

FLAC__bool EasyFLAC__set_metadata_ignore_all(EasyFLAC__StreamDecoder *decoder)
{
255 256 257 258
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_set_metadata_ignore_all(decoder->oggflac);
    else
        return FLAC__stream_decoder_set_metadata_ignore_all(decoder->flac);
259 260 261 262 263
}


FLAC__StreamDecoderState EasyFLAC__get_state(const EasyFLAC__StreamDecoder *decoder)
{
264 265 266 267
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(decoder->oggflac);
    else
        return FLAC__stream_decoder_get_state(decoder->flac);
268 269 270 271 272
}


unsigned EasyFLAC__get_channels(const EasyFLAC__StreamDecoder *decoder)
{
273 274 275 276
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_get_channels(decoder->oggflac);
    else
        return FLAC__stream_decoder_get_channels(decoder->flac);
277 278 279 280 281
}


FLAC__ChannelAssignment EasyFLAC__get_channel_assignment(const EasyFLAC__StreamDecoder *decoder)
{
282 283 284 285
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_get_channel_assignment(decoder->oggflac);
    else
        return FLAC__stream_decoder_get_channel_assignment(decoder->flac);
286 287 288 289 290
}


unsigned EasyFLAC__get_bits_per_sample(const EasyFLAC__StreamDecoder *decoder)
{
291 292 293 294
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_get_bits_per_sample(decoder->oggflac);
    else
        return FLAC__stream_decoder_get_bits_per_sample(decoder->flac);
295 296 297 298 299
}


unsigned EasyFLAC__get_sample_rate(const EasyFLAC__StreamDecoder *decoder)
{
300 301 302 303
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_get_sample_rate(decoder->oggflac);
    else
        return FLAC__stream_decoder_get_sample_rate(decoder->flac);
304 305 306 307 308
}


unsigned EasyFLAC__get_blocksize(const EasyFLAC__StreamDecoder *decoder)
{
309 310 311 312
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_get_blocksize(decoder->oggflac);
    else
        return FLAC__stream_decoder_get_blocksize(decoder->flac);
313 314 315 316 317
}


FLAC__StreamDecoderState EasyFLAC__init(EasyFLAC__StreamDecoder *decoder)
{
318 319 320 321 322 323 324
    if (decoder->is_oggflac)
    {
        OggFLAC__stream_decoder_init(decoder->oggflac);
        return OggFLAC__stream_decoder_get_FLAC_stream_decoder_state(decoder->oggflac);
    }
    else
        return FLAC__stream_decoder_init(decoder->flac);
325 326 327 328 329
}


void EasyFLAC__finish(EasyFLAC__StreamDecoder *decoder)
{
330 331 332 333
    if (decoder->is_oggflac)
        OggFLAC__stream_decoder_finish(decoder->oggflac);
    else
        FLAC__stream_decoder_finish(decoder->flac);
334 335 336 337 338
}


FLAC__bool EasyFLAC__flush(EasyFLAC__StreamDecoder *decoder)
{
339 340 341 342
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_flush(decoder->oggflac);
    else
        return FLAC__stream_decoder_flush(decoder->flac);
343 344 345 346 347
}


FLAC__bool EasyFLAC__reset(EasyFLAC__StreamDecoder *decoder)
{
348 349 350 351
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_reset(decoder->oggflac);
    else
        return FLAC__stream_decoder_reset(decoder->flac);
352 353 354 355 356
}


FLAC__bool EasyFLAC__process_single(EasyFLAC__StreamDecoder *decoder)
{
357 358 359 360
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_process_single(decoder->oggflac);
    else
        return FLAC__stream_decoder_process_single(decoder->flac);
361 362 363 364 365
}


FLAC__bool EasyFLAC__process_until_end_of_metadata(EasyFLAC__StreamDecoder *decoder)
{
366 367 368 369
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_process_until_end_of_metadata(decoder->oggflac);
    else
        return FLAC__stream_decoder_process_until_end_of_metadata(decoder->flac);
370 371 372 373 374
}


FLAC__bool EasyFLAC__process_until_end_of_stream(EasyFLAC__StreamDecoder *decoder)
{
375 376 377 378
    if (decoder->is_oggflac)
        return OggFLAC__stream_decoder_process_until_end_of_stream(decoder->oggflac);
    else
        return FLAC__stream_decoder_process_until_end_of_stream(decoder->flac);
379
}