Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add possibility to toggle audio with a key #156

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion src/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 8 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand 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) {
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 11 additions & 3 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -382,20 +385,24 @@ 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();
break;
}

// 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);
}

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/usb_audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading