Skip to content

Commit

Permalink
Martin_Storsj? - Sun, 22 Aug 2004 02:21:14 +0300 (EEST)
Browse files Browse the repository at this point in the history
 * Added SDL_RWops support for reading Ogg Vorbis files
  • Loading branch information
slouken committed Aug 22, 2004
1 parent 2bcd318 commit 07a2880
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES
@@ -1,4 +1,6 @@
1.2.6:
Martin_Storsj� - Sun, 22 Aug 2004 02:21:14 +0300 (EEST)
* Added SDL_RWops support for reading Ogg Vorbis files
Greg Lee - Wed, 14 Jul 2004 05:13:14 -1000
* Added 4 and 6 channel surround sound output support
* Added support for RMID format MIDI files
Expand Down
26 changes: 19 additions & 7 deletions music.c
Expand Up @@ -1203,19 +1203,21 @@ void close_music(void)
#ifdef USE_RWOPS

Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw) {
/*Uint8 magic[5]; Apparently there is no way to check if the file is really a MOD,*/
/* or there are too many formats supported by MikMod or MikMod does */
/* this check by itself. If someone implements other formats (e.g. MP3) */
/* the check can be uncommented */
Uint8 magic[5]; /*Apparently there is no way to check if the file is really a MOD,*/
/* or there are too many formats supported by MikMod or MikMod does */
/* this check by itself. If someone implements other formats (e.g. MP3) */
/* the check can be uncommented */
Mix_Music *music;
int start;

/* Just skip the check */
/* Figure out what kind of file this is */
/*if (SDL_RWread(rw,magic,1,4)!=4) {
start = SDL_RWtell(rw);
if (SDL_RWread(rw,magic,1,4)!=4) {
Mix_SetError("Couldn't read from RWops");
return NULL;
}
magic[4]='\0';*/
SDL_RWseek(rw, start, SEEK_SET);
magic[4]='\0';

/* Allocate memory for the music structure */
music=(Mix_Music *)malloc(sizeof(Mix_Music));
Expand All @@ -1225,6 +1227,16 @@ Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw) {
}
music->error = 0;

#ifdef OGG_MUSIC
/* Ogg Vorbis files have the magic four bytes "OggS" */
if ( strcmp((char *)magic, "OggS") == 0 ) {
music->type = MUS_OGG;
music->data.ogg = OGG_new_RW(rw);
if ( music->data.ogg == NULL ) {
music->error = 1;
}
} else
#endif
#ifdef MOD_MUSIC
if (1) {
music->type=MUS_MOD;
Expand Down
52 changes: 52 additions & 0 deletions music_ogg.c
Expand Up @@ -83,6 +83,58 @@ OGG_music *OGG_new(const char *file)
return(music);
}


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;

if ( ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
SDL_SetError("Not an Ogg Vorbis audio stream");
free(music);
SDL_RWclose(rw);
return(NULL);
}
} else {
SDL_SetError("Out of memory");
}
return(music);
}

/* Start playback of a given OGG stream */
void OGG_play(OGG_music *music)
{
Expand Down
3 changes: 3 additions & 0 deletions music_ogg.h
Expand Up @@ -49,6 +49,9 @@ extern void OGG_setvolume(OGG_music *music, int volume);
/* Load an OGG stream from the given file */
extern OGG_music *OGG_new(const char *file);

/* Load an OGG stream from an SDL_RWops object */
extern OGG_music *OGG_new_RW(SDL_RWops *rw);

/* Start playback of a given OGG stream */
extern void OGG_play(OGG_music *music);

Expand Down

0 comments on commit 07a2880

Please sign in to comment.