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 += SDL2
BREW = /home/linuxbrew/.linuxbrew
TEMPLATE = app
CONFIG += console
@ -9,9 +10,23 @@ CONFIG -= \
app_bundle \
qt
QMAKE_CFLAGS += \
-I$$PWD/src \
-D_REENTRANT
defined(BREW,var) {
# Use Homebrew libraries.
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 += \
src/3rdparty/memwatch/memwatch.h \
@ -25,15 +40,11 @@ SOURCES += \
src/test.c
SDL12 {
INCLUDEPATH += $$JOEY/sdks/linux/x64/include
SOURCES += \
src/jPixBuf.c \
src/jSDL12.c
LIBS += \
-L$$JOEY/sdks/linux/x64/lib \
-Wl,-rpath,$$JOEY/sdks/linux/x64/lib \
-lSDL \
-lm \
-ldl \
@ -41,7 +52,9 @@ SDL12 {
}
SDL2 {
INCLUDEPATH += $$JOEY/sdks/linux/x64/include
QMAKE_CFLAGS += \
-Wall \
-D_REENTRANT
HEADERS += \
src/3rdparty/pocketmod/pocketmod.h
@ -51,14 +64,10 @@ SDL2 {
src/jSDL2.c
LIBS += \
-L$$JOEY/sdks/linux/x64/lib \
-Wl,--enable-new-dtags \
-lSDL2 \
-Wl,--no-undefined \
-lm \
-ldl \
-lpthread \
-lrt
-lSDL2
# $$STATICLIB/libSDL2.a \
# -lm -ldl -lX11 -lXext -lXcursor -lXi -lXfixes -lXrandr -lXss -lpthread -lrt
}
OTHER_FILES += \
@ -68,11 +77,3 @@ OTHER_FILES += \
src/jIIgs.asm \
src/jIIgs.macro \
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 jbyte _jlModVolume = 255;
static jlSoundPlayingT *_jlSoundList = NULL;
static jbyte _jlSoundsPlaying = 0;
static Uint32 _jlUtilTimer(Uint32 interval, void *param);
@ -167,10 +168,7 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) {
float *out = NULL;
float adjust = 1;
double work = 0;
uint8_t *mix = NULL;
jbool isRight = jfalse;
static uint8_t *samples = NULL;
static uint32_t sampleBufferSize = 0;
(void)userdata;
@ -184,54 +182,40 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) {
}
// Adjust volume.
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) {
data[i] *= adjust;
data[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;
out[i] *= adjust;
out[i + 1] *= adjust;
}
}
// Mix in sounds.
// Just adding samples together seems fine for floating point data.
// For integers, we need to clamp.
if (_jlSoundList) {
sound = _jlSoundList;
while (sound) {
// Find length of data to mix.
length = ((uint32_t)bytes > sound->len) ? sound->len : (uint32_t)bytes;
// If we have a sample buffer, move sound to desired channel.
if (samples) {
mix = samples;
out = (float *)samples;
data = (float *)sound->buffer;
for (i=0; i<(int)(length / sizeof(float)); i+=2) {
// Combine channels into a mono sample.
work = (data[i] + data[i + 1]) * 0.5;
// Determine channel.
isRight = (sound->channel & 0x01);
if (_jlSwapChannels) isRight = !isRight;
if (isRight) {
// Move sound into right channel.
out[i] = 0;
out[i + 1] = work;
} else {
// Move sound into left channel.
out[i] = work;
out[i + 1] = 0;
}
// Adjust volume.
adjust = ((100.0 / 255.0) * (float)sound->volume) * 0.01;
// Point at sample data.
data = (float *)sound->buffer;
out = (float *)buffer;
for (i=0; i<(int)(length / sizeof(float)); i+=2) {
// Combine channels into a mono sample.
work = (data[i] + data[i + 1]) * 0.5;
// Determine channel.
isRight = (sound->channel & 0x01);
if (_jlSwapChannels) isRight = !isRight;
if (isRight) {
// Place sound in the right channel.
out[i + 1] += (work * adjust);
} else {
// Place sound in left channel.
out[i] += (work * adjust);
}
} else {
mix = sound->buffer;
}
SDL_MixAudioFormat(buffer, mix, AUDIO_FORMAT, length, sound->volume * 0.5);
sound->buffer += length;
sound->len -= length;
// Are we done with this sound?
@ -244,26 +228,33 @@ static void _jlAudioCallback(void *userdata, Uint8 *buffer, int bytes) {
temp = sound;
sound = sound->next;
jlFree(temp);
_jlSoundsPlaying--;
} else {
prev = sound;
sound = sound->next;
}
}
}
jlUtilSay("");
}
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.
// This extra step preserves palette effects.
//***TODO*** Fake border colors on PC
int i = 0;
int j = 0;
int pitch = 0;
void *pixelData = NULL;
jlUtilIdle();
SDL_LockTexture(_jlTexture, NULL, &pixelData, &pitch);
Uint32 *pixels = (Uint32 *)pixelData;
pixels = (Uint32 *)pixelData;
for (int y=0; y<200; y++) {
for (int x=0; x<160; x++) {
// 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);
result = jtrue;
}
jlFree(cvt.buf);
data = cvt.buf; // This prevents a warning about taking the address of a packed struct.
jlFree(data);
}
} else {
// No. Use as-is.
@ -527,9 +519,7 @@ jbool _jlSoundLoad(jlSoundT **sound, char *filename) {
}
}
if (!result) {
if (soundPlatform) jlFree(soundPlatform);
}
if (!result && soundPlatform) jlFree(soundPlatform);
return result;
}
@ -548,6 +538,7 @@ void jlSoundPlay(jlSoundT *sound, jlSoundChannelE channel, jbyte volume) {
sp->len = sp->sound->len;
sp->next = _jlSoundList;
_jlSoundList = sp;
_jlSoundsPlaying++;
SDL_UnlockAudioDevice(_jlAudioDevice);
}
}
@ -571,6 +562,7 @@ void jlSoundStop(jlSoundT *sound) {
temp = cur;
cur = cur->next;
jlFree(temp);
_jlSoundsPlaying--;
} else {
prev = cur;
cur = cur->next;
@ -616,6 +608,7 @@ char _jlUtilIdleCheckKey(int sym) {
void jlUtilIdle(void) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
@ -658,6 +651,8 @@ void jlUtilIdle(void) {
_jlIsRunning = jfalse;
break;
}
SDL_Delay(1);
}
//printf("Keys down: %d\n", _jlNumKeysDown);
@ -739,9 +734,7 @@ int main(int argc, char *argv[]) {
jPixBufStop();
SDL_RemoveTimer(_jlTimerId);
for (x=0; x<_jlControllerCount; x++) {
SDL_GameControllerClose(_jlControllers[x]);
}
for (x=0; x<_jlControllerCount; x++) SDL_GameControllerClose(_jlControllers[x]);
_jlControllerCount = 0;
jlFree(_jlControllers);
SDL_PauseAudioDevice(_jlAudioDevice, 1);