2 terminatorX - realtime audio scratching software
3 Copyright (C) 1999 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: Contains routines for loading the loop and scratch
22 files and allocating the record buffer.
26 #include "tX_mastergui.h"
28 #include "tX_wavfunc.h"
29 #include "tX_global.h"
36 #define min(a,b) ((a) < (b) ? (a) : (b))
38 #define SOX_BLOCKSIZE 32000
43 char buffer[SOX_BLOCKSIZE];
52 nwbf=(tempbuff *) malloc(sizeof(tempbuff));
60 int load_wav(char *name, int16_t **data_ptr, unsigned int *size)
77 if (!init_wav_read(name, &wav_in))
80 printf("[load_wav] Error. Couldn't open \"%s\".\n", name);
82 return(LW_ERROR_FILE_NOT_FOUND);
86 printf("Loading: %s\n", name);
87 if (parms.verbose) printf("File: %i Bytes Data, %i Bit Depth, %i Hz Samplerate.\n", wav_in.len, wav_in.depth, wav_in.srate);
92 if (wav_in.depth != 16)
95 puts("[load_wav] Error: Wave-File is not 16 Bit. Fatal. Giving up.");
97 return(LW_ERROR_FILE_NOT_16BIT);
100 if (wav_in.chans != 1)
103 puts("[load_wav] Error: Wave-File is not Mono. Fatal. Giving up.");
105 return(LW_ERROR_FILE_NOT_MONO);
109 if (wav_in.srate != 44100)
111 puts("[load_wav] Warning: Wave-File was not recorded at 44.100 Hz!");
113 if (wav_in.blkalign != 2)
115 printf("[load_wav] Warning: Unexpected block alignment: %i.\n", wav_in.blkalign);
120 data = (int16_t *) malloc (*size);
125 puts("[load_wav] Error: Failed to allocate sample memory. Fatal. Giving up.");
127 return(LW_ERROR_ALLOC);
137 wav_progress_update(0.5); /* Yeehha! A realistic Progessbar ;) */
143 while (wav_in.len>allbytes)
147 bytes = fread(w->buffer, 1, SOX_BLOCKSIZE, wav_in.handle);
156 wav_progress_update(0.5);
161 wav_progress_update(0.25);
166 #ifdef BIG_ENDIAN_MACHINE
167 swapbuffer(w->buffer, bytes/sizeof(int16_t));
173 if (!newb) return (LW_ERROR_ALLOC);
179 bytes = fread(p, 1, min(1024, wav_in.len-allbytes), wav_in.handle);
181 #ifdef BIG_ENDIAN_MACHINE
182 if (!wav_in.has_host_order) swapbuffer(p, bytes/sizeof(int16_t));
188 puts("[load_wav] Error: Failed to read Wave-Data (Corrupt?). Fatal. Giving up.");
189 if(bytes<0) perror("terminatorX");
192 wav_progress_update(0);
193 return (LW_ERROR_READ);
196 #endif // USE_SOX_INPUT
200 #ifndef USE_SOX_INPUT
201 wav_progress_update(((gfloat) allbytes)/((gfloat) wav_in.len));
203 p+=(ssize_t) bytes/sizeof(int16_t);
207 wav_close(wav_in.handle);
210 if (!allbytes) // Nothing read from pipe -> error
213 return (LW_ERROR_READ);
218 if (parms.verbose) printf("Read: %i Bytes of Wave-Data.\n", allbytes);
223 data = (int16_t *) malloc (*size);
227 return(LW_ERROR_ALLOC);
237 memcpy(p, w->buffer, w->used);
239 newb=(tempbuff*) w->next;
243 wav_progress_update(0.5 + 0.5*((gfloat) bytes)/((gfloat) allbytes));
245 p+=(ssize_t) SOX_BLOCKSIZE/sizeof(int16_t);
249 wav_progress_update(0);
253 return (LW_NO_ERROR);
256 int malloc_recbuffer()
258 if (globals.rec_buffer) free(globals.rec_buffer);
259 globals.rec_buffer=(int16_t*) malloc((ssize_t) globals.rec_size);
260 if (globals.rec_buffer!=NULL) return(0);