aboutsummaryrefslogtreecommitdiffstats
path: root/lib/param.h
diff options
context:
space:
mode:
authorMarkus Mittendrein <maxmitti@maxmitti.tk>2022-11-28 20:16:46 +0100
committerMarkus Mittendrein <maxmitti@maxmitti.tk>2022-11-28 20:16:46 +0100
commit4cb83acddbe154312b74d2d63985ac1100bad7c5 (patch)
treed49dc904acc9af57acca8864fb6a40e340accacc /lib/param.h
downloadfresample-4cb83acddbe154312b74d2d63985ac1100bad7c5.tar.gz
fresample-4cb83acddbe154312b74d2d63985ac1100bad7c5.zip
InitialHEADmaster
Diffstat (limited to 'lib/param.h')
-rw-r--r--lib/param.h135
1 files changed, 135 insertions, 0 deletions
diff --git a/lib/param.h b/lib/param.h
new file mode 100644
index 0000000..ab65c63
--- /dev/null
+++ b/lib/param.h
@@ -0,0 +1,135 @@
+/* Copyright 2012 Dietrich Epp <depp@zdome.net> */
+#pragma once
+
+#include <array>
+
+/*
+ Parameters for the filter generator.
+
+ Filter generation goes through two stages.
+
+ 1. In the first stage, the resampling parameters are used to create
+ a filter specification. The filter specification consists of
+ normalized frequencies for the pass band, stop band, and stop band
+ attenuation. This stage uses simple logic to create a filter
+ specification that "makes sense" for any input. It relaxes the
+ filter specification for ultrasonic (inaudible) frequencies and
+ ensures that enough of the input signal passes through.
+
+ 2. In the second stage, an FIR filter is generated that fits the
+ filter specified by the first stage.
+
+ Normally, for resampling, you will specify QUALITY, INRATE, and
+ OUTRATE. A filter specification for the conversion will be
+ automatically generated with the given subjective quality level.
+
+ If you are a signal-processing guru, you can create the filter
+ specification directly by setting FPASS, FSTOP, and ATTEN.
+ */
+enum lfr_param_t {
+ /* High level filter parameters. These are typi*cally the only
+ parameters you will need to set. */
+
+ /* Filter quality, default 8. An integer between 0 and 10 which
+ determines the default values for other para*meters. */
+ LFR_PARAM_QUALITY,
+
+ /* Input sample rate, in Hz. The default is -1, which creates a
+ generic filter. */
+ LFR_PARAM_INRATE,
+
+ /* Output sample rate. If the input sample rate is specified,
+ then this is measured in Hz. Otherwise, if *the input rate is
+ -1, then this value is relative to the input sample rate (so
+ you would use 0.5 for downsampling by a factor of two).
+ Defaults to the same value as the input sample rate, which
+ creates a filter which can be used for upsampling at any ratio.
+ Note that increasing the output rate above the input rate has
+ no effect, all upsampling filters for a given input frequency
+ are identical. */
+ LFR_PARAM_OUTRATE,
+
+ /*
+ Medium level filter parameters. These parame*ters affect how the
+ filter specification is generated from the input and output
+ sample rates. Most of these parameters have default values
+ which depend on the QUALITY setting.
+ */
+
+ /* The width of the filter transition band, as a fraction of the
+ input sample rate. This value will be enlar*ged to extend the
+ transition band to MAXFREQ and will be narrowed to extend the
+ pass band to MINBW. The default value depends on the QUALITY
+ setting, and gets narrower as QUALITY increases. */
+ LFR_PARAM_FTRANSITION,
+
+ /* Maximum audible frequency. The pass band will be narrowed to
+ fit within the range of audible frequencies.* Default value is
+ 20 kHz if the input frequency is set, otherwise this parameter
+ is unused. If you want to preserve ultrasonic frequencies,
+ disable this parameter by setting it to -1. This is disabled
+ by default at absurd quality settings (9 and 10). */
+ LFR_PARAM_MAXFREQ,
+
+ /* A flag which allows aliasing noise as long as it is above
+ MAXFREQ. This flag improves the subjective *quality of
+ low-quality filters by increasing their bandwidth, but causes
+ problems for high-quality filters by increasing noise. Default
+ value depends on QUALITY setting, and is set for low QUALITY
+ values. Has no effect if MAXFREQ is disabled. */
+ LFR_PARAM_LOOSE,
+
+ /* Minimum size of pass band, as a fraction of the output
+ bandwidth. This prevents the filter designe*r from filtering
+ out the entire signal, which can happen when downsampling by a
+ large enough ratio. The default value is 0.5 at low quality
+ settings, and higher at high quality settings. The filter size
+ can increase dramatically as this number approaches 1.0. */
+ LFR_PARAM_MINFPASS,
+
+ /*
+ Filter specification. These parameters are n*ormally generated
+ from the higher level parameters. If the filter specification
+ is set, then the higher level parameters will all be ignored.
+ */
+
+ /* The end of the pass band, as a fraction of the input sample
+ rate. Normally, the filter designer chooses* this value. */
+ LFR_PARAM_FPASS,
+
+ /* The start of the stop band, as a fraction of the input sample
+ rate. Normally, the filter designer chooses* this value. */
+ LFR_PARAM_FSTOP,
+
+ /* Desired stop band attenuation, in dB. Larger numbers are
+ increase filter quality. Default value depe*nds on the QUALITY
+ setting. */
+ LFR_PARAM_ATTEN,
+
+ LFR_PARAM_COUNT
+};
+
+class lfr_param {
+ /*
+ Bit set of which parameters were set by the user.
+ */
+ unsigned set{0};
+
+ /*
+ Set to 1 if the derived parameters are up to date.
+ */
+ int current{0};
+
+public:
+ /*
+ Parameter values.
+ */
+ std::array<double, LFR_PARAM_COUNT> param;
+
+ void calculate();
+ void seti(lfr_param_t pname, int value);
+ void setf(lfr_param_t pname, double value);
+ void geti(lfr_param_t pname, int* value);
+ void getf(lfr_param_t pname, double* value);
+};
+