summaryrefslogtreecommitdiffstats
path: root/flow/convert.cc
blob: d5ca2f8229cb0086ea90b7cb6aae6d0a0149aad3 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       stefan@space.twc.de

    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., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#include "convert.h"
#include <math.h>
#include <string.h>
#include <assert.h>
#include <config.h>

#ifdef HAVE_X86_FLOAT_INT
static inline long QRound (float inval)
{
#if (__GNUC__ == 3 && __GNUC_MINOR__ == 0)
  volatile
#warning "workaround for gcc-3.0 bug c++/2733 enabled"
#endif
  int ret;
  __asm__ ("fistpl %0" : "=m" (ret) : "t" (inval) : "st");
  return ret;
}
#else
static inline long QRound (float inval)
{
  return (long)inval;
}
#endif

/*---------------------------- new code begin -------------------------- */

#define compose_16le(ptr) \
	((((*((ptr)+1)+128)&0xff) << 8)+*(ptr))

#define compose_16be(ptr) \
	((((*(ptr)+128)&0xff) << 8)+*((ptr)+1))

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

#define convert_16le_float(x) conv_16_float(compose_16le(&(x)))
#define convert_16be_float(x) conv_16_float(compose_16be(&(x)))

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

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

#define convert_float_float(x) x

/*
 * 16le, 16be, 8, float
 */

#define datatype_16le unsigned char
#define datasize_16le 2

#define datatype_16be unsigned char
#define datasize_16be 2

#define datatype_8 unsigned char
#define datasize_8 1

#define datatype_float float
#define datasize_float 1

#define mk_converter(from_format,to_format) \
void Arts::convert_mono_ ## from_format ## _ ## to_format (unsigned long samples, \
									datatype_ ## from_format *from, \
                                    datatype_ ## to_format *to) \
{ \
	datatype_ ## to_format *end = to+samples * (datasize_ ## to_format); \
	while(to<end) { \
		*to = convert_ ## from_format ## _ ## to_format(*from); \
		from += datasize_ ## from_format; \
		to += datasize_ ## to_format; \
	} \
} \
void Arts::interpolate_mono_ ## from_format ## _ ## to_format (unsigned long samples,\
									double startpos, double speed, \
									datatype_ ## from_format *from, \
                                    datatype_ ## to_format *to) \
{ \
	double flpos = startpos; \
	while(samples) { \
		long position = ((long)(flpos)) * (datasize_ ## from_format); \
		double error = flpos - floor(flpos); \
		*to =	(convert_ ## from_format ## _ ## to_format(from[position])) * \
				(1.0-error) + (convert_ ## from_format ## _ ## \
				to_format(from[position + datasize_ ## from_format])) * error; \
		to += datasize_ ## to_format; \
		flpos += speed; \
		samples--; \
	} \
} \
void Arts::convert_stereo_i ## from_format ## _2 ## to_format (unsigned long samples,\
									datatype_ ## from_format *from, \
                                    datatype_ ## to_format *left, \
									datatype_ ## to_format *right) \
{ \
	datatype_ ## to_format *end = left+samples * (datasize_ ## to_format); \
	while(left<end) { \
		*left = convert_ ## from_format ## _ ## to_format(*from); \
		from += datasize_ ## from_format; \
		left += datasize_ ## to_format; \
		*right = convert_ ## from_format ## _ ## to_format(*from); \
		from += datasize_ ## from_format; \
		right += datasize_ ## to_format; \
	} \
} \
void Arts::interpolate_stereo_i ## from_format ## _2 ## to_format (unsigned long samples,\
									double startpos, double speed, \
									datatype_ ## from_format *from, \
                                    datatype_ ## to_format *left, \
                                    datatype_ ## to_format *right) \
{ \
	double flpos = startpos; \
	while(samples) { \
		long position = ((long)(flpos)) * (datasize_ ## from_format) * 2; \
		double error = flpos - floor(flpos); \
		*left =	(convert_ ## from_format ## _ ## to_format(from[position])) * \
				(1.0-error) + (convert_ ## from_format ## _ ## \
				to_format(from[position + 2*datasize_ ## from_format]))*error; \
		left += datasize_ ## to_format; \
		position += datasize_ ## from_format; \
		*right =(convert_ ## from_format ## _ ## to_format(from[position])) * \
				(1.0-error) + (convert_ ## from_format ## _ ## \
				to_format(from[position + 2*datasize_ ## from_format]))*error; \
		right += datasize_ ## to_format; \
		flpos += speed; \
		samples--; \
	} \
}

mk_converter(8,float)
mk_converter(16le,float)
mk_converter(16be,float)
mk_converter(float,float)

/*----------------------------- new code end --------------------------- */

/*
void old_convert_mono_8_float(unsigned long samples, unsigned char *from, float *to)
{
	float *end = to+samples;

	while(to<end) *to++ = conv_8_float(*from++);
}

void old_convert_mono_16le_float(unsigned long samples, unsigned char *from, float *to)
{
	float *end = to+samples;

	while(to<end)
	{
		*to++ = conv_16le_float(compose_16le(from));
		from += 2;
	}
}


void old_convert_stereo_i8_2float(unsigned long samples, unsigned char *from, float *left, float *right)
{
	float *end = left+samples;
	while(left < end)
	{
		*left++ = conv_8_float(*from++);
		*right++ = conv_8_float(*from++);
	}
}

void old_convert_stereo_i16le_2float(unsigned long samples, unsigned char *from, float *left, float *right)
{
	float *end = left+samples;
	while(left < end)
	{
		*left++ = conv_16le_float(compose_16le(from));
		*right++ = conv_16le_float(compose_16le(from+2));
		from += 4;
	}
}
*/

void Arts::convert_stereo_2float_i16le(unsigned long samples, float *left, float *right, unsigned char *to)
{
	float *end = left+samples;
	long syn;

	while(left < end)
	{
		syn = QRound((*left++)*32767);

		if(syn < -32768) syn = -32768;				/* clipping */
		if(syn > 32767) syn = 32767;

		*to++ = syn & 0xff;
		*to++ = (syn >> 8) & 0xff;

		syn = QRound((*right++)*32767);

		if(syn < -32768) syn = -32768;				/* clipping */
		if(syn > 32767) syn = 32767;

		*to++ = syn & 0xff;
		*to++ = (syn >> 8) & 0xff;
	}	
}

void Arts::convert_mono_float_16le(unsigned long samples, float *from, unsigned char *to)
{
	float *end = from+samples;

	while(from < end)
	{
		long syn = QRound((*from++)*32767);

		if(syn < -32768) syn = -32768;				/* clipping */
		if(syn > 32767) syn = 32767;

		*to++ = syn & 0xff;
		*to++ = (syn >> 8) & 0xff;
	}	
}

void Arts::convert_stereo_2float_i16be(unsigned long samples, float *left, float *right, unsigned char *to)
{
	float *end = left+samples;
	long syn;

	while(left < end)
	{
		syn = QRound((*left++)*32767);

		if(syn < -32768) syn = -32768;				/* clipping */
		if(syn > 32767) syn = 32767;

		*to++ = (syn >> 8) & 0xff;
		*to++ = syn & 0xff;

		syn = QRound((*right++)*32767);

		if(syn < -32768) syn = -32768;				/* clipping */
		if(syn > 32767) syn = 32767;

		*to++ = (syn >> 8) & 0xff;
		*to++ = syn & 0xff;
	}	
}

void Arts::convert_mono_float_16be(unsigned long samples, float *from, unsigned char *to)
{
	float *end = from+samples;

	while(from < end)
	{
		long syn = QRound((*from++)*32767);

		if(syn < -32768) syn = -32768;				/* clipping */
		if(syn > 32767) syn = 32767;

		*to++ = (syn >> 8) & 0xff;
		*to++ = syn & 0xff;
	}	
}

void Arts::convert_stereo_2float_i8(unsigned long samples, float *left, float *right, unsigned char *to)
{
	float *end = left+samples;
	long syn;

	while(left < end)
	{
		syn = (int)((*left++)*127+128);

		if(syn < 0) syn = 0;					/* clipping */
		if(syn > 255) syn = 255;

		*to++ = syn;

		syn = (int)((*right++)*127+128);

		if(syn < 0) syn = 0;					/* clipping */
		if(syn > 255) syn = 255;

		*to++ = syn;
	}	
}

void Arts::convert_mono_float_8(unsigned long samples, float *from, unsigned char *to)
{
	float *end = from+samples;

	while(from < end)
	{
		long syn = (int)((*from++)*127+128);

		if(syn < 0) syn = 0;					/* clipping */
		if(syn > 255) syn = 255;

		*to++ = syn;
	}	
}

unsigned long Arts::uni_convert_stereo_2float(
		unsigned long samples,		// number of required samples
		unsigned char *from,		// buffer containing the samples
		unsigned long fromLen,		// length of the buffer
	    unsigned int fromChannels,  // channels stored in the buffer
		unsigned int fromBits,		// number of bits per sample
	    float *left, float *right,	// output buffers for left and right channel
		double speed,				// speed (2.0 means twice as fast)
		double startposition		// startposition
	)
{
	unsigned long doSamples = 0, sampleSize = fromBits/8;

	if(fromBits == uni_convert_float_ne)
		sampleSize = sizeof(float);

	// how many samples does the from-buffer contain?
	double allSamples = fromLen / (fromChannels * sampleSize);

	// how many samples are remaining?
	//    subtract one due to interpolation and another against rounding errors
	double fHaveSamples = allSamples - startposition - 2.0;
	fHaveSamples /= speed;

	// convert do "how many samples to do"?
	if(fHaveSamples > 0)
	{
		doSamples = (int)fHaveSamples;
		if(doSamples > samples) doSamples = samples;
	}

	// do conversion
	if(doSamples)
	{
		if(fromChannels == 1)
		{
			if(fromBits == uni_convert_float_ne) {
				interpolate_mono_float_float(doSamples,
							startposition,speed,(float *)from,left);
			}
			else if(fromBits == uni_convert_s16_be) {
				interpolate_mono_16be_float(doSamples,
							startposition,speed,from,left);
			}
			else if(fromBits == uni_convert_s16_le) {
				interpolate_mono_16le_float(doSamples,
							startposition,speed,from,left);
			}
			else {
				interpolate_mono_8_float(doSamples,
							startposition,speed,from,left);
			}
			memcpy(right,left,sizeof(float)*doSamples);
		}
		else if(fromChannels == 2)
		{
			if(fromBits == uni_convert_float_ne) {
				interpolate_stereo_ifloat_2float(doSamples,
							startposition,speed,(float *)from,left,right);
			}
			else if(fromBits == uni_convert_s16_be) {
				interpolate_stereo_i16be_2float(doSamples,
							startposition,speed,from,left,right);
			}
			else if(fromBits == uni_convert_s16_le) {
				interpolate_stereo_i16le_2float(doSamples,
							startposition,speed,from,left,right);
			}
			else {
				interpolate_stereo_i8_2float(doSamples,
							startposition,speed,from,left,right);
			}
		} else {
			assert(false);
		}
	}
	return doSamples;
}

// undefine all that stuff (due to --enable-final)
#undef compose_16le
#undef compose_16be
#undef conv_16_float
#undef convert_16le_float
#undef convert_16be_float
#undef conv_8_float
#undef convert_8_float
#undef convert_float_float
#undef datatype_16le
#undef datasize_16le
#undef datatype_16be
#undef datasize_16be
#undef datatype_8
#undef datasize_8
#undef datatype_float
#undef datasize_float
#undef mk_converter