summaryrefslogtreecommitdiff
path: root/tools/wavtrim.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/wavtrim.c')
-rw-r--r--tools/wavtrim.c33
1 files changed, 19 insertions, 14 deletions
diff --git a/tools/wavtrim.c b/tools/wavtrim.c
index 86434235ba..3d77b4e972 100644
--- a/tools/wavtrim.c
+++ b/tools/wavtrim.c
@@ -75,6 +75,8 @@ int main (int argc, char** argv)
75 int bps; /* byte per sample */ 75 int bps; /* byte per sample */
76 int sps; /* samples per second */ 76 int sps; /* samples per second */
77 int datapos; /* where the payload starts */ 77 int datapos; /* where the payload starts */
78 int datalen; /* Length of the data chunk */
79 unsigned char *databuf; /* Pointer to the data chunk payload */
78 int skip_head, skip_tail, pad_head, pad_tail; 80 int skip_head, skip_tail, pad_head, pad_tail;
79 int i; 81 int i;
80 int max_silence = 0; 82 int max_silence = 0;
@@ -123,6 +125,7 @@ int main (int argc, char** argv)
123 125
124 bps = Read16(pBuf + 32); 126 bps = Read16(pBuf + 32);
125 datapos = 28 + Read16(pBuf + 16); 127 datapos = 28 + Read16(pBuf + 16);
128 databuf = &pBuf[datapos];
126 129
127 if (Read32(pBuf) != 0x46464952 /* "RIFF" */ 130 if (Read32(pBuf) != 0x46464952 /* "RIFF" */
128 || Read32(pBuf+8) != 0x45564157 /* "WAVE" */ 131 || Read32(pBuf+8) != 0x45564157 /* "WAVE" */
@@ -133,6 +136,8 @@ int main (int argc, char** argv)
133 free(pBuf); 136 free(pBuf);
134 return -1; 137 return -1;
135 } 138 }
139
140 datalen = Read32(pBuf+datapos-4);
136 141
137 sps = Read32(pBuf + 24); 142 sps = Read32(pBuf + 24);
138 pad_head = sps * 10 / 1000; /* 10 ms */ 143 pad_head = sps * 10 / 1000; /* 10 ms */
@@ -144,54 +149,54 @@ int main (int argc, char** argv)
144 max_silence >>= 8; 149 max_silence >>= 8;
145 150
146 /* clip the start */ 151 /* clip the start */
147 for (i=datapos; i<lFileSize; i++) 152 for (i=0; i<datalen; i++)
148 { 153 {
149 sample8 = pBuf[i] - 0x80; 154 sample8 = databuf[i] - 0x80;
150 if (abs(sample8) > max_silence) 155 if (abs(sample8) > max_silence)
151 break; 156 break;
152 } 157 }
153 skip_head = i - datapos; 158 skip_head = i;
154 skip_head = (skip_head > pad_head) ? skip_head - pad_head : 0; 159 skip_head = (skip_head > pad_head) ? skip_head - pad_head : 0;
155 160
156 /* clip the end */ 161 /* clip the end */
157 for (i=lFileSize-1; i>datapos+skip_head; i--) 162 for (i=datalen-1; i>skip_head; i--)
158 { 163 {
159 sample8 = pBuf[i] - 0x80; 164 sample8 = databuf[i] - 0x80;
160 if (abs(sample8) > max_silence) 165 if (abs(sample8) > max_silence)
161 break; 166 break;
162 } 167 }
163 skip_tail = lFileSize - 1 - i; 168 skip_tail = datalen - 1 - i;
164 skip_tail = (skip_tail > pad_tail) ? skip_tail - pad_tail : 0; 169 skip_tail = (skip_tail > pad_tail) ? skip_tail - pad_tail : 0;
165 } 170 }
166 else if (bps == 2) /* 16 bit samples */ 171 else if (bps == 2) /* 16 bit samples */
167 { 172 {
168 173
169 /* clip the start */ 174 /* clip the start */
170 for (i=datapos; i<lFileSize; i+=2) 175 for (i=0; i<datalen; i+=2)
171 { 176 {
172 sample16 = *(short *)(pBuf + i); 177 sample16 = *(short *)(databuf + i);
173 if (abs(sample16) > max_silence) 178 if (abs(sample16) > max_silence)
174 break; 179 break;
175 } 180 }
176 skip_head = i - datapos; 181 skip_head = i;
177 skip_head = (skip_head > 2 * pad_head) ? 182 skip_head = (skip_head > 2 * pad_head) ?
178 skip_head - 2 * pad_head : 0; 183 skip_head - 2 * pad_head : 0;
179 184
180 /* clip the end */ 185 /* clip the end */
181 for (i=lFileSize-2; i>datapos+skip_head; i-=2) 186 for (i=datalen-2; i>skip_head; i-=2)
182 { 187 {
183 sample16 = *(short *)(pBuf + i); 188 sample16 = *(short *)(databuf + i);
184 if (abs(sample16) > max_silence) 189 if (abs(sample16) > max_silence)
185 break; 190 break;
186 } 191 }
187 skip_tail = lFileSize - 2 - i; 192 skip_tail = datalen - 2 - i;
188 skip_tail = (skip_tail > 2 * pad_tail) ? 193 skip_tail = (skip_tail > 2 * pad_tail) ?
189 skip_tail - 2 * pad_tail : 0; 194 skip_tail - 2 * pad_tail : 0;
190 } 195 }
191 196
192 /* update the size in the headers */ 197 /* update the size in the headers */
193 Write32(pBuf+4, Read32(pBuf+4) - skip_head - skip_tail); 198 Write32(pBuf+4, Read32(pBuf+4) - skip_head - skip_tail);
194 Write32(pBuf+datapos-4, Read32(pBuf+datapos-4) - skip_head - skip_tail); 199 Write32(pBuf+datapos-4, datalen - skip_head - skip_tail);
195 200
196 pFile = fopen(argv[1], "wb"); 201 pFile = fopen(argv[1], "wb");
197 if (pFile == NULL) 202 if (pFile == NULL)
@@ -202,7 +207,7 @@ int main (int argc, char** argv)
202 207
203 /* write the new file */ 208 /* write the new file */
204 fwrite(pBuf, 1, datapos, pFile); /* write header */ 209 fwrite(pBuf, 1, datapos, pFile); /* write header */
205 fwrite(pBuf + datapos + skip_head, 1, lFileSize - datapos - skip_head - skip_tail, pFile); 210 fwrite(pBuf + datapos + skip_head, 1, datalen - skip_head - skip_tail, pFile);
206 fclose(pFile); 211 fclose(pFile);
207 212
208 free(pBuf); 213 free(pBuf);