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 tx_audiofile :: tx_audiofile()
74 mem_type=TX_AUDIO_UNDEFINED;
75 file_type=TX_FILE_UNDEFINED;
83 void tx_audiofile :: figure_file_type()
87 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 #ifdef USE_AUDIOFILE_INPUT
112 if ((load_err) && (file_type!=TX_FILE_MPG123) && (file_type!=TX_FILE_OGG123)) {
117 #ifdef USE_BUILTIN_WAV
118 if ((load_err) && (file_type==TX_FILE_WAV)) {
120 // if (load_err==TX_AUDIO_SUCCESS) return(load_err);
125 if ((load_err) && (file_type==TX_FILE_MPG123)) {
127 //if (load_err==TX_AUDIO_SUCCESS) return(load_err);
131 #ifdef USE_MPG123_INPUT
132 if ((load_err) && (file_type==TX_FILE_MPG123)) {
133 load_err=load_mpg123();
138 #ifdef USE_VORBIS_INPUT
139 if ((load_err) && (file_type==TX_FILE_OGG123)) {
140 load_err=load_vorbis();
141 //if (load_err==TX_AUDIO_SUCCESS) return(load_err);
145 #ifdef USE_OGG123_INPUT
146 if ((load_err) && (file_type==TX_FILE_OGG123)) {
147 load_err=load_ogg123();
159 tX_debug("tx_audiofile::load() Set samplerate to %i for file %s.", sample_rate, filename);
164 tx_audiofile :: ~tx_audiofile() {
166 case TX_AUDIO_MMAP: break;
167 case TX_AUDIO_LOAD: {
171 case TX_AUDIO_UNDEFINED: break;
175 if (mem_type==TX_AUDIO_MMAP) {
183 tX_audio_error tx_audiofile :: load_piped()
189 unsigned char buffer[SOX_BLOCKSIZE];
192 /* Irritating the user... */
193 ld_set_progress(0.5);
194 mem_type=TX_AUDIO_LOAD;
197 bytes = fread(buffer, 1, SOX_BLOCKSIZE, file);
202 int16_t *new_data=(int16_t *) realloc(data, allbytes);
203 //printf("All: %i, Bytes: %i, new: %08x, old: %08x\n", allbytes, bytes, new_data, data);
206 pclose(file); file=NULL;
207 if (data) free(data);
209 return TX_AUDIO_ERR_ALLOC;
213 ptr=(unsigned char *) data;
214 memcpy((void *) &ptr[prev_bytes],(void *) buffer, bytes);
218 pclose(file); file=NULL;
221 // If we get here we read zero Bytes...
222 if (data) free(data);
223 return TX_AUDIO_ERR_PIPE_READ;
226 no_samples=allbytes/sizeof(int16_t);
229 /* Irritating the user just a little more ;)*/
230 ld_set_progress(1.0);
232 return TX_AUDIO_SUCCESS;
238 tX_audio_error tx_audiofile :: load_sox() {
239 tX_debug("tx_audiofile::load_sox()");
241 char command[PATH_MAX*2];
243 sprintf(command, SOX_STR, filename);
244 file = popen(command, "r");
246 if (!file) return TX_AUDIO_ERR_SOX;
253 #ifdef USE_MPG123_INPUT
254 tX_audio_error tx_audiofile :: load_mpg123() {
255 tX_debug("tx_audiofile::load_mpg123()");
257 char command[PATH_MAX*2];
259 sprintf(command, MPG123_STR, filename);
260 file = popen(command, "r");
262 if (!file) return TX_AUDIO_ERR_MPG123;
268 #ifdef USE_OGG123_INPUT
269 tX_audio_error tx_audiofile :: load_ogg123() {
270 tX_debug("tx_audiofile::load_ogg123()");
272 char command[PATH_MAX*2];
274 sprintf(command, OGG123_STR, filename);
275 file = popen(command, "r");
277 if (!file) return TX_AUDIO_ERR_OGG123;
283 #ifdef USE_BUILTIN_WAV
284 #define min(a,b) ((a) < (b) ? (a) : (b))
285 tX_audio_error tx_audiofile :: load_wav() {
286 tX_debug("tx_audiofile::load_wav()");
294 mem_type=TX_AUDIO_LOAD;
296 if (!init_wav_read(filename, &wav_in))
298 return(TX_AUDIO_ERR_WAV_NOTFOUND);
301 if (wav_in.depth != 16)
303 return(TX_AUDIO_ERR_NOT_16BIT);
306 if (wav_in.chans != 1)
308 return(TX_AUDIO_ERR_NOT_MONO);
311 sample_rate=wav_in.srate;
314 data = (int16_t *) malloc (memsize);
318 return(TX_AUDIO_ERR_ALLOC);
322 #ifdef ENABLE_DEBUG_OUTPUT
324 unsigned char *debug_p=(unsigned char *) p;
326 while (wav_in.len>allbytes)
328 bytes = fread(p, 1, min(1024, wav_in.len-allbytes), wav_in.handle);
330 #ifdef ENABLE_DEBUG_OUTPUT
331 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]); }
336 return (TX_AUDIO_ERR_WAV_READ);
339 #ifdef BIG_ENDIAN_MACHINE
340 swapbuffer(p, bytes/sizeof(int16_t));
341 # ifdef ENABLE_DEBUG_OUTPUT
342 if (output) { tX_debug("tX_audiofile::load_wav() swapped %i Bytes [%04x %04x %04x %04x %04x %04x ..]",
343 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]); }
347 #ifdef ENABLE_DEBUG_OUTPUT
353 ld_set_progress((float) allbytes/(float)wav_in.len);
355 p+=(ssize_t) bytes/sizeof(int16_t);
358 wav_close(wav_in.handle);
361 no_samples=memsize/sizeof(int16_t);
363 return (TX_AUDIO_SUCCESS);
368 tX_audio_error tx_audiofile::load_mad() {
369 tX_debug("tx_audiofile::load_mad()");
371 struct stat stat_dat;
376 fd=open(filename, O_RDONLY);
377 if (!fd) return TX_AUDIO_ERR_MAD_OPEN;
379 if (fstat(fd, &stat_dat) == -1 ||
380 stat_dat.st_size == 0) {
381 return TX_AUDIO_ERR_MAD_STAT;
384 mp3_file = mmap(0, stat_dat.st_size, PROT_READ, MAP_SHARED, fd, 0);
386 if (mp3_file == MAP_FAILED) {
387 return TX_AUDIO_ERR_MAD_MMAP;
390 res=mad_decode((const unsigned char *) mp3_file, stat_dat.st_size);
393 return TX_AUDIO_ERR_MAD_DECODE;
396 mem_type=TX_AUDIO_LOAD;
398 if (munmap(mp3_file, stat_dat.st_size) == -1) {
399 return TX_AUDIO_ERR_MAD_MUNMAP;
402 return TX_AUDIO_SUCCESS;
405 #define TX_MAD_BLOCKSIZE 8096
408 const unsigned char *start;
409 const unsigned char *end;
410 const unsigned char *last_frame;
413 int16_t *target_buffer;
414 unsigned int target_samples;
415 unsigned int current_sample;
416 unsigned int sample_rate;
417 unsigned int lost_sync_counter;
420 const unsigned char *last_current=NULL;
422 static enum mad_flow tX_mad_input(void *data, struct mad_stream *stream) {
423 tX_mad_buffer *buffer = (tX_mad_buffer *) data;
427 if (buffer->first_call) {
428 bs=min(TX_MAD_BLOCKSIZE, buffer->size);
429 mad_stream_buffer(stream, buffer->start, bs);
430 buffer->first_call=false;
431 return MAD_FLOW_CONTINUE;
433 if (!stream->next_frame) return MAD_FLOW_STOP;
435 pos=stream->next_frame-buffer->start;
436 bs=min(TX_MAD_BLOCKSIZE, buffer->size-pos);
437 //tX_debug("last: %08x, new %08x, bs: %i, pos: %i", buffer->last_frame, stream->next_frame, bs, pos);
439 mad_stream_buffer(stream, stream->next_frame, bs);
440 if (stream->next_frame==buffer->last_frame) {
441 //tX_debug("tX_mad_input(): No new frame? Stopping.");
442 return MAD_FLOW_STOP;
444 ld_set_progress((float) pos/(float) buffer->size);
445 buffer->last_frame=stream->next_frame;
447 return MAD_FLOW_CONTINUE;
451 static enum mad_flow tX_mad_error(void *data, struct mad_stream *stream, struct mad_frame *frame) {
452 tX_mad_buffer *buffer = (tX_mad_buffer *) data;
453 if (MAD_RECOVERABLE(stream->error)) {
454 if ((stream->error==MAD_ERROR_LOSTSYNC) && (buffer->lost_sync_counter<3)) {
455 /* Ignore at least two sync errors to not annoy people
458 buffer->lost_sync_counter++;
459 return MAD_FLOW_CONTINUE;
461 tX_warning("mad reported stream error (%i) '%s'.", stream->error, mad_stream_errorstr(stream));
462 return MAD_FLOW_CONTINUE;
464 tX_error("mad reported fatal stream error (%i) '%s'.", stream->error, mad_stream_errorstr(stream));
465 return MAD_FLOW_STOP;
468 /* From minimad.c of mad */
469 static inline signed int scale(mad_fixed_t sample) {
470 //#ifdef BIG_ENDIAN_MACHINE
471 // swap32_inline(&sample);
474 sample += (1L << (MAD_F_FRACBITS - 16));
477 if (sample >= MAD_F_ONE)
478 sample = MAD_F_ONE - 1;
479 else if (sample < -MAD_F_ONE)
483 return sample >> (MAD_F_FRACBITS + 1 - 16);
486 static enum mad_flow tX_mad_output(void *data, struct mad_header const *header, struct mad_pcm *pcm) {
487 tX_mad_buffer *buffer=(tX_mad_buffer *) data;
488 unsigned int nchannels, nsamples;
489 mad_fixed_t const *left_ch, *right_ch;
491 nchannels = pcm->channels;
492 nsamples = pcm->length;
493 left_ch = pcm->samples[0];
494 right_ch = pcm->samples[1];
495 buffer->sample_rate = pcm->samplerate;
497 buffer->target_samples+=nsamples;
499 buffer->target_buffer=(int16_t *) realloc(buffer->target_buffer, buffer->target_samples<<1);
500 if (!buffer->target_buffer) {
501 tX_error("tX_mad_output(): Failed allocating sample memory!\n");
502 return MAD_FLOW_STOP;
509 sample=scale(*left_ch++);
511 double sample_l=(double) (*left_ch++);
512 double sample_r=(double) (*right_ch++);
513 double res=(sample_l+sample_r)/2.0;
514 mad_fixed_t mad_res=(mad_fixed_t) res;
516 sample=scale(mad_res);
519 buffer->target_buffer[buffer->current_sample]=sample;
520 buffer->current_sample++;
523 return MAD_FLOW_CONTINUE;
526 int tx_audiofile::mad_decode(unsigned char const *start, unsigned long length) {
527 tX_mad_buffer buffer;
528 struct mad_decoder decoder;
531 buffer.start = start;
532 buffer.end = &start[length];
533 buffer.last_frame = NULL;
534 buffer.size = length;
535 buffer.target_buffer = NULL;
536 buffer.target_samples = 0;
537 buffer.current_sample = 0;
538 buffer.first_call=true;
539 buffer.sample_rate=0;
540 buffer.lost_sync_counter=0;
542 tX_debug("tx_audiofile::mad_decode() - start %08x, length %i", buffer.start, buffer.size);
543 /* configure input, output, and error functions */
545 mad_decoder_init(&decoder, &buffer, tX_mad_input, NULL, NULL, tX_mad_output, tX_mad_error, NULL);
546 result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
549 this->mem=buffer.target_buffer;
550 this->no_samples=buffer.target_samples;
552 if (buffer.target_buffer) free(buffer.target_buffer);
555 /* release the decoder */
556 mad_decoder_finish(&decoder);
558 sample_rate=buffer.sample_rate;
564 /* AARG - OGG loading is not threadsafe !*/
565 size_t tX_ogg_file_size;
566 long tX_ogg_file_read;
568 #ifdef USE_VORBIS_INPUT
570 size_t (*read_func) (void *, size_t, size_t, FILE *);
571 int (*seek_func) (void *, long, int);
572 int (*close_func) (FILE *);
573 long (*tell_func) (FILE *);
576 int ogg_seek(void *ptr, long offset, int whence) {
577 /* ogg shall not seek ! */
582 size_t ogg_read(void *ptr, size_t size, size_t nmemb, FILE *stream) {
584 ret_val=fread(ptr, size, nmemb, stream);
586 tX_ogg_file_read+=ret_val*size;
587 ld_set_progress((double) tX_ogg_file_read/(double) tX_ogg_file_size);
592 #define VORBIS_BUFF_SIZE 4096 /*recommended*/
594 tX_audio_error tx_audiofile::load_vorbis() {
595 tX_debug("tx_audiofile::load_vorbis()");
597 /* VORBIS Callbacks */
598 ov_callbacks org_callbacks;
599 /* evil casting - to make g++ shut up */
600 tX_ov_callbacks *callbacks=(tX_ov_callbacks *) &org_callbacks;
603 char pcmout[VORBIS_BUFF_SIZE];
606 int current_section=0;
608 struct stat stat_dat;
610 callbacks->read_func=ogg_read;
611 callbacks->seek_func=ogg_seek;
612 callbacks->close_func=fclose;
613 callbacks->tell_func=ftell;
615 file=fopen(filename, "r");
617 return TX_AUDIO_ERR_WAV_NOTFOUND;
620 if (fstat(fileno(file), &stat_dat) == -1 || stat_dat.st_size == 0) {
621 return TX_AUDIO_ERR_MAD_STAT;
624 tX_ogg_file_size=stat_dat.st_size;
627 int res=ov_open_callbacks((void *) file, &vf, NULL, 0, org_callbacks);
631 return TX_AUDIO_ERR_VORBIS_OPEN;
634 vorbis_info *vi=ov_info(&vf,-1);
635 sample_rate=vi->rate;
637 unsigned int channels=vi->channels;
638 unsigned int samples_read=0;
639 unsigned int mono_samples;
643 while((!eof) && (!current_section)) {
644 #ifdef BIG_ENDIAN_MACHINE
649 long ret=ov_read(&vf,pcmout,VORBIS_BUFF_SIZE,ENDIANP,2,1,¤t_section);
652 } else if (ret < 0) {
653 /* ignore stream errors */
657 new_data=(int16_t *) realloc(data, bytes/channels);
659 if (data) free(data);
660 return TX_AUDIO_ERR_ALLOC;
\r
664 int16_t *src=(int16_t *) &pcmout;
669 for (i=0; i<mono_samples; i++) {
670 data[samples_read+i]=src[i];
676 for (i=0; i<mono_samples; i++) {
677 double l_value, r_value;
680 data[samples_read+i]=(int16_t) ((l_value+r_value)/2.0);
685 mono_samples=(ret/2)/channels;
686 for (i=0; i<mono_samples; i++) {
689 for (unsigned int c=0; c<channels; c++) {
690 value+=(double) src[i*channels+c];
692 value/=(double) channels;
693 data[samples_read+i]=(int16_t) value;
696 samples_read+=mono_samples;
702 mem=(int16_t *) data;
703 no_samples=samples_read;
708 return TX_AUDIO_ERR_VORBIS_NODATA;
711 return TX_AUDIO_SUCCESS;
715 #ifdef USE_AUDIOFILE_INPUT
716 #define TX_AF_SAMPLES_PER_BLOCK 2048
718 tX_audio_error tx_audiofile::load_af() {
719 tX_debug("tx_audiofile::load_af()");
721 AFfilehandle af_file;
722 AFframecount all_frames, frames_read=0, current_frames=1;
725 af_file = afOpenFile(filename, "r", NULL);
726 if (af_file==AF_NULL_FILEHANDLE) {
727 return TX_AUDIO_ERR_AF_OPEN;
730 all_frames=afGetFrameCount(af_file, AF_DEFAULT_TRACK);
731 sample_rate=(unsigned int) afGetRate(af_file, AF_DEFAULT_TRACK);
732 afSetVirtualChannels(af_file, AF_DEFAULT_TRACK, 1);
733 afSetVirtualSampleFormat(af_file, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16); // 2 == 16 Bit?
734 #ifdef BIG_ENDIAN_MACHINE
735 afSetVirtualByteOrder(af_file, AF_DEFAULT_TRACK, AF_BYTEORDER_BIGENDIAN);
737 afSetVirtualByteOrder(af_file, AF_DEFAULT_TRACK, AF_BYTEORDER_LITTLEENDIAN);
740 while (current_frames) {
743 new_data=(int16_t*) realloc(data, (frames_read+TX_AF_SAMPLES_PER_BLOCK)*2);
745 if (data) free(data);
746 afCloseFile(af_file);
747 return TX_AUDIO_ERR_ALLOC;
750 current_frames=afReadFrames(af_file,AF_DEFAULT_TRACK,(void *) &data[frames_read],TX_AF_SAMPLES_PER_BLOCK);
751 frames_read+=current_frames;
752 ld_set_progress(((double) frames_read)/((double) all_frames));
754 afCloseFile(af_file);
757 if (data) free(data);
758 return TX_AUDIO_ERR_AF_NODATA;
761 /* shorten to the actually read size of samples */
762 if (!realloc(data, frames_read*2)) {
763 if (data) free(data);
764 return TX_AUDIO_ERR_ALLOC;
768 no_samples=frames_read;
770 return TX_AUDIO_SUCCESS;