1 /* Copyright (C) 2015-2017 Sergey V. Mikayev
2  *
3  *  This program is free software: you can redistribute it and/or modify
4  *  it under the terms of the GNU Lesser General Public License as published by
5  *  the Free Software Foundation, either version 2.1 of the License, or
6  *  (at your option) any later version.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU Lesser General Public License for more details.
12  *
13  *  You should have received a copy of the GNU Lesser General Public License
14  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MT32EMU_SAMPLE_RATE_CONVERTER_H
18 #define MT32EMU_SAMPLE_RATE_CONVERTER_H
19 
20 #include "globals.h"
21 #include "Types.h"
22 #include "Enumerations.h"
23 
24 namespace MT32Emu {
25 
26 class Synth;
27 
28 /* SampleRateConverter class allows to convert the synthesiser output to any desired sample rate.
29  * It processes the completely mixed stereo output signal as it passes the analogue circuit emulation,
30  * so emulating the synthesiser output signal passing further through an ADC.
31  * Several conversion quality options are provided which allow to trade-off the conversion speed vs. the passband width.
32  * All the options except FASTEST guarantee full suppression of the aliasing noise in terms of the 16-bit integer samples.
33  */
34 class MT32EMU_EXPORT SampleRateConverter {
35 public:
36 	// Returns the value of AnalogOutputMode for which the output signal may retain its full frequency spectrum
37 	// at the sample rate specified by the targetSampleRate argument.
38 	static AnalogOutputMode getBestAnalogOutputMode(double targetSampleRate);
39 
40 	// Returns the sample rate supported by the sample rate conversion implementation currently in effect
41 	// that is closest to the one specified by the desiredSampleRate argument.
42 	static double getSupportedOutputSampleRate(double desiredSampleRate);
43 
44 	// Creates a SampleRateConverter instance that converts output signal from the synth to the given sample rate
45 	// with the specified conversion quality.
46 	SampleRateConverter(Synth &synth, double targetSampleRate, SamplerateConversionQuality quality);
47 	~SampleRateConverter();
48 
49 	// Fills the provided output buffer with the results of the sample rate conversion.
50 	// The input samples are automatically retrieved from the synth as necessary.
51 	void getOutputSamples(MT32Emu::Bit16s *buffer, unsigned int length);
52 
53 	// Fills the provided output buffer with the results of the sample rate conversion.
54 	// The input samples are automatically retrieved from the synth as necessary.
55 	void getOutputSamples(float *buffer, unsigned int length);
56 
57 	// Returns the number of samples produced at the internal synth sample rate (32000 Hz)
58 	// that correspond to the number of samples at the target sample rate.
59 	// Intended to facilitate audio time synchronisation.
60 	double convertOutputToSynthTimestamp(double outputTimestamp) const;
61 
62 	// Returns the number of samples produced at the target sample rate
63 	// that correspond to the number of samples at the internal synth sample rate (32000 Hz).
64 	// Intended to facilitate audio time synchronisation.
65 	double convertSynthToOutputTimestamp(double synthTimestamp) const;
66 
67 private:
68 	const double synthInternalToTargetSampleRateRatio;
69 	const bool useSynthDelegate;
70 	void * const srcDelegate;
71 }; // class SampleRateConverter
72 
73 } // namespace MT32Emu
74 
75 #endif // MT32EMU_SAMPLE_RATE_CONVERTER_H
76