Refactor and move more audio specific code to audio.c

This commit is contained in:
Sheldon Lee 2024-10-20 22:13:11 +08:00
parent 9a5486c03c
commit c7b35b705d
3 changed files with 21 additions and 7 deletions

14
audio.c
View File

@ -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;

View File

@ -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

10
main.c
View File

@ -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;