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
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 2001 Anthony Kruize <trandor@labyrinth.net.au>
4 *  Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net>
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 */
20
21#if defined HAVE_CONFIG_H
22#   include "config.h"
23#endif
24
25#include <cstring>
26
27#include <SDL.h>
28#include <SDL/SDL_mixer.h>
29
30#include "sound.h"
31#include "hmi.h"
32#include "specs.h"
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{
45    char *sfxdir, *datadir;
46    FILE *fd = NULL;
47
48    // Disable sound if requested.
49    if( flags.nosound )
50    {
51        // User requested that sound be disabled
52        printf( "Sound: Disabled (-nosound)\n" );
53        return 0;
54    }
55
56    // Check for the sfx directory, disable sound if we can't find it.
57    datadir = get_filename_prefix();
58    sfxdir = (char *)malloc( strlen( datadir ) + 5 + 1 );
59    sprintf( sfxdir, "%s/sfx/", datadir );
60    if( (fd = fopen( sfxdir,"r" )) == NULL )
61    {
62        // Didn't find the directory, so disable sound.
63        printf( "Sound: Disabled (couldn't find the sfx directory)\n" );
64        return 0;
65    }
66    free( sfxdir );
67
68    if (Mix_OpenAudio(11025, AUDIO_U8, 2, 128) < 0)
69    {
70        printf( "Sound: Unable to open audio - %s\nSound: Disabled (error)\n", SDL_GetError() );
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;
81
82    printf( "Sound: Enabled\n" );
83
84    // It's all good
85    return sound_enabled;
86}
87
88//
89// sound_uninit
90//
91// Shutdown audio and release any memory left over.
92//
93void sound_uninit()
94{
95    if (!sound_enabled)
96        return;
97
98    Mix_CloseAudio();
99}
100
101//
102// sound_effect constructor
103//
104// Read in the requested .wav file.
105//
106sound_effect::sound_effect(char const *filename)
107{
108    if (!sound_enabled)
109        return;
110
111    jFILE fp(filename, "rb");
112    if (fp.open_failure())
113        return;
114
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);
120}
121
122//
123// sound_effect destructor
124//
125// Release the audio data.
126//
127sound_effect::~sound_effect()
128{
129    if(!sound_enabled)
130        return;
131
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);
142}
143
144//
145// sound_effect::play
146//
147// Add a new sample for playing.
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//
153void sound_effect::play(int volume, int pitch, int panpot)
154{
155    if (!sound_enabled)
156        return;
157
158    int channel = Mix_PlayChannel(-1, m_chunk, 0);
159    if (channel > -1)
160    {
161        Mix_Volume(channel, volume);
162        Mix_SetPanning(channel, panpot, 255 - panpot);
163    }
164}
165
166
167// Play music using SDL_Mixer
168
169song::song(char const * filename)
170{
171    data = NULL;
172    Name = strdup(filename);
173    song_id = 0;
174
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
185    if (!data)
186    {
187        printf("Sound: ERROR - could not load %s\n", realname);
188        return;
189    }
190
191    rw = SDL_RWFromMem(data, data_size);
192    music = Mix_LoadMUS_RW(rw);
193
194    if (!music)
195    {
196        printf("Sound: ERROR - %s while loading %s\n",
197               Mix_GetError(), realname);
198        return;
199    }
200}
201
202song::~song()
203{
204    if(playing())
205        stop();
206    free(data);
207    free(Name);
208
209    Mix_FreeMusic(music);
210    SDL_FreeRW(rw);
211}
212
213void song::play( unsigned char volume )
214{
215    song_id = 1;
216
217    Mix_PlayMusic(this->music, 0);
218    Mix_VolumeMusic(volume);
219}
220
221void song::stop( long fadeout_time )
222{
223    song_id = 0;
224
225    Mix_FadeOutMusic(100);
226}
227
228int song::playing()
229{
230    return Mix_PlayingMusic();
231}
232
233void song::set_volume( int volume )
234{
235    Mix_VolumeMusic(volume);
236}
237
Note: See TracBrowser for help on using the repository browser.