Skip to content

Latest commit

 

History

History
232 lines (202 loc) · 5.51 KB

music_ogg.c

File metadata and controls

232 lines (202 loc) · 5.51 KB
 
Dec 31, 2011
Dec 31, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SDL_mixer: An audio mixer library based on the SDL library
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
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.
20
21
22
23
24
25
26
*/
#ifdef OGG_MUSIC
/* This file supports Ogg Vorbis music streams */
#include <stdio.h>
Dec 18, 2001
Dec 18, 2001
27
#include <stdlib.h>
28
29
30
#include <string.h>
#include "SDL_mixer.h"
May 12, 2006
May 12, 2006
31
#include "dynamic_ogg.h"
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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;
}
Aug 22, 2004
Aug 22, 2004
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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 long sdl_tell_func(void *datasource)
{
return SDL_RWtell((SDL_RWops*)datasource);
}
/* Load an OGG stream from an SDL_RWops object */
Dec 31, 2011
Dec 31, 2011
68
OGG_music *OGG_new_RW(SDL_RWops *rw, int freerw)
Aug 22, 2004
Aug 22, 2004
69
70
71
72
{
OGG_music *music;
ov_callbacks callbacks;
Jan 4, 2012
Jan 4, 2012
73
74
75
76
77
78
79
if ( !Mix_Init(MIX_INIT_OGG) ) {
if ( freerw ) {
SDL_RWclose(rw);
}
return(NULL);
}
Dec 31, 2011
Dec 31, 2011
80
SDL_memset(&callbacks, 0, sizeof(callbacks));
Aug 22, 2004
Aug 22, 2004
81
82
83
84
callbacks.read_func = sdl_read_func;
callbacks.seek_func = sdl_seek_func;
callbacks.tell_func = sdl_tell_func;
Jan 13, 2012
Jan 13, 2012
85
music = (OGG_music *)SDL_malloc(sizeof *music);
Aug 22, 2004
Aug 22, 2004
86
87
88
if ( music ) {
/* Initialize the music structure */
memset(music, 0, (sizeof *music));
Dec 31, 2011
Dec 31, 2011
89
90
music->rw = rw;
music->freerw = freerw;
Aug 22, 2004
Aug 22, 2004
91
92
93
94
OGG_stop(music);
OGG_setvolume(music, MIX_MAX_VOLUME);
music->section = -1;
May 12, 2006
May 12, 2006
95
if ( vorbis.ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
Jan 13, 2012
Jan 13, 2012
96
SDL_free(music);
Dec 31, 2011
Dec 31, 2011
97
98
99
if ( freerw ) {
SDL_RWclose(rw);
}
May 12, 2006
May 12, 2006
100
SDL_SetError("Not an Ogg Vorbis audio stream");
Aug 22, 2004
Aug 22, 2004
101
102
103
return(NULL);
}
} else {
Dec 31, 2011
Dec 31, 2011
104
105
106
if ( freerw ) {
SDL_RWclose(rw);
}
May 12, 2006
May 12, 2006
107
SDL_OutOfMemory();
Dec 31, 2011
Dec 31, 2011
108
return(NULL);
Aug 22, 2004
Aug 22, 2004
109
110
111
112
}
return(music);
}
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* 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
133
134
135
#ifdef OGG_USE_TREMOR
len = vorbis.ov_read(&music->vf, data, sizeof(data), &section);
#else
May 12, 2006
May 12, 2006
136
len = vorbis.ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
Jul 15, 2007
Jul 15, 2007
137
#endif
138
139
140
141
142
143
144
145
146
147
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
148
vi = vorbis.ov_info(&music->vf, -1);
149
150
151
SDL_BuildAudioCVT(cvt, AUDIO_S16, vi->channels, vi->rate,
mixer.format,mixer.channels,mixer.freq);
if ( cvt->buf ) {
Jan 13, 2012
Jan 13, 2012
152
SDL_free(cvt->buf);
Jan 13, 2012
Jan 13, 2012
154
cvt->buf = (Uint8 *)SDL_malloc(sizeof(data)*cvt->len_mult);
155
156
157
158
159
160
161
162
163
164
165
166
167
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
168
SDL_SetError("Out of memory");
169
170
171
172
173
music->playing = 0;
}
}
/* Play some of a stream previously started with OGG_play() */
Nov 19, 2005
Nov 19, 2005
174
int OGG_playAudio(OGG_music *music, Uint8 *snd, int len)
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
{
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
197
198
return len;
199
200
201
202
203
204
205
206
207
208
209
210
211
}
/* 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 ) {
Jan 13, 2012
Jan 13, 2012
212
SDL_free(music->cvt.buf);
Dec 31, 2011
Dec 31, 2011
214
215
216
if ( music->freerw ) {
SDL_RWclose(music->rw);
}
May 12, 2006
May 12, 2006
217
vorbis.ov_clear(&music->vf);
Jan 13, 2012
Jan 13, 2012
218
SDL_free(music);
219
220
221
}
}
Dec 20, 2001
Dec 20, 2001
222
223
224
/* Jump (seek) to a given position (time is in seconds) */
void OGG_jump_to_time(OGG_music *music, double time)
{
Mar 16, 2011
Mar 16, 2011
225
#ifdef OGG_USE_TREMOR
Oct 6, 2018
Oct 6, 2018
226
vorbis.ov_time_seek( &music->vf, (ogg_int64_t)(time * 1000.0) );
Mar 16, 2011
Mar 16, 2011
227
#else
May 12, 2006
May 12, 2006
228
vorbis.ov_time_seek( &music->vf, time );
Mar 16, 2011
Mar 16, 2011
229
#endif
Dec 20, 2001
Dec 20, 2001
230
231
}
232
#endif /* OGG_MUSIC */