File: rate.cpp

package info (click to toggle)
scummvm 2.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 450,268 kB
  • sloc: cpp: 4,297,604; asm: 28,322; python: 12,901; sh: 11,219; java: 8,477; xml: 7,843; perl: 2,633; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (317 lines) | stat: -rw-r--r-- 10,189 bytes parent folder | download | duplicates (2)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * The code in this file is based on code with Copyright 1998 Fabrice Bellard
 * Fabrice original code is part of SoX (http://sox.sourceforge.net).
 * Max Horn adapted that code to the needs of ScummVM and rewrote it partial,
 * in the process removing any use of floating point arithmetic. Various other
 * improvements over the original code were made.
 */

#include "audio/audiostream.h"
#include "audio/rate.h"
#include "audio/mixer.h"
#include "common/util.h"

namespace Audio {

/**
 * The default fractional type in frac.h (with 16 fractional bits) limits
 * the rate conversion code to 65536Hz audio: we need to able to handle
 * 96kHz audio, so we use fewer fractional bits in this code.
 */
enum {
	FRAC_BITS_LOW = 15,
	FRAC_ONE_LOW = (1L << FRAC_BITS_LOW),
	FRAC_HALF_LOW = (1L << (FRAC_BITS_LOW-1))
};

template<bool inStereo, bool outStereo, bool reverseStereo>
class RateConverter_Impl : public RateConverter {
private:
	/** Input and output rates */
	st_rate_t _inRate, _outRate;

	/**
	 * The intermediate input cache. Bigger values may increase performance,
	 * but only until some point (depends largely on cache size, target
	 * processor and various other factors), at which it will decrease again.
	 */
	st_sample_t _buffer[512];

	/** Current position inside the buffer */
	const st_sample_t *_bufferPos;

	/** Size of data currently loaded into the buffer */
	int _bufferSize;

	/** How far output is ahead of input when doing simple conversion */
	frac_t _outPos;

	/** Fractional position of the output stream in input stream unit */
	frac_t _outPosFrac;

	/** Last sample(s) in the input stream (left/right channel) */
	st_sample_t _inLastL, _inLastR;
	
	/** Current sample(s) in the input stream (left/right channel) */
	st_sample_t _inCurL, _inCurR;

	int copyConvert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t vol_l, st_volume_t vol_r);
	int simpleConvert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t vol_l, st_volume_t vol_r);
	int interpolateConvert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t vol_l, st_volume_t vol_r);

public:
	RateConverter_Impl(st_rate_t inputRate, st_rate_t outputRate);
	virtual ~RateConverter_Impl() {}

	int convert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t vol_l, st_volume_t vol_r) override;

	void setInputRate(st_rate_t inputRate) override { _inRate = inputRate; }
	void setOutputRate(st_rate_t outputRate) override { _outRate = outputRate; }

	st_rate_t getInputRate() const override { return _inRate; }
	st_rate_t getOutputRate() const override { return _outRate; }

	bool needsDraining() const override { return _bufferSize != 0; }
};

template<bool inStereo, bool outStereo, bool reverseStereo>
int RateConverter_Impl<inStereo, outStereo, reverseStereo>::copyConvert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t volL, st_volume_t volR) {
	st_sample_t *outStart, *outEnd;

	outStart = outBuffer;
	outEnd = outBuffer + numSamples * (outStereo ? 2 : 1);

	while (outBuffer < outEnd) {
		// Check if we have to refill the buffer
		if (_bufferSize == 0) {
			_bufferPos = _buffer;
			_bufferSize = input.readBuffer(_buffer, ARRAYSIZE(_buffer));

			if (_bufferSize <= 0)
				return (outBuffer - outStart) / (outStereo ? 2 : 1);
		}

		// Mix the data into the output buffer
		st_sample_t inL, inR;
		inL = *_bufferPos++;
		inR = (inStereo ? *_bufferPos++ : inL);
		_bufferSize -= (inStereo ? 2 : 1);

		st_sample_t outL, outR;
		outL = (inL * (int)volL) / Audio::Mixer::kMaxMixerVolume;
		outR = (inR * (int)volR) / Audio::Mixer::kMaxMixerVolume;

		if (outStereo) {
			// Output left channel
			clampedAdd(outBuffer[reverseStereo    ], outL);

			// Output right channel
			clampedAdd(outBuffer[reverseStereo ^ 1], outR);

			outBuffer += 2;
		} else {
			// Output mono channel
			clampedAdd(outBuffer[0], (outL + outR) / 2);

			outBuffer += 1;
		}
	}

	return (outBuffer - outStart) / (outStereo ? 2 : 1);
}

template<bool inStereo, bool outStereo, bool reverseStereo>
int RateConverter_Impl<inStereo, outStereo, reverseStereo>::simpleConvert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t volL, st_volume_t volR) {
	// How much to increment _outPos by
	frac_t outPos_inc = _inRate / _outRate;

	st_sample_t *outStart, *outEnd;

	outStart = outBuffer;
	outEnd = outBuffer + numSamples * (outStereo ? 2 : 1);

	while (outBuffer < outEnd) {
		// Read enough input samples so that _outPos >= 0
		do {
			// Check if we have to refill the buffer
			if (_bufferSize == 0) {
				_bufferPos = _buffer;
				_bufferSize = input.readBuffer(_buffer, ARRAYSIZE(_buffer));

				if (_bufferSize <= 0)
					return (outBuffer - outStart) / (outStereo ? 2 : 1);
			}

			_bufferSize -= (inStereo ? 2 : 1);
			_outPos--;

			if (_outPos >= 0) {
				_bufferPos += (inStereo ? 2 : 1);
			}
		} while (_outPos >= 0);

		st_sample_t inL, inR;
		inL = *_bufferPos++;
		inR = (inStereo ? *_bufferPos++ : inL);

		// Increment output position
		_outPos += outPos_inc;

		st_sample_t outL, outR;
		outL = (inL * (int)volL) / Audio::Mixer::kMaxMixerVolume;
		outR = (inR * (int)volR) / Audio::Mixer::kMaxMixerVolume;

		if (outStereo) {
			// output left channel
			clampedAdd(outBuffer[reverseStereo    ], outL);

			// output right channel
			clampedAdd(outBuffer[reverseStereo ^ 1], outR);

			outBuffer += 2;
		} else {
			// output mono channel
			clampedAdd(outBuffer[0], (outL + outR) / 2);

			outBuffer += 1;
		}
	}
	return (outBuffer - outStart) / (outStereo ? 2 : 1);
}

template<bool inStereo, bool outStereo, bool reverseStereo>
int RateConverter_Impl<inStereo, outStereo, reverseStereo>::interpolateConvert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t volL, st_volume_t volR) {
	// How much to increment _outPosFrac by
	frac_t outPos_inc = (_inRate << FRAC_BITS_LOW) / _outRate;

	st_sample_t *outStart, *outEnd;
	outStart = outBuffer;
	outEnd = outBuffer + numSamples * (outStereo ? 2 : 1);

	while (outBuffer < outEnd) {
		// Read enough input samples so that _outPosFrac < 0
		while ((frac_t)FRAC_ONE_LOW <= _outPosFrac) {
			// Check if we have to refill the buffer
			if (_bufferSize == 0) {
				_bufferPos = _buffer;
				_bufferSize = input.readBuffer(_buffer, ARRAYSIZE(_buffer));

				if (_bufferSize <= 0)
					return (outBuffer - outStart) / (outStereo ? 2 : 1);
			}

			_bufferSize -= (inStereo ? 2 : 1);
			_inLastL = _inCurL;
			_inCurL = *_bufferPos++;

			if (inStereo) {
				_inLastR = _inCurR;
				_inCurR = *_bufferPos++;
			}

			_outPosFrac -= FRAC_ONE_LOW;
		}

		// Loop as long as the _outPos trails behind, and as long as there is
		// still space in the output buffer.
		while (_outPosFrac < (frac_t)FRAC_ONE_LOW && outBuffer < outEnd) {
			// Interpolate
			st_sample_t inL, inR;
			inL = (st_sample_t)(_inLastL + (((_inCurL - _inLastL) * _outPosFrac + FRAC_HALF_LOW) >> FRAC_BITS_LOW));
			inR = (inStereo ?
						(st_sample_t)(_inLastR + (((_inCurR - _inLastR) * _outPosFrac + FRAC_HALF_LOW) >> FRAC_BITS_LOW)) :
						inL);

			st_sample_t outL, outR;
			outL = (inL * (int)volL) / Audio::Mixer::kMaxMixerVolume;
			outR = (inR * (int)volR) / Audio::Mixer::kMaxMixerVolume;

			if (outStereo) {
				// Output left channel
				clampedAdd(outBuffer[reverseStereo    ], outL);

				// Output right channel
				clampedAdd(outBuffer[reverseStereo ^ 1], outR);

				outBuffer += 2;
			} else {
				// Output mono channel
				clampedAdd(outBuffer[0], (outL + outR) / 2);

				outBuffer += 1;
			}

			// Increment output position
			_outPosFrac += outPos_inc;
		}
	}
	return (outBuffer - outStart) / (outStereo ? 2 : 1);
}

template<bool inStereo, bool outStereo, bool reverseStereo>
RateConverter_Impl<inStereo, outStereo, reverseStereo>::RateConverter_Impl(st_rate_t inputRate, st_rate_t outputRate) :
	_inRate(inputRate),
	_outRate(outputRate),
	_outPos(1),
	_outPosFrac(FRAC_ONE_LOW),
	_inLastL(0),
	_inLastR(0),
	_inCurL(0),
	_inCurR(0),
	_bufferSize(0),
	_bufferPos(nullptr) {}

template<bool inStereo, bool outStereo, bool reverseStereo>
int RateConverter_Impl<inStereo, outStereo, reverseStereo>::convert(AudioStream &input, st_sample_t *outBuffer, st_size_t numSamples, st_volume_t volL, st_volume_t volR) {
	assert(input.isStereo() == inStereo);

	if (_inRate == _outRate) {
		return copyConvert(input, outBuffer, numSamples, volL, volR);
	} else {
		if ((_inRate % _outRate) == 0 && (_inRate < 65536)) {
			return simpleConvert(input, outBuffer, numSamples, volL, volR);
		} else {
			return interpolateConvert(input, outBuffer, numSamples, volL, volR);
		}
	}
}

RateConverter *makeRateConverter(st_rate_t inRate, st_rate_t outRate, bool inStereo, bool outStereo, bool reverseStereo) {
	if (inStereo) {
		if (outStereo) {
			if (reverseStereo)
				return new RateConverter_Impl<true, true, true>(inRate, outRate);
			else
				return new RateConverter_Impl<true, true, false>(inRate, outRate);
		} else
			return new RateConverter_Impl<true, false, false>(inRate, outRate);
	} else {
		if (outStereo) {
			return new RateConverter_Impl<false, true, false>(inRate, outRate);
		} else
			return new RateConverter_Impl<false, false, false>(inRate, outRate);
	}
}

} // End of namespace Audio