1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 */
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;
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;
73 int wavtrim(char * filename
, int maxsilence
,char* errstring
,int errsize
)
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
;
85 int max_silence
= maxsilence
;
89 pFile
= fopen(filename
, "rb");
92 snprintf(errstring
,errsize
,"Error opening file %s for reading\n", filename
);
96 fseek(pFile
, 0, SEEK_END
);
97 lFileSize
= ftell(pFile
);
98 fseek(pFile
, 0, SEEK_SET
);
100 pBuf
= malloc(lFileSize
);
103 snprintf(errstring
,errsize
,"Out of memory to allocate %ld bytes for file.\n", lFileSize
);
108 lGot
= fread(pBuf
, 1, lFileSize
, pFile
);
110 if (lGot
!= lFileSize
)
112 snprintf(errstring
,errsize
,"File read error, got only %ld bytes out of %ld.\n", lGot
, lFileSize
);
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");
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 */
145 for (i
=0; i
<datalen
; i
++)
147 sample8
= databuf
[i
] - 0x80;
148 if (abs(sample8
) > max_silence
)
152 skip_head
= (skip_head
> pad_head
) ? skip_head
- pad_head
: 0;
155 for (i
=datalen
-1; i
>skip_head
; i
--)
157 sample8
= databuf
[i
] - 0x80;
158 if (abs(sample8
) > max_silence
)
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 */
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
)
177 skip_head
= (skip_head
> 2 * pad_head
) ?
178 skip_head
- 2 * pad_head
: 0;
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
)
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");
200 snprintf(errstring
,errsize
,"Error opening file %s for writing\n",filename
);
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
);
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
);
229 int main (int argc
, char** argv
)
237 printf("wavtrim removes silence at the begin and end of a WAV file.\n");
238 printf("usage: wavtrim <filename.wav> [<max_silence>]\n");
244 max_silence
= atoi(argv
[2]);
248 ret
= wavtrim(argv
[1],max_silence
,errbuffer
,255 );
251 printf("%s", errbuffer
);
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)
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