Index: Makefile.am =================================================================== RCS file: /home/zinnia/ncvs/SDL_mixer/Makefile.am,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 Makefile.am --- Makefile.am 18 Aug 2003 02:51:12 -0000 1.1.1.1 +++ Makefile.am 18 Aug 2003 11:43:35 -0000 @@ -16,12 +16,16 @@ load_voc.h \ load_ogg.c \ load_ogg.h \ + load_SDL_sound.c \ + load_SDL_sound.h \ mixer.c \ music.c \ music_cmd.c \ music_cmd.h \ music_ogg.c \ music_ogg.h \ + music_SDL_sound.h \ + music_SDL_sound.c \ wavestream.c \ wavestream.h \ effect_position.c \ Index: SDL_mixer.h =================================================================== RCS file: /home/zinnia/ncvs/SDL_mixer/SDL_mixer.h,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 SDL_mixer.h --- SDL_mixer.h 18 Aug 2003 02:51:13 -0000 1.1.1.1 +++ SDL_mixer.h 18 Aug 2003 11:44:59 -0000 @@ -103,7 +103,8 @@ MUS_MOD, MUS_MID, MUS_OGG, - MUS_MP3 + MUS_MP3, + MUS_SDL_SOUND, } Mix_MusicType; /* The internal format for a music chunk interpreted via mikmod */ Index: configure.in =================================================================== RCS file: /home/zinnia/ncvs/SDL_mixer/configure.in,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 configure.in --- configure.in 18 Aug 2003 02:51:13 -0000 1.1.1.1 +++ configure.in 2 Sep 2003 03:48:58 -0000 @@ -188,6 +188,14 @@ fi fi +AC_ARG_ENABLE(sdl-sound, +[ --enable-sdl-sound enable SDL_sound [defaut=yes]], + , enable_sdl_sound=yes) +if test x$enable_sdl_sound = xyes; then + CFLAGS="$CFLAGS -DUSE_SDL_SOUND -lSDL_sound" + SYSTEM_LIBS="$SYSTEM_LIBS -lSDL_sound" +fi + dnl Add Makefile conditionals AC_SUBST(MUSIC_SUBDIRS) AM_CONDITIONAL(USE_MIKMOD, test x$enable_music_mod = xyes) Index: load_SDL_sound.c =================================================================== RCS file: load_SDL_sound.c diff -N load_SDL_sound.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ load_SDL_sound.c 18 Aug 2003 11:24:06 -0000 @@ -0,0 +1,55 @@ +#ifdef USE_SDL_SOUND + +#include "SDL_sound.h" + +#define BUFFER_SIZE 0x1000 + +SDL_AudioSpec *Mix_SDL_sound_RW (SDL_RWops *src, int freesrc, + SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) +{ + Sound_Sample* sample; + int samplesize; + int must_close = 0; + fprintf(stderr, "Mix_SDL_sound_RW: loading\n"); + + if(!Sound_Init()){ + fprintf(stderr, "Sound_Init: %s\n", Sound_GetError()); + return NULL; + } + sample = Sound_NewSample(src, NULL, spec, BUFFER_SIZE); + if(sample == NULL){ + fprintf(stderr, "Sound_NewSample: %s\n", Sound_GetError()); + Sound_Quit(); + return NULL; + } + spec->format = sample->actual.format; + spec->channels = sample->actual.channels; + spec->freq = sample->actual.rate; + spec->samples = 4096; + + fprintf(stderr, "decode start\n"); + *audio_len = spec->size = Sound_DecodeAll(sample); + fprintf(stderr, "decode start\n"); + *audio_buf = (Uint8*)malloc(*audio_len); + fprintf(stderr, "%d\n", spec->size); + if(*audio_buf){ + memcpy(*audio_buf, sample->buffer, *audio_len); + } + + Sound_FreeSample(sample); + Sound_Quit(); + + /* derived from load_ogg.c */ + + /* Don't return a buffer that isn't a multiple of samplesize */ + samplesize = ((spec->format & 0xFF)/8)*spec->channels; + *audio_len &= ~(samplesize-1); + if(src && must_close){ + if(freesrc) + SDL_RWclose(src); + else + SDL_RWseek(src, 0, SEEK_SET); + } + return spec; +} +#endif /* USE_SDL_SOUND */ Index: load_SDL_sound.h =================================================================== RCS file: load_SDL_sound.h diff -N load_SDL_sound.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ load_SDL_sound.h 18 Aug 2003 05:14:43 -0000 @@ -0,0 +1,5 @@ +#ifdef USE_SDL_SOUND +SDL_AudioSpec *Mix_SDL_sound_RW (SDL_RWops *src, int freesrc, + SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len); + +#endif Index: mixer.c =================================================================== RCS file: /home/zinnia/ncvs/SDL_mixer/mixer.c,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 mixer.c --- mixer.c 18 Aug 2003 02:51:13 -0000 1.1.1.1 +++ mixer.c 18 Aug 2003 10:37:52 -0000 @@ -420,7 +420,7 @@ } return(NULL); } - +#ifndef USE_SDL_SOUND /* Find out what kind of audio file this is */ magic = SDL_ReadLE32(src); /* Seek backwards for compatibility with older loaders */ @@ -450,6 +450,11 @@ SDL_SetError("Unrecognized sound file type"); return(0); } +#else /* USE_SDL_SOUND */ + loaded = Mix_SDL_sound_RW(src, freesrc, &wavespec, + (Uint8**)&chunk->abuf, &chunk->alen); +#endif /* USE_SDL_SOUND */ + if ( !loaded ) { free(chunk); return(NULL); @@ -459,7 +464,6 @@ PrintFormat("Audio device", &mixer); PrintFormat("-- Wave file", &wavespec); #endif - /* Build the audio converter and create conversion buffers */ if ( SDL_BuildAudioCVT(&wavecvt, wavespec.format, wavespec.channels, wavespec.freq, Index: music.c =================================================================== RCS file: /home/zinnia/ncvs/SDL_mixer/music.c,v retrieving revision 1.1.1.1 diff -u -r1.1.1.1 music.c --- music.c 18 Aug 2003 02:51:13 -0000 1.1.1.1 +++ music.c 18 Aug 2003 13:02:58 -0000 @@ -31,6 +31,11 @@ #include "SDL_mixer.h" +#ifdef USE_SDL_SOUND +#define SDL_SOUND_MUSIC +#include "music_SDL_sound.h" +#endif + /* The music command hack is UNIX specific */ #ifndef unix #undef CMD_MUSIC @@ -115,6 +120,9 @@ #ifdef MP3_MUSIC SMPEG *mp3; #endif +#ifdef SDL_SOUND_MUSIC +music_SDL_sound_context* sdlsound; +#endif } data; Mix_Fading fading; int fade_step; @@ -197,6 +205,11 @@ } } switch (music_playing->type) { +#ifdef SDL_SOUND_MUSIC + case MUS_SDL_SOUND: + music_SDL_sound_playAudio(music_playing->data.sdlsound, stream, len); + break; +#endif #ifdef CMD_MUSIC case MUS_CMD: /* The playing is done externally */ @@ -266,6 +279,11 @@ int music_error; music_error = 0; +#ifdef SDL_SOUND_MUSIC + if( music_SDL_sound_init(mixer) < 0 ) { + music_error ++; + } +#endif #ifdef WAV_MUSIC if ( WAVStream_Init(mixer) < 0 ) { ++music_error; @@ -410,7 +428,17 @@ return(NULL); } music->error = 0; - +#ifdef SDL_SOUND_MUSIC + if(1){ + music_SDL_sound_context* t = music_SDL_sound_new(file); + music->type = MUS_SDL_SOUND; + if(t == NULL){ + music->error = 1; + } else { + music->data.sdlsound = t; + } + } else +#endif #ifdef CMD_MUSIC if ( music_cmd ) { music->type = MUS_CMD; @@ -541,6 +569,12 @@ } SDL_UnlockAudio(); switch (music->type) { +#ifdef SDL_SOUND_MUSIC + case MUS_SDL_SOUND: + music_SDL_sound_FreeSong(music->data.sdlsound); + break; +#endif + #ifdef CMD_MUSIC case MUS_CMD: MusicCMD_FreeSong(music->data.cmd); @@ -628,6 +662,11 @@ /* Set up for playback */ switch (music->type) { +#ifdef SDL_SOUND_MUSIC + case MUS_SDL_SOUND: + music_SDL_sound_play(music->data.sdlsound); + break; +#endif #ifdef CMD_MUSIC case MUS_CMD: MusicCMD_Start(music->data.cmd); @@ -742,6 +781,11 @@ int retval = 0; switch (music_playing->type) { +#ifdef SDL_SOUND_MUSIC + case MUS_SDL_SOUND: + music_SDL_sound_seek(music_playing->data.sdlsound, position); + break; +#endif #ifdef MOD_MUSIC case MUS_MOD: Player_SetPosition((UWORD)position); @@ -792,6 +836,11 @@ static void music_internal_volume(int volume) { switch (music_playing->type) { +#ifdef SDL_SOUND_MUSIC + case MUS_SDL_SOUND: + music_SDL_sound_setvolume(music_playing->data.sdlsound, volume); + break; +#endif #ifdef CMD_MUSIC case MUS_CMD: MusicCMD_SetVolume(volume); @@ -860,6 +909,11 @@ static void music_internal_halt(void) { switch (music_playing->type) { +#ifdef SDL_SOUND_MUSIC + case MUS_SDL_SOUND: + music_SDL_sound_stop(music_playing->data.sdlsound); + break; +#endif #ifdef CMD_MUSIC case MUS_CMD: MusicCMD_Stop(music_playing->data.cmd); @@ -973,6 +1027,11 @@ { int playing = 1; switch (music_playing->type) { +#ifdef SDL_SOUND_MUSIC + case MUS_SDL_SOUND: + playing = music_SDL_sound_playing(music_playing->data.sdlsound); + break; +#endif #ifdef CMD_MUSIC case MUS_CMD: if (!MusicCMD_Active(music_playing->data.cmd)) { Index: music_SDL_sound.c =================================================================== RCS file: music_SDL_sound.c diff -N music_SDL_sound.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ music_SDL_sound.c 2 Sep 2003 13:36:30 -0000 @@ -0,0 +1,151 @@ +#ifdef USE_SDL_SOUND + +#include "SDL_mixer.h" +#include "music_SDL_sound.h" + +#ifdef DEBUG +#undef DEBUG +#endif + +#define DEBUG 0 +#if DEBUG +#define DPRINTF(x) printf x +#else +#define DPRINTF(x) /**/ +#endif + +static SDL_AudioSpec* m_mixer; +int music_SDL_sound_init(SDL_AudioSpec* mixer) +{ + DPRINTF(("music_SDL_sound_init\n")); + m_mixer = mixer; + if(!Sound_Init()){ + fprintf(stderr, "Sound_Init: %s\n", Sound_GetError()); + return -1; + } + return 0; +} +void music_SDL_sound_setvolume(music_SDL_sound_context* ctx, int volume){ + ctx->volume = volume; +} + +static const int BUFFER_SIZE = 0x1000; +static void buffer_discard(music_SDL_sound_context* ctx) +{ + ctx->buffer_remain = 0; +} + +music_SDL_sound_context* music_SDL_sound_new(const char* file) +{ + music_SDL_sound_context* result; + Sound_AudioInfo sound_desired; + + DPRINTF(("music_SDL_sound_new\n")); + result = (music_SDL_sound_context*)malloc(sizeof(*result)); + if(result == NULL){ + SDL_OutOfMemory(); + return NULL; + } + sound_desired.rate = m_mixer->freq; + sound_desired.format = m_mixer->format; + sound_desired.channels = m_mixer->channels; + + DPRINTF(("invoking Sound_NewSampleFromFile(%s)\n", file)); + result->sample = Sound_NewSampleFromFile(file, &sound_desired, BUFFER_SIZE); + if(result->sample == NULL){ + fprintf(stderr, "Sound_NewSample: %s\n", Sound_GetError()); + goto err_clean_1; + } + result->playing = 0; + buffer_discard(result); + + music_SDL_sound_setvolume(result, MIX_MAX_VOLUME); + DPRINTF(("OK\n")); + return result; + + err_clean_1: + Sound_Quit(); + free(result); + return NULL; + +} + +void music_SDL_sound_play(music_SDL_sound_context* ctx){ + DPRINTF(("music_SDL_sound_play\n")); + ctx->playing = 1; +} + +int music_SDL_sound_playing(music_SDL_sound_context* ctx){ + return ctx->playing; +} + +void music_SDL_sound_playAudio(music_SDL_sound_context* ctx, Uint8* stream, int len) +{ + int remain; int srcsize; + Uint8* ptr, * srcptr; + + for(remain = len, ptr = stream; ctx->playing && remain > 0; ){ + if(ctx->buffer_remain <= 0){ + /* ctx->buffer empty. fill, it. */ + + int readsize = Sound_Decode(ctx->sample); + if(ctx->sample->flags & (SOUND_SAMPLEFLAG_ERROR | SOUND_SAMPLEFLAG_EOF)){ + music_SDL_sound_stop(ctx); + break; + } + if(readsize < 0) break; + + ctx->buffer_remain = readsize; + ctx->buffer_ptr = ctx->sample->buffer; + } + + srcptr = ctx->buffer_ptr; + srcsize = ctx->buffer_remain; + + if(remain < srcsize){ + srcsize = remain; + } + + if(ctx->volume == MIX_MAX_VOLUME){ + memcpy(ptr, srcptr, srcsize); + } else { + SDL_MixAudio(ptr, srcptr, srcsize, ctx->volume); + } + + ptr += srcsize; + remain -= srcsize; + + ctx->buffer_ptr += srcsize; + ctx->buffer_remain -= srcsize; + } +} + +void music_SDL_sound_seek(music_SDL_sound_context* ctx, double time) +{ + DPRINTF(("music_SDL_sound_seek: %f\n", time)); + Sound_Seek(ctx->sample, time); + buffer_discard(ctx); +} + +void music_SDL_sound_stop(music_SDL_sound_context* ctx) +{ + DPRINTF(("music_SDL_sound_stop\n")); + ctx->playing = 0; +} + +void music_SDL_sound_delete(music_SDL_sound_context* ctx) +{ + music_SDL_sound_FreeSong(ctx); + Sound_Quit(); +} + +void music_SDL_sound_FreeSong(music_SDL_sound_context* ctx) +{ + DPRINTF(("music_SDL_sound_FreeSong\n")); + if(ctx->sample){ + Sound_FreeSample(ctx->sample); + ctx->sample = NULL; + } +} + +#endif /* USE_SDL_SOUND */ Index: music_SDL_sound.h =================================================================== RCS file: music_SDL_sound.h diff -N music_SDL_sound.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ music_SDL_sound.h 2 Sep 2003 13:16:11 -0000 @@ -0,0 +1,27 @@ +#ifdef USE_SDL_SOUND + +#include "SDL_sound.h" + +typedef struct { + int playing; + int volume; + Sound_Sample* sample; + Uint8* buffer_ptr; + int buffer_remain; + +} music_SDL_sound_context; + +extern int music_SDL_sound_init(SDL_AudioSpec* mixer); +extern void music_SDL_sound_setvolume(music_SDL_sound_context* ctx, int volume); + +extern music_SDL_sound_context* music_SDL_sound_new(const char* file); +extern void music_SDL_sound_play(music_SDL_sound_context* ctx); +extern int music_SDL_sound_playing(music_SDL_sound_context* ctx); +extern void music_SDL_sound_playAudio(music_SDL_sound_context* ctx, Uint8* stream, int len); +extern void music_SDL_sound_stop(music_SDL_sound_context* ctx); +extern void music_SDL_sound_delete(music_SDL_sound_context* ctx); +extern void music_SDL_sound_seek(music_SDL_sound_context* ctx, double time); + +extern void music_SDL_sound_FreeSong(music_SDL_sound_context* ctx); + +#endif /* USE_SDL_SOUND */