From 522c173084a23013912cf518c9fdafa18748632e Mon Sep 17 00:00:00 2001 From: laamaa Date: Fri, 28 Jun 2024 15:45:41 +0300 Subject: [PATCH] add possibility to toggle audio with a key --- src/audio.c | 24 +++++++++++++++++++++++- src/audio.h | 3 ++- src/config.c | 9 ++++++++- src/config.h | 1 + src/input.c | 14 +++++++++++--- src/input.h | 6 +++++- src/main.c | 3 +++ src/usb_audio.c | 4 ++++ 8 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/audio.c b/src/audio.c index ce7d9f6..bda6bb1 100644 --- a/src/audio.c +++ b/src/audio.c @@ -8,11 +8,25 @@ static SDL_AudioDeviceID devid_in = 0; static SDL_AudioDeviceID devid_out = 0; +static unsigned int audio_paused = 0; +static unsigned int audio_initialized = 0; + +void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name) { + if (!audio_initialized) { + audio_init(audio_buffer_size, output_device_name); + return; + } + audio_paused = !audio_paused; + SDL_PauseAudioDevice(devid_in, audio_paused); + SDL_PauseAudioDevice(devid_out, audio_paused); + SDL_Log(audio_paused ? "Audio paused" : "Audio resumed"); +} + void audio_cb_in(void *userdata, uint8_t *stream, int len) { SDL_QueueAudio(devid_out, stream, len); } -int audio_init(int audio_buffer_size, const char *output_device_name) { +int audio_init(unsigned int audio_buffer_size, const char *output_device_name) { int i = 0; int m8_device_id = -1; @@ -75,14 +89,22 @@ int audio_init(int audio_buffer_size, const char *output_device_name) { SDL_PauseAudioDevice(devid_in, 0); SDL_PauseAudioDevice(devid_out, 0); + audio_paused = 0; + audio_initialized = 1; + return 1; } void audio_destroy() { + if (!audio_initialized) + return; SDL_Log("Closing audio devices"); SDL_PauseAudioDevice(devid_in, 1); SDL_PauseAudioDevice(devid_out, 1); SDL_CloseAudioDevice(devid_in); SDL_CloseAudioDevice(devid_out); + + audio_initialized = 0; } + #endif diff --git a/src/audio.h b/src/audio.h index 4fd597b..72c011d 100644 --- a/src/audio.h +++ b/src/audio.h @@ -3,7 +3,8 @@ #ifndef AUDIO_H #define AUDIO_H -int audio_init(int audio_buffer_size, const char *output_device_name); +int audio_init(unsigned int audio_buffer_size, const char *output_device_name); +void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name); void audio_destroy(); #endif diff --git a/src/config.c b/src/config.c index 0a85a96..c816c62 100644 --- a/src/config.c +++ b/src/config.c @@ -51,6 +51,7 @@ config_params_s init_config() { c.key_jazz_dec_octave = SDL_SCANCODE_KP_DIVIDE; c.key_jazz_inc_velocity = SDL_SCANCODE_KP_MINUS; c.key_jazz_dec_velocity = SDL_SCANCODE_KP_PLUS; + c.key_toggle_audio = SDL_SCANCODE_F12; c.gamepad_up = SDL_CONTROLLER_BUTTON_DPAD_UP; c.gamepad_left = SDL_CONTROLLER_BUTTON_DPAD_LEFT; @@ -85,7 +86,7 @@ void write_config(config_params_s *conf) { SDL_Log("Writing config file to %s", config_path); - const unsigned int INI_LINE_COUNT = 48; + const unsigned int INI_LINE_COUNT = 50; const unsigned int LINELEN = 50; // Entries for the config file @@ -107,6 +108,7 @@ void write_config(config_params_s *conf) { snprintf(ini_values[initPointer++], LINELEN, "audio_device_name=%s\n", conf->audio_device_name ? conf->audio_device_name : "Default"); snprintf(ini_values[initPointer++], LINELEN, "[keyboard]\n"); + snprintf(ini_values[initPointer++], LINELEN, ";Ref: https://wiki.libsdl.org/SDL2/SDL_Scancode\n"); snprintf(ini_values[initPointer++], LINELEN, "key_up=%d\n", conf->key_up); snprintf(ini_values[initPointer++], LINELEN, "key_left=%d\n", conf->key_left); snprintf(ini_values[initPointer++], LINELEN, "key_down=%d\n", conf->key_down); @@ -129,6 +131,8 @@ void write_config(config_params_s *conf) { conf->key_jazz_inc_velocity); snprintf(ini_values[initPointer++], LINELEN, "key_jazz_dec_velocity=%d\n", conf->key_jazz_dec_velocity); + snprintf(ini_values[initPointer++], LINELEN, "key_toggle_audio=%d\n", + conf->key_toggle_audio); snprintf(ini_values[initPointer++], LINELEN, "[gamepad]\n"); snprintf(ini_values[initPointer++], LINELEN, "gamepad_up=%d\n", conf->gamepad_up); snprintf(ini_values[initPointer++], LINELEN, "gamepad_left=%d\n", conf->gamepad_left); @@ -277,6 +281,7 @@ void read_key_config(ini_t *ini, config_params_s *conf) { const char *key_jazz_dec_octave = ini_get(ini, "keyboard", "key_jazz_dec_octave"); const char *key_jazz_inc_velocity = ini_get(ini, "keyboard", "key_jazz_inc_velocity"); const char *key_jazz_dec_velocity = ini_get(ini, "keyboard", "key_jazz_dec_velocity"); + const char *key_toggle_audio = ini_get(ini, "keyboard", "key_toggle_audio"); if (key_up) conf->key_up = SDL_atoi(key_up); @@ -314,6 +319,8 @@ void read_key_config(ini_t *ini, config_params_s *conf) { conf->key_jazz_inc_velocity = SDL_atoi(key_jazz_inc_velocity); if (key_jazz_dec_velocity) conf->key_jazz_dec_velocity = SDL_atoi(key_jazz_dec_velocity); + if (key_toggle_audio) + conf->key_jazz_dec_velocity = SDL_atoi(key_toggle_audio); } void read_gamepad_config(ini_t *ini, config_params_s *conf) { diff --git a/src/config.h b/src/config.h index 1b9ecdd..8c0ca39 100644 --- a/src/config.h +++ b/src/config.h @@ -35,6 +35,7 @@ typedef struct config_params_s { int key_jazz_dec_octave; int key_jazz_inc_velocity; int key_jazz_dec_velocity; + int key_toggle_audio; int gamepad_up; int gamepad_left; diff --git a/src/input.c b/src/input.c index 2f1e72b..7f13389 100644 --- a/src/input.c +++ b/src/input.c @@ -33,7 +33,7 @@ uint8_t keyjazz_velocity = 0x64; static uint8_t keycode = 0; // value of the pressed key static int num_joysticks = 0; -input_msg_s key = {normal, 0}; +static input_msg_s key = {normal, 0}; uint8_t toggle_input_keyjazz() { keyjazz_enabled = !keyjazz_enabled; @@ -252,6 +252,9 @@ static input_msg_s handle_normal_keys(SDL_Event *event, config_params_s *conf, u key.value = key_opt | key_edit; } else if (event->key.keysym.scancode == conf->key_reset) { key = (input_msg_s){special, msg_reset_display}; + } else if (event->key.keysym.scancode == conf->key_toggle_audio) { + key = (input_msg_s){special, msg_toggle_audio}; + } else { key.value = 0; } @@ -382,6 +385,10 @@ void handle_sdl_events(config_params_s *conf) { // Keyboard events. Special events are handled within SDL_KEYDOWN. case SDL_KEYDOWN: + if (event.key.repeat > 0) { + break; + } + // ALT+ENTER toggles fullscreen if (event.key.keysym.sym == SDLK_RETURN && (event.key.keysym.mod & KMOD_ALT) > 0) { toggle_fullscreen(); @@ -389,13 +396,13 @@ void handle_sdl_events(config_params_s *conf) { } // ALT+F4 quits program - if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) > 0) { + else if (event.key.keysym.sym == SDLK_F4 && (event.key.keysym.mod & KMOD_ALT) > 0) { key = (input_msg_s){special, msg_quit}; break; } // ESC = toggle keyjazz - if (event.key.keysym.sym == SDLK_ESCAPE) { + else if (event.key.keysym.sym == SDLK_ESCAPE) { display_keyjazz_overlay(toggle_input_keyjazz(), keyjazz_base_octave, keyjazz_velocity); } @@ -425,6 +432,7 @@ void handle_sdl_events(config_params_s *conf) { if (event.type == SDL_KEYDOWN) { keycode = key.value; } else { + key.value = 0; keycode = 0; } break; diff --git a/src/input.h b/src/input.h index bb2b288..72de886 100644 --- a/src/input.h +++ b/src/input.h @@ -21,7 +21,11 @@ typedef enum input_buttons_t { typedef enum input_type_t { normal, keyjazz, special } input_type_t; -typedef enum special_messages_t { msg_quit = 1, msg_reset_display = 2 } special_messages_t; +typedef enum special_messages_t { + msg_quit = 1, + msg_reset_display = 2, + msg_toggle_audio = 3 +} special_messages_t; typedef struct input_msg_s { input_type_t type; diff --git a/src/main.c b/src/main.c index 4168d79..9dfe12b 100644 --- a/src/main.c +++ b/src/main.c @@ -216,6 +216,9 @@ int main(int argc, char *argv[]) { case msg_reset_display: reset_display(); break; + case msg_toggle_audio: + toggle_audio(conf.audio_buffer_size, conf.audio_device_name); + break; default: break; } diff --git a/src/usb_audio.c b/src/usb_audio.c index c2b7170..fcc1dc2 100644 --- a/src/usb_audio.c +++ b/src/usb_audio.c @@ -193,4 +193,8 @@ int audio_destroy() { return 1; } +void toggle_audio(unsigned int audio_buffer_size, const char *output_device_name) { + SDL_Log("Libusb audio toggling not implemented yet"); +} + #endif