diff --git a/audio.c b/audio.c index 1ef0594..7aa06b9 100644 --- a/audio.c +++ b/audio.c @@ -83,13 +83,11 @@ int audio_playback_init(audio_state* state) void audio_recording_callback(void* userdata, uint8_t* stream, int len) { - char string[64]; audio_state* state = userdata; int space_left = state->recording_buffer_size - state->recording_buffer_position; if (space_left <= 0) { - sprintf(string, "Stopping recording ID: %i", state->recording_device_id); - log_message(LOG_INFO, string); + log_message(LOG_INFO, "Stopping recording ID: %i", state->recording_device_id); SDL_PauseAudioDevice(state->recording_device_id, 1); return; } @@ -101,19 +99,16 @@ void audio_recording_callback(void* userdata, uint8_t* stream, int len) memcpy(&state->recording_buffer[state->recording_buffer_position], stream, len); state->recording_buffer_position += len; - sprintf(string, "Record pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len); - log_message(LOG_INFO, string); + log_message(LOG_INFO, "Record pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len); } void audio_playback_callback(void* userdata, uint8_t* stream, int len) { - char string[64]; audio_state* state = userdata; int space_left = state->recording_buffer_size - state->recording_buffer_position; if (space_left <= 0) { - sprintf(string, "Stopping playback ID: %i", state->recording_device_id); - log_message(LOG_INFO, string); + log_message(LOG_INFO, "Stopping playback ID: %i", state->recording_device_id); SDL_PauseAudioDevice(state->playback_device_id, 1); return; } @@ -125,6 +120,5 @@ void audio_playback_callback(void* userdata, uint8_t* stream, int len) memcpy(stream, &state->recording_buffer[state->recording_buffer_position], len); state->recording_buffer_position += len; - sprintf(string, "Playback pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len); - log_message(LOG_INFO, string); + log_message(LOG_INFO, "Playback pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len); } diff --git a/main.c b/main.c index 0deca74..364b6ad 100644 --- a/main.c +++ b/main.c @@ -16,10 +16,11 @@ typedef enum { PLAYBACK, } application_state; -typedef struct state { +typedef struct { application_state application_state; audio_state audio; int device_index; + int update_ui; } state; int init(); @@ -39,7 +40,6 @@ int main(int argc, char* argv[]) if (!init()) return 1; num_devices = SDL_GetNumAudioDevices(1); - printf("Num audio devices: %i\n", num_devices); log_message(LOG_INFO, "Num audio devices: %i", num_devices); char char_buffer[64]; char default_fmt[] = "Device selected: %i"; @@ -48,6 +48,7 @@ int main(int argc, char* argv[]) state.application_state = SELECTING_DEVICE; if (!audio_init(&state.audio)) return 1; state.device_index = -1; + state.update_ui = 0; text_texture display_text_texture; text_texture_init(&display_text_texture, renderer); @@ -85,10 +86,14 @@ int main(int argc, char* argv[]) handle_task(&state); - sprintf(char_buffer, default_fmt, state.device_index); - text_texture_load(&display_text_texture, char_buffer); - application_state_to_string(state.application_state, char_buffer); - text_texture_load(&state_text_texture, char_buffer); + if (state.update_ui) { + sprintf(char_buffer, default_fmt, state.device_index); + text_texture_load(&display_text_texture, char_buffer); + application_state_to_string(state.application_state, char_buffer); + text_texture_load(&state_text_texture, char_buffer); + log_message(LOG_INFO, "Update UI State: %s", char_buffer); + state.update_ui = 0; + } // Clear the screen SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); @@ -136,12 +141,11 @@ 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; - SDL_LockAudioDevice(state->audio.recording_device_id); memset(state->audio.recording_buffer, 0, state->audio.recording_buffer_size); state->audio.recording_buffer_position = 0; - SDL_UnlockAudioDevice(state->audio.recording_device_id); SDL_PauseAudioDevice(state->audio.recording_device_id, 0); state->application_state = RECORDING; + state->update_ui = 1; break; case SDLK_q: case SDLK_ESCAPE: @@ -154,6 +158,7 @@ int handle_input(SDL_Event* event, state* state) state->device_index = device_index_new; SDL_CloseAudioDevice(state->audio.recording_device_id); audio_recording_init(&state->audio, state->device_index); + state->update_ui = 1; break; @@ -163,6 +168,7 @@ int handle_input(SDL_Event* event, state* state) case SDLK_ESCAPE: SDL_PauseAudioDevice(state->audio.recording_device_id, 1); state->application_state = SELECTING_DEVICE; + state->update_ui = 1; break; } break; @@ -173,15 +179,15 @@ int handle_input(SDL_Event* event, state* state) case SDLK_RETURN: if (!state->audio.playback_device_id) audio_playback_init(&state->audio); - SDL_LockAudioDevice(state->audio.playback_device_id); state->audio.recording_buffer_position = 0; - SDL_UnlockAudioDevice(state->audio.playback_device_id); SDL_PauseAudioDevice(state->audio.playback_device_id, 0); state->application_state = PLAYBACK; + state->update_ui = 1; break; case SDLK_q: case SDLK_ESCAPE: state->application_state = SELECTING_DEVICE; + state->update_ui = 1; break; } break; @@ -192,6 +198,7 @@ int handle_input(SDL_Event* event, state* state) case SDLK_ESCAPE: SDL_PauseAudioDevice(state->audio.playback_device_id, 1); state->application_state = RECORDED; + state->update_ui = 1; break; } break; @@ -208,16 +215,20 @@ void handle_task(state* state) break; case RECORDING: - if (state->audio.recording_buffer_position >= state->audio.recording_buffer_size) + if (state->audio.recording_buffer_position >= state->audio.recording_buffer_size) { state->application_state = RECORDED; + state->update_ui = 1; + } break; case RECORDED: break; case PLAYBACK: - if (state->audio.recording_buffer_position >= state->audio.recording_buffer_size) + if (state->audio.recording_buffer_position >= state->audio.recording_buffer_size) { state->application_state = RECORDED; + state->update_ui = 1; + } break; }