New sound effects mixer. Odd audio stumbling issue.

This commit is contained in:
Scott Duensing 2022-09-15 19:07:43 -05:00
parent 2ca7c683dd
commit 92894cd6d2
2 changed files with 69 additions and 75 deletions

View file

@ -2,6 +2,7 @@ JOEY = /home/scott/joey
#CONFIG += SDL12 #CONFIG += SDL12
CONFIG += SDL2 CONFIG += SDL2
BREW = /home/linuxbrew/.linuxbrew
TEMPLATE = app TEMPLATE = app
CONFIG += console CONFIG += console
@ -9,9 +10,23 @@ CONFIG -= \
app_bundle \ app_bundle \
qt qt
QMAKE_CFLAGS += \ defined(BREW,var) {
-I$$PWD/src \ # Use Homebrew libraries.
-D_REENTRANT STATICLIB = $$BREW/lib
INCLUDEPATH += $$BREW/include
LIBS += \
-L$$STATICLIB \
-Wl,-rpath,$$STATICLIB
} else {
# Use our libraries.
STATICLIB = $$JOEY/sdks/linux/x64/lib
INCLUDEPATH += $$JOEY/sdks/linux/x64/include
LIBS += \
-L$$STATICLIB \
-Wl,-rpath,$$STATICLIB
}
INCLUDEPATH += $$PWD/src
HEADERS += \ HEADERS += \
src/3rdparty/memwatch/memwatch.h \ src/3rdparty/memwatch/memwatch.h \
@ -25,15 +40,11 @@ SOURCES += \
src/test.c src/test.c
SDL12 { SDL12 {
INCLUDEPATH += $$JOEY/sdks/linux/x64/include
SOURCES += \ SOURCES += \
src/jPixBuf.c \ src/jPixBuf.c \
src/jSDL12.c src/jSDL12.c
LIBS += \ LIBS += \
-L$$JOEY/sdks/linux/x64/lib \
-Wl,-rpath,$$JOEY/sdks/linux/x64/lib \
-lSDL \ -lSDL \
-lm \ -lm \
-ldl \ -ldl \
@ -41,7 +52,9 @@ SDL12 {
} }
SDL2 { SDL2 {
INCLUDEPATH += $$JOEY/sdks/linux/x64/include QMAKE_CFLAGS += \
-Wall \
-D_REENTRANT
HEADERS += \ HEADERS += \
src/3rdparty/pocketmod/pocketmod.h src/3rdparty/pocketmod/pocketmod.h
@ -51,14 +64,10 @@ SDL2 {
src/jSDL2.c src/jSDL2.c
LIBS += \ LIBS += \
-L$$JOEY/sdks/linux/x64/lib \
-Wl,--enable-new-dtags \ -Wl,--enable-new-dtags \
-lSDL2 \ -lSDL2
-Wl,--no-undefined \ # $$STATICLIB/libSDL2.a \
-lm \ # -lm -ldl -lX11 -lXext -lXcursor -lXi -lXfixes -lXrandr -lXss -lpthread -lrt
-ldl \
-lpthread \
-lrt
} }
OTHER_FILES += \ OTHER_FILES += \
@ -68,11 +77,3 @@ OTHER_FILES += \
src/jIIgs.asm \ src/jIIgs.asm \
src/jIIgs.macro \ src/jIIgs.macro \
joey.pri joey.pri
dmalloc {
DEFINES += DMALLOC
INCLUDEPATH += /opt/dmalloc/include
LIBS += \
-L/opt/dmalloc/lib \
-ldmallocth
}

View file

@ -75,6 +75,7 @@ static jbool _jlModPlaying = jfalse;
static jlPlatformModT *_jlModCurrent = NULL; static jlPlatformModT *_jlModCurrent = NULL;
static jbyte _jlModVolume = 255; static jbyte _jlModVolume = 255;
static jlSoundPlayingT *_jlSoundList = NULL; static jlSoundPlayingT *_jlSoundList = NULL;
static jbyte _jlSoundsPlaying = 0;
static Uint32 _jlUtilTimer(Uint32 interval, void *param); static Uint32 _jlUtilTimer(Uint32 interval, void *param);
@ -167,10 +168,7 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) {
float *out = NULL; float *out = NULL;
float adjust = 1; float adjust = 1;
double work = 0; double work = 0;
uint8_t *mix = NULL;
jbool isRight = jfalse; jbool isRight = jfalse;
static uint8_t *samples = NULL;
static uint32_t sampleBufferSize = 0;
(void)userdata; (void)userdata;
@ -184,54 +182,40 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) {
} }
// Adjust volume. // Adjust volume.
adjust = ((100.0 / 255.0) * (float)_jlModVolume) * 0.01; adjust = ((100.0 / 255.0) * (float)_jlModVolume) * 0.01;
data = (float *)buffer; out = (float *)buffer;
for (i=0; i<(int)(bytes / sizeof(float)); i+=2) { for (i=0; i<(int)(bytes / sizeof(float)); i+=2) {
data[i] *= adjust; out[i] *= adjust;
data[i + 1] *= adjust; out[i + 1] *= adjust;
}
}
// Create a sample buffer for manipulating sound effects.
if (sampleBufferSize < (uint32_t)bytes) {
if (samples) jlFree(samples);
samples = jlMalloc(bytes);
if (samples) {
sampleBufferSize = bytes;
} else {
sampleBufferSize = 0;
} }
} }
// Mix in sounds. // Mix in sounds.
// Just adding samples together seems fine for floating point data.
// For integers, we need to clamp.
if (_jlSoundList) { if (_jlSoundList) {
sound = _jlSoundList; sound = _jlSoundList;
while (sound) { while (sound) {
// Find length of data to mix.
length = ((uint32_t)bytes > sound->len) ? sound->len : (uint32_t)bytes; length = ((uint32_t)bytes > sound->len) ? sound->len : (uint32_t)bytes;
// If we have a sample buffer, move sound to desired channel. // Adjust volume.
if (samples) { adjust = ((100.0 / 255.0) * (float)sound->volume) * 0.01;
mix = samples; // Point at sample data.
out = (float *)samples; data = (float *)sound->buffer;
data = (float *)sound->buffer; out = (float *)buffer;
for (i=0; i<(int)(length / sizeof(float)); i+=2) { for (i=0; i<(int)(length / sizeof(float)); i+=2) {
// Combine channels into a mono sample. // Combine channels into a mono sample.
work = (data[i] + data[i + 1]) * 0.5; work = (data[i] + data[i + 1]) * 0.5;
// Determine channel. // Determine channel.
isRight = (sound->channel & 0x01); isRight = (sound->channel & 0x01);
if (_jlSwapChannels) isRight = !isRight; if (_jlSwapChannels) isRight = !isRight;
if (isRight) { if (isRight) {
// Move sound into right channel. // Place sound in the right channel.
out[i] = 0; out[i + 1] += (work * adjust);
out[i + 1] = work; } else {
} else { // Place sound in left channel.
// Move sound into left channel. out[i] += (work * adjust);
out[i] = work;
out[i + 1] = 0;
}
} }
} else {
mix = sound->buffer;
} }
SDL_MixAudioFormat(buffer, mix, AUDIO_FORMAT, length, sound->volume * 0.5);
sound->buffer += length; sound->buffer += length;
sound->len -= length; sound->len -= length;
// Are we done with this sound? // Are we done with this sound?
@ -244,26 +228,33 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) {
temp = sound; temp = sound;
sound = sound->next; sound = sound->next;
jlFree(temp); jlFree(temp);
_jlSoundsPlaying--;
} else { } else {
prev = sound; prev = sound;
sound = sound->next; sound = sound->next;
} }
} }
} }
jlUtilSay("");
} }
void jlDisplayPresent(void) { void jlDisplayPresent(void) {
int i = 0;
int j = 0;
int pitch = 0;
void *pixelData = NULL;
Uint32 *pixels = NULL;
// Render 4 bit copy to proper pixel format. // Render 4 bit copy to proper pixel format.
// This extra step preserves palette effects. // This extra step preserves palette effects.
//***TODO*** Fake border colors on PC //***TODO*** Fake border colors on PC
int i = 0;
int j = 0;
int pitch = 0;
void *pixelData = NULL;
jlUtilIdle(); jlUtilIdle();
SDL_LockTexture(_jlTexture, NULL, &pixelData, &pitch); SDL_LockTexture(_jlTexture, NULL, &pixelData, &pitch);
Uint32 *pixels = (Uint32 *)pixelData; pixels = (Uint32 *)pixelData;
for (int y=0; y<200; y++) { for (int y=0; y<200; y++) {
for (int x=0; x<160; x++) { for (int x=0; x<160; x++) {
// We decode this R/L instead of L/R for some reason. NO idea why yet. Endians? // We decode this R/L instead of L/R for some reason. NO idea why yet. Endians?
@ -512,7 +503,8 @@ jbool _jlSoundLoad(jlSoundT **sound, char *filename) {
memcpy(soundPlatform->data, cvt.buf, soundPlatform->len); memcpy(soundPlatform->data, cvt.buf, soundPlatform->len);
result = jtrue; result = jtrue;
} }
jlFree(cvt.buf); data = cvt.buf; // This prevents a warning about taking the address of a packed struct.
jlFree(data);
} }
} else { } else {
// No. Use as-is. // No. Use as-is.
@ -527,9 +519,7 @@ jbool _jlSoundLoad(jlSoundT **sound, char *filename) {
} }
} }
if (!result) { if (!result && soundPlatform) jlFree(soundPlatform);
if (soundPlatform) jlFree(soundPlatform);
}
return result; return result;
} }
@ -548,6 +538,7 @@ void jlSoundPlay(jlSoundT *sound, jlSoundChannelE channel, jbyte volume) {
sp->len = sp->sound->len; sp->len = sp->sound->len;
sp->next = _jlSoundList; sp->next = _jlSoundList;
_jlSoundList = sp; _jlSoundList = sp;
_jlSoundsPlaying++;
SDL_UnlockAudioDevice(_jlAudioDevice); SDL_UnlockAudioDevice(_jlAudioDevice);
} }
} }
@ -571,6 +562,7 @@ void jlSoundStop(jlSoundT *sound) {
temp = cur; temp = cur;
cur = cur->next; cur = cur->next;
jlFree(temp); jlFree(temp);
_jlSoundsPlaying--;
} else { } else {
prev = cur; prev = cur;
cur = cur->next; cur = cur->next;
@ -616,6 +608,7 @@ char _jlUtilIdleCheckKey(int sym) {
void jlUtilIdle(void) { void jlUtilIdle(void) {
SDL_Event event; SDL_Event event;
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
switch(event.type) { switch(event.type) {
case SDL_KEYDOWN: case SDL_KEYDOWN:
@ -658,6 +651,8 @@ void jlUtilIdle(void) {
_jlIsRunning = jfalse; _jlIsRunning = jfalse;
break; break;
} }
SDL_Delay(1);
} }
//printf("Keys down: %d\n", _jlNumKeysDown); //printf("Keys down: %d\n", _jlNumKeysDown);
@ -739,9 +734,7 @@ int main(int argc, char *argv[]) {
jPixBufStop(); jPixBufStop();
SDL_RemoveTimer(_jlTimerId); SDL_RemoveTimer(_jlTimerId);
for (x=0; x<_jlControllerCount; x++) { for (x=0; x<_jlControllerCount; x++) SDL_GameControllerClose(_jlControllers[x]);
SDL_GameControllerClose(_jlControllers[x]);
}
_jlControllerCount = 0; _jlControllerCount = 0;
jlFree(_jlControllers); jlFree(_jlControllers);
SDL_PauseAudioDevice(_jlAudioDevice, 1); SDL_PauseAudioDevice(_jlAudioDevice, 1);