From c7b35b705dbfdabcebf8826f5809eddc7319bf65 Mon Sep 17 00:00:00 2001 From: Sheldon Lee Date: Sun, 20 Oct 2024 22:13:11 +0800 Subject: [PATCH] Refactor and move more audio specific code to audio.c --- audio.c | 14 ++++++++++++++ audio.h | 4 ++-- main.c | 10 +++++----- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/audio.c b/audio.c index 7aa06b9..afd43c7 100644 --- a/audio.c +++ b/audio.c @@ -8,6 +8,9 @@ static SDL_AudioSpec playback_spec; static SDL_AudioSpec received_recording_spec; static SDL_AudioSpec received_playback_spec; +void audio_recording_callback(void* userdata, uint8_t* stream, int len); +void audio_playback_callback(void* userdata, uint8_t* stream, int len); + int audio_init(audio_state* state) { state->recording_device_id = 0; @@ -81,6 +84,17 @@ int audio_playback_init(audio_state* state) return 1; } +void audio_playback_pause(audio_state* state, int is_recording, int is_pause) +{ + int device_id = is_recording ? state->recording_device_id : state->playback_device_id; + SDL_PauseAudioDevice(device_id, is_pause); +} + +void audio_buffer_reset(audio_state* state) +{ + memset(state->recording_buffer, 0, state->recording_buffer_size); +} + void audio_recording_callback(void* userdata, uint8_t* stream, int len) { audio_state* state = userdata; diff --git a/audio.h b/audio.h index 57f96f6..6cb7fdd 100644 --- a/audio.h +++ b/audio.h @@ -16,7 +16,7 @@ int audio_init(audio_state* state); void audio_destroy(audio_state* state); int audio_recording_init(audio_state* state, int index); int audio_playback_init(audio_state* state); -void audio_recording_callback(void* userdata, uint8_t* stream, int len); -void audio_playback_callback(void* userdata, uint8_t* stream, int len); +void audio_playback_pause(audio_state* state, int is_recording, int pause); +void audio_buffer_reset(audio_state* state); #endif diff --git a/main.c b/main.c index 364b6ad..73f90c8 100644 --- a/main.c +++ b/main.c @@ -141,9 +141,9 @@ int handle_input(SDL_Event* event, state* state) case SDLK_y: case SDLK_RETURN: if (state->device_index == -1 || !state->audio.recording_device_id) break; - memset(state->audio.recording_buffer, 0, state->audio.recording_buffer_size); + audio_buffer_reset(&state->audio); state->audio.recording_buffer_position = 0; - SDL_PauseAudioDevice(state->audio.recording_device_id, 0); + audio_playback_pause(&state->audio, 1, 0); state->application_state = RECORDING; state->update_ui = 1; break; @@ -166,7 +166,7 @@ int handle_input(SDL_Event* event, state* state) switch (keysym) { case SDLK_q: case SDLK_ESCAPE: - SDL_PauseAudioDevice(state->audio.recording_device_id, 1); + audio_playback_pause(&state->audio, 1, 1); state->application_state = SELECTING_DEVICE; state->update_ui = 1; break; @@ -180,7 +180,7 @@ int handle_input(SDL_Event* event, state* state) if (!state->audio.playback_device_id) audio_playback_init(&state->audio); state->audio.recording_buffer_position = 0; - SDL_PauseAudioDevice(state->audio.playback_device_id, 0); + audio_playback_pause(&state->audio, 0, 0); state->application_state = PLAYBACK; state->update_ui = 1; break; @@ -196,7 +196,7 @@ int handle_input(SDL_Event* event, state* state) switch (keysym) { case SDLK_q: case SDLK_ESCAPE: - SDL_PauseAudioDevice(state->audio.playback_device_id, 1); + audio_playback_pause(&state->audio, 0, 1); state->application_state = RECORDED; state->update_ui = 1; break;