@@ -22,16 +22,14 @@ BSoundEngine::BSoundEngine() {
2222 mAudioBuffer = ENull;
2323 mMuted = ETrue;
2424 musicFileLoaded = EFalse;
25+ // mMusicVolume = 200;
2526
2627 BSoundEngine::mAudioPaused = false ;
2728#ifdef DISABLE_AUDIO
2829 return ;
2930#endif
3031 xmpContext1 = xmp_create_context ();
3132 xmpContext2 = xmp_create_context ();
32- // printf("xmpContext1 = %p\n", &xmpContext1);
33- // printf("xmpContext2 = %p\n", &xmpContext2);
34-
3533}
3634
3735BSoundEngine::~BSoundEngine () {
@@ -48,7 +46,6 @@ BSoundEngine::~BSoundEngine() {
4846 }
4947 delete[] mEffects ;
5048
51- Mixer_HookMusic (ENull, ENull);
5249 SDL_CloseAudio ();
5350
5451 // Cleanup LibXMP
@@ -66,11 +63,6 @@ BSoundEngine::~BSoundEngine() {
6663
6764bool WARNED_OF_PLAY_BUFFER = false ;
6865
69- // #ifdef __DINGUX__
70- // static Uint32 audio_len;
71- // static Uint8 *audio_pos;
72- // #endif
73-
7466static void fillBuffer (Uint8 *audioBuffer, size_t length) {
7567// printf("Mixer_PlayingMusic() = %i\n",Mixer_PlayingMusic());
7668
@@ -81,7 +73,7 @@ static void fillBuffer(Uint8 *audioBuffer, size_t length) {
8173 if (musicFileLoaded && ! soundEngine.mAudioPaused ) {
8274// int result = 0;
8375 int result = xmp_play_buffer (*currentContext, audioBuffer, (int )length, 0 );
84- // printf("xmp_play_buffer result = %i , length = %i\n", result, length);
76+ // printf("xmp_play_buffer result = %i , length = %i\n", result, (int) length);
8577 if (result != 0 ) {
8678 if (!WARNED_OF_PLAY_BUFFER ) {
8779 // Something really bad happened, and audio stopped :(
@@ -91,7 +83,6 @@ static void fillBuffer(Uint8 *audioBuffer, size_t length) {
9183 }
9284 bzero (audioBuffer, length);
9385 }
94- Mixer_MixChannels (audioBuffer, length);
9586 }
9687 else {
9788 bzero (audioBuffer, length);
@@ -101,6 +92,8 @@ static void fillBuffer(Uint8 *audioBuffer, size_t length) {
10192 if (soundEngine.IsMuted ()) {
10293 bzero (audioBuffer, length);
10394 }
95+
96+ Mixer_MixChannels (audioBuffer, length);
10497}
10598
10699// SDL style timer
@@ -148,11 +141,8 @@ void BSoundEngine::InitAudioEngine(TUint8 aNumSfxChannels, TUint8 aNumSfxFiles)
148141 exit (-1 );
149142 return ;
150143 }
151- printf (" Allocating mNumSfxChannels = %i \n " , mNumSfxChannels );
152144 Mixer_SetAudioSpec (&audioSpec);
153145 Mixer_AllocateChannels (mNumSfxChannels );
154- fprintf (stderr, " %s\n " , SDL_GetError ());
155- // Mixer_HookMusic(timerCallback, ENull);
156146
157147 // Kick off SDL audio engine
158148 SDL_PauseAudio (0 );
@@ -162,9 +152,9 @@ void BSoundEngine::InitAudioEngine(TUint8 aNumSfxChannels, TUint8 aNumSfxFiles)
162152
163153TBool BSoundEngine::LoadSong (BRaw *aSong) {
164154 xmp_context *loadingContext = (currentContext == &xmpContext1) ? &xmpContext2 : &xmpContext1;
165- // #ifdef DISABLE_AUDIO
166- // return 0;
167- // #endif
155+ #ifdef DISABLE_AUDIO
156+ return 0 ;
157+ #endif
168158
169159 int loadResult = xmp_load_module_from_memory (*loadingContext, aSong->mData , aSong->mSize );
170160
@@ -176,41 +166,31 @@ TBool BSoundEngine::LoadSong(BRaw *aSong) {
176166}
177167
178168TBool BSoundEngine::LoadEffect (TUint8 aSfxIndex, TUint16 aResourceId, TUint8 aResourceSlot) {
179- printf (" \n BSoundEngine::LoadEffect(aSfxIndex = %i, aResourceId = %i, aResourceSlot = %i)\n " , aSfxIndex, aResourceId, aResourceSlot);
180-
169+ #ifdef DISABLE_AUDIO
170+ return 0 ;
171+ #endif
181172 if (gResourceManager .GetRaw (aResourceSlot)) {
182173 gResourceManager .ReleaseRawSlot (aResourceId);
183174 }
184175
185176 gResourceManager .LoadRaw (aResourceId, aResourceSlot);
186177
187178 BRaw *rawEffect = gResourceManager .GetRaw (aResourceSlot);
188- // printf("rawEffect->mSize %i\n", rawEffect->mSize);
189179
190180 SDL_RWops *file = SDL_RWFromMem (rawEffect->mData , rawEffect->mSize );
191181 Mixer_Chunk *chunk = Mixer_LoadWAV_RW (file, 1 );
192- // mEffects[aSfxIndex] = Mixer_QuickLoad_WAV(rawEffect->mData);
193- // Mixer_Chunk *chunk = Mixer_QuickLoad_WAV(rawEffect->mData);
194- // printf("Chunk %i : alen %i, allocated %i, volume %i\n", aSfxIndex, chunk->alen, chunk->allocated, chunk->volume);
195182
196183 mEffects [aSfxIndex] = chunk;
197- // gResourceManager.ReleaseRawSlot(aResourceId);
198- // printf("Result = %i\n", result);
199-
200- // printf("mEffects[%i] = %p\n", aResourceSlot, mEffects[aSfxIndex]);
201184 return ETrue;
202185}
203186
204187
205188TBool BSoundEngine::PlaySfx (TInt aSoundNumber) {
206- // return ETrue;
207- for (int i = 0 ; i< mNumberFxChannels ; i++) {
208- Mixer_Volume (i, MIXER_MAX_VOLUME );
209- }
189+ #ifdef DISABLE_AUDIO
190+ return 0 ;
191+ #endif
210192 SDL_ClearError ();
211- printf (" %s(%i)\n " , __FUNCTION__, aSoundNumber);
212193 Mixer_Chunk *chunk = mEffects [aSoundNumber];
213- printf (" Chunk %i : alen %i, allocated %i, volume %i\n " , aSoundNumber, chunk->alen , chunk->allocated , chunk->volume );
214194
215195 int result = Mixer_PlayChannel (-1 , chunk, 0 );
216196 if (result < 0 ) {
@@ -272,6 +252,7 @@ TBool BSoundEngine::PlayMusic(BRaw *aRawFile, TInt16 aResourceId) {
272252 }
273253
274254 xmp_start_player (*loadingContext, SAMPLE_RATE , 0 );
255+ printf (" Setting loading context volume %i\n " , mMusicVolume );
275256 xmp_set_player (*loadingContext, XMP_PLAYER_VOLUME , mMusicVolume );
276257 xmp_set_player (*loadingContext, XMP_PLAYER_MIX , 0 );
277258
@@ -310,7 +291,7 @@ TBool BSoundEngine::SetVolume(TFloat aPercent) {
310291 printf (" setVolume %2f\n " , aPercent);
311292
312293 mMusicVolume = (TUint8)(aPercent * 200 );
313- mEffectsVolume = (TUint8)(aPercent * 254 );
294+ mEffectsVolume = (TUint8)(aPercent * MIXER_MAX_VOLUME );
314295 xmp_set_player (*currentContext, XMP_PLAYER_VOLUME , mMusicVolume );
315296 return ETrue;
316297 }
@@ -328,24 +309,31 @@ TBool BSoundEngine::SetMusicVolume(TFloat aPercent) {
328309 if (aPercent < 0 .0f ) {
329310 aPercent = 0 ;
330311 }
331-
332- // aPercent = .1;
333- mMusicVolume = (TUint8)(aPercent * 200 );
334- // Mixer_Volume((TUint8)aPercent * MIXER_MAX_VOLUME);
312+ auto volume = (TUint8)(aPercent * 200 );
313+ printf (" SetMusicVolume volume = %i, pct = %2f\n " , volume, aPercent);
314+ mMusicVolume = volume;
335315 xmp_set_player (*currentContext, XMP_PLAYER_VOLUME , mMusicVolume );
336- return true ;
316+ return ETrue ;
337317}
338318
339319TBool BSoundEngine::SetEffectsVolume (TFloat aPercent) {
340- TUint8 volume = (TUint8)aPercent * MIXER_MAX_VOLUME ;
320+ #ifdef DISABLE_AUDIO
321+ return 0 ;
322+ #endif
323+ if (aPercent > 1 .0f ) {
324+ aPercent = 1 .0f ;
325+ }
326+
327+ if (aPercent < 0 .0f ) {
328+ aPercent = 0 ;
329+ }
341330
331+ auto volume = (TUint8)(aPercent * MIXER_MAX_VOLUME );
332+ printf (" SetEffectsVolume volume = %i, pct = %2f\n " , volume, aPercent);
342333 for (int i = 0 ; i < mNumSfxChannels ; i++) {
343334 Mixer_Volume (i, volume);
344335 }
345- #ifdef DISABLE_AUDIO
346- return false ;
347- #endif
348- return false ;
336+ return ETrue;
349337}
350338
351339BSoundEngine soundEngine;
0 commit comments