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)
40 block->size = b[0]|b[1]<<8|b[2]<<16|b[3]<<24;
41 /*printf("Chunk: [%c%c%c%c] (%d bytes)\n",
42 block->id[0],block->id[1],
43 block->id[2],block->id[3],
48 int readWAV(char* filename, struct WAV*wav)
50 FILE*fi = fopen(filename, "rb");
53 struct WAVBlock block;
57 fseek(fi, 0, SEEK_END);
59 fseek(fi, 0, SEEK_SET);
61 //printf("Filesize: %d\n", filesize);
63 if(!getWAVBlock (fi, &block))
65 if(strncmp(block.id,"RIFF",4)) {
66 fprintf(stderr, "readWAV: not a wav file\n");
69 if(block.size + 8 < filesize)
70 fprintf(stderr, "readWAV: warning - more tags (%d extra bytes)\n",
71 filesize - block.size - 8);
72 if(block.size + 8 > filesize)
73 fprintf(stderr, "readWAV: warning - short file (%d bytes missing)\n",
74 block.size + 8 - filesize);
75 if(fread(b, 1, 4, fi) < 4) {
78 if(strncmp(b, "WAVE", 4)) {
79 fprintf(stderr, "readWAV: not a WAV file (2)\n");
85 getWAVBlock(fi, &block);
87 if(!strncmp(block.id, "fmt ", 4)) {
88 if(fread(&b, 1, 16, fi)<16)
90 wav->tag = b[0]|b[1]<<8;
91 wav->channels = b[2]|b[3]<<8;
92 wav->sampsPerSec = b[4]|b[5]<<8|b[6]<<16|b[7]<<24;
93 wav->bytesPerSec = b[8]|b[9]<<8|b[10]<<16|b[11]<<24;
94 wav->align = b[12]|b[13]<<8;
95 wav->bps = b[14]|b[15]<<8;
96 } else if (!strncmp(block.id, "LIST", 4)) {
97 // subchunk ICMT (comment) may exist
98 } else if (!strncmp(block.id, "data", 4)) {
99 wav->data = malloc(block.size);
101 fprintf(stderr, "Out of memory (%d bytes needed)", block.size);
104 if(fread(wav->data, 1, block.size, fi) < block.size)
106 wav->size = block.size;
109 fseek(fi, pos, SEEK_SET);
111 while (pos < filesize);
116 int writeWAV(char*filename, struct WAV*wav)
118 FILE*fi = fopen(filename, "wb");
119 char*b="RIFFWAVEfmt \x10\0\0\0data";
121 unsigned long int w32;
125 w32=(/*fmt*/8+0x10+/*data*/8+wav->size);
131 fwrite(&b[4], 12, 1, fi);
134 c[2] = wav->channels;
135 c[3] = wav->channels>>8;
136 c[4] = wav->sampsPerSec;
137 c[5] = wav->sampsPerSec>>8;
138 c[6] = wav->sampsPerSec>>16;
139 c[7] = wav->sampsPerSec>>24;
140 c[8] = wav->bytesPerSec;
141 c[9] = wav->bytesPerSec>>8;
142 c[10] = wav->bytesPerSec>>16;
143 c[11] = wav->bytesPerSec>>24;
145 c[13] = wav->align>>8;
148 fwrite(c, 16, 1, fi);
149 fwrite(&b[16], 4, 1, fi);
152 c[2] = wav->size>>16;
153 c[3] = wav->size>>24;
155 printf("writing %d converted bytes\n", wav->size);
156 fwrite(wav->data,wav->size,1,fi);
161 void printWAVInfo(struct WAV*wav)
163 printf("tag:%04x channels:%d samples/sec:%d bytes/sec:%d align:%d bits/sample:%d size:%d\n",
164 wav->tag, wav->channels, wav->sampsPerSec, wav->bytesPerSec,
165 wav->align, wav->bps, wav->size);
168 int convertWAV2mono(struct WAV*src, struct WAV*dest, int rate)
170 int samplelen=src->size/src->align;
175 int channels=src->channels;
179 dest->sampsPerSec = rate;
183 dest->tag = src->tag;
184 dest->bytesPerSec = dest->sampsPerSec*dest->align;
186 ratio = (double)dest->sampsPerSec/(double)src->sampsPerSec;
187 fill = (int)(ratio+1)*2;
189 dest->data = (unsigned char*)malloc((int)(samplelen*ratio*2)+128);
192 dest->size = (int)(samplelen*ratio)*2;
196 for(i=0; i<src->size; i+=channels) {
197 int pos2 = ((int)pos)*2;
198 dest->data[pos2] = 0;
199 dest->data[pos2+1] = src->data[i]+128;
203 for(i=0; i<src->size; i+=channels) {
205 int pos2 = ((int)pos)*2;
206 for(j=0;j<fill;j+=2) {
207 dest->data[pos2+j+0] = 0;
208 dest->data[pos2+j+1] = src->data[i]+128;
213 } else if(bps == 16) {
215 for(i=0; i<src->size/2; i+=channels) {
216 int pos2 = ((int)pos)*2;
217 dest->data[pos2+0]=src->data[i*2];
218 dest->data[pos2+1]=src->data[i*2+1];
222 for(i=0; i<src->size/2; i+=channels) {
224 int pos2 = ((int)pos)*2;
225 for(j=0;j<fill;j+=2) {
226 dest->data[pos2+j+0] = src->data[i*2];
227 dest->data[pos2+j+1] = src->data[i*2+1];
232 } else if(bps == 32) {
234 for(i=0; i<src->size/4; i+=channels) {
235 int pos2 = ((int)pos)*2;
236 dest->data[pos2+0]=src->data[i*4+2];
237 dest->data[pos2+1]=src->data[i*4+3];
241 for(i=0; i<src->size/4; i+=channels) {
243 int pos2 = ((int)pos)*2;
244 for(j=0;j<fill;j+=2) {
245 dest->data[pos2+j+0] = src->data[i*4+2];
246 dest->data[pos2+j+1] = src->data[i*4+3];