Changeset 479


Ignore:
Timestamp:
Apr 17, 2011, 10:27:50 AM (6 years ago)
Author:
sam
Message:

sdlport: add music playback support through SDL_Mixer, courtesy of
Jochen Schleu.

Location:
abuse/trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/configure.ac

    r478 r479  
    4646CFLAGS="$CFLAGS $SDL_CFLAGS"
    4747LIBS="$LIBS $SDL_LIBS $X_LIBS"
     48
     49# Add SDL_Mixer
     50LIBS="$LIBS -lSDL_mixer"
     51CFLAGS="${CFLAGS} -DUSE_SDL_MIXER"
     52CPPFLAGS="${CPPFLAGS} -DUSE_SDL_MIXER"
    4853
    4954if test "${enable_debug}" = "yes"; then
  • abuse/trunk/src/sdlport/Makefile.am

    r130 r479  
    1313    joystick.cpp joy.hpp \
    1414    setup.cpp setup.h \
     15    hmi.cpp hmi.h \
    1516    $(NULL)
    1617libsdlport_a_LIBADD =
  • abuse/trunk/src/sdlport/sound.cpp

    r131 r479  
    2323
    2424#include <SDL.h>
     25#ifdef USE_SDL_MIXER
     26#include <SDL/SDL_mixer.h>
     27#include "hmi.hpp"
     28#endif
    2529
    2630#include "sound.hpp"
     
    7781void mix_audio(void *udata, Uint8 *stream, int len)
    7882{
     83#ifndef USE_SDL_MIXER
    7984    effect_handle *handle = fx_list;
    8085
     
    106111        }
    107112    }
     113#endif
    108114}
    109115
     
    138144    free( sfxdir );
    139145
     146#ifdef USE_SDL_MIXER
     147    if (Mix_OpenAudio(11025, AUDIO_U8, 2, 128) < 0)
     148    {
     149        printf( "Sound : Unable to open audio - %s\nSound : Disabled (error)\n", SDL_GetError() );
     150        return 0;
     151    }
     152
     153    Mix_AllocateChannels(50);
     154
     155    int tempChannels = 0;
     156    Mix_QuerySpec(&audioObtained.freq, &audioObtained.format, &tempChannels);
     157    audioObtained.channels = tempChannels & 0xFF;
     158
     159    sound_enabled = SFX_INITIALIZED | MUSIC_INITIALIZED;
     160#else
    140161    audioWanted.freq = 11025;
    141162    audioWanted.format = AUDIO_U8;
     
    152173    }
    153174
    154     sound_enabled = 1;
     175    SDL_PauseAudio( 0 );
     176
     177    sound_enabled = SFX_INITIALIZED;
     178#endif
     179
    155180    printf( "Sound : Enabled\n" );
    156 
    157     SDL_PauseAudio( 0 );
    158181
    159182    // It's all good
     
    170193    if( sound_enabled )
    171194    {
     195#ifdef USE_SDL_MIXER
     196        Mix_CloseAudio();
     197#else
    172198        SDL_PauseAudio( 1 );
    173199        while( fx_list )
     
    178204        }
    179205        SDL_CloseAudio();
     206#endif
    180207    }
    181208}
     
    191218    {
    192219        long sample_speed;
     220
     221#ifdef USE_SDL_MIXER
     222        void* temp_data = (void *)read_wav( filename, sample_speed, size );
     223
     224        SDL_AudioCVT audiocvt;
     225   
     226        SDL_BuildAudioCVT( &audiocvt, AUDIO_U8, 1, 11025, audioObtained.format, audioObtained.channels, audioObtained.freq );
     227        data = malloc( size * audiocvt.len_mult );
     228
     229        memcpy( data, temp_data, size );
     230        audiocvt.buf = (Uint8*)data;
     231        audiocvt.len = size;
     232        SDL_ConvertAudio( &audiocvt );
     233        size = (Uint32)((double)size * audiocvt.len_ratio);
     234
     235        free(temp_data);
     236
     237        this->chunk = Mix_QuickLoad_RAW((Uint8*)data, size);
     238#else
    193239        data = (void *)read_wav( filename, sample_speed, size );
     240#endif
    194241    }
    195242}
     
    204251    if( sound_enabled )
    205252    {
     253#ifdef USE_SDL_MIXER
     254        // Sound effect deletion only happens on level load, so there
     255        // is no problem in stopping everything. But the original playing
     256        // code handles the sound effects and the "playlist" differently.
     257        // Therefore with SDL_mixer, a sound that has not finished playing
     258        // on a level load will cut off in the middle. This is most noticable
     259        // for the button sound of the load savegame dialog.
     260        Mix_FadeOutGroup(-1, 100);
     261        while (Mix_Playing(-1))
     262            SDL_Delay(10);
     263        Mix_FreeChunk(this->chunk);
     264#endif
     265
    206266        if( data )
    207267        {
     
    225285    if( sound_enabled )
    226286    {
     287#ifdef USE_SDL_MIXER
     288        int channel = Mix_PlayChannel(-1, this->chunk, 0);
     289        if (channel > -1)
     290        {
     291            Mix_Volume(channel, volume);
     292            Mix_SetPanning(channel, panpot, 255 - panpot);
     293        }
     294#else
    227295        SDL_LockAudio();
    228296
     
    285353        }
    286354        SDL_UnlockAudio();
    287     }
    288 }
    289 
    290 
    291 //
    292 // We don't handle songs.  These are just stubs to basically do nothing.
    293 // I tried using SDL_mixer to do this, but with no success.
    294 //
     355#endif
     356    }
     357}
     358
     359
     360// Play music using SDL_Mixer
    295361
    296362song::song( char const * filename )
     
    299365    Name = strdup(filename);
    300366    song_id = 0;
     367   
     368#ifdef USE_SDL_MIXER
     369    rw = NULL;
     370    music = NULL;
     371
     372    char realname[255];
     373    strcpy(realname, get_filename_prefix());
     374    strcat(realname, filename);
     375
     376    unsigned int data_size;
     377    data = load_hmi(realname, data_size);
     378
     379    if (data)
     380    {
     381        rw = SDL_RWFromMem(data, data_size);
     382        music = Mix_LoadMUS_RW(rw);
     383    }
     384#endif
    301385}
    302386
     
    312396    }
    313397    free( Name );
     398
     399#ifdef USE_SDL_MIXER
     400    Mix_FreeMusic(this->music);
     401    SDL_FreeRW(this->rw);
     402#endif
    314403}
    315404
     
    317406{
    318407    song_id = 1;
     408
     409#ifdef USE_SDL_MIXER
     410    Mix_PlayMusic(this->music, 0);
     411    Mix_VolumeMusic(volume);
     412#endif
    319413}
    320414
     
    322416{
    323417    song_id = 0;
     418
     419#ifdef USE_SDL_MIXER
     420    Mix_FadeOutMusic(100);
     421#endif
    324422}
    325423
    326424int song::playing()
    327425{
     426#ifdef USE_SDL_MIXER
     427    return Mix_PlayingMusic();
     428#else
    328429    return song_id;
     430#endif
    329431}
    330432
    331433void song::set_volume( int volume )
    332434{
    333 //    do nothing...
    334 }
     435#ifdef USE_SDL_MIXER
     436    Mix_VolumeMusic(volume);
     437#else
     438    // do nothing...
     439#endif
     440}
  • abuse/trunk/src/sdlport/sound.hpp

    r124 r479  
    1010#ifndef __SOUND_HPP_
    1111#define __SOUND_HPP_
     12
     13#ifdef USE_SDL_MIXER
     14#include <SDL/SDL_mixer.h>
     15#endif
    1216
    1317/* options are passed via command line */
     
    2428  long size;
    2529  void *data;
     30#ifdef USE_SDL_MIXER
     31  Mix_Chunk* chunk;
     32#endif
    2633public :
    2734  sound_effect(char *filename);
     
    3643  unsigned char *data;
    3744  unsigned long song_id;
     45#ifdef USE_SDL_MIXER
     46  Mix_Music* music;
     47  SDL_RWops* rw;
     48#endif
    3849public :
    3950  char const *name() { return Name; }
Note: See TracChangeset for help on using the changeset viewer.