Update log statements

This commit is contained in:
Sheldon Lee 2024-10-17 03:16:41 +08:00
parent f9cf93eaee
commit 9a5486c03c
2 changed files with 27 additions and 22 deletions

14
audio.c
View File

@ -83,13 +83,11 @@ int audio_playback_init(audio_state* state)
void audio_recording_callback(void* userdata, uint8_t* stream, int len) void audio_recording_callback(void* userdata, uint8_t* stream, int len)
{ {
char string[64];
audio_state* state = userdata; audio_state* state = userdata;
int space_left = state->recording_buffer_size - state->recording_buffer_position; int space_left = state->recording_buffer_size - state->recording_buffer_position;
if (space_left <= 0) { if (space_left <= 0) {
sprintf(string, "Stopping recording ID: %i", state->recording_device_id); log_message(LOG_INFO, "Stopping recording ID: %i", state->recording_device_id);
log_message(LOG_INFO, string);
SDL_PauseAudioDevice(state->recording_device_id, 1); SDL_PauseAudioDevice(state->recording_device_id, 1);
return; 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); memcpy(&state->recording_buffer[state->recording_buffer_position], stream, len);
state->recording_buffer_position += 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, "Record pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len);
log_message(LOG_INFO, string);
} }
void audio_playback_callback(void* userdata, uint8_t* stream, int len) void audio_playback_callback(void* userdata, uint8_t* stream, int len)
{ {
char string[64];
audio_state* state = userdata; audio_state* state = userdata;
int space_left = state->recording_buffer_size - state->recording_buffer_position; int space_left = state->recording_buffer_size - state->recording_buffer_position;
if (space_left <= 0) { if (space_left <= 0) {
sprintf(string, "Stopping playback ID: %i", state->recording_device_id); log_message(LOG_INFO, "Stopping playback ID: %i", state->recording_device_id);
log_message(LOG_INFO, string);
SDL_PauseAudioDevice(state->playback_device_id, 1); SDL_PauseAudioDevice(state->playback_device_id, 1);
return; 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); memcpy(stream, &state->recording_buffer[state->recording_buffer_position], len);
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, "Playback pos: %u/%u (%u)", state->recording_buffer_position, state->recording_buffer_size, len);
log_message(LOG_INFO, string);
} }

35
main.c
View File

@ -16,10 +16,11 @@ typedef enum {
PLAYBACK, PLAYBACK,
} application_state; } application_state;
typedef struct state { typedef struct {
application_state application_state; application_state application_state;
audio_state audio; audio_state audio;
int device_index; int device_index;
int update_ui;
} state; } state;
int init(); int init();
@ -39,7 +40,6 @@ int main(int argc, char* argv[])
if (!init()) return 1; if (!init()) return 1;
num_devices = SDL_GetNumAudioDevices(1); num_devices = SDL_GetNumAudioDevices(1);
printf("Num audio devices: %i\n", num_devices);
log_message(LOG_INFO, "Num audio devices: %i", num_devices); log_message(LOG_INFO, "Num audio devices: %i", num_devices);
char char_buffer[64]; char char_buffer[64];
char default_fmt[] = "Device selected: %i"; char default_fmt[] = "Device selected: %i";
@ -48,6 +48,7 @@ int main(int argc, char* argv[])
state.application_state = SELECTING_DEVICE; state.application_state = SELECTING_DEVICE;
if (!audio_init(&state.audio)) return 1; if (!audio_init(&state.audio)) return 1;
state.device_index = -1; state.device_index = -1;
state.update_ui = 0;
text_texture display_text_texture; text_texture display_text_texture;
text_texture_init(&display_text_texture, renderer); text_texture_init(&display_text_texture, renderer);
@ -85,10 +86,14 @@ int main(int argc, char* argv[])
handle_task(&state); handle_task(&state);
sprintf(char_buffer, default_fmt, state.device_index); if (state.update_ui) {
text_texture_load(&display_text_texture, char_buffer); sprintf(char_buffer, default_fmt, state.device_index);
application_state_to_string(state.application_state, char_buffer); text_texture_load(&display_text_texture, char_buffer);
text_texture_load(&state_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 // Clear the screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
@ -136,12 +141,11 @@ int handle_input(SDL_Event* event, state* state)
case SDLK_y: case SDLK_y:
case SDLK_RETURN: case SDLK_RETURN:
if (state->device_index == -1 || !state->audio.recording_device_id) break; 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); memset(state->audio.recording_buffer, 0, state->audio.recording_buffer_size);
state->audio.recording_buffer_position = 0; state->audio.recording_buffer_position = 0;
SDL_UnlockAudioDevice(state->audio.recording_device_id);
SDL_PauseAudioDevice(state->audio.recording_device_id, 0); SDL_PauseAudioDevice(state->audio.recording_device_id, 0);
state->application_state = RECORDING; state->application_state = RECORDING;
state->update_ui = 1;
break; break;
case SDLK_q: case SDLK_q:
case SDLK_ESCAPE: case SDLK_ESCAPE:
@ -154,6 +158,7 @@ int handle_input(SDL_Event* event, state* state)
state->device_index = device_index_new; state->device_index = device_index_new;
SDL_CloseAudioDevice(state->audio.recording_device_id); SDL_CloseAudioDevice(state->audio.recording_device_id);
audio_recording_init(&state->audio, state->device_index); audio_recording_init(&state->audio, state->device_index);
state->update_ui = 1;
break; break;
@ -163,6 +168,7 @@ int handle_input(SDL_Event* event, state* state)
case SDLK_ESCAPE: case SDLK_ESCAPE:
SDL_PauseAudioDevice(state->audio.recording_device_id, 1); SDL_PauseAudioDevice(state->audio.recording_device_id, 1);
state->application_state = SELECTING_DEVICE; state->application_state = SELECTING_DEVICE;
state->update_ui = 1;
break; break;
} }
break; break;
@ -173,15 +179,15 @@ int handle_input(SDL_Event* event, state* state)
case SDLK_RETURN: case SDLK_RETURN:
if (!state->audio.playback_device_id) if (!state->audio.playback_device_id)
audio_playback_init(&state->audio); audio_playback_init(&state->audio);
SDL_LockAudioDevice(state->audio.playback_device_id);
state->audio.recording_buffer_position = 0; state->audio.recording_buffer_position = 0;
SDL_UnlockAudioDevice(state->audio.playback_device_id);
SDL_PauseAudioDevice(state->audio.playback_device_id, 0); SDL_PauseAudioDevice(state->audio.playback_device_id, 0);
state->application_state = PLAYBACK; state->application_state = PLAYBACK;
state->update_ui = 1;
break; break;
case SDLK_q: case SDLK_q:
case SDLK_ESCAPE: case SDLK_ESCAPE:
state->application_state = SELECTING_DEVICE; state->application_state = SELECTING_DEVICE;
state->update_ui = 1;
break; break;
} }
break; break;
@ -192,6 +198,7 @@ int handle_input(SDL_Event* event, state* state)
case SDLK_ESCAPE: case SDLK_ESCAPE:
SDL_PauseAudioDevice(state->audio.playback_device_id, 1); SDL_PauseAudioDevice(state->audio.playback_device_id, 1);
state->application_state = RECORDED; state->application_state = RECORDED;
state->update_ui = 1;
break; break;
} }
break; break;
@ -208,16 +215,20 @@ void handle_task(state* state)
break; break;
case RECORDING: 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->application_state = RECORDED;
state->update_ui = 1;
}
break; break;
case RECORDED: case RECORDED:
break; break;
case PLAYBACK: 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->application_state = RECORDED;
state->update_ui = 1;
}
break; break;
} }