summaryrefslogtreecommitdiffstats
path: root/flow/resample.cpp
blob: 1c9e408a197c8c4d3643386eb38882e3f4a8bd34 (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
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
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       [email protected]

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#include "resample.h"
#include "debug.h"
#include <math.h>
#include <assert.h>
#include <stdio.h>

#define compose_16le(first,second) \
    (((((second)+128)&0xff) << 8)+(first))
 
#define compose_16be(first,second) \
    (((((first)+128)&0xff) << 8)+(second))

#define conv_16_float(x) \
    ((float)((x)-32768)/32768.0)                                                

#define conv_8_float(x) \
	((float)((x)-128)/128.0)

using namespace Arts;

class Arts::ResamplerPrivate {
public:
	bool underrun;	
	Resampler::Endianness endianness;
};

const unsigned int Resampler::bufferSize;
const unsigned int Resampler::bufferWrap;

Resampler::Resampler(Refiller *refiller) :
	dropBytes(0), refiller(refiller), pos(0.0), step(1.0), channels(2),
	bits(16),
	block(0), haveBlock(-1)
{
	d = new ResamplerPrivate();
	d->underrun = false;
	d->endianness = littleEndian;
	updateSampleSize();
}

Resampler::~Resampler()
{
	delete d;
}

void Resampler::updateSampleSize()
{
	sampleSize = channels * bits / 8;
	bufferSamples = bufferSize / sampleSize;
}

void Resampler::setStep(double newStep)
{
	arts_return_if_fail(newStep > 0);

	step = newStep;
}

void Resampler::setChannels(int newChannels)
{
	arts_return_if_fail(newChannels == 1 || newChannels == 2);

	channels = newChannels;
	updateSampleSize();
}

void Resampler::setBits(int newBits)
{
	arts_return_if_fail(newBits == 8 || newBits == 16);

	bits = newBits;
	updateSampleSize();
}

void Resampler::setEndianness(Endianness newEndianness)
{
	arts_return_if_fail(newEndianness == bigEndian || newEndianness == littleEndian);

	d->endianness = newEndianness;
}

bool Resampler::underrun()
{
	return d->underrun;
}

void Resampler::ensureRefill()
{
	if(haveBlock == block) return;

	unsigned long missing;
	if(block == 0)
	{
		missing = bufferSize+sampleSize
				- refiller->read(buffer,bufferSize+sampleSize);

		d->underrun = (missing == bufferSize+sampleSize);
	}
	else
	{
		/*
		 * try to drop away "half-sample" reads from the last refill
		 */
		if(dropBytes > 0)
			dropBytes -= refiller->read(buffer,dropBytes);

		/*
		 * only if this worked there is hope that we can read sane data
		 */
		if(dropBytes == 0)
		{
			missing = bufferSize
					- refiller->read(&buffer[sampleSize], bufferSize);

			d->underrun = (missing == bufferSize);
		}
		else
		{
			missing = bufferSize;
			d->underrun = true;
		}
	}
	haveBlock++;
	assert(haveBlock == block);

	/*
	 * If we don't have enough input to fill the block fully, it might be
	 * that the input stall occurred in the middle of a sample. For instance,
	 * if samples are 4 bytes long, it might be that we would have needed
	 * 13 more bytes to do a full refill.
	 *
	 * In this situation, there are four samples and one byte missing to
	 * refill the buffer - the one byte is what we need to care about here:
	 * on the next read, we'll have one byte too much (if we simply ignore
	 * the fact, we end up with misaligned reading, causing noise, or
	 * swapped stereo channels or similar).
	 *
	 * So we set dropBytes here, which is a variable which indicates how
	 * many bytes to drop away upon next refill.
	 */
	if(missing & (sampleSize - 1))
		dropBytes = missing & (sampleSize - 1);

	unsigned int i = 0, wrap = (block == 0)?0:sampleSize;
	if(bits == 16)
	{
		// wrap the last part of the buffer back to the beginning (history)
		while(i<wrap)
		{
			fbuffer[i/2] = fbuffer[(bufferSize+i)/2];
			i += 2;
		}

		// convert data from incoming
		if(d->endianness == littleEndian)
		{
			while(i<bufferSize+sampleSize-missing)
			{
				fbuffer[i/2] = conv_16_float(compose_16le(buffer[i],buffer[i+1]));
				i += 2;
			}
		}
		else
		{
			while(i<bufferSize+sampleSize-missing)
			{
				fbuffer[i/2] = conv_16_float(compose_16be(buffer[i],buffer[i+1]));
				i += 2;
			}
		}

		// fill up missing bytes with zero samples
		while(i<bufferSize+sampleSize)
		{
			fbuffer[i/2] = 0.0;
			i += 2;
		}
	}
	else if(bits == 8)
	{
		// wrap the last part of the buffer back to the beginning (history)
		while(i<wrap)
		{
			fbuffer[i] = fbuffer[bufferSize+i];
			i++;
		}

		// convert data from incoming
		while(i<bufferSize+sampleSize-missing)
		{
			fbuffer[i] = conv_8_float(buffer[i]);
			i++;
		}

		// fill up missing bytes with zero samples
		while(i<bufferSize+sampleSize)
		{
			fbuffer[i++] = 0.0;
		}
	}
	else
	{
		assert(false);
	}
}

#define RESAMPLER_STEP()							\
	pos += step;									\
	i++;											\
	while(pos >= bufferSamples)						\
	{												\
		pos -= bufferSamples;						\
		block++;									\
		ensureRefill();								\
	}

void Resampler::run(float *left, float *right, unsigned long samples)
{
	ensureRefill();
	unsigned long i = 0;
	double delta = step - floor(step);
	bool interpolate = fabs(delta) > 0.001;

	if(channels == 2 && interpolate)
	{
		while(i < samples)
		{
			double error = pos - floor(pos);
			unsigned long offset = 2*(unsigned long)pos;

			left[i]  = fbuffer[offset+0]*(1.0-error)+fbuffer[offset+2]*error;
			right[i] = fbuffer[offset+1]*(1.0-error)+fbuffer[offset+3]*error;
			RESAMPLER_STEP();
		}
	}
	else if(channels == 1 && interpolate)
	{
		while(i < samples)
		{
			double error = pos - floor(pos);
			unsigned long offset = (unsigned long)pos;

			left[i] = right[i] = fbuffer[offset]*(1.0-error)
							   + fbuffer[offset+1]*error;
			RESAMPLER_STEP();
		}
	}
	else if(channels == 2)
	{
		while(i < samples)
		{
			unsigned long offset = 2*(unsigned long)pos;

			left[i]  = fbuffer[offset+0];
			right[i] = fbuffer[offset+1];
			RESAMPLER_STEP();
		}
	}
	else if(channels == 1)
	{
		while(i < samples)
		{
			unsigned long offset = (unsigned long)pos;

			left[i] = right[i] = fbuffer[offset];
			RESAMPLER_STEP();
		}
	}
	else
	{
		assert(false);
	}
}

Refiller::~Refiller()
{
}

#undef RESAMPLER_STEP
#undef compose_16le
#undef compose_16be
#undef conv_16_float
#undef conv_8_float