summaryrefslogtreecommitdiff
path: root/tools/wavtrim.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/wavtrim.c')
-rw-r--r--tools/wavtrim.c69
1 files changed, 44 insertions, 25 deletions
diff --git a/tools/wavtrim.c b/tools/wavtrim.c
index 3d77b4e972..fe212ef93c 100644
--- a/tools/wavtrim.c
+++ b/tools/wavtrim.c
@@ -23,6 +23,8 @@
23#include <stdio.h> /* for file I/O */ 23#include <stdio.h> /* for file I/O */
24#include <stdlib.h> /* for malloc */ 24#include <stdlib.h> /* for malloc */
25 25
26#include "wavtrim.h"
27
26/* place a 32 bit value into memory, little endian */ 28/* place a 32 bit value into memory, little endian */
27void Write32(unsigned char* pByte, unsigned long value) 29void Write32(unsigned char* pByte, unsigned long value)
28{ 30{
@@ -66,9 +68,8 @@ unsigned long Read16(unsigned char* pByte)
66 return value; 68 return value;
67} 69}
68 70
69 71int wavtrim(char * filename, int maxsilence ,char* errstring,int errsize)
70int main (int argc, char** argv) 72{
71{
72 FILE* pFile; 73 FILE* pFile;
73 long lFileSize, lGot; 74 long lFileSize, lGot;
74 unsigned char* pBuf; 75 unsigned char* pBuf;
@@ -79,29 +80,17 @@ int main (int argc, char** argv)
79 unsigned char *databuf; /* Pointer to the data chunk payload */ 80 unsigned char *databuf; /* Pointer to the data chunk payload */
80 int skip_head, skip_tail, pad_head, pad_tail; 81 int skip_head, skip_tail, pad_head, pad_tail;
81 int i; 82 int i;
82 int max_silence = 0; 83 int max_silence = maxsilence;
83 signed char sample8; 84 signed char sample8;
84 short sample16; 85 short sample16;
85 86
86 if (argc < 2) 87 pFile = fopen(filename, "rb");
87 {
88 printf("wavtrim removes silence at the begin and end of a WAV file.\n");
89 printf("usage: wavtrim <filename.wav> [<max_silence>]\n");
90 return 0;
91 }
92
93 if (argc == 3)
94 {
95 max_silence = atoi(argv[2]);
96 }
97
98 pFile = fopen(argv[1], "rb");
99 if (pFile == NULL) 88 if (pFile == NULL)
100 { 89 {
101 printf("Error opening file %s for reading\n", argv[1]); 90 snprintf(errstring,errsize,"Error opening file %s for reading\n", filename);
102 return -1; 91 return -1;
103 } 92 }
104 93
105 fseek(pFile, 0, SEEK_END); 94 fseek(pFile, 0, SEEK_END);
106 lFileSize = ftell(pFile); 95 lFileSize = ftell(pFile);
107 fseek(pFile, 0, SEEK_SET); 96 fseek(pFile, 0, SEEK_SET);
@@ -109,7 +98,7 @@ int main (int argc, char** argv)
109 pBuf = malloc(lFileSize); 98 pBuf = malloc(lFileSize);
110 if (pBuf == NULL) 99 if (pBuf == NULL)
111 { 100 {
112 printf("Out of memory to allocate %ld bytes for file.\n", lFileSize); 101 snprintf(errstring,errsize,"Out of memory to allocate %ld bytes for file.\n", lFileSize);
113 fclose(pFile); 102 fclose(pFile);
114 return -1; 103 return -1;
115 } 104 }
@@ -118,11 +107,12 @@ int main (int argc, char** argv)
118 fclose(pFile); 107 fclose(pFile);
119 if (lGot != lFileSize) 108 if (lGot != lFileSize)
120 { 109 {
121 printf("File read error, got only %ld bytes out of %ld.\n", lGot, lFileSize); 110 snprintf(errstring,errsize,"File read error, got only %ld bytes out of %ld.\n", lGot, lFileSize);
122 free(pBuf); 111 free(pBuf);
123 return -1; 112 return -1;
124 } 113 }
125 114
115
126 bps = Read16(pBuf + 32); 116 bps = Read16(pBuf + 32);
127 datapos = 28 + Read16(pBuf + 16); 117 datapos = 28 + Read16(pBuf + 16);
128 databuf = &pBuf[datapos]; 118 databuf = &pBuf[datapos];
@@ -132,7 +122,7 @@ int main (int argc, char** argv)
132 || Read32(pBuf+12) != 0x20746d66 /* "fmt " */ 122 || Read32(pBuf+12) != 0x20746d66 /* "fmt " */
133 || Read32(pBuf+datapos-8) != 0x61746164) /* "data" */ 123 || Read32(pBuf+datapos-8) != 0x61746164) /* "data" */
134 { 124 {
135 printf("No valid input WAV file?\n", lGot, lFileSize); 125 snprintf(errstring,errsize,"No valid input WAV file?\n");
136 free(pBuf); 126 free(pBuf);
137 return -1; 127 return -1;
138 } 128 }
@@ -198,10 +188,10 @@ int main (int argc, char** argv)
198 Write32(pBuf+4, Read32(pBuf+4) - skip_head - skip_tail); 188 Write32(pBuf+4, Read32(pBuf+4) - skip_head - skip_tail);
199 Write32(pBuf+datapos-4, datalen - skip_head - skip_tail); 189 Write32(pBuf+datapos-4, datalen - skip_head - skip_tail);
200 190
201 pFile = fopen(argv[1], "wb"); 191 pFile = fopen(filename, "wb");
202 if (pFile == NULL) 192 if (pFile == NULL)
203 { 193 {
204 printf("Error opening file %s for writing\n", argv[1]); 194 snprintf(errstring,errsize,"Error opening file %s for writing\n",filename);
205 return -1; 195 return -1;
206 } 196 }
207 197
@@ -212,8 +202,37 @@ int main (int argc, char** argv)
212 202
213 free(pBuf); 203 free(pBuf);
214 return 0; 204 return 0;
205
215} 206}
216 207
208#ifndef RBUTIL
209int main (int argc, char** argv)
210{
211 int max_silence = 0;
212 char errbuffer[255];
213 int ret=0;
214
215 if (argc < 2)
216 {
217 printf("wavtrim removes silence at the begin and end of a WAV file.\n");
218 printf("usage: wavtrim <filename.wav> [<max_silence>]\n");
219 return 0;
220 }
221
222 if (argc == 3)
223 {
224 max_silence = atoi(argv[2]);
225 }
226
227
228 ret = wavtrim(argv[1],max_silence,errbuffer,255 );
229 if( ret< 0)
230 {
231 printf(errbuffer);
232 }
233 return ret;
234}
235#endif
217/* 236/*
218RIFF Chunk (12 bytes in length total) 237RIFF Chunk (12 bytes in length total)
2190 - 3 "RIFF" (ASCII Characters) 2380 - 3 "RIFF" (ASCII Characters)