2
terminatorX - realtime audio scratching software
3
Copyright (C) 1999-2003 Alexander K�nig
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
15
You should have received a copy of the GNU General Public License
16
along with this program; if not, write to the Free Software
17
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
Description: This implements the new audiofile class. Unlike earlier
22
versions of terminatorX where wav_read.c was evilishly
23
"#ifdef"ed to support multiple loading strategies this new
24
design allows multiple load_*()-methods/strategies at
25
the same time. (e.g. tx can now try to load a wavfile
26
with the builtin routines and if that fails it'll try
27
to load the file through sox (if available)).
31
13 Sept 2002 - Wrote a seperate loading routine to be used with
32
libmad, which is significantly better then piping mpg?1?.
33
Rewrote load_piped to use realloc() instead - much easier,
34
faster and uses less memory - wish I'd known about realloc()
35
when coding load_piped() for the first time ;)
39
#include "tX_audiofile.h"
44
#include "tX_loaddlg.h"
45
#include "tX_endian.h"
49
# include <sys/types.h>
51
# ifndef _POSIX_MAPPED_FILES
52
# define _POSIX_MAPPED_FILES
54
# include <sys/stat.h>
56
# include <sys/mman.h>
59
#ifdef USE_VORBIS_INPUT
60
# include <vorbis/codec.h>
61
# include <vorbis/vorbisfile.h>
63
# include <sys/stat.h>
65
# include <sys/mman.h>
68
#ifdef USE_AUDIOFILE_INPUT
69
# include <audiofile.h>
72
#define min(a,b) ((a) < (b) ? (a) : (b))
74
tx_audiofile :: tx_audiofile()
76
mem_type=TX_AUDIO_UNDEFINED;
77
file_type=TX_FILE_UNDEFINED;
85
void tx_audiofile :: figure_file_type()
89
ext=strrchr(filename, (int) '.');
94
if (!strcasecmp("wav", ext)) file_type=TX_FILE_WAV;
95
else if (!strncasecmp("mp", ext, 2)) file_type=TX_FILE_MPG123;
96
else if (!strncasecmp("ogg", ext, 2)) file_type=TX_FILE_OGG123;
101
tX_audio_error tx_audiofile :: load(char *p_file_name)
103
tX_audio_error load_err=TX_AUDIO_ERR_NOT_SUPPORTED;
105
strcpy(filename, p_file_name);
111
if (mem) { free(mem); mem=NULL; }
113
#ifdef USE_AUDIOFILE_INPUT
114
if ((load_err) && (file_type!=TX_FILE_MPG123) && (file_type!=TX_FILE_OGG123)) {
119
#ifdef USE_BUILTIN_WAV
120
if ((load_err) && (file_type==TX_FILE_WAV)) {
122
// if (load_err==TX_AUDIO_SUCCESS) return(load_err);
127
if ((load_err) && (file_type==TX_FILE_MPG123)) {
129
//if (load_err==TX_AUDIO_SUCCESS) return(load_err);
133
#ifdef USE_MPG123_INPUT
134
if ((load_err) && (file_type==TX_FILE_MPG123)) {
135
load_err=load_mpg123();
140
#ifdef USE_VORBIS_INPUT
141
if ((load_err) && (file_type==TX_FILE_OGG123)) {
142
load_err=load_vorbis();
143
//if (load_err==TX_AUDIO_SUCCESS) return(load_err);
147
#ifdef USE_OGG123_INPUT
148
if ((load_err) && (file_type==TX_FILE_OGG123)) {
149
load_err=load_ogg123();
161
tX_debug("tx_audiofile::load() Set samplerate to %i for file %s.", sample_rate, filename);
166
tx_audiofile :: ~tx_audiofile() {
174
tX_audio_error tx_audiofile :: load_piped()
180
unsigned char buffer[SOX_BLOCKSIZE];
183
/* Irritating the user... */
184
ld_set_progress(0.5);
185
mem_type=TX_AUDIO_LOAD;
188
bytes = fread(buffer, 1, SOX_BLOCKSIZE, file);
193
int16_t *new_data=(int16_t *) realloc(data, allbytes);
194
//printf("All: %i, Bytes: %i, new: %08x, old: %08x\n", allbytes, bytes, new_data, data);
197
pclose(file); file=NULL;
198
if (data) free(data);
200
return TX_AUDIO_ERR_ALLOC;
204
ptr=(unsigned char *) data;
205
memcpy((void *) &ptr[prev_bytes],(void *) buffer, bytes);
209
pclose(file); file=NULL;
212
// If we get here we read zero Bytes...
213
if (data) free(data);
214
return TX_AUDIO_ERR_PIPE_READ;
217
no_samples=allbytes/sizeof(int16_t);
220
/* Irritating the user just a little more ;)*/
221
ld_set_progress(1.0);
223
return TX_AUDIO_SUCCESS;
229
tX_audio_error tx_audiofile :: load_sox() {
230
tX_debug("tx_audiofile::load_sox()");
232
char command[PATH_MAX*2];
234
sprintf(command, SOX_STR, filename);
235
file = popen(command, "r");
237
if (!file) return TX_AUDIO_ERR_SOX;
244
#ifdef USE_MPG123_INPUT
245
tX_audio_error tx_audiofile :: load_mpg123() {
246
tX_debug("tx_audiofile::load_mpg123()");
248
char command[PATH_MAX*2];
250
sprintf(command, MPG123_STR, filename);
251
file = popen(command, "r");
253
if (!file) return TX_AUDIO_ERR_MPG123;
259
#ifdef USE_OGG123_INPUT
260
tX_audio_error tx_audiofile :: load_ogg123() {
261
tX_debug("tx_audiofile::load_ogg123()");
263
char command[PATH_MAX*2];
265
sprintf(command, OGG123_STR, filename);
266
file = popen(command, "r");
268
if (!file) return TX_AUDIO_ERR_OGG123;
274
#ifdef USE_BUILTIN_WAV
275
tX_audio_error tx_audiofile :: load_wav() {
276
tX_debug("tx_audiofile::load_wav()");
284
mem_type=TX_AUDIO_LOAD;
286
if (!init_wav_read(filename, &wav_in))
288
return(TX_AUDIO_ERR_WAV_NOTFOUND);
291
if (wav_in.depth != 16)
293
return(TX_AUDIO_ERR_NOT_16BIT);
296
if (wav_in.chans != 1)
298
return(TX_AUDIO_ERR_NOT_MONO);
301
sample_rate=wav_in.srate;
304
data = (int16_t *) malloc (memsize);
308
return(TX_AUDIO_ERR_ALLOC);
312
#ifdef ENABLE_DEBUG_OUTPUT
315
while (wav_in.len>allbytes)
317
bytes = fread(p, 1, min(1024, wav_in.len-allbytes), wav_in.handle);
319
#ifdef ENABLE_DEBUG_OUTPUT
320
if (output) { tX_debug("tX_audiofile::load_wav() read %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]); }
325
return (TX_AUDIO_ERR_WAV_READ);
328
#ifdef BIG_ENDIAN_MACHINE
329
swapbuffer(p, bytes/sizeof(int16_t));
330
# ifdef ENABLE_DEBUG_OUTPUT
331
if (output) { tX_debug("tX_audiofile::load_wav() swapped %i Bytes [%04x %04x %04x %04x %04x %04x ..]",
332
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]); }
336
#ifdef ENABLE_DEBUG_OUTPUT
342
ld_set_progress((float) allbytes/(float)wav_in.len);
344
p+=(ssize_t) bytes/sizeof(int16_t);
347
wav_close(wav_in.handle);
350
no_samples=memsize/sizeof(int16_t);
352
return (TX_AUDIO_SUCCESS);
357
tX_audio_error tx_audiofile::load_mad() {
358
tX_debug("tx_audiofile::load_mad()");
360
struct stat stat_dat;
365
fd=open(filename, O_RDONLY);
366
if (!fd) return TX_AUDIO_ERR_MAD_OPEN;
368
if (fstat(fd, &stat_dat) == -1 ||
369
stat_dat.st_size == 0) {
370
return TX_AUDIO_ERR_MAD_STAT;
373
mp3_file = mmap(0, stat_dat.st_size, PROT_READ, MAP_SHARED, fd, 0);
375
if (mp3_file == MAP_FAILED) {
376
return TX_AUDIO_ERR_MAD_MMAP;
379
res=mad_decode((const unsigned char *) mp3_file, stat_dat.st_size);
382
return TX_AUDIO_ERR_MAD_DECODE;
385
mem_type=TX_AUDIO_LOAD;
387
if (munmap(mp3_file, stat_dat.st_size) == -1) {
388
return TX_AUDIO_ERR_MAD_MUNMAP;
391
return TX_AUDIO_SUCCESS;
394
#define TX_MAD_BLOCKSIZE 8096
397
const unsigned char *start;
398
const unsigned char *end;
399
const unsigned char *last_frame;
402
int16_t *target_buffer;
403
unsigned int target_samples;
404
unsigned int current_sample;
405
unsigned int sample_rate;
406
unsigned int lost_sync_counter;
409
const unsigned char *last_current=NULL;
411
static enum mad_flow tX_mad_input(void *data, struct mad_stream *stream) {
412
tX_mad_buffer *buffer = (tX_mad_buffer *) data;
416
if (buffer->first_call) {
417
bs=min(TX_MAD_BLOCKSIZE, buffer->size);
418
mad_stream_buffer(stream, buffer->start, bs);
419
buffer->first_call=false;
420
return MAD_FLOW_CONTINUE;
422
if (!stream->next_frame) return MAD_FLOW_STOP;
424
pos=stream->next_frame-buffer->start;
425
bs=min(TX_MAD_BLOCKSIZE, buffer->size-pos);
426
//tX_debug("last: %08x, new %08x, bs: %i, pos: %i", buffer->last_frame, stream->next_frame, bs, pos);
428
mad_stream_buffer(stream, stream->next_frame, bs);
429
if (stream->next_frame==buffer->last_frame) {
430
//tX_debug("tX_mad_input(): No new frame? Stopping.");
431
return MAD_FLOW_STOP;
433
ld_set_progress((float) pos/(float) buffer->size);
434
buffer->last_frame=stream->next_frame;
436
return MAD_FLOW_CONTINUE;
440
static enum mad_flow tX_mad_error(void *data, struct mad_stream *stream, struct mad_frame *frame) {
441
tX_mad_buffer *buffer = (tX_mad_buffer *) data;
442
if (MAD_RECOVERABLE(stream->error)) {
443
if ((stream->error==MAD_ERROR_LOSTSYNC) && (buffer->lost_sync_counter<3)) {
444
/* Ignore at least two sync errors to not annoy people
447
buffer->lost_sync_counter++;
448
return MAD_FLOW_CONTINUE;
450
tX_warning("mad reported stream error (%i) '%s'.", stream->error, mad_stream_errorstr(stream));
451
return MAD_FLOW_CONTINUE;
453
tX_error("mad reported fatal stream error (%i) '%s'.", stream->error, mad_stream_errorstr(stream));
454
return MAD_FLOW_STOP;
457
/* From minimad.c of mad */
458
static inline signed int scale(mad_fixed_t sample) {
459
//#ifdef BIG_ENDIAN_MACHINE
460
// swap32_inline(&sample);
463
sample += (1L << (MAD_F_FRACBITS - 16));
466
if (sample >= MAD_F_ONE)
467
sample = MAD_F_ONE - 1;
468
else if (sample < -MAD_F_ONE)
472
return sample >> (MAD_F_FRACBITS + 1 - 16);
475
static enum mad_flow tX_mad_output(void *data, struct mad_header const *header, struct mad_pcm *pcm) {
476
tX_mad_buffer *buffer=(tX_mad_buffer *) data;
477
unsigned int nchannels, nsamples;
478
mad_fixed_t const *left_ch, *right_ch;
480
nchannels = pcm->channels;
481
nsamples = pcm->length;
482
left_ch = pcm->samples[0];
483
right_ch = pcm->samples[1];
484
buffer->sample_rate = pcm->samplerate;
486
buffer->target_samples+=nsamples;
488
buffer->target_buffer=(int16_t *) realloc(buffer->target_buffer, buffer->target_samples<<1);
489
if (!buffer->target_buffer) {
490
tX_error("tX_mad_output(): Failed allocating sample memory!\n");
491
return MAD_FLOW_STOP;
498
sample=scale(*left_ch++);
500
double sample_l=(double) (*left_ch++);
501
double sample_r=(double) (*right_ch++);
502
double res=(sample_l+sample_r)/2.0;
503
mad_fixed_t mad_res=(mad_fixed_t) res;
505
sample=scale(mad_res);
508
buffer->target_buffer[buffer->current_sample]=sample;
509
buffer->current_sample++;
512
return MAD_FLOW_CONTINUE;
515
int tx_audiofile::mad_decode(unsigned char const *start, unsigned long length) {
516
tX_mad_buffer buffer;
517
struct mad_decoder decoder;
520
buffer.start = start;
521
buffer.end = &start[length];
522
buffer.last_frame = NULL;
523
buffer.size = length;
524
buffer.target_buffer = NULL;
525
buffer.target_samples = 0;
526
buffer.current_sample = 0;
527
buffer.first_call=true;
528
buffer.sample_rate=0;
529
buffer.lost_sync_counter=0;
531
tX_debug("tx_audiofile::mad_decode() - start %08x, length %i", (int) buffer.start, buffer.size);
532
/* configure input, output, and error functions */
534
mad_decoder_init(&decoder, &buffer, tX_mad_input, NULL, NULL, tX_mad_output, tX_mad_error, NULL);
535
result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
538
this->mem=buffer.target_buffer;
539
this->no_samples=buffer.target_samples;
541
if (buffer.target_buffer) free(buffer.target_buffer);
544
/* release the decoder */
545
mad_decoder_finish(&decoder);
547
sample_rate=buffer.sample_rate;
553
/* AARG - OGG loading is not threadsafe !*/
554
size_t tX_ogg_file_size;
555
long tX_ogg_file_read;
557
#ifdef USE_VORBIS_INPUT
559
size_t (*read_func) (void *, size_t, size_t, FILE *);
560
int (*seek_func) (void *, long, int);
561
int (*close_func) (FILE *);
562
long (*tell_func) (FILE *);
565
int ogg_seek(void *ptr, long offset, int whence) {
566
/* ogg shall not seek ! */
571
size_t ogg_read(void *ptr, size_t size, size_t nmemb, FILE *stream) {
573
ret_val=fread(ptr, size, nmemb, stream);
575
tX_ogg_file_read+=ret_val*size;
576
ld_set_progress((double) tX_ogg_file_read/(double) tX_ogg_file_size);
581
#define VORBIS_BUFF_SIZE 4096 /*recommended*/
583
tX_audio_error tx_audiofile::load_vorbis() {
584
tX_debug("tx_audiofile::load_vorbis()");
586
/* VORBIS Callbacks */
587
ov_callbacks org_callbacks;
588
/* evil casting - to make g++ shut up */
589
tX_ov_callbacks *callbacks=(tX_ov_callbacks *) &org_callbacks;
592
char pcmout[VORBIS_BUFF_SIZE];
595
int current_section=0;
597
struct stat stat_dat;
599
callbacks->read_func=ogg_read;
600
callbacks->seek_func=ogg_seek;
601
callbacks->close_func=fclose;
602
callbacks->tell_func=ftell;
604
file=fopen(filename, "r");
606
return TX_AUDIO_ERR_WAV_NOTFOUND;
609
if (fstat(fileno(file), &stat_dat) == -1 || stat_dat.st_size == 0) {
610
return TX_AUDIO_ERR_MAD_STAT;
613
tX_ogg_file_size=stat_dat.st_size;
616
int res=ov_open_callbacks((void *) file, &vf, NULL, 0, org_callbacks);
620
return TX_AUDIO_ERR_VORBIS_OPEN;
623
vorbis_info *vi=ov_info(&vf,-1);
624
sample_rate=vi->rate;
626
unsigned int channels=vi->channels;
627
unsigned int samples_read=0;
628
unsigned int mono_samples;
632
while((!eof) && (!current_section)) {
633
#ifdef BIG_ENDIAN_MACHINE
638
long ret=ov_read(&vf,pcmout,VORBIS_BUFF_SIZE,ENDIANP,2,1,¤t_section);
641
} else if (ret < 0) {
642
/* ignore stream errors */
646
new_data=(int16_t *) realloc(data, bytes/channels);
648
if (data) free(data);
649
return TX_AUDIO_ERR_ALLOC;
653
int16_t *src=(int16_t *) &pcmout;
658
for (i=0; i<mono_samples; i++) {
659
data[samples_read+i]=src[i];
665
for (i=0; i<mono_samples; i++) {
666
double l_value, r_value;
669
data[samples_read+i]=(int16_t) ((l_value+r_value)/2.0);
674
mono_samples=(ret/2)/channels;
675
for (i=0; i<mono_samples; i++) {
678
for (unsigned int c=0; c<channels; c++) {
679
value+=(double) src[i*channels+c];
681
value/=(double) channels;
682
data[samples_read+i]=(int16_t) value;
685
samples_read+=mono_samples;
691
mem=(int16_t *) data;
692
no_samples=samples_read;
697
return TX_AUDIO_ERR_VORBIS_NODATA;
700
return TX_AUDIO_SUCCESS;
704
#ifdef USE_AUDIOFILE_INPUT
705
#define TX_AF_SAMPLES_PER_BLOCK 2048
707
tX_audio_error tx_audiofile::load_af() {
708
tX_debug("tx_audiofile::load_af()");
710
AFfilehandle af_file;
711
AFframecount all_frames, frames_read=0, current_frames=1;
714
af_file = afOpenFile(filename, "r", NULL);
715
if (af_file==AF_NULL_FILEHANDLE) {
716
return TX_AUDIO_ERR_AF_OPEN;
719
all_frames=afGetFrameCount(af_file, AF_DEFAULT_TRACK);
720
sample_rate=(unsigned int) afGetRate(af_file, AF_DEFAULT_TRACK);
721
afSetVirtualChannels(af_file, AF_DEFAULT_TRACK, 1);
722
afSetVirtualSampleFormat(af_file, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); // 2 == 16 Bit?
723
#ifdef BIG_ENDIAN_MACHINE
724
afSetVirtualByteOrder(af_file, AF_DEFAULT_TRACK, AF_BYTEORDER_BIGENDIAN);
726
afSetVirtualByteOrder(af_file, AF_DEFAULT_TRACK, AF_BYTEORDER_LITTLEENDIAN);
729
while (current_frames) {
732
new_data=(int16_t*) realloc(data, (frames_read+TX_AF_SAMPLES_PER_BLOCK)*2);
734
if (data) free(data);
735
afCloseFile(af_file);
736
return TX_AUDIO_ERR_ALLOC;
739
current_frames=afReadFrames(af_file,AF_DEFAULT_TRACK,(void *) &data[frames_read],TX_AF_SAMPLES_PER_BLOCK);
740
frames_read+=current_frames;
741
ld_set_progress(((double) frames_read)/((double) all_frames));
743
afCloseFile(af_file);
746
if (data) free(data);
747
return TX_AUDIO_ERR_AF_NODATA;
750
/* shorten to the actually read size of samples */
751
if (!realloc(data, frames_read*2)) {
752
if (data) free(data);
753
return TX_AUDIO_ERR_ALLOC;
757
no_samples=frames_read;
759
return TX_AUDIO_SUCCESS;