Skip to content

Latest commit

 

History

History
161 lines (126 loc) · 4.34 KB

load_ogg.c

File metadata and controls

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