Debian Codegraph @

src/audiomanagersdl.cpp

raw symbols SCIP
1 /***************************************************************************
2 audiomanagersdl.cpp - description
3 -------------------
4 begin : december 26th, 2003
5 copyright : (C) 2003-2005 by Duong Khang NGUYEN
6 email : neoneurone @ gmail com
7
8 $Id: audiomanagersdl.cpp 375 2008-10-28 14:47:15Z neoneurone $
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * any later version. *
17 * *
18 ***************************************************************************/
19
20 #include "audiomanagersdl.h"
21
22 #include <fstream>
23
24
25 /*====================================================================*/
26 AudioManager::AudioManager():
27 boolAudioDeviceInitialized( false ),
28 boolMusicEnabled( true ),
29 boolSoundEnabled( true ),
30 boolRandomMusic( true ),
31 uiNumberSound( 0 ),
32 uiNumberMusic( 0 ),
33 uiCurrentMusic( 0 ),
34 boolShuffleMusic( false ),
35 pMusicData( NULL )
36 {
37 OPENCITY_DEBUG("ctor");
38
39 // if the audio system is not init. then init. it
40 if (SDL_WasInit(SDL_INIT_AUDIO) == 0) {
41 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
42 OPENCITY_DEBUG( "Audio ERROR" );
43 }
44 }
45 }
46
47
48 /*====================================================================*/
49 AudioManager::~AudioManager()
50 {
51 OPENCITY_DEBUG("dtor");
52
53 // clear the music filename list
54 this->vectorMusicFilename.clear();
55 this->uiNumberMusic = 0;
56 this->uiCurrentMusic = 0;
57
58 // if there's a playing music, stop it
59 // and free the music loaded
60 if (pMusicData != NULL) {
61 if (PlayingMusic())
62 StopMusic();
63 Mix_FreeMusic( pMusicData );
64 pMusicData = NULL; // safe
65 }
66
67 // clear the sound filename list
68 _vSoundFilename.clear();
69 this->uiNumberSound = 0;
70
71 // now free all chunks
72 uint uiVectorSize = _vpSoundChunk.size();
73 for (uint i = 0; i < uiVectorSize; i++) {
74 Mix_FreeChunk( _vpSoundChunk[i] );
75 }
76 _vpSoundChunk.clear();
77
78 // quit the audio device
79 SDL_QuitSubSystem( SDL_INIT_AUDIO );
80 }
81
82
83 /*====================================================================*/
84 OPENCITY_ERR_CODE
85 AudioManager::OpenAudio(void)
86 {
87 if ( Mix_OpenAudio( OC_AUDIO_FREQUENCY,
88 OC_AUDIO_FORMAT,
89 OC_AUDIO_CHANNELS,
90 OC_AUDIO_CHUNK_SIZE ) < 0) {
91 OPENCITY_DEBUG( "Audio opening error" );
92 return OC_ERR_SOMETHING;
93 }
94 else {
95 // reserve channels for panning effects
96 if (Mix_ReserveChannels(OC_AUDIO_RESERVED_CHANNELS)
97 != OC_AUDIO_RESERVED_CHANNELS) {
98 OPENCITY_DEBUG( "Audio reserving channels error" );
99 return OC_ERR_SOMETHING;
100 }
101
102 // register the sounds effects, this doesn't perform test
103 Mix_SetPanning( AUDIO_LEFT_CHANNEL, 254, 1 );
104 Mix_SetPanning( AUDIO_LEFT_HALF_CHANNEL, 254, 127 );
105 Mix_SetPanning( AUDIO_RIGHT_CHANNEL, 1, 254 );
106 Mix_SetPanning( AUDIO_RIGHT_HALF_CHANNEL, 127, 254 );
107
108 // initialize music / sound volume
109 VolumeMusic( OC_AUDIO_VOLUME_MAX * 70 / 100 );
110 VolumeSound( OC_AUDIO_VOLUME_MAX );
111
112 boolAudioDeviceInitialized = true;
113 return OC_ERR_FREE;
114 }
115 }
116
117
118 /*====================================================================*/
119 OPENCITY_ERR_CODE
120 AudioManager::CloseAudio(void)
121 {
122 // warning: we are going to close the audio device
123 boolAudioDeviceInitialized = false;
124
125 // unregister all sound effects
126 Mix_UnregisterAllEffects( AUDIO_LEFT_CHANNEL );
127 Mix_UnregisterAllEffects( AUDIO_LEFT_HALF_CHANNEL );
128 Mix_UnregisterAllEffects( AUDIO_RIGHT_CHANNEL );
129 Mix_UnregisterAllEffects( AUDIO_RIGHT_HALF_CHANNEL );
130
131 // unreserve all previously reversed channels for panning
132 Mix_ReserveChannels( 0 );
133
134 // close the audio device
135 Mix_CloseAudio();
136
137 return OC_ERR_FREE;
138 }
139
140
141 /*====================================================================*/
142 OPENCITY_ERR_CODE
143 AudioManager::LoadMusicList(
144 const string& csrFilename,
145 const string& csrPrefix )
146 {
147 // Return immediatetly if the audio device is not yet initialized
148 if (boolAudioDeviceInitialized == false) {
149 return OC_ERR_SOMETHING;
150 }
151
152 // Set the current music index to 0
153 this->uiCurrentMusic = 0;
154 this->strMusicList = csrFilename;
155
156 // Now parse the music M3U file liste
157 if (ParseM3UList(csrFilename, this->vectorMusicFilename, this->uiNumberMusic )
158 != OC_ERR_FREE)
159 return OC_ERR_SOMETHING;
160
161 // Add the prefix
162 uint uiVectorSize = vectorMusicFilename.size();
163 for (uint i = 0; i < uiVectorSize; i++) {
164 this->vectorMusicFilename[i] = csrPrefix + this->vectorMusicFilename[i];
165 } // for
166
167 return OC_ERR_FREE;
168 }
169
170
171 /*====================================================================*/
172 const uint &
173 AudioManager::GetNumberMusic(void) const
174 {
175 return this->uiNumberMusic;
176 }
177
178
179 /*====================================================================*/
180 bool
181 AudioManager::PlayingMusic(void) const
182 {
183 return Mix_PlayingMusic();
184 }
185
186
187 /*====================================================================*/
188 OPENCITY_ERR_CODE
189 AudioManager::PlayMusic(
190 const uint & rcuiMusicIndex,
191 const int & rciLoops )
192 {
193 // return immediatetly if the audio device is not yet initialized
194 if (boolAudioDeviceInitialized == false) {
195 return OC_ERR_SOMETHING;
196 }
197
198 // if the music is disabled then we return
199 if (boolMusicEnabled == false) {
200 return OC_ERR_SOMETHING;
201 }
202
203 if (rcuiMusicIndex >= this->uiNumberMusic) {
204 OPENCITY_DEBUG( "Audio play music error" );
205 return OC_ERR_SOMETHING;
206 }
207 else {
208 // if there's a playing music, stop it
209 // and free the music loaded
210 if (pMusicData != NULL) {
211 if (PlayingMusic())
212 StopMusic();
213 //WARNING: this is buggy
214 Mix_FreeMusic( pMusicData );
215 pMusicData = NULL; // safe
216 }
217
218 // load the new music
219 OPENCITY_DEBUG( vectorMusicFilename[ rcuiMusicIndex ].c_str() );
220 pMusicData = Mix_LoadMUS(
221 this->vectorMusicFilename[ rcuiMusicIndex ].c_str() );
222 if (pMusicData == NULL) {
223 return OC_ERR_SOMETHING;
224 }
225 OPENCITY_DEBUG("Music succesfully loaded ");
226
227 if (Mix_PlayMusic( pMusicData, rciLoops ) < 0) {
228 return OC_ERR_SOMETHING;
229 }
230 else {
231 // set the current music to rcuiMusicIndex
232 // in case this function is called directly
233 // by the user with a music file index
234 this->uiCurrentMusic = rcuiMusicIndex;
235 }
236 } // rcuiMusicIndex OK
237
238 OPENCITY_DEBUG("Start playing music");
239 return OC_ERR_FREE;
240 }
241
242
243 /*====================================================================*/
244 OPENCITY_ERR_CODE
245 AudioManager::PlayNextMusic(
246 const int & rciLoops )
247 {
248 if ( uiNumberMusic > 0 ) {
249 // is random function activated ?
250 if ( boolRandomMusic == true ) {
251 uiCurrentMusic = rand() % uiNumberMusic;
252 }
253 else {
254 if ( this->uiCurrentMusic < this->uiNumberMusic-1 ) {
255 uiCurrentMusic++;
256 }
257 else {
258 uiCurrentMusic = 0;
259 }
260 }
261
262 return PlayMusic( uiCurrentMusic, rciLoops );
263 }
264
265 return OC_ERR_SOMETHING;
266 }
267
268
269 /*====================================================================*/
270 OPENCITY_ERR_CODE
271 AudioManager::PlayPreviousMusic(
272 const int & rciLoops )
273 {
274 if ( uiNumberMusic > 0 ) {
275 // is random function activated ?
276 if ( boolRandomMusic == true ) {
277 uiCurrentMusic = rand() % uiNumberMusic;
278 }
279 else {
280 if (this->uiCurrentMusic > 0) {
281 uiCurrentMusic--;
282 }
283 else {
284 uiCurrentMusic = uiNumberMusic-1;
285 }
286 }
287
288 return PlayMusic( uiCurrentMusic, rciLoops );
289 }
290
291 return OC_ERR_SOMETHING;
292 }
293
294
295 /*====================================================================*/
296 void
297 AudioManager::StopMusic(void) const
298 {
299 Mix_HaltMusic();
300 }
301
302
303 /*====================================================================*/
304 void
305 AudioManager::ToggleRandomMusic(void)
306 {
307 boolRandomMusic = !boolRandomMusic;
308 }
309
310
311 /*====================================================================*/
312 void
313 AudioManager::ToggleMusic(void)
314 {
315 boolMusicEnabled = !boolMusicEnabled;
316
317 if (boolMusicEnabled == false) {
318 if (this->PlayingMusic() == true)
319 this->StopMusic();
320 }
321 else {
322 if (this->PlayingMusic() == false)
323 this->PlayMusic( uiCurrentMusic, 1 );
324 }
325 }
326
327
328 /*====================================================================*/
329 void
330 AudioManager::VolumeMusic(
331 const int & rciVol ) const
332 {
333 // set the volume of the music channel to rciVol
334 Mix_VolumeMusic( rciVol );
335 }
336
337
338
339
340
341
342
343
344 /*====================================================================*/
345 OPENCITY_ERR_CODE
346 AudioManager::LoadSoundList(
347 const string& csrFilename,
348 const string& csrPrefix )
349 {
350 // Return immediatetly if the audio device is not yet initialized
351 if (boolAudioDeviceInitialized == false) {
352 return OC_ERR_SOMETHING;
353 }
354
355 // Save the filename of the list
356 this->strSoundList = csrFilename;
357
358 // Now parse the list
359 if (ParseM3UList(csrFilename, _vSoundFilename, this->uiNumberSound )
360 == OC_ERR_SOMETHING) {
361 // list parsing error
362 return OC_ERR_SOMETHING;
363 }
364
365 // Free any old vector of sounds loaded
366 if (!_vpSoundChunk.empty()) {
367 uint uiVectorSize = _vpSoundChunk.size();
368 for (uint i = 0; i < uiVectorSize; i++) {
369 Mix_FreeChunk(_vpSoundChunk[i]);
370 } // for
371 _vpSoundChunk.clear();
372 } // if
373
374 // Then load all the new sounds
375 for (uint i = 0; i < this->uiNumberSound; i++) {
376 // Store all pointer's values even if that's NULL;
377 _vpSoundChunk.push_back( Mix_LoadWAV((csrPrefix+_vSoundFilename[i]).c_str()) );
378 }
379
380 // Even if some sound files don't exist
381 return OC_ERR_FREE;
382 }
383
384
385 /*====================================================================*/
386 const uint &
387 AudioManager::GetNumberSound(void) const
388 {
389 return this->uiNumberSound;
390 }
391
392
393 /*====================================================================*/
394 OPENCITY_ERR_CODE
395 AudioManager::PlaySound(
396 const uint & rcuiSoundIndex,
397 const AUDIO_CHANNEL & enumChannel )
398 {
399 // return immediatetly if the audio device is not yet initialized
400 if (boolAudioDeviceInitialized == false) {
401 return OC_ERR_SOMETHING;
402 }
403
404 // if sound is disabled then we return
405 if (boolSoundEnabled == false) {
406 return OC_ERR_SOMETHING;
407 }
408
409 if (rcuiSoundIndex >= this->uiNumberSound) {
410 OPENCITY_DEBUG( "Audio play sound error" );
411 return OC_ERR_SOMETHING;
412 }
413
414 if (_vpSoundChunk[ rcuiSoundIndex ] == NULL) {
415 OPENCITY_DEBUG( "The sound chunk is null" );
416 return OC_ERR_SOMETHING;
417 }
418
419 // play the sound once
420 if (Mix_PlayChannel( enumChannel, _vpSoundChunk[ rcuiSoundIndex ], 0 ) == -1) {
421 OPENCITY_DEBUG( "Could not play the sound" );
422 return OC_ERR_SOMETHING;
423 }
424
425 // re-register effects for next use
426 switch ( enumChannel ) {
427 case AUDIO_LEFT_CHANNEL:
428 Mix_SetPanning( enumChannel, 254, 1 );
429 break;
430 case AUDIO_LEFT_HALF_CHANNEL:
431 Mix_SetPanning( enumChannel, 254, 127 );
432 break;
433 case AUDIO_RIGHT_CHANNEL:
434 Mix_SetPanning( enumChannel, 1, 254 );
435 break;
436 case AUDIO_RIGHT_HALF_CHANNEL:
437 Mix_SetPanning( enumChannel, 127, 254 );
438 break;
439 default:
440 break;
441 }
442
443 return OC_ERR_FREE;
444 }
445
446
447 /*====================================================================*/
448 void
449 AudioManager::ToggleSound(void)
450 {
451 boolSoundEnabled = !boolSoundEnabled;
452 }
453
454
455 /*====================================================================*/
456 void
457 AudioManager::VolumeSound(
458 const int & rciVol ) const
459 {
460 // set the volume of all channels to rciVol
461 Mix_Volume( -1, rciVol );
462 }
463
464
465
466
467
468
469
470 /*====================================================================*/
471 OPENCITY_ERR_CODE
472 AudioManager::ParseM3UList(
473 const string& csrFilename,
474 std::vector<string>& vectorFilename,
475 uint& uiNumberFiles )
476 {
477 //debug cout << (string)csrFilename << endl;
478 // If there's already an old list, free it
479 if (!vectorFilename.empty()) {
480 vectorFilename.clear();
481 uiNumberFiles = 0;
482 }
483
484 // If we are here, uiNumberFiles must be 0
485
486 // Open the ifstream for reading
487 std::ifstream listFile( csrFilename.c_str() );
488 if (listFile == NULL) {
489 OPENCITY_DEBUG( "Audio file list open error" );
490 return OC_ERR_SOMETHING;
491 }
492
493 char tempStr [OC_MAX_FILENAME_LENGTH];
494 do {
495 // Read the next info line
496 listFile.getline( tempStr, OC_MAX_FILENAME_LENGTH );
497 if ((strlen(tempStr) != 0)
498 && (tempStr[0] != '#')) {
499 // Implicite conversion from char* to string
500 vectorFilename.push_back( tempStr );
501 uiNumberFiles++;
502 //debug cout << tempStr << endl;
503 }
504 }
505 while (!listFile.eof());
506
507 listFile.close();
508 return OC_ERR_FREE;
509 }
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535