From 600d1ed6a54ee396de2969c536d2ab12845df331 Mon Sep 17 00:00:00 2001 From: gameblabla Date: Tue, 13 Oct 2020 15:17:29 +0200 Subject: [PATCH] Fix sound issues for good with triple/double buffering. The source code needed some changes before this happened --- Makefile | 6 +++--- Makefile.rs90 | 15 ++++++++------- source/ports/sdl/smsplus.c | 16 +++++++++------- source/sound_output/oss/sound_output_oss.c | 3 ++- source/sound_output/portaudio/sound_portaudio.c | 3 ++- source/sound_output/pulse/sound_output_pulse.c | 13 ++++++++++--- source/sound_output/sdl12/sound_output_sdl.c | 2 +- 7 files changed, 35 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index dbce6b6..4daa906 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CC = gcc # Possible choices : rs97, k3s (PAP K3S), sdl, amini, fbdev PORT = sdl # Possible choices : alsa, pulse (pulseaudio), oss, sdl12 (SDL 1.2 sound output), portaudio, libao -SOUND_OUTPUT = alsa +SOUND_OUTPUT = pulse # Possible choices : crabemu_sn76489 (less accurate, GPLv2), maxim_sn76489 (somewhat problematic license but good accuracy) SOUND_ENGINE = maxim_sn76489 # Possible choices : z80 (accurate but proprietary), eighty (EightyZ80's core, GPLv2) @@ -22,8 +22,8 @@ OBJ_C = $(notdir $(patsubst %.c, %.o, $(SRC_C))) OBJ_CP = $(notdir $(patsubst %.cpp, %.o, $(SRC_CP))) OBJS = $(OBJ_C) $(OBJ_CP) -CFLAGS = -O0 -g -Wextra -Wall -CFLAGS += -DLSB_FIRST -std=gnu99 -DALIGN_DWORD +CFLAGS = -O0 -g3 -Wall -Wextra +CFLAGS += -DLSB_FIRST -std=gnu99 -DALIGN_DWORD -DNOYUV CFLAGS += -Isource -Isource/cpu_cores/$(Z80_CORE) -Isource/scalers -Isource/ports/$(PORT) -I./source/sound -Isource/unzip -Isource/sdl -Isource/sound/$(SOUND_ENGINE) -Isource/sound_output SRCDIR += ./source/text/fb diff --git a/Makefile.rs90 b/Makefile.rs90 index 5031285..fb56178 100644 --- a/Makefile.rs90 +++ b/Makefile.rs90 @@ -4,7 +4,7 @@ CC = /opt/rs90-toolchain/bin/mipsel-linux-gcc # Possible choices : rs97, k3s (PAP K3S), sdl, amini, fbdev PORT = rs90 # Possible choices : alsa, pulse (pulseaudio), oss, sdl12 (SDL 1.2 sound output), portaudio, libao -SOUND_OUTPUT = alsa +SOUND_OUTPUT = sdl12 # Possible choices : crabemu_sn76489 (less accurate, GPLv2), maxim_sn76489 (somewhat problematic license but good accuracy) SOUND_ENGINE = maxim_sn76489 # Possible choices : z80 (accurate but proprietary), eighty (EightyZ80's core, GPLv2) @@ -13,7 +13,7 @@ PROFILE = 0 ZIP_SUPPORT = 1 SRCDIR = ./source ./source/cpu_cores/$(Z80_CORE) ./source/sound ./source/unzip -SRCDIR += ./source/scalers ./source/ports/$(PORT) ./source/sound/$(SOUND_ENGINE) ./source/sound_output/$(SOUND_OUTPUT) +SRCDIR += ./source/ports/$(PORT) ./source/sound/$(SOUND_ENGINE) ./source/sound_output/$(SOUND_OUTPUT) VPATH = $(SRCDIR) SRC_C = $(foreach dir, $(SRCDIR), $(wildcard $(dir)/*.c)) SRC_CP = $(foreach dir, $(SRCDIR), $(wildcard $(dir)/*.cpp)) @@ -21,9 +21,10 @@ OBJ_C = $(notdir $(patsubst %.c, %.o, $(SRC_C))) OBJ_CP = $(notdir $(patsubst %.cpp, %.o, $(SRC_CP))) OBJS = $(OBJ_C) $(OBJ_CP) -CFLAGS = -Ofast -fdata-sections -ffunction-sections -mplt -mframe-header-opt -fno-common -march=mips32 -mtune=mips32 -flto -CFLAGS += -falign-functions=1 -falign-jumps=1 -falign-loops=1 -falign-labels=1 -fsingle-precision-constant -CFLAGS += -DALIGN_DWORD -D_8BPP_COLOR -DNOBLANKING_LEFTCOLUM +CFLAGS = -Ofast -fdata-sections -ffunction-sections -mplt -mno-shared -mframe-header-opt -fno-common -march=mips32 -mtune=mips32 -flto=auto +CFLAGS += -falign-functions=4 -falign-jumps=4 -falign-loops=4 -falign-labels=4 -fsingle-precision-constant +CFLAGS += -mno-fp-exceptions -mno-check-zero-division -fno-common -fno-short-enums -ffinite-math-only -funsafe-math-optimizations -fsection-anchors -fgcse-las -fgcse-sm +CFLAGS += -DALIGN_DWORD -D_8BPP_COLOR -DNOBLANKING_LEFTCOLUM -DNONBLOCKING_AUDIO CFLAGS += -DLSB_FIRST CFLAGS += -Isource -Isource/cpu_cores/$(Z80_CORE) -Isource/scalers -Isource/ports/$(PORT) -I./source/sound -Isource/unzip -Isource/sdl -Isource/sound/$(SOUND_ENGINE) -Isource/sound_output @@ -54,7 +55,7 @@ CFLAGS += -Isource/scale2x SRCDIR += ./source/scale2x endif -LDFLAGS += -lc -lgcc -lSDL -Wl,--as-needed -Wl,--gc-sections -flto -s +LDFLAGS += -lc -lgcc -lSDL -Wl,--as-needed -Wl,--gc-sections -flto=auto -s ifeq ($(SOUND_OUTPUT), portaudio) LDFLAGS += -lportaudio @@ -71,7 +72,7 @@ endif # Rules to make executable $(PRGNAME): $(OBJS) - $(CC) -o $(PRGNAME) $^ $(LDFLAGS) + $(CC) $(CFLAGS) -o $(PRGNAME) $^ $(LDFLAGS) $(OBJ_C) : %.o : %.c $(CC) $(CFLAGS) -std=gnu99 -c -o $@ $< diff --git a/source/ports/sdl/smsplus.c b/source/ports/sdl/smsplus.c index 0ed2379..36a95fc 100644 --- a/source/ports/sdl/smsplus.c +++ b/source/ports/sdl/smsplus.c @@ -38,10 +38,9 @@ static const uint32_t upscalers_available = 1 +1 #endif ; -uint16_t real_FPS; +double real_FPS; Uint32 start; - static void video_update(void) { uint32_t dst_x, dst_y, dst_w, dst_h, hide_left; @@ -107,7 +106,9 @@ static void video_update(void) } SDL_Flip(sdl_screen); - if(real_FPS > SDL_GetTicks()-start) SDL_Delay(real_FPS-(SDL_GetTicks()-start)); +#ifdef NONBLOCKING_AUDIO + if(real_FPS > SDL_GetTicks()-start) usleep((real_FPS-(SDL_GetTicks()-start))*1000); +#endif } void smsp_state(uint8_t slot_number, uint8_t mode) @@ -684,7 +685,7 @@ int main (int argc, char *argv[]) SDL_Init(SDL_INIT_VIDEO); - sdl_screen = SDL_SetVideoMode(HOST_WIDTH_RESOLUTION, HOST_HEIGHT_RESOLUTION, 16, SDL_HWSURFACE); + sdl_screen = SDL_SetVideoMode(HOST_WIDTH_RESOLUTION, HOST_HEIGHT_RESOLUTION, 16, SDL_HWSURFACE | SDL_DOUBLEBUF); if (!sdl_screen) { fprintf(stdout, "Could not create display, exiting\n"); @@ -717,8 +718,8 @@ int main (int argc, char *argv[]) sms.use_fm = 1; } - if (sms.display == DISPLAY_PAL) real_FPS = 1000 / 50; - else real_FPS = 1000 / 60; + if (sms.display == DISPLAY_PAL) real_FPS = 1000 / 49.701459; + else real_FPS = 1000 / 59.922743; printf("sms.display %d, PAL is %d\n", sms.display, DISPLAY_PAL); @@ -732,8 +733,9 @@ int main (int argc, char *argv[]) // Loop until the user closes the window while (!quit) { +#ifdef NONBLOCKING_AUDIO start = SDL_GetTicks(); - +#endif if (selectpressed == 1) { Menu(); diff --git a/source/sound_output/oss/sound_output_oss.c b/source/sound_output/oss/sound_output_oss.c index aea6351..1e253d0 100644 --- a/source/sound_output/oss/sound_output_oss.c +++ b/source/sound_output/oss/sound_output_oss.c @@ -21,6 +21,7 @@ void Sound_Init() uint32_t channels = 2; uint32_t format = AFMT_S16_LE; int32_t err_ret; + int32_t tmp; tmp = option.sndrate; @@ -60,7 +61,7 @@ void Sound_Init() void Sound_Update(int16_t* sound_buffer, unsigned long len) { if (!oss_audio_fd) return; - write(oss_audio_fd, sound_buffer, len); + write(oss_audio_fd, sound_buffer, len * 4); } void Sound_Close() diff --git a/source/sound_output/portaudio/sound_portaudio.c b/source/sound_output/portaudio/sound_portaudio.c index 2ae5f12..536b03c 100644 --- a/source/sound_output/portaudio/sound_portaudio.c +++ b/source/sound_output/portaudio/sound_portaudio.c @@ -22,7 +22,8 @@ static int patestCallback( const void *inputBuffer, void *outputBuffer, void *userData ) { /* Cast data passed through stream to our structure. */ - uint16_t *out = (uint16_t*)snd.output; + snd.output = (int16_t*)outputBuffer; + int32_t i; (void) inputBuffer; /* Prevent unused variable warning. */ return 0; diff --git a/source/sound_output/pulse/sound_output_pulse.c b/source/sound_output/pulse/sound_output_pulse.c index bb248e1..c1cf431 100644 --- a/source/sound_output/pulse/sound_output_pulse.c +++ b/source/sound_output/pulse/sound_output_pulse.c @@ -19,12 +19,19 @@ static pa_simple *pulse_stream; void Sound_Init() { pa_sample_spec ss; + pa_buffer_attr paattr; + ss.format = PA_SAMPLE_S16LE; ss.channels = 2; ss.rate = option.sndrate; - + + paattr.tlength = snd.buffer_size * 4; + paattr.prebuf = -1; + paattr.maxlength = -1; + paattr.minreq = snd.buffer_size; + /* Create a new playback stream */ - pulse_stream = pa_simple_new(NULL, "smsplusgx", PA_STREAM_PLAYBACK, NULL, "smsplusgx", &ss, NULL, NULL, NULL); + pulse_stream = pa_simple_new(NULL, "SMS Plus GX", PA_STREAM_PLAYBACK, NULL, "SMS Plus GX", &ss, NULL, &paattr, NULL); if (!pulse_stream) { fprintf(stderr, "PulseAudio: pa_simple_new() failed!\n"); @@ -34,7 +41,7 @@ void Sound_Init() void Sound_Update(int16_t* sound_buffer, unsigned long len) { - if (pa_simple_write(pulse_stream, sound_buffer, len, NULL) < 0) + if (pa_simple_write(pulse_stream, sound_buffer, len * 4, NULL) < 0) { fprintf(stderr, "PulseAudio: pa_simple_write() failed!\n"); } diff --git a/source/sound_output/sdl12/sound_output_sdl.c b/source/sound_output/sdl12/sound_output_sdl.c index 03ddd1b..9ba3b1c 100644 --- a/source/sound_output/sdl12/sound_output_sdl.c +++ b/source/sound_output/sdl12/sound_output_sdl.c @@ -136,7 +136,7 @@ void Sound_Init() void Sound_Update(int16_t* sound_buffer, unsigned long len) { SDL_LockAudio(); - sdl_write_buffer(sound_buffer, len); + sdl_write_buffer(sound_buffer, len * 4); SDL_UnlockAudio(); }