New automatic configuration.
[ahxm.git] / input.c
blob3efe6f5b801c009078f4f40c6085c38096301772
1 /*
3 PROGRAM_NAME PROGRAM_VERSION - PROGRAM_DESCRIPTION
5 Copyright (C) 2003 Angel Ortega <angel@triptico.com>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 http://www.triptico.com
25 #include "config.h"
27 #include <stdio.h>
28 #include <stdlib.h>
30 #include "input.h"
32 /*******************
33 Data
34 ********************/
36 extern int _frequency;
39 /*******************
40 Code
41 ********************/
43 int fget16(FILE * f)
45 int c;
47 c=fgetc(f);
48 c+=(fgetc(f) * 256);
50 return(c);
54 int fget32(FILE * f)
56 int c;
58 c=fgetc(f);
59 c+=(fgetc(f) * 256);
60 c+=(fgetc(f) * 65536);
61 c+=(fgetc(f) * 16777216);
63 return(c);
67 int load_sample(FILE * f, int bits)
69 short int s;
71 if(bits==8)
73 s=fgetc(f)-128;
74 s<<=8;
76 else
78 /* s=fgetc(f);
79 s|=(((unsigned char)fgetc(f)) << 8);*/
80 s=fget16(f);
83 return(s);
87 void load_pcm_wave(FILE * f, int * size, int bits, int channels,
88 int frequency, int ** lwave, int ** rwave)
90 int n;
91 int * ls;
92 int * rs;
94 ls=(int *)malloc(*size * sizeof(int));
96 if(channels==2)
97 rs=(int *)malloc(*size * sizeof(int));
98 else
99 rs=ls;
101 for(n=0;n < *size;n++)
103 ls[n]=load_sample(f, bits);
105 if(channels==2)
106 rs[n]=load_sample(f, bits);
109 /* resample if needed */
110 if(frequency != _frequency)
112 ls=wave_resample(ls, frequency, size);
114 if(channels==2)
115 rs=wave_resample(rs, frequency, size);
116 else
117 rs=ls;
120 *lwave=ls;
121 *rwave=rs;
125 int * wave_resample(int * wave, int freq, int * size)
127 double ratio;
128 int new_size;
129 int * new_wave;
130 int n;
131 double i;
133 ratio=(double) freq / (double) _frequency;
134 new_size=(int)((double) (*size) / ratio);
136 new_wave=(int *)malloc(new_size * sizeof(int));
138 printf("wave_resample: ratio %f\n", (float)ratio);
140 for(n=i=0;n < new_size;n++,i+=ratio)
141 new_wave[n]=wave[(int)i];
143 *size=new_size;
145 /* free the old wave */
146 free(wave);
148 return(new_wave);
152 int load_wav_file(char * filename, int * size, int ** lwave, int ** rwave)
154 FILE * f;
155 char dummydata[256];
156 int rlen,flen;
157 short int b_per_sec,num_channels,tag;
158 char riffid[5],waveid[5],fmtid[5],dataid[5];
159 int wav_frequency, wav_bits;
160 int * ls;
161 int * rs;
163 if((f=fopen(filename,"r"))==NULL)
164 return(0);
166 fread(riffid,1,4,f);
167 riffid[4] = 0;
168 fread(&rlen,1,4,f);
169 fread(waveid,1,4,f);
170 waveid[4] = 0;
172 if(strcmp(waveid,"WAVE"))
174 fclose(f);
175 return(0);
178 fread(fmtid,1,4,f);
179 fmtid[4] = 0;
180 flen=fget32(f);
182 if(flen>240)
183 flen=240;
185 fread(&tag,1,2,f);
187 num_channels=fget16(f);
188 wav_frequency=fget32(f);
189 b_per_sec=fget32(f);
191 wav_bits=fget16(f) / num_channels;
192 wav_bits*=8;
194 fread(dummydata,1,(size_t)flen-14,f);
195 fread(dataid,1,4,f);
196 dataid[4] = 0;
198 *size=fget32(f);
199 if(wav_bits==16) (*size)/=2;
200 if(num_channels==2) (*size)/=2;
202 load_pcm_wave(f, size, wav_bits, num_channels,
203 wav_frequency, &ls, &rs);
205 fclose(f);
207 *lwave=ls;
208 *rwave=rs;
210 return(1);