Skip to content

Commit

Permalink
add error check for write file
Browse files Browse the repository at this point in the history
  • Loading branch information
litongmacos committed Nov 18, 2023
1 parent 2f94d9d commit 405f59c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class wav_writer {

// Check if write was successful
if (!fstream.good()) {
std::cerr << "Error writing to WAV file\n";
fprintf(stderr, "Error writing to WAV file\n");
return false; // Stop writing and return an error
}
}
Expand Down
7 changes: 5 additions & 2 deletions examples/recorder/recorder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main(int argc, char **argv) {

if (SDL_Init(SDL_INIT_AUDIO) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
return false;
return -1;
}

SDL_SetHintWithPriority(SDL_HINT_AUDIO_RESAMPLING_MODE, "medium", SDL_HINT_OVERRIDE);
Expand Down Expand Up @@ -71,7 +71,10 @@ int main(int argc, char **argv) {
// main audio loop
while (is_running) {
audio.get(3000, pcmf32_new);
wavWriter.write(pcmf32_new.data(), pcmf32_new.size());
if (!wavWriter.write(pcmf32_new.data(), pcmf32_new.size())) {
fprintf(stderr, "Failed to write audio data. Stopping.\n");
return -1;
}
// handle Ctrl + C
is_running = sdl_poll_events();

Expand Down

0 comments on commit 405f59c

Please sign in to comment.