Skip to content

Commit

Permalink
Added option to disable sound (#337)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Steil <[email protected]>
  • Loading branch information
JimmyDansbo and mist64 authored Apr 4, 2021
1 parent 2c3612f commit 706d916
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
66 changes: 41 additions & 25 deletions audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ static int num_bufs = 0;
static void
audio_callback(void *userdata, Uint8 *stream, int len)
{
if (audio_dev == 0) {
return;
}

int expected = 2 * SAMPLES_PER_BUFFER * sizeof(int16_t);
if (len != expected) {
printf("Audio buffer size mismatch! (expected: %d, got: %d)\n", expected, len);
Expand All @@ -56,6 +60,12 @@ audio_init(const char *dev_name, int num_audio_buffers)
audio_close();
}

if (dev_name) {
if (!strcmp("none", dev_name)) {
return;
}
}

// Set number of buffers
num_bufs = num_audio_buffers;
if (num_bufs < 3) {
Expand Down Expand Up @@ -102,6 +112,10 @@ audio_init(const char *dev_name, int num_audio_buffers)
void
audio_close(void)
{
if (audio_dev == 0) {
return;
}

SDL_CloseAudioDevice(audio_dev);
audio_dev = 0;

Expand All @@ -121,6 +135,10 @@ audio_close(void)
void
audio_render(int cpu_clocks)
{
if (audio_dev == 0) {
return;
}

cpu_clks += cpu_clocks;
if (cpu_clks > 8) {
int c = cpu_clks / 8;
Expand All @@ -131,36 +149,34 @@ audio_render(int cpu_clocks)
while (vera_clks >= 512 * SAMPLES_PER_BUFFER) {
vera_clks -= 512 * SAMPLES_PER_BUFFER;

if (audio_dev != 0) {
int16_t psg_buf[2 * SAMPLES_PER_BUFFER];
psg_render(psg_buf, SAMPLES_PER_BUFFER);
int16_t psg_buf[2 * SAMPLES_PER_BUFFER];
psg_render(psg_buf, SAMPLES_PER_BUFFER);

int16_t pcm_buf[2 * SAMPLES_PER_BUFFER];
pcm_render(pcm_buf, SAMPLES_PER_BUFFER);
int16_t pcm_buf[2 * SAMPLES_PER_BUFFER];
pcm_render(pcm_buf, SAMPLES_PER_BUFFER);

int16_t ym_buf[2 * SAMPLES_PER_BUFFER];
YM_stream_update((uint16_t *)ym_buf, SAMPLES_PER_BUFFER);
int16_t ym_buf[2 * SAMPLES_PER_BUFFER];
YM_stream_update((uint16_t *)ym_buf, SAMPLES_PER_BUFFER);

bool buf_available;
SDL_LockAudioDevice(audio_dev);
buf_available = buf_cnt < num_bufs;
SDL_UnlockAudioDevice(audio_dev);
bool buf_available;
SDL_LockAudioDevice(audio_dev);
buf_available = buf_cnt < num_bufs;
SDL_UnlockAudioDevice(audio_dev);

if (buf_available) {
// Mix PSG, PCM and YM output
int16_t *buf = buffers[wridx];
for (int i = 0; i < 2 * SAMPLES_PER_BUFFER; i++) {
buf[i] = ((int)psg_buf[i] + (int)pcm_buf[i] + (int)ym_buf[i]) / 3;
}

SDL_LockAudioDevice(audio_dev);
wridx++;
if (wridx == num_bufs) {
wridx = 0;
}
buf_cnt++;
SDL_UnlockAudioDevice(audio_dev);
if (buf_available) {
// Mix PSG, PCM and YM output
int16_t *buf = buffers[wridx];
for (int i = 0; i < 2 * SAMPLES_PER_BUFFER; i++) {
buf[i] = ((int)psg_buf[i] + (int)pcm_buf[i] + (int)ym_buf[i]) / 3;
}

SDL_LockAudioDevice(audio_dev);
wridx++;
if (wridx == num_bufs) {
wridx = 0;
}
buf_cnt++;
SDL_UnlockAudioDevice(audio_dev);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ usage()
printf("\tChoose what type of joystick to use, e.g. -joy2 SNES\n");
printf("-sound <output device>\n");
printf("\tSet the output device used for audio emulation\n");
printf("\tIf output device is 'none', no audio is generated\n");
printf("-abufs <number of audio buffers>\n");
printf("\tSet the number of audio buffers used for playback. (default: 8)\n");
printf("\tIncreasing this will reduce stutter on slower computers,\n");
Expand Down

0 comments on commit 706d916

Please sign in to comment.