From 91eeec218f99f96013e62150d82d7eb1f7e2d5b0 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 12 Mar 2015 00:54:30 +0100 Subject: [PATCH] Make leveler minimum level configurable, since it is connected to hysteresis limit. --- decode.cpp | 15 +++++++++++++-- level.cpp | 10 ++-------- level.h | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/decode.cpp b/decode.cpp index e8263ea..b3b8aa3 100644 --- a/decode.cpp +++ b/decode.cpp @@ -42,6 +42,12 @@ static bool output_leveled = false; static std::vector train_snap_points; static bool do_train = false; +// The minimum estimated sound level (for do_auto_level) at any given point. +// If you decrease this, you'll be able to amplify really silent signals +// by more, but you'll also increase the level of silent (ie. noise-only) segments, +// possibly caused misdetected pulses in these segments. +static float min_level = 0.05f; + // between [x,x+1] double find_zerocrossing(const std::vector &pcm, int x) { @@ -182,6 +188,7 @@ void help() fprintf(stderr, "\n"); fprintf(stderr, " -a, --auto-level automatically adjust amplitude levels throughout the file\n"); fprintf(stderr, " -A, --output-leveled output leveled waveform to leveled.raw\n"); + fprintf(stderr, " -m, --min-level minimum estimated sound level (0..32768) for --auto-level\n"); fprintf(stderr, " -s, --no-calibrate do not try to calibrate on sync pulse length\n"); fprintf(stderr, " -p, --plot-cycles output debugging info to cycles.plot\n"); fprintf(stderr, " -l, --hysteresis-limit VAL change amplitude threshold for ignoring pulses (0..32768)\n"); @@ -199,7 +206,7 @@ void parse_options(int argc, char **argv) { for ( ;; ) { int option_index = 0; - int c = getopt_long(argc, argv, "aAspl:f:Fc:t:qh", long_options, &option_index); + int c = getopt_long(argc, argv, "aAm:spl:f:Fc:t:qh", long_options, &option_index); if (c == -1) break; @@ -212,6 +219,10 @@ void parse_options(int argc, char **argv) output_leveled = true; break; + case 'm': + min_level = atof(optarg) / 32768.0; + break; + case 's': do_calibrate = false; break; @@ -459,7 +470,7 @@ int main(int argc, char **argv) } if (do_auto_level) { - pcm = level_samples(pcm, sample_rate); + pcm = level_samples(pcm, min_level, sample_rate); if (output_leveled) { FILE *fp = fopen("leveled.raw", "wb"); fwrite(pcm.data(), pcm.size() * sizeof(pcm[0]), 1, fp); diff --git a/level.cpp b/level.cpp index fea5926..e95b48e 100644 --- a/level.cpp +++ b/level.cpp @@ -11,12 +11,6 @@ // ruin the waveforms themselves. #define LPFILTER_FREQ 50.0 -// The minimum estimated sound level at any given point. -// If you decrease this, you'll be able to amplify really silent signals -// by more, but you'll also increase the level of silent (ie. noise-only) segments, -// possibly caused misdetected pulses in these segments. -#define MIN_LEVEL 0.05 - // A final scalar to get the audio within approximately the right range. // Increase to _lower_ overall volume. #define DAMPENING_FACTOR 5.0 @@ -59,7 +53,7 @@ static float filter_update(float in) return out; } -std::vector level_samples(const std::vector &pcm, int sample_rate) +std::vector level_samples(const std::vector &pcm, float min_level, int sample_rate) { // filter forwards, then backwards (perfect phase filtering) std::vector filtered_samples, refiltered_samples, leveled_samples; @@ -88,7 +82,7 @@ std::vector level_samples(const std::vector &pcm, int sample_rate) } for (unsigned i = 0; i < pcm.size(); ++i) { - float f = DAMPENING_FACTOR * std::max(refiltered_samples[i], MIN_LEVEL); + float f = DAMPENING_FACTOR * std::max(refiltered_samples[i], min_level); leveled_samples[i] = pcm[i] / f; } diff --git a/level.h b/level.h index 3f830d7..763e617 100644 --- a/level.h +++ b/level.h @@ -3,6 +3,6 @@ #include -std::vector level_samples(const std::vector &pcm, int sample_rate); +std::vector level_samples(const std::vector &pcm, float min_level, int sample_rate); #endif // !defined(_LEVEL_H) -- 2.39.2