load_ogg.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 22:12:31 -0800
changeset 712 7e59d684b070
parent 711 f40c5ac95b12
child 725 bdf7b8d20566
permissions -rw-r--r--
Updated to version 2.0.1
     1 /*
     2   SDL_mixer:  An audio mixer library based on the SDL library
     3   Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 
    21   This is the source needed to decode an Ogg Vorbis into a waveform.
    22   This file by Vaclav Slavik (vaclav.slavik@matfyz.cz).
    23 */
    24 
    25 /* $Id$ */
    26 
    27 #ifdef OGG_MUSIC
    28 
    29 #include <stdio.h>
    30 #include <stdlib.h>
    31 #include <string.h>
    32 
    33 #include "SDL_mutex.h"
    34 #include "SDL_endian.h"
    35 #include "SDL_timer.h"
    36 
    37 #include "SDL_mixer.h"
    38 #include "dynamic_ogg.h"
    39 #include "load_ogg.h"
    40 
    41 static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
    42 {
    43     return SDL_RWread((SDL_RWops*)datasource, ptr, size, nmemb);
    44 }
    45 
    46 static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
    47 {
    48     return (int)SDL_RWseek((SDL_RWops*)datasource, offset, whence);
    49 }
    50 
    51 static int sdl_close_func_freesrc(void *datasource)
    52 {
    53     return SDL_RWclose((SDL_RWops*)datasource);
    54 }
    55 
    56 static int sdl_close_func_nofreesrc(void *datasource)
    57 {
    58     SDL_RWseek((SDL_RWops*)datasource, 0, RW_SEEK_SET);
    59     return 0;
    60 }
    61 
    62 static long sdl_tell_func(void *datasource)
    63 {
    64     return (long)SDL_RWtell((SDL_RWops*)datasource);
    65 }
    66 
    67 
    68 /* don't call this directly; use Mix_LoadWAV_RW() for now. */
    69 SDL_AudioSpec *Mix_LoadOGG_RW (SDL_RWops *src, int freesrc,
    70         SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len)
    71 {
    72     OggVorbis_File vf;
    73     ov_callbacks callbacks;
    74     vorbis_info *info;
    75     Uint8 *buf;
    76     int bitstream = -1;
    77     long samplesize;
    78     long samples;
    79     int read, to_read;
    80     int must_close = 1;
    81     int was_error = 1;
    82 
    83     if ( (!src) || (!audio_buf) || (!audio_len) )   /* sanity checks. */
    84         goto done;
    85 
    86     if ( !Mix_Init(MIX_INIT_OGG) )
    87         goto done;
    88 
    89     callbacks.read_func = sdl_read_func;
    90     callbacks.seek_func = sdl_seek_func;
    91     callbacks.tell_func = sdl_tell_func;
    92     callbacks.close_func = freesrc ?
    93                            sdl_close_func_freesrc : sdl_close_func_nofreesrc;
    94 
    95     if (vorbis.ov_open_callbacks(src, &vf, NULL, 0, callbacks) != 0)
    96     {
    97         SDL_SetError("OGG bitstream is not valid Vorbis stream!");
    98         goto done;
    99     }
   100 
   101     must_close = 0;
   102 
   103     info = vorbis.ov_info(&vf, -1);
   104 
   105     *audio_buf = NULL;
   106     *audio_len = 0;
   107     memset(spec, '\0', sizeof (SDL_AudioSpec));
   108 
   109     spec->format = AUDIO_S16;
   110     spec->channels = info->channels;
   111     spec->freq = info->rate;
   112     spec->samples = 4096; /* buffer size */
   113 
   114     samples = (long)vorbis.ov_pcm_total(&vf, -1);
   115 
   116     *audio_len = spec->size = samples * spec->channels * 2;
   117     *audio_buf = (Uint8 *)SDL_malloc(*audio_len);
   118     if (*audio_buf == NULL)
   119         goto done;
   120 
   121     buf = *audio_buf;
   122     to_read = *audio_len;
   123 #ifdef OGG_USE_TREMOR
   124     for (read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream);
   125      read > 0;
   126      read = vorbis.ov_read(&vf, (char *)buf, to_read, &bitstream))
   127 #else
   128     for (read = vorbis.ov_read(&vf, (char *)buf, to_read, 0/*LE*/, 2/*16bit*/, 1/*signed*/, &bitstream);
   129          read > 0;
   130          read = vorbis.ov_read(&vf, (char *)buf, to_read, 0, 2, 1, &bitstream))
   131 #endif
   132     {
   133         if (read == OV_HOLE || read == OV_EBADLINK)
   134             break; /* error */
   135 
   136         to_read -= read;
   137         buf += read;
   138     }
   139 
   140     vorbis.ov_clear(&vf);
   141     was_error = 0;
   142 
   143     /* Don't return a buffer that isn't a multiple of samplesize */
   144     samplesize = ((spec->format & 0xFF)/8)*spec->channels;
   145     *audio_len &= ~(samplesize-1);
   146 
   147 done:
   148     if (freesrc && src && must_close) {
   149         SDL_RWclose(src);
   150     }
   151 
   152     if (was_error) {
   153         spec = NULL;
   154     }
   155 
   156     return(spec);
   157 } /* Mix_LoadOGG_RW */
   158 
   159 /* end of load_ogg.c ... */
   160 
   161 #endif