Skip to content

Commit ea63c2d

Browse files
author
aquanull
committed
Restructured sound code.
1 parent bdcc749 commit ea63c2d

File tree

17 files changed

+490
-809
lines changed

17 files changed

+490
-809
lines changed

src/common/System.cpp

Lines changed: 194 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@
66
#include "../common/movie.h"
77
#include "../common/vbalua.h"
88

9+
// evil macros
10+
#ifndef countof
11+
#define countof(a) (sizeof(a) / sizeof(a[0]))
12+
#endif
13+
914
// evil static variables
10-
static u32 lastFrameTime = 0;
11-
static int32 frameSkipCount = 0;
12-
static int32 frameCount = 0;
13-
static bool pauseAfterFrameAdvance = false;
15+
static u32 lastFrameTime = 0;
16+
static int32 frameSkipCount = 0;
17+
static int32 frameCount = 0;
18+
static bool pauseAfterFrameAdvance = false;
19+
20+
static s16 soundFilter[4000];
21+
static s16 soundRight[5] = { 0, 0, 0, 0, 0 };
22+
static s16 soundLeft[5] = { 0, 0, 0, 0, 0 };
23+
static int32 soundEchoIndex = 0;
1424

1525
// systemABC stuff are core-related
1626

@@ -350,3 +360,183 @@ void systemGetLCDBaseOffset(int32 &xofs, int32 &yofs)
350360
break;
351361
}
352362
}
363+
364+
// sound
365+
366+
bool systemSoundCleanInit()
367+
{
368+
if (systemSoundInit())
369+
{
370+
memset(soundBuffer[0], 0, 735);
371+
memset(soundBuffer[1], 0, 735);
372+
memset(soundBuffer[2], 0, 735);
373+
memset(soundBuffer[3], 0, 735);
374+
memset(soundFinalWave, 0, soundBufferLen);
375+
376+
soundPaused = 1;
377+
return true;
378+
}
379+
return false;
380+
}
381+
382+
void systemSoundEnableChannels(int channels)
383+
{
384+
int c = (channels & 0x0f) << 4;
385+
soundEnableFlag |= ((channels & 0x30f) | c);
386+
}
387+
388+
void systemSoundDisableChannels(int channels)
389+
{
390+
int c = (channels & 0x0f) << 4;
391+
soundEnableFlag &= ~((channels & 0x30f) | c);
392+
}
393+
394+
int systemSoundGetEnabledChannels()
395+
{
396+
return (soundEnableFlag & 0x30f);
397+
}
398+
399+
void systemSoundSetQuality(int quality)
400+
{
401+
if (soundQuality != quality)
402+
{
403+
if (!soundOffFlag)
404+
systemSoundShutdown();
405+
soundQuality = quality;
406+
if (!soundOffFlag)
407+
systemSoundCleanInit();
408+
}
409+
else if (soundQuality != quality)
410+
{
411+
soundNextPosition = 0;
412+
}
413+
soundNextPosition = 0;
414+
soundTickStep = USE_TICKS_AS * soundQuality;
415+
soundIndex = 0;
416+
soundBufferIndex = 0;
417+
}
418+
419+
void systemSoundMixReset()
420+
{
421+
soundBufferIndex = 0;
422+
memset(soundFinalWave, 0, soundBufferLen);
423+
memset(soundFilter, 0, sizeof(soundFilter));
424+
soundEchoIndex = 0;
425+
}
426+
427+
void systemSoundMixSilence()
428+
{
429+
soundFinalWave[soundBufferIndex++] = 0;
430+
soundFinalWave[soundBufferIndex++] = 0;
431+
if ((soundFrameSoundWritten + 1) < countof(soundFrameSound))
432+
{
433+
soundFrameSound[soundFrameSoundWritten++] = 0;
434+
soundFrameSound[soundFrameSoundWritten++] = 0;
435+
}
436+
}
437+
438+
void systemSoundMix(int resL, int resR)
439+
{
440+
bool usesDSP = systemSoundAppliesDSP();
441+
if (soundEcho)
442+
{
443+
if (soundEchoIndex >= countof(soundFilter))
444+
soundEchoIndex = 0;
445+
446+
resL += soundFilter[soundEchoIndex] / 2;
447+
soundFilter[soundEchoIndex++] = resL;
448+
449+
resR += soundFilter[soundEchoIndex] / 2;
450+
soundFilter[soundEchoIndex++] = resR;
451+
}
452+
453+
if (soundLowPass)
454+
{
455+
soundLeft[4] = soundLeft[3];
456+
soundLeft[3] = soundLeft[2];
457+
soundLeft[2] = soundLeft[1];
458+
soundLeft[1] = soundLeft[0];
459+
soundLeft[0] = resL;
460+
resL = (soundLeft[4] + 2 * soundLeft[3] + 8 * soundLeft[2] + 2 * soundLeft[1] + soundLeft[0]) / 14;
461+
462+
soundRight[4] = soundRight[3];
463+
soundRight[3] = soundRight[2];
464+
soundRight[2] = soundRight[1];
465+
soundRight[1] = soundRight[0];
466+
soundRight[0] = resR;
467+
resR = (soundRight[4] + 2 * soundRight[3] + 8 * soundRight[2] + 2 * soundRight[1] + soundRight[0]) / 14;
468+
}
469+
470+
if (usesDSP)
471+
{
472+
switch (soundVolume)
473+
{
474+
case 0:
475+
case 1:
476+
case 2:
477+
case 3:
478+
resL *= (soundVolume + 1);
479+
resR *= (soundVolume + 1);
480+
break;
481+
case 4:
482+
resL >>= 2;
483+
resR >>= 2;
484+
break;
485+
case 5:
486+
resL >>= 1;
487+
resR >>= 1;
488+
break;
489+
}
490+
}
491+
492+
if (resL > 32767)
493+
resL = 32767;
494+
else if (resL < -32768)
495+
resL = -32768;
496+
497+
if (resR > 32767)
498+
resR = 32767;
499+
else if (resR < -32768)
500+
resR = -32768;
501+
502+
if (soundReverse && usesDSP)
503+
{
504+
soundFinalWave[soundBufferIndex++] = resR;
505+
soundFinalWave[soundBufferIndex++] = resL;
506+
if ((soundFrameSoundWritten + 1) < countof(soundFrameSound))
507+
{
508+
soundFrameSound[soundFrameSoundWritten++] = resR;
509+
soundFrameSound[soundFrameSoundWritten++] = resL;
510+
}
511+
}
512+
else
513+
{
514+
soundFinalWave[soundBufferIndex++] = resL;
515+
soundFinalWave[soundBufferIndex++] = resR;
516+
if (soundFrameSoundWritten + 1 < countof(soundFrameSound))
517+
{
518+
soundFrameSound[soundFrameSoundWritten++] = resL;
519+
soundFrameSound[soundFrameSoundWritten++] = resR;
520+
}
521+
}
522+
}
523+
524+
void systemSoundNext()
525+
{
526+
soundIndex++;
527+
528+
if (2 * soundBufferIndex >= soundBufferLen)
529+
{
530+
if (systemSoundOn)
531+
{
532+
if (soundPaused && !systemIsPaused()) // this checking is for the old frame timing
533+
{
534+
systemSoundResume();
535+
}
536+
537+
systemSoundWriteToBuffer();
538+
}
539+
soundIndex = 0;
540+
soundBufferIndex = 0;
541+
}
542+
}

src/common/System.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ extern void systemSoundShutdown();
4545
extern void systemSoundPause();
4646
extern void systemSoundResume();
4747
extern bool systemSoundIsPaused();
48-
extern void systemSoundReset();
48+
extern void systemSoundResetBuffer();
4949
extern void systemSoundWriteToBuffer();
5050
extern void systemSoundClearBuffer();
51-
extern bool systemSoundCanChangeQuality();
52-
extern bool systemSoundSetQuality(int quality);
51+
extern bool systemSoundCleanInit();
52+
extern void systemSoundEnableChannels(int);
53+
extern void systemSoundDisableChannels(int);
54+
extern int systemSoundGetEnabledChannels();
55+
extern void systemSoundSetQuality(int quality);
56+
extern bool systemSoundAppliesDSP();
57+
extern void systemSoundMixReset();
58+
extern void systemSoundMixSilence();
59+
extern void systemSoundMix(int resL, int resR);
60+
extern void systemSoundNext();
5361
// speed-related stuff
5462
extern u32 systemGetClock();
5563
extern void systemSetTitle(const char *);

src/common/SystemGlobals.cpp

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,66 @@ u16 lastKeys = 0;
2727
int32 sensorX = 0;
2828
int32 sensorY = 0;
2929

30-
bool newFrame = true;
31-
bool8 speedup = false;
32-
u32 extButtons = 0;
33-
bool8 capturePrevious = false;
34-
int32 captureNumber = 0;
30+
bool newFrame = true;
31+
bool8 speedup = false;
32+
u32 extButtons = 0;
33+
bool8 capturePrevious = false;
34+
int32 captureNumber = 0;
35+
36+
soundtick_t USE_TICKS_AS = 0;
37+
soundtick_t soundTickStep = soundQuality * USE_TICKS_AS;
38+
soundtick_t soundTicks = 0;
39+
40+
u32 soundIndex = 0;
41+
int32 soundPaused = 1;
42+
int32 soundPlay = 0;
43+
u32 soundNextPosition = 0;
44+
45+
u8 soundBuffer[6][735];
46+
u32 soundBufferLen = 1470;
47+
u32 soundBufferTotalLen = 14700;
48+
u32 soundBufferIndex = 0;
49+
50+
51+
u16 soundFinalWave[1470];
52+
u16 soundFrameSound[735 * 30 * 2]; // for avi logging
53+
int32 soundFrameSoundWritten = 0;
54+
55+
bool tempSaveSafe = true;
56+
int tempSaveID = 0;
57+
int tempSaveAttempts = 0;
3558

3659
// settings
37-
bool8 synchronize = true;
38-
int32 gbFrameSkip = 0;
39-
int32 frameSkip = 0;
60+
bool8 synchronize = true;
61+
int32 gbFrameSkip = 0;
62+
int32 frameSkip = 0;
4063

41-
bool8 cpuDisableSfx = false;
42-
int32 layerSettings = 0xff00;
64+
bool8 cpuDisableSfx = false;
65+
int32 layerSettings = 0xff00;
4366

4467
#ifdef USE_GB_CORE_V7
45-
bool8 gbNullInputHackEnabled = false;
46-
bool8 gbNullInputHackTempEnabled = false;
68+
bool8 gbNullInputHackEnabled = false;
69+
bool8 gbNullInputHackTempEnabled = false;
4770
#endif
4871

4972
#ifdef USE_GBA_CORE_V7
50-
bool8 memLagEnabled = false;
51-
bool8 memLagTempEnabled = false;
73+
bool8 memLagEnabled = false;
74+
bool8 memLagTempEnabled = false;
5275
#endif
5376

54-
bool8 useOldFrameTiming = false;
55-
bool8 useBios = false;
56-
bool8 skipBios = false;
57-
bool8 skipSaveGameBattery = false;
58-
bool8 skipSaveGameCheats = false;
59-
bool8 cheatsEnabled = true;
60-
bool8 mirroringEnable = false;
77+
bool8 useOldFrameTiming = false;
78+
bool8 useBios = false;
79+
bool8 skipBios = false;
80+
bool8 skipSaveGameBattery = false;
81+
bool8 skipSaveGameCheats = false;
82+
bool8 cheatsEnabled = true;
83+
bool8 mirroringEnable = false;
6184

62-
bool8 cpuEnhancedDetection = true;
85+
bool8 cpuEnhancedDetection = true;
6386

64-
bool tempSaveSafe = true;
65-
int tempSaveID = 0;
66-
int tempSaveAttempts = 0;
87+
int32 soundVolume = 0;
88+
int32 soundQuality = 2;
89+
bool8 soundEcho = false;
90+
bool8 soundLowPass = false;
91+
bool8 soundReverse = false;
92+
bool8 soundOffFlag = false;

0 commit comments

Comments
 (0)