source: abuse/trunk/src/sdlport/sound.cpp @ 555

Last change on this file since 555 was 555, checked in by Sam Hocevar, 11 years ago

ps3: make everything compile on the PS3. Of course, nothing links yet
because so much support is missing.

File size: 5.2 KB
RevLine 
[56]1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au>
[494]4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
[56]5 *
6 *  This program is free software; you can redistribute it and/or modify
7 *  it under the terms of the GNU General Public License as published by
8 *  the Free Software Foundation; either version 2 of the License, or
9 *  (at your option) any later version.
10 *
11 *  This program is distributed in the hope that it will be useful,
12 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 *  GNU General Public License for more details.
15 *
16 *  You should have received a copy of the GNU General Public License
17 *  along with this program; if not, write to the Free Software Foundation,
18 *  Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 */
[2]20
[555]21#if defined HAVE_CONFIG_H
22#   include "config.h"
23#endif
[56]24
[551]25#include <cstring>
[131]26
[2]27#include <SDL.h>
[479]28#include <SDL/SDL_mixer.h>
[2]29
[481]30#include "sound.h"
[551]31#include "hmi.h"
[481]32#include "specs.h"
[2]33#include "setup.h"
34
35extern flags_struct flags;
36static int sound_enabled = 0;
37static SDL_AudioSpec audioObtained;
38
39//
40// sound_init()
41// Initialise audio
42//
43int sound_init( int argc, char **argv )
44{
[124]45    char *sfxdir, *datadir;
46    FILE *fd = NULL;
[2]47
[124]48    // Disable sound if requested.
49    if( flags.nosound )
50    {
51        // User requested that sound be disabled
[536]52        printf( "Sound: Disabled (-nosound)\n" );
[124]53        return 0;
54    }
[2]55
[124]56    // Check for the sfx directory, disable sound if we can't find it.
57    datadir = get_filename_prefix();
[129]58    sfxdir = (char *)malloc( strlen( datadir ) + 5 + 1 );
[124]59    sprintf( sfxdir, "%s/sfx/", datadir );
60    if( (fd = fopen( sfxdir,"r" )) == NULL )
61    {
62        // Didn't find the directory, so disable sound.
[536]63        printf( "Sound: Disabled (couldn't find the sfx directory)\n" );
[124]64        return 0;
65    }
[129]66    free( sfxdir );
[2]67
[479]68    if (Mix_OpenAudio(11025, AUDIO_U8, 2, 128) < 0)
69    {
[536]70        printf( "Sound: Unable to open audio - %s\nSound: Disabled (error)\n", SDL_GetError() );
[479]71        return 0;
72    }
73
74    Mix_AllocateChannels(50);
75
76    int tempChannels = 0;
77    Mix_QuerySpec(&audioObtained.freq, &audioObtained.format, &tempChannels);
78    audioObtained.channels = tempChannels & 0xFF;
79
80    sound_enabled = SFX_INITIALIZED | MUSIC_INITIALIZED;
[536]81
82    printf( "Sound: Enabled\n" );
[2]83
[124]84    // It's all good
85    return sound_enabled;
[2]86}
87
88//
89// sound_uninit
90//
91// Shutdown audio and release any memory left over.
92//
93void sound_uninit()
94{
[552]95    if (!sound_enabled)
96        return;
97
98    Mix_CloseAudio();
[2]99}
100
101//
102// sound_effect constructor
103//
104// Read in the requested .wav file.
105//
[552]106sound_effect::sound_effect(char const *filename)
[2]107{
[552]108    if (!sound_enabled)
109        return;
[479]110
[552]111    jFILE fp(filename, "rb");
112    if (fp.open_failure())
113        return;
[479]114
[552]115    void *temp_data = malloc(fp.file_size());
116    fp.read(temp_data, fp.file_size());
117    SDL_RWops *rw = SDL_RWFromMem(temp_data, fp.file_size());
118    m_chunk = Mix_LoadWAV_RW(rw, 1);
119    free(temp_data);
[2]120}
121
122//
123// sound_effect destructor
124//
125// Release the audio data.
126//
127sound_effect::~sound_effect()
128{
[552]129    if(!sound_enabled)
130        return;
[479]131
[552]132    // Sound effect deletion only happens on level load, so there
133    // is no problem in stopping everything. But the original playing
134    // code handles the sound effects and the "playlist" differently.
135    // Therefore with SDL_mixer, a sound that has not finished playing
136    // on a level load will cut off in the middle. This is most noticable
137    // for the button sound of the load savegame dialog.
138    Mix_FadeOutGroup(-1, 100);
139    while (Mix_Playing(-1))
140        SDL_Delay(10);
141    Mix_FreeChunk(m_chunk);
[2]142}
143
144//
145// sound_effect::play
146//
[551]147// Add a new sample for playing.
[2]148// panpot defines the pan position for the sound effect.
149//   0   - Completely to the right.
150//   128 - Centered.
151//   255 - Completely to the left.
152//
[552]153void sound_effect::play(int volume, int pitch, int panpot)
[2]154{
[552]155    if (!sound_enabled)
156        return;
157
158    int channel = Mix_PlayChannel(-1, m_chunk, 0);
159    if (channel > -1)
[124]160    {
[552]161        Mix_Volume(channel, volume);
162        Mix_SetPanning(channel, panpot, 255 - panpot);
[124]163    }
[2]164}
165
166
[479]167// Play music using SDL_Mixer
[2]168
[536]169song::song(char const * filename)
[2]170{
[124]171    data = NULL;
[131]172    Name = strdup(filename);
[124]173    song_id = 0;
[494]174
[479]175    rw = NULL;
176    music = NULL;
177
178    char realname[255];
179    strcpy(realname, get_filename_prefix());
180    strcat(realname, filename);
181
182    unsigned int data_size;
183    data = load_hmi(realname, data_size);
184
[536]185    if (!data)
[479]186    {
[551]187        printf("Sound: ERROR - could not load %s\n", realname);
[536]188        return;
[479]189    }
[536]190
191    rw = SDL_RWFromMem(data, data_size);
192    music = Mix_LoadMUS_RW(rw);
193
194    if (!music)
195    {
[551]196        printf("Sound: ERROR - %s while loading %s\n",
[536]197               Mix_GetError(), realname);
198        return;
199    }
[2]200}
201
202song::~song()
203{
[536]204    if(playing())
[124]205        stop();
[536]206    free(data);
207    free(Name);
[479]208
[536]209    Mix_FreeMusic(music);
210    SDL_FreeRW(rw);
[2]211}
212
213void song::play( unsigned char volume )
214{
[124]215    song_id = 1;
[479]216
217    Mix_PlayMusic(this->music, 0);
218    Mix_VolumeMusic(volume);
[2]219}
220
221void song::stop( long fadeout_time )
222{
[124]223    song_id = 0;
[479]224
225    Mix_FadeOutMusic(100);
[2]226}
227
228int song::playing()
229{
[479]230    return Mix_PlayingMusic();
[2]231}
232
233void song::set_volume( int volume )
234{
[479]235    Mix_VolumeMusic(volume);
[2]236}
[551]237
Note: See TracBrowser for help on using the repository browser.