Android: Fix long unnoticed typo in function call, leading to the wrong function...
[kugel-rb.git] / tools / wavtrim.c
blob36a1151b4d75e3c93f06202300f73cf7ce755ae4
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 by Jörg Hohensohn
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 * Details at http://www.rockbox.org/twiki/bin/view/Main/VoiceBuilding
22 ****************************************************************************/
25 #include <stdio.h> /* for file I/O */
26 #include <stdlib.h> /* for malloc */
28 #include "wavtrim.h"
30 /* place a 32 bit value into memory, little endian */
31 void Write32(unsigned char* pByte, unsigned long value)
33 pByte[0] = (unsigned char)value;
34 pByte[1] = (unsigned char)(value >> 8);
35 pByte[2] = (unsigned char)(value >> 16);
36 pByte[3] = (unsigned char)(value >> 24) ;
40 /* read a 32 bit value from memory, little endian */
41 unsigned long Read32(unsigned char* pByte)
43 unsigned long value = 0;
45 value |= (unsigned long)pByte[0];
46 value |= (unsigned long)pByte[1] << 8;
47 value |= (unsigned long)pByte[2] << 16;
48 value |= (unsigned long)pByte[3] << 24;
50 return value;
54 /* place a 16 bit value into memory, little endian */
55 void Write16(unsigned char* pByte, unsigned short value)
57 pByte[0] = (unsigned char)value;
58 pByte[1] = (unsigned char)(value >> 8);
62 /* read a 16 bit value from memory, little endian */
63 unsigned long Read16(unsigned char* pByte)
65 unsigned short value = 0;
67 value |= (unsigned short)pByte[0];
68 value |= (unsigned short)pByte[1] << 8;
70 return value;
73 int wavtrim(char * filename, int maxsilence ,char* errstring,int errsize)
75 FILE* pFile;
76 long lFileSize, lGot;
77 unsigned char* pBuf;
78 int bps; /* byte per sample */
79 int sps; /* samples per second */
80 int datapos; /* where the payload starts */
81 int datalen; /* Length of the data chunk */
82 unsigned char *databuf; /* Pointer to the data chunk payload */
83 int skip_head, skip_tail, pad_head, pad_tail;
84 int i;
85 int max_silence = maxsilence;
86 signed char sample8;
87 short sample16;
89 pFile = fopen(filename, "rb");
90 if (pFile == NULL)
92 snprintf(errstring,errsize,"Error opening file %s for reading\n", filename);
93 return -1;
96 fseek(pFile, 0, SEEK_END);
97 lFileSize = ftell(pFile);
98 fseek(pFile, 0, SEEK_SET);
100 pBuf = malloc(lFileSize);
101 if (pBuf == NULL)
103 snprintf(errstring,errsize,"Out of memory to allocate %ld bytes for file.\n", lFileSize);
104 fclose(pFile);
105 return -1;
108 lGot = fread(pBuf, 1, lFileSize, pFile);
109 fclose(pFile);
110 if (lGot != lFileSize)
112 snprintf(errstring,errsize,"File read error, got only %ld bytes out of %ld.\n", lGot, lFileSize);
113 free(pBuf);
114 return -1;
118 bps = Read16(pBuf + 32);
119 datapos = 28 + Read16(pBuf + 16);
120 databuf = &pBuf[datapos];
122 if (Read32(pBuf) != 0x46464952 /* "RIFF" */
123 || Read32(pBuf+8) != 0x45564157 /* "WAVE" */
124 || Read32(pBuf+12) != 0x20746d66 /* "fmt " */
125 || Read32(pBuf+datapos-8) != 0x61746164) /* "data" */
127 snprintf(errstring,errsize,"No valid input WAV file?\n");
128 free(pBuf);
129 return -1;
132 datalen = Read32(pBuf+datapos-4);
133 skip_head = skip_tail = 0;
135 sps = Read32(pBuf + 24);
136 pad_head = sps * 10 / 1000; /* 10 ms */
137 pad_tail = sps * 10 / 1000; /* 10 ms */
139 if (bps == 1) /* 8 bit samples */
142 max_silence >>= 8;
144 /* clip the start */
145 for (i=0; i<datalen; i++)
147 sample8 = databuf[i] - 0x80;
148 if (abs(sample8) > max_silence)
149 break;
151 skip_head = i;
152 skip_head = (skip_head > pad_head) ? skip_head - pad_head : 0;
154 /* clip the end */
155 for (i=datalen-1; i>skip_head; i--)
157 sample8 = databuf[i] - 0x80;
158 if (abs(sample8) > max_silence)
159 break;
161 skip_tail = datalen - 1 - i;
162 skip_tail = (skip_tail > pad_tail) ? skip_tail - pad_tail : 0;
164 else if (bps == 2) /* 16 bit samples */
167 /* clip the start */
168 for (i=0; i<datalen; i+=2)
170 /* samples are little endian */
171 sample16 = (*(databuf + i + 1) << 8) | *(databuf + i);
173 if (abs(sample16) > max_silence)
174 break;
176 skip_head = i;
177 skip_head = (skip_head > 2 * pad_head) ?
178 skip_head - 2 * pad_head : 0;
180 /* clip the end */
181 for (i=datalen-2; i>skip_head; i-=2)
183 /* samples are little endian */
184 sample16 = (*(databuf + i + 1) << 8) | *(databuf + i);
185 if (abs(sample16) > max_silence)
186 break;
188 skip_tail = datalen - 2 - i;
189 skip_tail = (skip_tail > 2 * pad_tail) ?
190 skip_tail - 2 * pad_tail : 0;
193 /* update the size in the headers */
194 Write32(pBuf+4, Read32(pBuf+4) - skip_head - skip_tail);
195 Write32(pBuf+datapos-4, datalen - skip_head - skip_tail);
197 pFile = fopen(filename, "wb");
198 if (pFile == NULL)
200 snprintf(errstring,errsize,"Error opening file %s for writing\n",filename);
201 free(pBuf);
202 return -1;
205 /* write the new file */
206 if ((int)fwrite(pBuf, 1, datapos, pFile) != datapos) /* write header */
208 snprintf(errstring,errsize,"Error writing file %s header\n",filename);
209 fclose(pFile);
210 free(pBuf);
211 return -1;
213 if ((int)fwrite(pBuf + datapos + skip_head, 1, datalen - skip_head - skip_tail, pFile)
214 != datalen - skip_head - skip_tail)
216 snprintf(errstring,errsize,"Error writing file %s data\n",filename);
217 fclose(pFile);
218 free(pBuf);
219 return -1;
221 fclose(pFile);
223 free(pBuf);
224 return 0;
228 #ifndef RBUTIL
229 int main (int argc, char** argv)
231 int max_silence = 0;
232 char errbuffer[255];
233 int ret=0;
235 if (argc < 2)
237 printf("wavtrim removes silence at the begin and end of a WAV file.\n");
238 printf("usage: wavtrim <filename.wav> [<max_silence>]\n");
239 return 0;
242 if (argc == 3)
244 max_silence = atoi(argv[2]);
248 ret = wavtrim(argv[1],max_silence,errbuffer,255 );
249 if( ret< 0)
251 printf("%s", errbuffer);
253 return ret;
255 #endif
257 RIFF Chunk (12 bytes in length total)
258 0 - 3 "RIFF" (ASCII Characters)
259 4 - 7 Total Length Of Package To Follow (Binary, little endian)
260 8 - 11 "WAVE" (ASCII Characters)
263 FORMAT Chunk (24 or 26 bytes in length total) Byte Number
264 12 - 15 "fmt_" (ASCII Characters)
265 16 - 19 Length Of FORMAT Chunk (Binary, 0x10 or 0x12 seen)
266 20 - 21 Always 0x01
267 22 - 23 Channel Numbers (Always 0x01=Mono, 0x02=Stereo)
268 24 - 27 Sample Rate (Binary, in Hz)
269 28 - 31 Bytes Per Second
270 32 - 33 Bytes Per Sample: 1=8 bit Mono, 2=8 bit Stereo or 16 bit Mono, 4=16 bit Stereo
271 34 - 35 Bits Per Sample
274 DATA Chunk Byte Number
275 36 - 39 "data" (ASCII Characters)
276 40 - 43 Length Of Data To Follow
277 44 - end
278 Data (Samples)