2 Routines for handling .wav files
4 Part of the swftools package.
6 Copyright (c) 2001 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
32 int getWAVBlock(FILE*fi, struct WAVBlock*block)
36 if(fread(block->id,1,4,fi)<4)
41 block->size = b[0]|b[1]<<8|b[2]<<16|b[3]<<24;
42 /*printf("Chunk: [%c%c%c%c] (%d bytes)\n",
43 block->id[0],block->id[1],
44 block->id[2],block->id[3],
49 int wav_read(struct WAV*wav, char* filename)
51 FILE*fi = fopen(filename, "rb");
54 struct WAVBlock block;
59 fseek(fi, 0, SEEK_END);
61 fseek(fi, 0, SEEK_SET);
63 //printf("Filesize: %d\n", filesize);
65 if(!getWAVBlock (fi, &block))
70 if(strncmp(block.id,"RIFF",4))
72 fprintf(stderr, "wav_read: not a WAV file\n");
76 if(block.size + 8 < filesize)
77 fprintf(stderr, "wav_read: warning - more tags (%d extra bytes)\n",
78 filesize - block.size - 8);
80 if(block.size == filesize)
81 /* some buggy software doesn't generate the right tag length */
82 block.size = filesize - 8;
84 if(block.size + 8 > filesize)
85 fprintf(stderr, "wav_read: warning - short file (%d bytes missing)\n",
86 block.size + 8 - filesize);
87 if(fread(b, 1, 4, fi) < 4)
92 if(strncmp((const char*)b, "WAVE", 4))
94 fprintf(stderr, "wav_read: not a WAV file (2)\n");
101 getWAVBlock(fi, &block);
103 if(!strncmp(block.id, "fmt ", 4))
105 if(fread(&b, 1, 16, fi)<16)
110 wav->tag = b[0]|b[1]<<8;
111 wav->channels = b[2]|b[3]<<8;
112 wav->sampsPerSec = b[4]|b[5]<<8|b[6]<<16|b[7]<<24;
113 wav->bytesPerSec = b[8]|b[9]<<8|b[10]<<16|b[11]<<24;
114 wav->align = b[12]|b[13]<<8;
115 wav->bps = b[14]|b[15]<<8;
118 if (!strncmp(block.id, "LIST", 4))
120 // subchunk ICMT (comment) may exist
123 if (!strncmp(block.id, "data", 4))
126 wav->data = (unsigned char*)malloc(block.size);
129 fprintf(stderr, "Out of memory (%d bytes needed)", block.size);
133 l = fread(wav->data, 1, block.size, fi);
136 fprintf(stderr, "Error while reading data block of size %d (%d bytes missing)", block.size, block.size-l);
140 wav->size = block.size;
143 fseek(fi, pos, SEEK_SET);
145 while (pos < filesize);
150 int wav_write(struct WAV*wav, char*filename)
152 FILE*fi = fopen(filename, "wb");
153 char*b="RIFFWAVEfmt \x10\0\0\0data";
155 unsigned long int w32;
159 w32=(/*fmt*/8+0x10+/*data*/8+wav->size);
165 fwrite(&b[4], 12, 1, fi);
168 c[2] = wav->channels;
169 c[3] = wav->channels>>8;
170 c[4] = wav->sampsPerSec;
171 c[5] = wav->sampsPerSec>>8;
172 c[6] = wav->sampsPerSec>>16;
173 c[7] = wav->sampsPerSec>>24;
174 c[8] = wav->bytesPerSec;
175 c[9] = wav->bytesPerSec>>8;
176 c[10] = wav->bytesPerSec>>16;
177 c[11] = wav->bytesPerSec>>24;
179 c[13] = wav->align>>8;
182 fwrite(c, 16, 1, fi);
183 fwrite(&b[16], 4, 1, fi);
186 c[2] = wav->size>>16;
187 c[3] = wav->size>>24;
189 printf("writing %d converted bytes\n", wav->size);
190 fwrite(wav->data,wav->size,1,fi);
195 void wav_print(struct WAV*wav)
197 printf("tag:%04x channels:%d samples/sec:%d bytes/sec:%d align:%d bits/sample:%d size:%d\n",
198 wav->tag, wav->channels, wav->sampsPerSec, wav->bytesPerSec,
199 wav->align, wav->bps, wav->size);
202 int wav_convert2mono(struct WAV*src, struct WAV*dest, int rate)
204 int samplelen=src->size/src->align;
209 int channels=src->channels;
213 dest->sampsPerSec = rate;
217 dest->tag = src->tag;
218 dest->bytesPerSec = dest->sampsPerSec*dest->align;
220 ratio = (double)dest->sampsPerSec/(double)src->sampsPerSec;
221 fill = (int)(ratio+1)*2;
223 dest->data = (unsigned char*)malloc((int)(samplelen*ratio*2)+128);
226 dest->size = (int)(samplelen*ratio)*2;
230 for(i=0; i<src->size; i+=channels) {
231 int pos2 = ((int)pos)*2;
232 dest->data[pos2] = 0;
233 dest->data[pos2+1] = src->data[i]+128;
237 for(i=0; i<src->size; i+=channels) {
239 int pos2 = ((int)pos)*2;
240 for(j=0;j<fill;j+=2) {
241 dest->data[pos2+j+0] = 0;
242 dest->data[pos2+j+1] = src->data[i]+128;
247 } else if(bps == 16) {
249 for(i=0; i<src->size/2; i+=channels) {
250 int pos2 = ((int)pos)*2;
251 dest->data[pos2+0]=src->data[i*2];
252 dest->data[pos2+1]=src->data[i*2+1];
256 for(i=0; i<src->size/2; i+=channels) {
258 int pos2 = ((int)pos)*2;
259 for(j=0;j<fill;j+=2) {
260 dest->data[pos2+j+0] = src->data[i*2];
261 dest->data[pos2+j+1] = src->data[i*2+1];
266 } else if(bps == 32) {
268 for(i=0; i<src->size/4; i+=channels) {
269 int pos2 = ((int)pos)*2;
270 dest->data[pos2+0]=src->data[i*4+2];
271 dest->data[pos2+1]=src->data[i*4+3];
275 for(i=0; i<src->size/4; i+=channels) {
277 int pos2 = ((int)pos)*2;
278 for(j=0;j<fill;j+=2) {
279 dest->data[pos2+j+0] = src->data[i*4+2];
280 dest->data[pos2+j+1] = src->data[i*4+3];
286 fprintf(stderr, "Unsupported bitspersample value: %d\n", bps);