Skip to content

Latest commit

 

History

History
235 lines (200 loc) · 5.41 KB

music_ogg.c

File metadata and controls

235 lines (200 loc) · 5.41 KB
 
Dec 14, 2001
Dec 14, 2001
2
SDL_mixer: An audio mixer library based on the SDL library
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
21
22
*/
Dec 14, 2001
Dec 14, 2001
23
/* $Id$ */
Dec 14, 2001
Dec 14, 2001
24
25
26
27
28
29
#ifdef OGG_MUSIC
/* This file supports Ogg Vorbis music streams */
#include <stdio.h>
Dec 18, 2001
Dec 18, 2001
30
#include <stdlib.h>
31
32
33
#include <string.h>
#include "SDL_mixer.h"
May 12, 2006
May 12, 2006
34
#include "dynamic_ogg.h"
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "music_ogg.h"
/* This is the format of the audio mixer data */
static SDL_AudioSpec mixer;
/* Initialize the Ogg Vorbis player, with the given mixer settings
This function returns 0, or -1 if there was an error.
*/
int OGG_init(SDL_AudioSpec *mixerfmt)
{
mixer = *mixerfmt;
return(0);
}
/* Set the volume for an OGG stream */
void OGG_setvolume(OGG_music *music, int volume)
{
music->volume = volume;
}
/* Load an OGG stream from the given file */
OGG_music *OGG_new(const char *file)
{
Jul 15, 2007
Jul 15, 2007
58
59
60
61
62
63
SDL_RWops *rw;
rw = SDL_RWFromFile(file, "rb");
if ( rw == NULL ) {
SDL_SetError("Couldn't open %s", file);
return NULL;
Jul 15, 2007
Jul 15, 2007
65
return OGG_new_RW(rw);
66
67
}
Aug 22, 2004
Aug 22, 2004
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
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);
}
static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
{
return SDL_RWseek((SDL_RWops*)datasource, (int)offset, whence);
}
static int sdl_close_func(void *datasource)
{
return SDL_RWclose((SDL_RWops*)datasource);
}
static long sdl_tell_func(void *datasource)
{
return SDL_RWtell((SDL_RWops*)datasource);
}
/* Load an OGG stream from an SDL_RWops object */
OGG_music *OGG_new_RW(SDL_RWops *rw)
{
OGG_music *music;
ov_callbacks callbacks;
callbacks.read_func = sdl_read_func;
callbacks.seek_func = sdl_seek_func;
callbacks.close_func = sdl_close_func;
callbacks.tell_func = sdl_tell_func;
music = (OGG_music *)malloc(sizeof *music);
if ( music ) {
/* Initialize the music structure */
memset(music, 0, (sizeof *music));
OGG_stop(music);
OGG_setvolume(music, MIX_MAX_VOLUME);
music->section = -1;
Nov 8, 2009
Nov 8, 2009
108
if ( !Mix_Init(MIX_INIT_OGG) ) {
May 12, 2006
May 12, 2006
109
110
111
return(NULL);
}
if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
Aug 22, 2004
Aug 22, 2004
112
113
free(music);
SDL_RWclose(rw);
May 12, 2006
May 12, 2006
114
SDL_SetError("Not an Ogg Vorbis audio stream");
Aug 22, 2004
Aug 22, 2004
115
116
117
return(NULL);
}
} else {
May 12, 2006
May 12, 2006
118
SDL_OutOfMemory();
Aug 22, 2004
Aug 22, 2004
119
120
121
122
}
return(music);
}
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* Start playback of a given OGG stream */
void OGG_play(OGG_music *music)
{
music->playing = 1;
}
/* Return non-zero if a stream is currently playing */
int OGG_playing(OGG_music *music)
{
return(music->playing);
}
/* Read some Ogg stream data and convert it for output */
static void OGG_getsome(OGG_music *music)
{
int section;
int len;
char data[4096];
SDL_AudioCVT *cvt;
Jul 15, 2007
Jul 15, 2007
143
144
145
#ifdef OGG_USE_TREMOR
len = vorbis.ov_read(&music->vf, data, sizeof(data), &section);
#else
May 12, 2006
May 12, 2006
146
len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
Jul 15, 2007
Jul 15, 2007
147
#endif
148
149
150
151
152
153
154
155
156
157
if ( len <= 0 ) {
if ( len == 0 ) {
music->playing = 0;
}
return;
}
cvt = &music->cvt;
if ( section != music->section ) {
vorbis_info *vi;
May 12, 2006
May 12, 2006
158
vi = vorbis.ov_info(&music->vf, -1);
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
mixer.format,mixer.channels,mixer.freq);
if ( cvt->buf ) {
free(cvt->buf);
}
cvt->buf = (Uint8 *)malloc(sizeof(data)*cvt->len_mult);
music->section = section;
}
if ( cvt->buf ) {
memcpy(cvt->buf, data, len);
if ( cvt->needed ) {
cvt->len = len;
SDL_ConvertAudio(cvt);
} else {
cvt->len_cvt = len;
}
music->len_available = music->cvt.len_cvt;
music->snd_available = music->cvt.buf;
} else {
Aug 18, 2001
Aug 18, 2001
178
SDL_SetError("Out of memory");
179
180
181
182
183
music->playing = 0;
}
}
/* Play some of a stream previously started with OGG_play() */
Nov 19, 2005
Nov 19, 2005
184
int OGG_playAudio(OGG_music *music, Uint8 *snd, int len)
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
{
int mixable;
while ( (len > 0) && music->playing ) {
if ( ! music->len_available ) {
OGG_getsome(music);
}
mixable = len;
if ( mixable > music->len_available ) {
mixable = music->len_available;
}
if ( music->volume == MIX_MAX_VOLUME ) {
memcpy(snd, music->snd_available, mixable);
} else {
SDL_MixAudio(snd, music->snd_available, mixable,
music->volume);
}
music->len_available -= mixable;
music->snd_available += mixable;
len -= mixable;
snd += mixable;
}
Nov 19, 2005
Nov 19, 2005
207
208
return len;
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
}
/* Stop playback of a stream previously started with OGG_play() */
void OGG_stop(OGG_music *music)
{
music->playing = 0;
}
/* Close the given OGG stream */
void OGG_delete(OGG_music *music)
{
if ( music ) {
if ( music->cvt.buf ) {
free(music->cvt.buf);
}
May 12, 2006
May 12, 2006
224
vorbis.ov_clear(&music->vf);
225
226
227
228
free(music);
}
}
Dec 20, 2001
Dec 20, 2001
229
230
231
/* Jump (seek) to a given position (time is in seconds) */
void OGG_jump_to_time(OGG_music *music, double time)
{
May 12, 2006
May 12, 2006
232
vorbis.ov_time_seek( &music->vf, time );
Dec 20, 2001
Dec 20, 2001
233
234
}
235
#endif /* OGG_MUSIC */