1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 #pragma once
18 
19 /**
20  * @file JOSResampleReader.h
21  * @ingroup respec
22  * The JOSResampleReader class.
23  */
24 
25 #include "respec/ResampleReader.h"
26 #include "util/Buffer.h"
27 
28 AUD_NAMESPACE_BEGIN
29 
30 /**
31  * This resampling reader uses Julius O. Smith's resampling algorithm.
32  */
33 class AUD_API JOSResampleReader : public ResampleReader
34 {
35 private:
36 	typedef void (JOSResampleReader::*resample_f)(double target_factor, int length, sample_t* buffer);
37 
38 	/**
39 	 * The half filter length.
40 	 */
41 	static const int m_len;
42 
43 	/**
44 	 * The sample step size for the filter.
45 	 */
46 	static const int m_L;
47 
48 	/**
49 	 * The filter coefficients.
50 	 */
51 	static const float m_coeff[];
52 
53 	/**
54 	 * The reader channels.
55 	 */
56 	Channels m_channels;
57 
58 	/**
59 	 * The sample position in the cache.
60 	 */
61 	unsigned int m_n;
62 
63 	/**
64 	 * The subsample position in the cache.
65 	 */
66 	double m_P;
67 
68 	/**
69 	 * The input data buffer.
70 	 */
71 	Buffer m_buffer;
72 
73 	/**
74 	 * Double buffer for the sums.
75 	 */
76 	Buffer m_sums;
77 
78 	/**
79 	 * How many samples in the cache are valid.
80 	 */
81 	int m_cache_valid;
82 
83 	/**
84 	 * Resample function.
85 	 */
86 	resample_f m_resample;
87 
88 	/**
89 	 * Last resampling factor.
90 	 */
91 	double m_last_factor;
92 
93 	// delete copy constructor and operator=
94 	JOSResampleReader(const JOSResampleReader&) = delete;
95 	JOSResampleReader& operator=(const JOSResampleReader&) = delete;
96 
97 	/**
98 	 * Resets the resampler to its initial state.
99 	 */
100 	void AUD_LOCAL reset();
101 
102 	/**
103 	 * Updates the buffer to be as small as possible for the coming reading.
104 	 * \param size The size of samples to be read.
105 	 * \param factor The next resampling factor.
106 	 * \param samplesize The size of a sample.
107 	 */
108 	void AUD_LOCAL updateBuffer(int size, double factor, int samplesize);
109 
110 	void AUD_LOCAL resample(double target_factor, int length, sample_t* buffer);
111 	void AUD_LOCAL resample_mono(double target_factor, int length, sample_t* buffer);
112 	void AUD_LOCAL resample_stereo(double target_factor, int length, sample_t* buffer);
113 
114 public:
115 	/**
116 	 * Creates a resampling reader.
117 	 * \param reader The reader to mix.
118 	 * \param rate The target sampling rate.
119 	 */
120 	JOSResampleReader(std::shared_ptr<IReader> reader, SampleRate rate);
121 
122 	virtual void seek(int position);
123 	virtual int getLength() const;
124 	virtual int getPosition() const;
125 	virtual Specs getSpecs() const;
126 	virtual void read(int& length, bool& eos, sample_t* buffer);
127 };
128 
129 AUD_NAMESPACE_END
130