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
36 extern int _frequency
;
60 c
+=(fgetc(f
) * 65536);
61 c
+=(fgetc(f
) * 16777216);
67 int load_sample(FILE * f
, int bits
, int sign
)
79 s|=(((unsigned char)fgetc(f)) << 8);*/
81 s
=(short int)fget16(f
);
90 void load_pcm_wave(FILE * f
, int * size
, int bits
, int channels
,
91 int sign
, int frequency
, int ** lwave
, int ** rwave
)
97 ls
=(int *)malloc(*size
* sizeof(int));
100 rs
=(int *)malloc(*size
* sizeof(int));
104 for(n
=0;n
< *size
;n
++)
106 ls
[n
]=load_sample(f
, bits
, sign
);
109 rs
[n
]=load_sample(f
, bits
, sign
);
112 /* resample if needed */
113 if(frequency
!= _frequency
)
117 ls
=wave_resample(ls
, frequency
, size
);
120 rs
=wave_resample(rs
, frequency
, &n
);
130 int * wave_resample(int * wave
, int freq
, int * size
)
138 ratio
=(double) freq
/ (double) _frequency
;
139 new_size
=(int)((double) (*size
) / ratio
);
141 new_wave
=(int *)malloc(new_size
* sizeof(int));
143 printf("wave_resample: ratio %f\n", (float)ratio
);
145 for(n
=i
=0;n
< new_size
;n
++,i
+=ratio
)
146 new_wave
[n
]=wave
[(int)i
];
150 /* free the old wave */
157 int load_wav_file(char * filename
, int * size
, int ** lwave
, int ** rwave
)
162 short int b_per_sec
,num_channels
,tag
;
163 char riffid
[5],waveid
[5],fmtid
[5],dataid
[5];
164 int wav_frequency
, wav_bits
;
168 if((f
=fopen(filename
,"r"))==NULL
)
177 if(strcmp(waveid
,"WAVE"))
192 num_channels
=fget16(f
);
193 wav_frequency
=fget32(f
);
196 wav_bits
=fget16(f
) / num_channels
;
199 fread(dummydata
,1,(size_t)flen
-14,f
);
204 if(wav_bits
==16) (*size
)/=2;
205 if(num_channels
==2) (*size
)/=2;
207 load_pcm_wave(f
, size
, wav_bits
, num_channels
,
208 1, wav_frequency
, &ls
, &rs
);
219 /* table of frequencies for each note */
221 static int _pat_freq_table
[128]=
223 8176, 8662, 9177, 9723,
224 10301, 10913, 11562, 12250,
225 12978, 13750, 14568, 15434,
227 16352, 17324, 18354, 19445,
228 20602, 21827, 23125, 24500,
229 25957, 27500, 29135, 30868,
231 32703, 34648, 36708, 38891,
232 41203, 43654, 46249, 48999,
233 51913, 55000, 58270, 61735,
235 65406, 69296, 73416, 77782,
236 82407, 87307, 92499, 97999,
237 103826, 110000, 116541, 123471,
239 130813, 138591, 146832, 155563,
240 164814, 174614, 184997, 195998,
241 207652, 220000, 233082, 246942,
243 261626, 277183, 293665, 311127,
244 329628, 349228, 369994, 391995,
245 415305, 440000, 466164, 493883,
247 523251, 554365, 587330, 622254,
248 659255, 698456, 739989, 783991,
249 830609, 880000, 932328, 987767,
251 1046502, 1108731, 1174659, 1244508,
252 1318510, 1396913, 1479978, 1567982,
253 1661219, 1760000, 1864655, 1975533,
255 2093005, 2217461, 2349318, 2489016,
256 2637020, 2793826, 2959955, 3135963,
257 3322438, 3520000, 3729310, 3951066,
259 4186009, 4434922, 4698636, 4978032,
260 5274041, 5587652, 5919911, 6271927,
261 6644875, 7040000, 7458620, 7902133,
263 8372018, 8869844, 9397273, 9956063,
264 10548082, 11175303, 11839822, 12543854
268 static int _pat_find_note(int frequency
)
274 if(_pat_freq_table
[n
] >= frequency
)
282 int load_pat_file(char * filename
)
288 int size
, loop_start
, loop_end
;
289 int base_note
, min_note
, max_note
;
290 int frequency
, flags
, bits
, sign
, loop
;
294 if((f
=fopen(filename
,"r"))==NULL
)
297 /* read signatures */
298 fread(buffer
, 12, 1, f
);
299 if(strcmp(buffer
, "GF1PATCH110")!=0)
305 fread(buffer
, 10, 1, f
);
306 if(strcmp(buffer
, "ID#000002")!=0)
312 /* skip description */
313 fread(buffer
, 65, 1, f
);
317 printf("vol: %d\n", i
);
320 fread(buffer
, 109, 1, f
);
324 printf("layers: %d\n", n_layers
);
327 fread(buffer
, 40, 1, f
);
329 for(n
=0;n
< n_layers
;n
++)
332 fread(buffer
, 8, 1, f
);
333 printf("name: '%s'\n", buffer
);
336 loop_start
=fget32(f
);
340 printf("size: %d [%d-%d], freq: %d\n",
341 size
, loop_start
, loop_end
, frequency
);
347 printf("freqs: %d - %d - %d\n",
348 min_note
, base_note
, max_note
);
349 printf("notes: %d - %d - %d\n",
350 _pat_find_note(min_note
),
351 _pat_find_note(base_note
),
352 _pat_find_note(max_note
));
354 /* ignore fine-tune */
359 printf("pan: %d\n", i
);
361 /* skip envelope rate, value, tremolo and vibrato */
362 fread(buffer
, 18, 1, f
);
365 printf("flags: 0x%02X\n", flags
);
367 bits
=flags
& 0x01 ? 16 : 8;
368 sign
=flags
& 0x02 ? 0 : 1;
369 loop
=flags
& 0x04 ? 1 : 0;
371 if(flags
& 8) printf("PINGPONG!!!\n");
373 printf("bits: %d, sign: %d, loop: %d\n",
383 /* skip frequency scale data */
384 fget16(f
); fget16(f
);
387 fread(buffer
, 36, 1, f
);
390 load_pcm_wave(f
, &size
, bits
, 1, sign
,
391 frequency
, &lwave
, &rwave
);
396 q
=fopen("qq.raw", "w");
397 fwrite(lwave
, sizeof(int), size
, q
);