From bef7ae60a51a6b3841032a4cd74d603c0c6dff43 Mon Sep 17 00:00:00 2001 From: Vladimir Serbinenko Date: Fri, 1 Nov 2019 17:06:08 +0100 Subject: [PATCH] Encapsulate SDL-sound code into separate file --- Makefile | 4 +- sdlsound.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ sound.c | 117 ++++++++--------------------------------------------- 3 files changed, 127 insertions(+), 103 deletions(-) create mode 100644 sdlsound.c diff --git a/Makefile b/Makefile index c498c71..0f501c5 100644 --- a/Makefile +++ b/Makefile @@ -49,11 +49,11 @@ UTILS = maketape readtape SRCS = access.c boot.c branch.c conf.c covox.c double.c ea.c itab.c \ main.c service.c ui.c scr-sdl.c scr.c timer.c tape.c disk.c mouse.c printer.c \ single.c weird.c tty.c io.c timing.c sound.c disas.c serial.c bkplip.c \ - terakdisk.c synth.c emu2149.c sdltty.c + terakdisk.c synth.c emu2149.c sdlsound.c sdltty.c OBJS = access.o boot.o branch.o conf.o covox.o double.o ea.o itab.o icon.o \ main.o service.o ui.o scr-sdl.o scr.o timer.o tape.o disk.o mouse.o printer.o \ single.o weird.o tty.o io.o timing.o sound.o disas.o serial.o bkplip.o \ - terakdisk.o synth.o emu2149.o sdltty.o + terakdisk.o synth.o emu2149.o sdlsound.o sdltty.o INCS = defines.h scr.h conf.h emu2149.h emutypes.h USRCS = readtape.c maketape.c pngtorgba.c TEXTS = README.html configure.in icon.c diff --git a/sdlsound.c b/sdlsound.c new file mode 100644 index 0000000..0f8b683 --- /dev/null +++ b/sdlsound.c @@ -0,0 +1,109 @@ +#include "defines.h" +#include "conf.h" +#include +#include +#include +#include +#define _(String) gettext (String) + +//#define SOUND_EXPONENT (8+io_sound_freq/20000) +//#define SOUND_BUFSIZE (1< -#include -#include -#include -#include -#define _(String) gettext (String) +#include "intl.h" -#define SOUND_EXPONENT (8+io_sound_freq/20000) -//#define SOUND_BUFSIZE (1<= io_max_sound_age && covox_age >= io_max_sound_age) { - if (sound_buf[cur_buf].ptr != 0) - { - SDL_SemWait(sem); - sound_buf[cur_buf].ptr = 0; - cur_buf = (cur_buf + 1) % NUMBUF; - } + platform_sound_flush(); return; } while (ticks >= io_sound_count) { - short * p = &sound_buf[cur_buf].buf[sound_buf[cur_buf].ptr++]; if (io_sound_age < 1000) - *p = io_sound_val + covox_val << 4; + sound_write_sample (io_sound_val + covox_val << 4); else - *p = (covox_val << 4) + synth_next(); - io_sound_count += io_sound_pace; + sound_write_sample ((covox_val << 4) + synth_next()); io_sound_age++; - covox_age++; - if (io_sound_bufsize == sound_buf[cur_buf].ptr || - io_sound_age == io_max_sound_age) { - SDL_SemWait(sem); - sound_buf[cur_buf].ptr = 0; - cur_buf = (cur_buf + 1) % NUMBUF; - + if (io_sound_age == io_max_sound_age) { + platform_sound_flush(); } + covox_age++; + io_sound_count += io_sound_pace; } } -void sound_finish() { - /* release the write thread so it can terminate */ - SDL_PauseAudio(1); - SDL_DestroySemaphore(sem); -} - -SDL_AudioSpec desired; - void sound_init() { - static init_done = 0; - int iarg, i; - io_max_sound_age = MAX_SOUND_AGE; - io_sound_age = MAX_SOUND_AGE; - + static int init_done = 0; if (!nflag) return; - if (fullspeed) { - io_max_sound_age = 2 * SOUND_BUFSIZE; - /* otherwise UINT_MAX */ - } if (init_done) { - sound_buf[cur_buf].ptr = 0; io_sound_age = io_max_sound_age; - return; - } - fprintf(stderr, _("sound_init called\n")); - - if (-1 == SDL_InitSubSystem(SDL_INIT_AUDIO)) { - fprintf(stderr, _("Failed to initialize audio subsystem\n")); - } - - desired.format = 16; - desired.channels = 1; - desired.freq = io_sound_freq; - desired.samples = io_sound_bufsize = SOUND_BUFSIZE; - desired.callback = callback; - if (-1 == SDL_OpenAudio(&desired, 0)) { - fprintf(stderr, _("Failed to initialize sound, freq %d, %d samples\n"), io_sound_freq, SOUND_BUFSIZE); - nflag = 0; + sound_discard(); return; } - io_sound_pace = TICK_RATE/io_sound_freq; - sem = SDL_CreateSemaphore(NUMBUF); + platform_sound_init(); - for (i = 0; i < NUMBUF; i++) { - sound_buf[i].ptr = 0; - sound_buf[i].buf = malloc(io_sound_bufsize * sizeof(short)); - } - if (!sound_buf[NUMBUF-1].buf) { - fprintf(stderr, _("Failed to allocate sound buffers\n")); - exit(1); - } - atexit(sound_finish); - SDL_PauseAudio(0); + io_sound_pace = TICK_RATE/(io_sound_freq + 0.0); + io_max_sound_age = MAX_SOUND_AGE; + io_sound_age = MAX_SOUND_AGE; /* in io_sound_pace's since last change */ init_done = 1; }