load_ogg.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Oct 2009 01:03:40 +0000
changeset 462 028b317d01d2
parent 386 695494546b3c
child 470 5cebd6c5be2d
permissions -rw-r--r--
Added the AudioUnit framework for the native MIDI code.
slouken@190
     1
/*
slouken@190
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@386
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@190
     4
slouken@190
     5
    This library is free software; you can redistribute it and/or
slouken@190
     6
    modify it under the terms of the GNU Library General Public
slouken@190
     7
    License as published by the Free Software Foundation; either
slouken@190
     8
    version 2 of the License, or (at your option) any later version.
slouken@190
     9
slouken@190
    10
    This library is distributed in the hope that it will be useful,
slouken@190
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@190
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@190
    13
    Library General Public License for more details.
slouken@190
    14
slouken@190
    15
    You should have received a copy of the GNU Library General Public
slouken@190
    16
    License along with this library; if not, write to the Free
slouken@190
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@190
    18
slouken@190
    19
    This is the source needed to decode an Ogg Vorbis into a waveform.
slouken@190
    20
    This file by Vaclav Slavik (vaclav.slavik@matfyz.cz).
slouken@190
    21
*/
slouken@190
    22
slouken@190
    23
/* $Id$ */
slouken@190
    24
slouken@190
    25
#ifdef OGG_MUSIC
slouken@190
    26
slouken@190
    27
#include <stdio.h>
slouken@190
    28
#include <stdlib.h>
slouken@190
    29
#include <string.h>
slouken@190
    30
slouken@190
    31
#include "SDL_mutex.h"
slouken@190
    32
#include "SDL_endian.h"
slouken@190
    33
#include "SDL_timer.h"
slouken@190
    34
slouken@190
    35
#include "SDL_mixer.h"
slouken@312
    36
#include "dynamic_ogg.h"
slouken@190
    37
#include "load_ogg.h"
slouken@190
    38
slouken@190
    39
static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
slouken@190
    40
{
slouken@190
    41
    return SDL_RWread((SDL_RWops*)datasource, ptr, size, nmemb);
slouken@190
    42
}
slouken@190
    43
slouken@190
    44
static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
slouken@190
    45
{
slouken@221
    46
    return SDL_RWseek((SDL_RWops*)datasource, (int)offset, whence);
slouken@190
    47
}
slouken@190
    48
slouken@190
    49
static int sdl_close_func_freesrc(void *datasource)
slouken@190
    50
{
slouken@190
    51
    return SDL_RWclose((SDL_RWops*)datasource);
slouken@190
    52
}
slouken@190
    53
slouken@190
    54
static int sdl_close_func_nofreesrc(void *datasource)
slouken@190
    55
{
slouken@190
    56
    return SDL_RWseek((SDL_RWops*)datasource, 0, SEEK_SET);
slouken@190
    57
}
slouken@190
    58
slouken@190
    59
static long sdl_tell_func(void *datasource)
slouken@190
    60
{
slouken@190
    61
    return SDL_RWtell((SDL_RWops*)datasource);
slouken@190
    62
}
slouken@190
    63
slouken@190
    64
slouken@190
    65
/* don't call this directly; use Mix_LoadWAV_RW() for now. */
slouken@190
    66
SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc,
slouken@190
    67
        SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
slouken@190
    68
{
slouken@190
    69
    OggVorbis_File vf;
slouken@190
    70
    ov_callbacks callbacks;
slouken@190
    71
    vorbis_info *info;
slouken@190
    72
    Uint8 *buf;
slouken@190
    73
    int bitstream = -1;
slouken@190
    74
    long samplesize;
slouken@190
    75
    long samples;
slouken@190
    76
    int read, to_read;
slouken@190
    77
    int must_close = 1;
slouken@190
    78
    int was_error = 1;
slouken@190
    79
    
slouken@190
    80
    if ( (!src) || (!audio_buf) || (!audio_len) )   /* sanity checks. */
slouken@190
    81
        goto done;
slouken@190
    82
slouken@312
    83
    if ( Mix_InitOgg() < 0 )
slouken@312
    84
        goto done;
slouken@312
    85
slouken@190
    86
    callbacks.read_func = sdl_read_func;
slouken@190
    87
    callbacks.seek_func = sdl_seek_func;
slouken@190
    88
    callbacks.tell_func = sdl_tell_func;
slouken@190
    89
    callbacks.close_func = freesrc ? 
slouken@190
    90
                           sdl_close_func_freesrc : sdl_close_func_nofreesrc;
slouken@190
    91
slouken@312
    92
    if (vorbis.ov_open_callbacks(src, &vf, NULL, 0, callbacks) != 0)
slouken@190
    93
    {
slouken@190
    94
        SDL_SetError("OGG bitstream is not valid Vorbis stream!");
slouken@190
    95
        goto done;
slouken@190
    96
    }
slouken@190
    97
slouken@190
    98
    must_close = 0;
slouken@190
    99
    
slouken@312
   100
    info = vorbis.ov_info(&vf, -1);
slouken@190
   101
    
slouken@190
   102
    *audio_buf = NULL;
slouken@190
   103
    *audio_len = 0;
slouken@190
   104
    memset(spec, '\0', sizeof (SDL_AudioSpec));
slouken@190
   105
slouken@190
   106
    spec->format = AUDIO_S16;
slouken@190
   107
    spec->channels = info->channels;
slouken@190
   108
    spec->freq = info->rate;
slouken@190
   109
    spec->samples = 4096; /* buffer size */
slouken@190
   110
    
slouken@312
   111
    samples = (long)vorbis.ov_pcm_total(&vf, -1);
slouken@190
   112
slouken@190
   113
    *audio_len = spec->size = samples * spec->channels * 2;
slouken@190
   114
    *audio_buf = malloc(*audio_len);
slouken@190
   115
    if (*audio_buf == NULL)
slouken@190
   116
        goto done;
slouken@190
   117
slouken@190
   118
    buf = *audio_buf;
slouken@190
   119
    to_read = *audio_len;
slouken@353
   120
#ifdef OGG_USE_TREMOR
slouken@353
   121
    for (read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream);
slouken@353
   122
	 read > 0;
slouken@353
   123
	 read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream))
slouken@353
   124
#else
slouken@312
   125
    for (read = vorbis.ov_read(&vf, (char *)buf, to_read, 0/*LE*/, 2/*16bit*/, 1/*signed*/, &bitstream);
slouken@190
   126
         read > 0;
slouken@312
   127
         read = vorbis.ov_read(&vf, (char *)buf, to_read, 0, 2, 1, &bitstream))
slouken@353
   128
#endif	 
slouken@190
   129
    {
slouken@190
   130
        if (read == OV_HOLE || read == OV_EBADLINK)
slouken@190
   131
            break; /* error */
slouken@190
   132
        
slouken@190
   133
        to_read -= read;
slouken@190
   134
        buf += read;
slouken@190
   135
    }
slouken@190
   136
slouken@312
   137
    vorbis.ov_clear(&vf);
slouken@190
   138
    was_error = 0;
slouken@190
   139
slouken@190
   140
    /* Don't return a buffer that isn't a multiple of samplesize */
slouken@190
   141
    samplesize = ((spec->format & 0xFF)/8)*spec->channels;
slouken@190
   142
    *audio_len &= ~(samplesize-1);
slouken@190
   143
slouken@190
   144
done:
slouken@190
   145
    if (src && must_close)
slouken@190
   146
    {
slouken@190
   147
        if (freesrc)
slouken@190
   148
            SDL_RWclose(src);
slouken@190
   149
        else
slouken@190
   150
            SDL_RWseek(src, 0, SEEK_SET);
slouken@190
   151
    }
slouken@190
   152
slouken@190
   153
    if ( was_error )
slouken@190
   154
        spec = NULL;
slouken@190
   155
slouken@312
   156
    Mix_QuitOgg();
slouken@312
   157
slouken@190
   158
    return(spec);
slouken@190
   159
} /* Mix_LoadOGG_RW */
slouken@190
   160
slouken@190
   161
/* end of load_ogg.c ... */
slouken@190
   162
slouken@190
   163
#endif