aboutsummaryrefslogtreecommitdiffstats
path: root/lib/param.h
blob: ab65c63e6671c953a92a65d0752ba76e3ddb9bab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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);
};