From 16e71fe97173f6d8c7c1c4836eea04d460174912 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 5 Mar 2015 19:38:08 +0100 Subject: [PATCH] Support any sample rate, not only 44100 Hz. --- audioreader.cpp | 4 +++- audioreader.h | 2 +- decode.cpp | 10 +++++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/audioreader.cpp b/audioreader.cpp index b95dbf4..f0d07d8 100644 --- a/audioreader.cpp +++ b/audioreader.cpp @@ -88,7 +88,7 @@ int decode_packet(const char *filename, AVCodecContext *codec_ctx, SwrContext *s } // namespace -bool read_audio_file(const char *filename, std::vector *samples) +bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate) { av_register_all(); @@ -190,5 +190,7 @@ bool read_audio_file(const char *filename, std::vector *samples) // Convert any leftover samples from the converter. convert_samples(swr, codec_ctx->sample_rate, nullptr, 0, samples); + *sample_rate = codec_ctx->sample_rate; + return true; } diff --git a/audioreader.h b/audioreader.h index 273ebd2..47bb8f5 100644 --- a/audioreader.h +++ b/audioreader.h @@ -5,6 +5,6 @@ #include -bool read_audio_file(const char *filename, std::vector *samples); +bool read_audio_file(const char *filename, std::vector *samples, int *sample_rate); #endif // !defined(_AUDIOREADER_H) diff --git a/decode.cpp b/decode.cpp index e85f579..c5aa966 100644 --- a/decode.cpp +++ b/decode.cpp @@ -12,7 +12,6 @@ #define BUFSIZE 4096 #define HYSTERESIS_LIMIT 3000 -#define SAMPLE_RATE 44100 #define C64_FREQUENCY 985248 #define SYNC_PULSE_START 1000 @@ -56,7 +55,8 @@ int main(int argc, char **argv) { make_lanczos_weight_table(); std::vector pcm; - if (!read_audio_file(argv[1], &pcm)) { + int sample_rate; + if (!read_audio_file(argv[1], &pcm, &sample_rate)) { exit(1); } @@ -96,14 +96,14 @@ int main(int argc, char **argv) if (!true_pulse) { #if 0 fprintf(stderr, "Ignored down-flank at %.6f seconds due to hysteresis (%d < %d).\n", - double(i) / SAMPLE_RATE, -min_level_after, HYSTERESIS_LIMIT); + double(i) / sample_rate, -min_level_after, HYSTERESIS_LIMIT); #endif i = j; continue; } // down-flank! - double t = find_zerocrossing(pcm, i - 1) * (1.0 / SAMPLE_RATE); + double t = find_zerocrossing(pcm, i - 1) * (1.0 / sample_rate); if (last_downflank > 0) { pulse p; p.time = t; @@ -186,7 +186,7 @@ int main(int argc, char **argv) short zero = 0; unsigned pulsenum = 0; for (unsigned i = 0; i < pcm.size(); ++i) { - unsigned next_pulse = (pulsenum >= pulses.size()) ? INT_MAX : int(pulses[pulsenum].time * SAMPLE_RATE); + unsigned next_pulse = (pulsenum >= pulses.size()) ? INT_MAX : int(pulses[pulsenum].time * sample_rate); if (i >= next_pulse) { fwrite(&one, sizeof(one), 1, fp); ++pulsenum; -- 2.39.2