shithub: zelda3

Download patch

ref: 5a0503bd2ba3c479489f557889a1b9ca63a1b517
parent: ad9e25c0879389dac842edb5d2e705483880b7dc
author: Nutzzz <[email protected]>
date: Tue Sep 20 00:16:18 EDT 2022

Allow audio to be disabled in .ini (#100)

Allow audio to be disabled in .ini

--- a/config.c
+++ b/config.c
@@ -226,7 +226,10 @@
       return true;
     }
   } else if (section == 2) {
-    if (StringEqualsNoCase(key, "AudioFreq")) {
+    if (StringEqualsNoCase(key, "EnableAudio")) {
+      g_config.enable_audio = (bool)strtol(value, (char**)NULL, 10);
+      return true;
+    } else if (StringEqualsNoCase(key, "AudioFreq")) {
       g_config.audio_freq = (uint16)strtol(value, (char**)NULL, 10);
       return true;
     } else if (StringEqualsNoCase(key, "AudioChannels")) {
--- a/config.h
+++ b/config.h
@@ -38,6 +38,7 @@
   bool ignore_aspect_ratio;
   uint8 fullscreen;
   uint8 window_scale;
+  bool enable_audio;
   uint16 audio_freq;
   uint8 audio_channels;
   uint16 audio_samples;
--- a/main.c
+++ b/main.c
@@ -258,20 +258,24 @@
   }
   SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
 
-  SDL_AudioSpec want = { 0 }, have;
   SDL_AudioDeviceID device;
-  want.freq = g_config.audio_freq;
-  want.format = AUDIO_S16;
-  want.channels = g_config.audio_channels;
-  want.samples = g_config.audio_samples;
-  device = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
-  if(device == 0) {
-    printf("Failed to open audio device: %s\n", SDL_GetError());
-    return 1;
+  SDL_AudioSpec want = { 0 }, have;
+  int16_t* audioBuffer = NULL;
+
+  if (g_config.enable_audio) {
+    want.freq = g_config.audio_freq;
+    want.format = AUDIO_S16;
+    want.channels = g_config.audio_channels;
+    want.samples = g_config.audio_samples;
+    device = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
+    if (device == 0) {
+      printf("Failed to open audio device: %s\n", SDL_GetError());
+      return 1;
+    }
+    g_samples_per_block = (534 * have.freq) / 32000;
+    audioBuffer = (int16_t*)malloc(g_samples_per_block * have.channels * sizeof(int16));
+    SDL_PauseAudioDevice(device, 0);
   }
-  g_samples_per_block = (534 * have.freq) / 32000;
-  int16_t *audioBuffer = (int16_t * )malloc(g_samples_per_block * have.channels * sizeof(int16));
-  SDL_PauseAudioDevice(device, 0);
 
   Snes *snes = snes_init(g_emulated_ram), *snes_run = NULL;
   if (argc >= 2 && !g_run_without_emu) {
@@ -365,7 +369,8 @@
 
 
     uint64 t1 = SDL_GetPerformanceCounter();
-    PlayAudio(snes_run, device, have.channels, audioBuffer);
+    if (audioBuffer)
+      PlayAudio(snes_run, device, have.channels, audioBuffer);
     uint64 t2 = SDL_GetPerformanceCounter();
 
     RenderScreen(window, renderer, texture, (g_win_flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0);
@@ -406,8 +411,10 @@
   // clean snes
   snes_free(snes);
   // clean sdl
-  SDL_PauseAudioDevice(device, 1);
-  SDL_CloseAudioDevice(device);
+  if (g_config.enable_audio) {
+    SDL_PauseAudioDevice(device, 1);
+    SDL_CloseAudioDevice(device);
+  }
   free(audioBuffer);
   SDL_DestroyTexture(texture);
   SDL_DestroyRenderer(renderer);
--- a/zelda3.ini
+++ b/zelda3.ini
@@ -21,6 +21,8 @@
 
 
 [Sound]
+EnableAudio = 1
+
 # DSP frequency in samples per second (e.g. 48000, 44100, 32000, 22050, 11025)
 AudioFreq = 44100
 # number of separate sound channels (1=mono, 2=stereo)