Skip to content
Permalink
Browse files

Sam Lantinga - Sun Jul 2 14:16:44 PDT 2000

 * Added support for the Ogg Vorbis music format: http://www.vorbis.org/
 * Cleaned up the streaming wave code a bit
  • Loading branch information
Sam Lantinga committed Jul 3, 2000
1 parent 7adf3f7 commit 9c21f8e47ef58e80548769e98d95ea986cae769f
Showing with 359 additions and 59 deletions.
  1. +2 −0 CHANGES
  2. +2 −0 Makefile.am
  3. +2 −1 autogen.sh
  4. +18 −0 configure.in
  5. +57 −2 music.c
  6. +185 −0 music_ogg.c
  7. +67 −0 music_ogg.h
  8. +24 −54 wavestream.c
  9. +2 −2 wavestream.h
@@ -1,5 +1,7 @@

1.0.6:
Sam Lantinga - Sun Jul 2 14:16:44 PDT 2000
* Added support for the Ogg Vorbis music format: http://www.vorbis.org/
Darrell Walisser - Wed Jun 28 11:59:40 PDT 2000
* Added Codewarrior projects for MacOS
Sam Lantinga - Mon Jun 26 12:01:11 PDT 2000
@@ -14,6 +14,8 @@ libSDL_mixer_la_SOURCES = \
music.c \
music_cmd.c \
music_cmd.h \
music_ogg.c \
music_ogg.h \
wave.h \
wavestream.c \
wavestream.h
@@ -4,4 +4,5 @@ aclocal
automake --foreign
autoconf

./configure $*
#./configure $*
echo "Now you are ready to run ./configure"
@@ -95,6 +95,24 @@ if test x$enable_music_midi = xyes; then
CFLAGS="$CFLAGS -DMID_MUSIC -I\$(top_srcdir)/timidity"
MUSIC_SUBDIRS="$MUSIC_SUBDIRS timidity"
fi
AC_ARG_ENABLE(music-ogg,
[ --enable-music-ogg enable Ogg Vorbis music [default=yes]],
, enable_music_ogg=yes)
if test x$enable_music_ogg = xyes; then
AC_MSG_CHECKING(for Ogg Vorbis headers and libraries)
have_vorbis=no
AC_TRY_COMPILE([
#include <vorbis/vorbisfile.h>
],[
],[
have_vorbis=yes
])
AC_MSG_RESULT($have_vorbis)
if test x$have_vorbis = xyes; then
CFLAGS="$CFLAGS -DOGG_MUSIC"
LIBS="$LIBS -lvorbisfile -lvorbis"
fi
fi
AC_ARG_ENABLE(music-mp3,
[ --enable-music-mp3 enable MP3 music via smpeg [default=yes]],
, enable_music_mp3=yes)
59 music.c
@@ -57,6 +57,9 @@
#ifdef MID_MUSIC
#include "timidity.h"
#endif
#ifdef OGG_MUSIC
#include "music_ogg.h"
#endif
#ifdef MP3_MUSIC
#include <smpeg/smpeg.h>

@@ -79,6 +82,7 @@ struct _Mix_Music {
MUS_WAV,
MUS_MOD,
MUS_MID,
MUS_OGG,
MUS_MP3
} type;
union {
@@ -94,6 +98,9 @@ struct _Mix_Music {
#ifdef MID_MUSIC
MidiSong *midi;
#endif
#ifdef OGG_MUSIC
OGG_music *ogg;
#endif
#ifdef MP3_MUSIC
SMPEG *mp3;
#endif
@@ -186,7 +193,7 @@ void music_mixer(void *udata, Uint8 *stream, int len)
#endif
#ifdef WAV_MUSIC
case MUS_WAV:
WAVStream_PlaySome(stream, len);
WAVStream_PlaySome(music_playing->data.wave, stream, len);
break;
#endif
#ifdef MOD_MUSIC
@@ -220,6 +227,11 @@ void music_mixer(void *udata, Uint8 *stream, int len)
Timidity_PlaySome(stream, len/samplesize);
break;
#endif
#ifdef OGG_MUSIC
case MUS_OGG:
OGG_playAudio(music_playing->data.ogg, stream, len);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
SMPEG_playAudio(music_playing->data.mp3, stream, len);
@@ -308,6 +320,11 @@ int open_music(SDL_AudioSpec *mixer)
timidity_ok = 0;
}
#endif
#ifdef OGG_MUSIC
if ( OGG_init(mixer) < 0 ) {
++music_error;
}
#endif
#ifdef MP3_MUSIC
/* Keep a copy of the mixer */
used_mixer = *mixer;
@@ -391,6 +408,16 @@ Mix_Music *Mix_LoadMUS(const char *file)
}
} else
#endif
#ifdef OGG_MUSIC
/* Ogg Vorbis files have the magic four bytes "OggS" */
if ( strcmp(magic, "OggS") == 0 ) {
music->type = MUS_OGG;
music->data.ogg = OGG_new(file);
if ( music->data.ogg == NULL ) {
music->error = 1;
}
} else
#endif
#ifdef MP3_MUSIC
if ( magic[0]==0xFF && (magic[1]&0xF0)==0xF0) {
SMPEG_Info info;
@@ -460,6 +487,11 @@ void Mix_FreeMusic(Mix_Music *music)
Timidity_FreeSong(music->data.midi);
break;
#endif
#ifdef OGG_MUSIC
case MUS_OGG:
OGG_delete(music->data.ogg);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
SMPEG_delete(music->data.mp3);
@@ -504,6 +536,12 @@ static int lowlevel_play(Mix_Music *music)
Timidity_Start(music->data.midi);
break;
#endif
#ifdef OGG_MUSIC
case MUS_OGG:
OGG_setvolume(music->data.ogg, music_volume);
OGG_play(music->data.ogg);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
SMPEG_enableaudio(music->data.mp3,1);
@@ -594,6 +632,11 @@ int Mix_VolumeMusic(int volume)
Timidity_SetVolume(music_volume);
break;
#endif
#ifdef OGG_MUSIC
case MUS_OGG:
OGG_setvolume(music_playing->data.ogg, music_volume);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
SMPEG_setvolume(music_playing->data.mp3,((float)music_volume/(float)MIX_MAX_VOLUME)*100.0);
@@ -630,6 +673,11 @@ static void lowlevel_halt(void)
Timidity_Stop();
break;
#endif
#ifdef OGG_MUSIC
case MUS_OGG:
OGG_stop(music_playing->data.ogg);
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
SMPEG_stop(music_playing->data.mp3);
@@ -740,7 +788,7 @@ int Mix_PlayingMusic(void)
#endif
#ifdef WAV_MUSIC
case MUS_WAV:
if ( ! WAVStream_Active() ) {
if ( ! WAVStream_Active(music_playing->data.wave) ) {
return(0);
}
break;
@@ -759,6 +807,13 @@ int Mix_PlayingMusic(void)
}
break;
#endif
#ifdef OGG_MUSIC
case MUS_OGG:
if ( ! OGG_playing(music_playing->data.ogg) ) {
return(0);
}
break;
#endif
#ifdef MP3_MUSIC
case MUS_MP3:
if(SMPEG_status(music_playing->data.mp3)!=SMPEG_PLAYING)
@@ -0,0 +1,185 @@
/*
MIXERLIB: An audio mixer library based on the SDL library
Copyright (C) 1997-1999 Sam Lantinga
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
5635-34 Springhouse Dr.
Pleasanton, CA 94588 (USA)
slouken@devolution.com
*/

#ifdef OGG_MUSIC

/* This file supports Ogg Vorbis music streams */

#include <stdio.h>
#include <string.h>

#include "SDL_mixer.h"
#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)
{
OGG_music *music;
FILE *fp;

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;

fp = fopen(file, "rb");
if ( fp == NULL ) {
SDL_SetError("Couldn't open %s", file);
free(music);
return(NULL);
}
if ( ov_open(fp, &music->vf, NULL, 0) < 0 ) {
SDL_SetError("Not an Ogg Vorbis audio stream");
free(music);
fclose(fp);
return(NULL);
}
} else {
SDL_OutOfMemory();
}
return(music);
}

/* 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;

len = ov_read(&music->vf, data, sizeof(data), 0, 2, 1, &section);
if ( len <= 0 ) {
if ( len == 0 ) {
music->playing = 0;
}
return;
}
cvt = &music->cvt;
if ( section != music->section ) {
vorbis_info *vi;

vi = ov_info(&music->vf, -1);
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 {
SDL_OutOfMemory();
music->playing = 0;
}
}

/* Play some of a stream previously started with OGG_play() */
void OGG_playAudio(OGG_music *music, Uint8 *snd, int len)
{
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;
}
}

/* 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);
}
ov_clear(&music->vf);
free(music);
}
}

#endif /* OGG_MUSIC */

0 comments on commit 9c21f8e

Please sign in to comment.