diff --git a/.gitignore b/.gitignore index b2954aab..d675840a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,8 @@ dsd *.dylib build .DS_Store +.idea +cmake-build* +*.swp + +build/ diff --git a/cmake/FindLibSndFile.cmake b/cmake/FindLibSndFile.cmake index b12b358d..474ea413 100644 --- a/cmake/FindLibSndFile.cmake +++ b/cmake/FindLibSndFile.cmake @@ -12,5 +12,10 @@ SET(LIBSNDFILE_NAMES ${LIBSNDFILE_NAMES} sndfile libsndfile) FIND_LIBRARY(LIBSNDFILE_LIBRARY NAMES ${LIBSNDFILE_NAMES}) include(FindPackageHandleStandardArgs) -find_package_handle_standard_args(LIBSNDFILE DEFAULT_MSG LIBSNDFILE_LIBRARY - LIBSNDFILE_INCLUDE_DIR) \ No newline at end of file +if (APPLE) + find_package_handle_standard_args(LibSndFile DEFAULT_MSG LIBSNDFILE_LIBRARY + LIBSNDFILE_INCLUDE_DIR) +elseif (UNIX) + find_package_handle_standard_args(LIBSNDFILE DEFAULT_MSG LIBSNDFILE_LIBRARY + LIBSNDFILE_INCLUDE_DIR) +endif() #TODO elseif windows lol \ No newline at end of file diff --git a/include/dsd.h b/include/dsd.h index 353b8ded..04f2fbc6 100644 --- a/include/dsd.h +++ b/include/dsd.h @@ -316,5 +316,6 @@ void processX2TDMAvoice (dsd_opts * opts, dsd_state * state); void processDSTAR_HD (dsd_opts * opts, dsd_state * state); short dmr_filter(short sample); short nxdn_filter(short sample); - +int checkFileError(FILE *file); +int handleFileError(char *fileName, int fileStatus); #endif // DSD_H diff --git a/src/dsd_file.c b/src/dsd_file.c index c805885d..fcdadcae 100644 --- a/src/dsd_file.c +++ b/src/dsd_file.c @@ -17,6 +17,49 @@ #include "dsd.h" +static void openFile(FILE **file, char *fileName) { + char errorMsg[255]; + + *file = fopen(fileName, "ro"); + if (NULL == *file) { + sprintf(errorMsg, "Error: could not open %s ", fileName); + perror(errorMsg); + exitflag = 1; + } +} + +int checkFileError(FILE *file) { + if (ferror(file)) { + return 1; + } else if (feof(file)) { + return EOF; + } + + return 0; +} + +int handleFileError(char *fileName, int fileStatus) { + + char errorMsg[255]; + + switch (fileStatus) { + case 0: + break; + case EOF: + sprintf(errorMsg, "Unexpected EOF when reading file %s ", fileName); + perror(errorMsg); + break; + case 1: + default: + sprintf(errorMsg, "I/O error when reading file %s ", fileName); + perror(errorMsg); + exitflag = 1; + break; + } + + return fileStatus; +} + void saveImbe4400Data (dsd_opts * opts, dsd_state * state, char *imbe_d) { @@ -130,38 +173,35 @@ readAmbe2450Data (dsd_opts * opts, dsd_state * state, char *ambe_d) return (0); } -void -openMbeInFile (dsd_opts * opts, dsd_state * state) -{ +void openMbeInFile(dsd_opts *opts, dsd_state *state) { - char cookie[5]; + char cookie[5]; + int c, i; - opts->mbe_in_f = fopen (opts->mbe_in_file, "ro"); - if (opts->mbe_in_f == NULL) - { - printf ("Error: could not open %s\n", opts->mbe_in_file); - } + openFile(&opts->mbe_in_f, opts->mbe_in_file); - // read cookie - cookie[0] = fgetc (opts->mbe_in_f); - cookie[1] = fgetc (opts->mbe_in_f); - cookie[2] = fgetc (opts->mbe_in_f); - cookie[3] = fgetc (opts->mbe_in_f); - cookie[4] = 0; - if (strstr (cookie, ".amb") != NULL) - { - state->mbe_file_type = 1; - } - else if (strstr (cookie, ".imb") != NULL) - { - state->mbe_file_type = 0; + for (i = 0; !exitflag && i < 4; ++i) { + + c = fgetc(opts->mbe_in_f); + + if (handleFileError(opts->mbe_in_file, checkFileError(opts->mbe_in_f))) { + break; + } + + // read cookie + cookie[i] = (char) c; } - else - { - state->mbe_file_type = -1; - printf ("Error - unrecognized file type\n"); + cookie[4] = 0; + + if (strstr(cookie, ".amb") != NULL) { + state->mbe_file_type = 1; + } else if (strstr(cookie, ".imb") != NULL) { + state->mbe_file_type = 0; + } else { + state->mbe_file_type = -1; + fprintf(stderr, "%s\n", "Error - unrecognized file type"); + exitflag = 1; } - } void diff --git a/src/dsd_mbe.c b/src/dsd_mbe.c index 3e93a826..4d38cedc 100644 --- a/src/dsd_mbe.c +++ b/src/dsd_mbe.c @@ -17,56 +17,94 @@ #include "dsd.h" -void -playMbeFiles (dsd_opts * opts, dsd_state * state, int argc, char **argv) -{ +typedef void (*dataReaderDef)(dsd_opts *opts, dsd_state *state); - int i; - char imbe_d[88]; - char ambe_d[49]; +static void readImbe(dsd_opts *opts, dsd_state *state) { - for (i = state->optind; i < argc; i++) - { - sprintf (opts->mbe_in_file, "%s", argv[i]); - openMbeInFile (opts, state); - mbe_initMbeParms (state->cur_mp, state->prev_mp, state->prev_mp_enhanced); - printf ("playing %s\n", opts->mbe_in_file); - while (feof (opts->mbe_in_f) == 0) - { - if (state->mbe_file_type == 0) - { - readImbe4400Data (opts, state, imbe_d); - mbe_processImbe4400Dataf (state->audio_out_temp_buf, &state->errs, &state->errs2, state->err_str, imbe_d, state->cur_mp, state->prev_mp, state->prev_mp_enhanced, opts->uvquality); - processAudio (opts, state); - if (opts->wav_out_f != NULL) - { - writeSynthesizedVoice (opts, state); - } - - if (opts->audio_out == 1) - { - playSynthesizedVoice (opts, state); - } - } - else if (state->mbe_file_type == 1) - { - readAmbe2450Data (opts, state, ambe_d); - mbe_processAmbe2450Dataf (state->audio_out_temp_buf, &state->errs, &state->errs2, state->err_str, ambe_d, state->cur_mp, state->prev_mp, state->prev_mp_enhanced, opts->uvquality); - processAudio (opts, state); - if (opts->wav_out_f != NULL) - { - writeSynthesizedVoice (opts, state); - } - if (opts->audio_out == 1) - { - playSynthesizedVoice (opts, state); - } - } - if (exitflag == 1) - { - cleanupAndExit (opts, state); + char imbe_d[88]; + readImbe4400Data(opts, state, imbe_d); + mbe_processImbe4400Dataf(state->audio_out_temp_buf, + &state->errs, + &state->errs2, + state->err_str, + imbe_d, + state->cur_mp, + state->prev_mp, + state->prev_mp_enhanced, + opts->uvquality); +} + +static void readAmbe(dsd_opts *opts, dsd_state *state) { + + char ambe_d[49]; + readAmbe2450Data(opts, state, ambe_d); + mbe_processAmbe2450Dataf(state->audio_out_temp_buf, + &state->errs, + &state->errs2, + state->err_str, + ambe_d, + state->cur_mp, + state->prev_mp, + state->prev_mp_enhanced, + opts->uvquality); +} + +static void playMbeFile(dataReaderDef readData, dsd_opts *opts, dsd_state *state) { + + int ret; + + while (!exitflag && readData != NULL) { + + readData(opts, state); + processAudio(opts, state); + + if (opts->wav_out_f != NULL) { + writeSynthesizedVoice(opts, state); + } + + if (1 == opts->audio_out) { + playSynthesizedVoice(opts, state); + } + + if ((ret = checkFileError(opts->mbe_in_f))) { + + if (ret != EOF) { + handleFileError(opts->mbe_in_file, ret); } + break; + } + } +} + +void playMbeFiles(dsd_opts *opts, dsd_state *state, int argc, char **argv) { + + int i; + for (i = state->optind; !exitflag && i < argc; i++) { + + strcpy(opts->mbe_in_file, argv[i]); + openMbeInFile(opts, state); + mbe_initMbeParms(state->cur_mp, + state->prev_mp, + state->prev_mp_enhanced); + + if (opts->verbose > 0) { + printf("playing %s\n", opts->mbe_in_file); + } + + switch (state->mbe_file_type) { + case 0: + playMbeFile(readImbe, opts, state); + break; + case 1: + playMbeFile(readAmbe, opts, state); + break; + default: + fprintf(stderr, "Error - unrecognized file type %d\n", state->mbe_file_type); + exitflag = 1; + break; } + + handleFileError(opts->mbe_in_file, fclose(opts->mbe_in_f)); } }