Martin_Storsj� - Sun, 22 Aug 2004 02:21:14 +0300 (EEST)
authorSam Lantinga <slouken@libsdl.org>
Sun, 22 Aug 2004 07:55:14 +0000
changeset 2469fa5d0f9d042
parent 245 63b3650714de
child 247 6b7e60e28033
Martin_Storsj� - Sun, 22 Aug 2004 02:21:14 +0300 (EEST)
* Added SDL_RWops support for reading Ogg Vorbis files
CHANGES
music.c
music_ogg.c
music_ogg.h
     1.1 --- a/CHANGES	Sat Aug 21 12:27:02 2004 +0000
     1.2 +++ b/CHANGES	Sun Aug 22 07:55:14 2004 +0000
     1.3 @@ -1,4 +1,6 @@
     1.4  1.2.6:
     1.5 +Martin_Storsj -  Sun, 22 Aug 2004 02:21:14 +0300 (EEST)
     1.6 + * Added SDL_RWops support for reading Ogg Vorbis files
     1.7  Greg Lee - Wed, 14 Jul 2004 05:13:14 -1000
     1.8   * Added 4 and 6 channel surround sound output support
     1.9   * Added support for RMID format MIDI files
     2.1 --- a/music.c	Sat Aug 21 12:27:02 2004 +0000
     2.2 +++ b/music.c	Sun Aug 22 07:55:14 2004 +0000
     2.3 @@ -1203,19 +1203,21 @@
     2.4  #ifdef USE_RWOPS
     2.5  
     2.6  Mix_Music *Mix_LoadMUS_RW(SDL_RWops *rw) {
     2.7 -	/*Uint8     magic[5]; Apparently there is no way to check if the file is really a MOD,*/
     2.8 -      /*		    or there are too many formats supported by MikMod or MikMod does */
     2.9 -      /*		    this check by itself. If someone implements other formats (e.g. MP3) */
    2.10 -      /*		    the check can be uncommented */
    2.11 +	Uint8 magic[5];	  /*Apparently there is no way to check if the file is really a MOD,*/
    2.12 +	/*		    or there are too many formats supported by MikMod or MikMod does */
    2.13 +	/*		    this check by itself. If someone implements other formats (e.g. MP3) */
    2.14 +	/*		    the check can be uncommented */
    2.15  	Mix_Music *music;
    2.16 +	int start;
    2.17  
    2.18 -      /* Just skip the check */
    2.19  	/* Figure out what kind of file this is */
    2.20 -	/*if (SDL_RWread(rw,magic,1,4)!=4) {
    2.21 +	start = SDL_RWtell(rw);
    2.22 +	if (SDL_RWread(rw,magic,1,4)!=4) {
    2.23  		Mix_SetError("Couldn't read from RWops");
    2.24  		return NULL;
    2.25  	}
    2.26 -	magic[4]='\0';*/
    2.27 +	SDL_RWseek(rw, start, SEEK_SET);
    2.28 +	magic[4]='\0';
    2.29  
    2.30  	/* Allocate memory for the music structure */
    2.31  	music=(Mix_Music *)malloc(sizeof(Mix_Music));
    2.32 @@ -1225,6 +1227,16 @@
    2.33  	}
    2.34  	music->error = 0;
    2.35  
    2.36 +#ifdef OGG_MUSIC
    2.37 +	/* Ogg Vorbis files have the magic four bytes "OggS" */
    2.38 +	if ( strcmp((char *)magic, "OggS") == 0 ) {
    2.39 +		music->type = MUS_OGG;
    2.40 +		music->data.ogg = OGG_new_RW(rw);
    2.41 +		if ( music->data.ogg == NULL ) {
    2.42 +			music->error = 1;
    2.43 +		}
    2.44 +	} else
    2.45 +#endif
    2.46  #ifdef MOD_MUSIC
    2.47  	if (1) {
    2.48  		music->type=MUS_MOD;
     3.1 --- a/music_ogg.c	Sat Aug 21 12:27:02 2004 +0000
     3.2 +++ b/music_ogg.c	Sun Aug 22 07:55:14 2004 +0000
     3.3 @@ -83,6 +83,58 @@
     3.4  	return(music);
     3.5  }
     3.6  
     3.7 +
     3.8 +static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
     3.9 +{
    3.10 +    return SDL_RWread((SDL_RWops*)datasource, ptr, size, nmemb);
    3.11 +}
    3.12 +
    3.13 +static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
    3.14 +{
    3.15 +    return SDL_RWseek((SDL_RWops*)datasource, (int)offset, whence);
    3.16 +}
    3.17 +
    3.18 +static int sdl_close_func(void *datasource)
    3.19 +{
    3.20 +    return SDL_RWclose((SDL_RWops*)datasource);
    3.21 +}
    3.22 +
    3.23 +static long sdl_tell_func(void *datasource)
    3.24 +{
    3.25 +    return SDL_RWtell((SDL_RWops*)datasource);
    3.26 +}
    3.27 +
    3.28 +/* Load an OGG stream from an SDL_RWops object */
    3.29 +OGG_music *OGG_new_RW(SDL_RWops *rw)
    3.30 +{
    3.31 +	OGG_music *music;
    3.32 +	ov_callbacks callbacks;
    3.33 +
    3.34 +	callbacks.read_func = sdl_read_func;
    3.35 +	callbacks.seek_func = sdl_seek_func;
    3.36 +	callbacks.close_func = sdl_close_func;
    3.37 +	callbacks.tell_func = sdl_tell_func;
    3.38 +
    3.39 +	music = (OGG_music *)malloc(sizeof *music);
    3.40 +	if ( music ) {
    3.41 +		/* Initialize the music structure */
    3.42 +		memset(music, 0, (sizeof *music));
    3.43 +		OGG_stop(music);
    3.44 +		OGG_setvolume(music, MIX_MAX_VOLUME);
    3.45 +		music->section = -1;
    3.46 +
    3.47 +		if ( ov_open_callbacks(rw, &music->vf, NULL, 0, callbacks) < 0 ) {
    3.48 +			SDL_SetError("Not an Ogg Vorbis audio stream");
    3.49 +			free(music);
    3.50 +			SDL_RWclose(rw);
    3.51 +			return(NULL);
    3.52 +		}
    3.53 +	} else {
    3.54 +		SDL_SetError("Out of memory");
    3.55 +	}
    3.56 +	return(music);
    3.57 +}
    3.58 +
    3.59  /* Start playback of a given OGG stream */
    3.60  void OGG_play(OGG_music *music)
    3.61  {
     4.1 --- a/music_ogg.h	Sat Aug 21 12:27:02 2004 +0000
     4.2 +++ b/music_ogg.h	Sun Aug 22 07:55:14 2004 +0000
     4.3 @@ -49,6 +49,9 @@
     4.4  /* Load an OGG stream from the given file */
     4.5  extern OGG_music *OGG_new(const char *file);
     4.6  
     4.7 +/* Load an OGG stream from an SDL_RWops object */
     4.8 +extern OGG_music *OGG_new_RW(SDL_RWops *rw);
     4.9 +
    4.10  /* Start playback of a given OGG stream */
    4.11  extern void OGG_play(OGG_music *music);
    4.12