Fix two warnings that appear with gcc4.4.3. The one in recording.c ("compact_view...
[kugel-rb.git] / tools / wavtrim.c
blobb82f270c8d6845e88d43f86ee43762934c115355
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 sample16 = *(short *)(databuf + i);
171 if (abs(sample16) > max_silence)
172 break;
174 skip_head = i;
175 skip_head = (skip_head > 2 * pad_head) ?
176 skip_head - 2 * pad_head : 0;
178 /* clip the end */
179 for (i=datalen-2; i>skip_head; i-=2)
181 sample16 = *(short *)(databuf + i);
182 if (abs(sample16) > max_silence)
183 break;
185 skip_tail = datalen - 2 - i;
186 skip_tail = (skip_tail > 2 * pad_tail) ?
187 skip_tail - 2 * pad_tail : 0;
190 /* update the size in the headers */
191 Write32(pBuf+4, Read32(pBuf+4) - skip_head - skip_tail);
192 Write32(pBuf+datapos-4, datalen - skip_head - skip_tail);
194 pFile = fopen(filename, "wb");
195 if (pFile == NULL)
197 snprintf(errstring,errsize,"Error opening file %s for writing\n",filename);
198 return -1;
201 /* write the new file */
202 fwrite(pBuf, 1, datapos, pFile); /* write header */
203 fwrite(pBuf + datapos + skip_head, 1, datalen - skip_head - skip_tail, pFile);
204 fclose(pFile);
206 free(pBuf);
207 return 0;
211 #ifndef RBUTIL
212 int main (int argc, char** argv)
214 int max_silence = 0;
215 char errbuffer[255];
216 int ret=0;
218 if (argc < 2)
220 printf("wavtrim removes silence at the begin and end of a WAV file.\n");
221 printf("usage: wavtrim <filename.wav> [<max_silence>]\n");
222 return 0;
225 if (argc == 3)
227 max_silence = atoi(argv[2]);
231 ret = wavtrim(argv[1],max_silence,errbuffer,255 );
232 if( ret< 0)
234 printf(errbuffer);
236 return ret;
238 #endif
240 RIFF Chunk (12 bytes in length total)
241 0 - 3 "RIFF" (ASCII Characters)
242 4 - 7 Total Length Of Package To Follow (Binary, little endian)
243 8 - 11 "WAVE" (ASCII Characters)
246 FORMAT Chunk (24 or 26 bytes in length total) Byte Number
247 12 - 15 "fmt_" (ASCII Characters)
248 16 - 19 Length Of FORMAT Chunk (Binary, 0x10 or 0x12 seen)
249 20 - 21 Always 0x01
250 22 - 23 Channel Numbers (Always 0x01=Mono, 0x02=Stereo)
251 24 - 27 Sample Rate (Binary, in Hz)
252 28 - 31 Bytes Per Second
253 32 - 33 Bytes Per Sample: 1=8 bit Mono, 2=8 bit Stereo or 16 bit Mono, 4=16 bit Stereo
254 34 - 35 Bits Per Sample
257 DATA Chunk Byte Number
258 36 - 39 "data" (ASCII Characters)
259 40 - 43 Length Of Data To Follow
260 44 - end
261 Data (Samples)