File: tX_audiofile.cc

package info (click to toggle)
terminatorx 4.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 4,344 kB
  • sloc: cpp: 13,085; ansic: 3,738; sh: 1,263; makefile: 128; xml: 113; awk: 3
file content (754 lines) | stat: -rw-r--r-- 17,689 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
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
    terminatorX - realtime audio scratching software
    Copyright (C) 1999-2016  Alexander König
 
    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 2 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/>.
 
    File: tX_audiofile.cc
 
    Description: This implements the new audiofile class. Unlike earlier
    		 versions of terminatorX where wav_read.c was evilishly
		 "#ifdef"ed to support multiple loading strategies this new
		 design allows multiple load_*()-methods/strategies at
		 the same time. (e.g. tx can now try to load a wavfile
		 with the builtin routines and if that fails it'll try
		 to load the file through sox (if available)).
*/   


#include "tX_audiofile.h"

#include <string.h>
#include <malloc.h>
#include "wav_file.h"
#include "tX_loaddlg.h"
#include "tX_endian.h"

#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdint.h>

#ifdef USE_MAD_INPUT
#	include <mad.h>
#	include <sys/types.h>
#	include <unistd.h>
#	ifndef _POSIX_MAPPED_FILES
#		define _POSIX_MAPPED_FILES
#	endif
#	include <sys/stat.h>
#	include <fcntl.h>
#	include <sys/mman.h>
#endif

#ifdef USE_VORBIS_INPUT
#	include <vorbis/codec.h>
#	include <vorbis/vorbisfile.h>
#	include <errno.h>
#	include <sys/stat.h>
#	include <fcntl.h>
#	include <sys/mman.h>
#endif

#ifdef USE_AUDIOFILE_INPUT
#	include <audiofile.h>
#endif

tx_audiofile :: tx_audiofile()
{
	mem_type=TX_AUDIO_UNDEFINED;
	file_type=TX_FILE_UNDEFINED;
	file=NULL;
	strcpy(filename,"");
	mem=NULL;
	no_samples=0;
	sample_rate=44100;
}

void tx_audiofile :: figure_file_type()
{
	char *ext;
	
	ext=strrchr(filename, (int) '.');
	
	if (ext) {
		if (strlen(ext) >3) {
			ext++;
			if (!strcasecmp("wav", ext)) file_type=TX_FILE_WAV;
			else if (!strncasecmp("mp", ext, 2)) file_type=TX_FILE_MPG123;
			else if (!strncasecmp("ogg", ext, 2)) file_type=TX_FILE_OGG123;
		}
	}
}

tX_audio_error tx_audiofile :: load(char *p_file_name)
{
	tX_audio_error load_err=TX_AUDIO_ERR_NOT_SUPPORTED;
	
	strcpy(filename, p_file_name);
	
	ld_set_progress(0);
		
	figure_file_type();
	
	if (mem) { free(mem); mem=NULL; }

#ifdef USE_AUDIOFILE_INPUT
	if ((load_err) && (file_type!=TX_FILE_MPG123) && (file_type!=TX_FILE_OGG123)) {
		load_err=load_af();
	}
#endif
	
#ifdef USE_BUILTIN_WAV
	if ((load_err) && (file_type==TX_FILE_WAV)) {
		load_err=load_wav();	
//		if (load_err==TX_AUDIO_SUCCESS) return(load_err);
	}
#endif	

#ifdef USE_MAD_INPUT
	if ((load_err) && (file_type==TX_FILE_MPG123)) {
		load_err=load_mad();
		//if (load_err==TX_AUDIO_SUCCESS) return(load_err);
	}
#endif	
	
#ifdef USE_MPG123_INPUT
	if ((load_err) && (file_type==TX_FILE_MPG123)) {
		load_err=load_mpg123();
		//return(load_err);
	}
#endif	

#ifdef USE_VORBIS_INPUT
	if ((load_err) && (file_type==TX_FILE_OGG123)) {
		load_err=load_vorbis();
		//if (load_err==TX_AUDIO_SUCCESS) return(load_err);
	}
#endif
	
#ifdef USE_OGG123_INPUT
	if ((load_err) && (file_type==TX_FILE_OGG123)) {
		load_err=load_ogg123();
		//return(load_err);
	}
#endif

#ifdef USE_SOX_INPUT
	if (load_err) {
		load_err=load_sox();
	}
#endif	

	if (!load_err) {
		tX_debug("tx_audiofile::load() Set samplerate to %i for file %s.", sample_rate, filename);
	}
	return(load_err);
}

tx_audiofile :: ~tx_audiofile() {
	if (mem) {
		free(mem);
	}
 }
 
#ifdef NEED_PIPED 

tX_audio_error tx_audiofile :: load_piped()
{
	int16_t *data=NULL;
	ssize_t allbytes=0;
	ssize_t prev_bytes;
	ssize_t bytes=1;
	unsigned char buffer[SOX_BLOCKSIZE];
	unsigned char *ptr;
	
	/* Irritating the user... */
	ld_set_progress(0.5);
	mem_type=TX_AUDIO_LOAD;	
	
	while (bytes) {
		bytes = fread(buffer, 1, SOX_BLOCKSIZE, file);
		
		if (bytes>0) {
			prev_bytes=allbytes;
			allbytes+=bytes;
			int16_t *new_data=(int16_t *) realloc(data, allbytes);
			//printf("All: %i, Bytes: %i, new: %08x, old: %08x\n", allbytes, bytes, new_data, data);
			
			if (!new_data) {
				pclose(file); file=NULL;
				if (data) free(data);
				data=NULL;
				return TX_AUDIO_ERR_ALLOC;
			}
			
			data=new_data;
			ptr=(unsigned char *) data;
			memcpy((void *) &ptr[prev_bytes],(void *) buffer, bytes);
		}
	}
	
	pclose(file); file=NULL;
	
	if (!allbytes) {
		// If we get here we read zero Bytes...
		if (data) free(data);
		return TX_AUDIO_ERR_PIPE_READ;
	}
	
	no_samples=allbytes/sizeof(int16_t);
	mem=data;
	
	/* Irritating the user just a little more ;)*/
	ld_set_progress(1.0);
	
	return TX_AUDIO_SUCCESS;
}

#endif
	
#ifdef USE_SOX_INPUT
tX_audio_error tx_audiofile :: load_sox() {
	tX_debug("tx_audiofile::load_sox()");

	char command[PATH_MAX*2];

	sprintf(command, SOX_STR, filename);
	file = popen(command, "r");
	
	if (!file) return TX_AUDIO_ERR_SOX;
	
	return load_piped();
	
}
#endif	

#ifdef USE_MPG123_INPUT
tX_audio_error tx_audiofile :: load_mpg123() {
	tX_debug("tx_audiofile::load_mpg123()");
	
	char command[PATH_MAX*2];
	
	sprintf(command, MPG123_STR, filename);
	file = popen(command, "r");
	
	if (!file) return TX_AUDIO_ERR_MPG123;
	
	return load_piped();
}
#endif	

#ifdef USE_OGG123_INPUT
tX_audio_error tx_audiofile :: load_ogg123() {
	tX_debug("tx_audiofile::load_ogg123()");
	
	char command[PATH_MAX*2];

	sprintf(command, OGG123_STR, filename);
	file = popen(command, "r");

	if (!file) return TX_AUDIO_ERR_OGG123;

	return load_piped();
}
#endif

#ifdef USE_BUILTIN_WAV
tX_audio_error tx_audiofile :: load_wav() {
	tX_debug("tx_audiofile::load_wav()");
	
	wav_sig wav_in;
	int16_t *data;
	int16_t *p;
	ssize_t allbytes=0;
	ssize_t bytes=0;

	mem_type=TX_AUDIO_LOAD;
	
	if (!init_wav_read(filename, &wav_in))
	{
		return(TX_AUDIO_ERR_WAV_NOTFOUND);
	}

	if (wav_in.depth != 16)
	{
		return(TX_AUDIO_ERR_NOT_16BIT);
	}

	if (wav_in.chans != 1)
	{
		return(TX_AUDIO_ERR_NOT_MONO);
	}

	sample_rate=wav_in.srate;
	
	memsize=wav_in.len;
	data = (int16_t *) malloc (memsize);
		
	if (!data)
	{
		return(TX_AUDIO_ERR_ALLOC);
	}

	p=data;
#ifdef ENABLE_DEBUG_OUTPUT
	int output=1;
#endif	
	while (wav_in.len>allbytes)
	{	
		bytes = fread(p, 1, tX_min(1024, wav_in.len-allbytes), wav_in.handle);

#ifdef ENABLE_DEBUG_OUTPUT
		if (output) { tX_debug("tX_audiofile::load_wav() read %zu Bytes [%04x %04x %04x %04x %04x %04x ..]", bytes, (unsigned int) p[0],  (unsigned int) p[1], (unsigned int) p[2], (unsigned int) p[3], (unsigned int) p[4], (unsigned int) p[5]); }
#endif

		if (bytes<=0) {
			free(data);
			return (TX_AUDIO_ERR_WAV_READ);
		}

#ifdef BIG_ENDIAN_MACHINE
		swapbuffer(p, bytes/sizeof(int16_t));
#	ifdef ENABLE_DEBUG_OUTPUT
		if (output) { tX_debug("tX_audiofile::load_wav() swapped %i Bytes [%04x %04x %04x %04x %04x %04x ..]",
		bytes, (unsigned int) p[0],  (unsigned int) p[1], (unsigned int) p[2], (unsigned int) p[3], (unsigned int) p[4], (unsigned int) p[5]); }
#	endif
#endif		

#ifdef ENABLE_DEBUG_OUTPUT
		output=0;
#endif

		allbytes+=bytes;
		
		ld_set_progress((float) allbytes/(float)wav_in.len);
		
		p+=(ssize_t) bytes/sizeof(int16_t);
	}
	
	wav_close(wav_in.handle);

	mem=data;
	no_samples=memsize/sizeof(int16_t);
	
	return (TX_AUDIO_SUCCESS);
}
#endif

#ifdef USE_MAD_INPUT
tX_audio_error tx_audiofile::load_mad() {
	tX_debug("tx_audiofile::load_mad()");
	
	struct stat stat_dat;
	int fd;
	int res;
	void *mp3_file;
	
	fd=open(filename, O_RDONLY);
	if (!fd) return TX_AUDIO_ERR_MAD_OPEN;
	
	if (fstat(fd, &stat_dat) == -1 ||
      stat_dat.st_size == 0) {
		return TX_AUDIO_ERR_MAD_STAT;
	}
	
	mp3_file = mmap(0, stat_dat.st_size, PROT_READ, MAP_SHARED, fd, 0);
	
	if (mp3_file == MAP_FAILED) {
		return TX_AUDIO_ERR_MAD_MMAP;
	}
	
	res=mad_decode((const unsigned char *) mp3_file, stat_dat.st_size);
	
	if (res) {
		return TX_AUDIO_ERR_MAD_DECODE;
	}
	
	mem_type=TX_AUDIO_LOAD;
	
	if (munmap(mp3_file, stat_dat.st_size) == -1) {
		return TX_AUDIO_ERR_MAD_MUNMAP;
	}
	
	return TX_AUDIO_SUCCESS;
}

#define TX_MAD_BLOCKSIZE 8096

typedef struct {
	const unsigned char *start;
	const unsigned char *end;
	const unsigned char *last_frame;
	bool first_call;
	ssize_t size;
	int16_t *target_buffer;
	unsigned int target_samples;
	unsigned int current_sample;
	unsigned int sample_rate;
	unsigned int lost_sync_counter;
} tX_mad_buffer;

const unsigned char *last_current=NULL;

static enum mad_flow tX_mad_input(void *data, struct mad_stream *stream) {
	tX_mad_buffer *buffer = (tX_mad_buffer *) data;
	ssize_t bs;
	unsigned int pos;

	if (buffer->first_call) {
		bs=tX_min(TX_MAD_BLOCKSIZE, buffer->size);
		mad_stream_buffer(stream, buffer->start, bs);
		buffer->first_call=false;
		return MAD_FLOW_CONTINUE;
	} else {
		if (!stream->next_frame) return MAD_FLOW_STOP;
		
		pos=stream->next_frame-buffer->start;
		bs=tX_min(TX_MAD_BLOCKSIZE, buffer->size-pos);
		//tX_debug("last: %08x, new %08x, bs: %i, pos: %i",  buffer->last_frame, stream->next_frame, bs, pos);
		
		mad_stream_buffer(stream, stream->next_frame, bs);
		if (stream->next_frame==buffer->last_frame) {
			//tX_debug("tX_mad_input(): No new frame? Stopping.");
			return MAD_FLOW_STOP;
		}
		ld_set_progress((float) pos/(float) buffer->size);
		buffer->last_frame=stream->next_frame;

		return MAD_FLOW_CONTINUE;
	}
}

static enum mad_flow tX_mad_error(void *data, struct mad_stream *stream, struct mad_frame *frame) {
	tX_mad_buffer *buffer = (tX_mad_buffer *) data;
	if (MAD_RECOVERABLE(stream->error)) {
		if ((stream->error==MAD_ERROR_LOSTSYNC) && (buffer->lost_sync_counter<3)) {
			/* Ignore at least two sync errors to not annoy people
			   about ID3 tags.
			*/
			buffer->lost_sync_counter++;
			return MAD_FLOW_CONTINUE;
		}
		tX_warning("mad reported stream error (%i) '%s'.", stream->error, mad_stream_errorstr(stream));
		return MAD_FLOW_CONTINUE;
	}
	tX_error("mad reported fatal stream error (%i) '%s'.", stream->error, mad_stream_errorstr(stream));
	return MAD_FLOW_STOP;
}

/* From minimad.c of mad */
static inline signed int scale(mad_fixed_t sample) {
//#ifdef BIG_ENDIAN_MACHINE
//	swap32_inline(&sample);
//#endif
  /* round */
  sample += (1L << (MAD_F_FRACBITS - 16));

  /* clip */
  if (sample >= MAD_F_ONE)
    sample = MAD_F_ONE - 1;
  else if (sample < -MAD_F_ONE)
    sample = -MAD_F_ONE;

  /* quantize */
  return sample >> (MAD_F_FRACBITS + 1 - 16);
}

static enum mad_flow tX_mad_output(void *data, struct mad_header const *header, struct mad_pcm *pcm) {
	tX_mad_buffer *buffer=(tX_mad_buffer *) data;
	unsigned int nchannels, nsamples;
	mad_fixed_t const *left_ch, *right_ch;	

	nchannels = pcm->channels;
	nsamples  = pcm->length;
	left_ch   = pcm->samples[0];
	right_ch  = pcm->samples[1];
	buffer->sample_rate = pcm->samplerate;
	
	buffer->target_samples+=nsamples;

	buffer->target_buffer=(int16_t *) realloc(buffer->target_buffer, buffer->target_samples<<1);
	if (!buffer->target_buffer) { 
			tX_error("tX_mad_output(): Failed allocating sample memory!\n");
			return MAD_FLOW_STOP;
	}
		
	while (nsamples--) {
		signed int sample;

		if (nchannels==1) {
			sample=scale(*left_ch++);
		} else {
			double sample_l=(double) (*left_ch++);
			double sample_r=(double) (*right_ch++); 
			double res=(sample_l+sample_r)/2.0;
			mad_fixed_t mad_res=(mad_fixed_t) res;
			
			sample=scale(mad_res);
		}
		
		buffer->target_buffer[buffer->current_sample]=sample;
		buffer->current_sample++;
	}

	return MAD_FLOW_CONTINUE;
}

int tx_audiofile::mad_decode(unsigned char const *start, unsigned long length) {
	tX_mad_buffer buffer;
	struct mad_decoder decoder;
	int result;

	buffer.start  = start;
	buffer.end = &start[length];
	buffer.last_frame = NULL;
	buffer.size = length;
	buffer.target_buffer = NULL;
	buffer.target_samples = 0;
	buffer.current_sample = 0;
	buffer.first_call=true;
	buffer.sample_rate=0;
	buffer.lost_sync_counter=0;

	tX_debug("tx_audiofile::mad_decode() - start %016" PRIxPTR ", length %zu", (uintptr_t) buffer.start, buffer.size);
	/* configure input, output, and error functions */

	mad_decoder_init(&decoder, &buffer, tX_mad_input, NULL, NULL, tX_mad_output, tX_mad_error, NULL);
	result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);

	if (!result) {
		this->mem=buffer.target_buffer;
		this->no_samples=buffer.target_samples;
	} else {
		if (buffer.target_buffer) free(buffer.target_buffer);
	}
	
	/* release the decoder */
	mad_decoder_finish(&decoder);  

	sample_rate=buffer.sample_rate;
  return result;
}

#endif

/* AARG - OGG loading is not threadsafe !*/
size_t tX_ogg_file_size;
long tX_ogg_file_read;

#ifdef USE_VORBIS_INPUT
typedef struct {
  size_t (*read_func) (void *, size_t, size_t, FILE *);
  int  (*seek_func) (void *, long, int);
  int (*close_func) (FILE *);
  long (*tell_func) (FILE *);
} tX_ov_callbacks;

int ogg_seek(void *ptr, long offset, int whence) {
	/* ogg shall not seek ! */
	return -1;
	errno=EBADF;
}

size_t ogg_read(void  *ptr, size_t size, size_t nmemb, FILE *stream) {
	size_t ret_val;
	ret_val=fread(ptr, size, nmemb, stream);
	if (ret_val>0) {
		tX_ogg_file_read+=ret_val*size;
		ld_set_progress((double) tX_ogg_file_read/(double) tX_ogg_file_size);	
	}
	return ret_val;
}

#define VORBIS_BUFF_SIZE 4096 /*recommended*/

tX_audio_error tx_audiofile::load_vorbis() {
	tX_debug("tx_audiofile::load_vorbis()");
	
	/*  VORBIS Callbacks */
	ov_callbacks org_callbacks;
	/* evil casting - to make g++ shut up */
	tX_ov_callbacks *callbacks=(tX_ov_callbacks *) &org_callbacks;
	
	/* buffer */
	char pcmout[VORBIS_BUFF_SIZE];
	OggVorbis_File vf;
	bool eof=false;
	int current_section=0;
	unsigned int i;
	struct stat stat_dat;
		
	callbacks->read_func=ogg_read;
	callbacks->seek_func=ogg_seek;
	callbacks->close_func=fclose;
	callbacks->tell_func=ftell;
	
	file=fopen(filename, "r");
	if (!file) {
		return TX_AUDIO_ERR_WAV_NOTFOUND;		
	}
	
	if (fstat(fileno(file), &stat_dat)  == -1 || stat_dat.st_size == 0) {
		return TX_AUDIO_ERR_MAD_STAT;
	}
	
	tX_ogg_file_size=stat_dat.st_size;
	tX_ogg_file_read=0;
	
	int res=ov_open_callbacks((void *) file, &vf, NULL, 0, org_callbacks);
	if (res<0) {
		fclose(file); 
		file=NULL;
		return TX_AUDIO_ERR_VORBIS_OPEN;		
	}
	
	vorbis_info *vi=ov_info(&vf,-1);
	sample_rate=vi->rate;

	unsigned int channels=vi->channels;	
	unsigned int samples_read=0;
	unsigned int mono_samples;
	int16_t* data=NULL;
	size_t bytes=0;
	
	while((!eof) && (!current_section)) {
#ifdef BIG_ENDIAN_MACHINE
#	define ENDIANP 1
#else
#	define ENDIANP 0		
#endif
		long ret=ov_read(&vf,pcmout,VORBIS_BUFF_SIZE,ENDIANP,2,1,&current_section);
		if (ret == 0) {
			eof=true;
		} else if (ret < 0) {
		  /* ignore stream errors */
		} else {
			int16_t *new_data;
			bytes+=ret;
			new_data=(int16_t *) realloc(data, bytes/channels);
			if (!new_data) {
				if (data) free(data);
				return TX_AUDIO_ERR_ALLOC;
			}
			
			data=new_data;
			int16_t *src=(int16_t *) &pcmout;
			
			switch (channels) {
				case 1:
					mono_samples=ret/2;
					for (i=0; i<mono_samples; i++) {
						data[samples_read+i]=src[i];
					}
					break;
					
				case 2:
					mono_samples=ret/4;
					for (i=0; i<mono_samples; i++) {
						double l_value, r_value;
						l_value=src[i*2];
						r_value=src[i*2+1];
						data[samples_read+i]=(int16_t) ((l_value+r_value)/2.0);
					}
					break;
					
				default:
					mono_samples=(ret/2)/channels;
					for (i=0; i<mono_samples; i++) {
						double value=0.0;
				
						for (unsigned int c=0; c<channels; c++) {
							value+=(double) src[i*channels+c];
						}
						value/=(double) channels;
						data[samples_read+i]=(int16_t) value;
					}		
			}
			samples_read+=mono_samples;
		}
    }
	
	ov_clear(&vf);
	
	mem=(int16_t *) data;
	no_samples=samples_read;
	
	if (no_samples==0) {
		if (mem) free(mem);
		mem=NULL;
		return TX_AUDIO_ERR_VORBIS_NODATA;
	}
	
	return TX_AUDIO_SUCCESS;
}
#endif

#ifdef USE_AUDIOFILE_INPUT
#define TX_AF_SAMPLES_PER_BLOCK 2048

tX_audio_error tx_audiofile::load_af() {
	tX_debug("tx_audiofile::load_af()");
	
	AFfilehandle af_file;
	AFframecount	all_frames, frames_read=0, current_frames=1;
	int16_t *data=NULL;
	
	af_file = afOpenFile(filename, "r", NULL);
	if (af_file==AF_NULL_FILEHANDLE) {
		return TX_AUDIO_ERR_AF_OPEN;
	}
	
	all_frames=afGetFrameCount(af_file, AF_DEFAULT_TRACK);
	sample_rate=(unsigned int) afGetRate(af_file, AF_DEFAULT_TRACK);
	afSetVirtualChannels(af_file, AF_DEFAULT_TRACK, 1);
	afSetVirtualSampleFormat(af_file, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); // 2 == 16 Bit?
#ifdef BIG_ENDIAN_MACHINE
	afSetVirtualByteOrder(af_file, AF_DEFAULT_TRACK, AF_BYTEORDER_BIGENDIAN);
#else
	afSetVirtualByteOrder(af_file, AF_DEFAULT_TRACK, AF_BYTEORDER_LITTLEENDIAN);
#endif
	
	while (current_frames) {
		int16_t *new_data;
		
		new_data=(int16_t*) realloc(data, (frames_read+TX_AF_SAMPLES_PER_BLOCK)*2);
		if (!new_data) {
			if (data) free(data);
			afCloseFile(af_file);
			return TX_AUDIO_ERR_ALLOC;
		}
		data=new_data;
		current_frames=afReadFrames(af_file,AF_DEFAULT_TRACK,(void *) &data[frames_read],TX_AF_SAMPLES_PER_BLOCK);
		frames_read+=current_frames;
		ld_set_progress(((double) frames_read)/((double) all_frames));
	}
	afCloseFile(af_file);
	
	if (!frames_read) {
		if (data) free(data);
		return TX_AUDIO_ERR_AF_NODATA;
	}
	
	/* shorten to the actually read size of samples */
	if (!realloc(data, frames_read*2)) {
			if (data) free(data);
			return TX_AUDIO_ERR_ALLOC;
	}
	
	mem=data;
	no_samples=frames_read;
	
	return TX_AUDIO_SUCCESS;
}
#endif