summaryrefslogtreecommitdiffstats
path: root/tools/wavtrim.c
blob: 3d77b4e972ad03f2ee09b1f8d835bcfa5760a21a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2004 by Jrg Hohensohn
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * Details at http://www.rockbox.org/twiki/bin/view/Main/VoiceBuilding
 *
 ****************************************************************************/


#include <stdio.h>  /* for file I/O */
#include <stdlib.h> /* for malloc */

/* place a 32 bit value into memory, little endian */
void Write32(unsigned char* pByte, unsigned long value)
{
    pByte[0] = (unsigned char)value;    
    pByte[1] = (unsigned char)(value >> 8);    
    pByte[2] = (unsigned char)(value >> 16);
    pByte[3] = (unsigned char)(value >> 24) ;    
}


/* read a 32 bit value from memory, little endian */
unsigned long Read32(unsigned char* pByte)
{
    unsigned long value = 0;

    value |= (unsigned long)pByte[0];
    value |= (unsigned long)pByte[1] << 8;
    value |= (unsigned long)pByte[2] << 16;
    value |= (unsigned long)pByte[3] << 24;

    return value;
}


/* place a 16 bit value into memory, little endian */
void Write16(unsigned char* pByte, unsigned short value)
{
    pByte[0] = (unsigned char)value;    
    pByte[1] = (unsigned char)(value >> 8);    
}


/* read a 16 bit value from memory, little endian */
unsigned long Read16(unsigned char* pByte)
{
    unsigned short value = 0;

    value |= (unsigned short)pByte[0];
    value |= (unsigned short)pByte[1] << 8;

    return value;
}


int main (int argc, char** argv)
{
    FILE* pFile;
    long lFileSize, lGot;
    unsigned char* pBuf;
    int bps; /* byte per sample */
    int sps; /* samples per second */
    int datapos; /* where the payload starts */
    int datalen; /* Length of the data chunk */
    unsigned char *databuf; /* Pointer to the data chunk payload */
    int skip_head, skip_tail, pad_head, pad_tail;
    int i;
    int max_silence = 0;
    signed char sample8;
    short sample16;

    if (argc < 2)
    {
        printf("wavtrim removes silence at the begin and end of a WAV file.\n");
        printf("usage: wavtrim <filename.wav> [<max_silence>]\n");
        return 0;
    }
    
    if (argc == 3)
    {
        max_silence = atoi(argv[2]);
    }

    pFile = fopen(argv[1], "rb");
    if (pFile == NULL)
    {
        printf("Error opening file %s for reading\n", argv[1]);
        return -1;
    }

    fseek(pFile, 0, SEEK_END);
    lFileSize = ftell(pFile);
    fseek(pFile, 0, SEEK_SET);

    pBuf = malloc(lFileSize);
    if (pBuf == NULL)
    {
        printf("Out of memory to allocate %ld bytes for file.\n", lFileSize);
        fclose(pFile);
        return -1;
    }

    lGot = fread(pBuf, 1, lFileSize, pFile);
    fclose(pFile);
    if (lGot != lFileSize)
    {
        printf("File read error, got only %ld bytes out of %ld.\n", lGot, lFileSize);
        free(pBuf);
        return -1;
    }

    bps = Read16(pBuf + 32);
    datapos = 28 + Read16(pBuf + 16);
    databuf = &pBuf[datapos];

    if (Read32(pBuf) != 0x46464952 /* "RIFF" */
     || Read32(pBuf+8) != 0x45564157 /* "WAVE" */
     || Read32(pBuf+12) != 0x20746d66 /* "fmt " */
     || Read32(pBuf+datapos-8) != 0x61746164) /* "data" */
    {
        printf("No valid input WAV file?\n", lGot, lFileSize);
        free(pBuf);
        return -1;
    }

    datalen = Read32(pBuf+datapos-4);
    
    sps = Read32(pBuf + 24);
    pad_head = sps * 10 / 1000; /* 10 ms */
    pad_tail = sps * 10 / 1000; /* 10 ms */

    if (bps == 1) /* 8 bit samples */
    {
        
        max_silence >>= 8;

        /* clip the start */
        for (i=0; i<datalen; i++)
        {
            sample8 = databuf[i] - 0x80;
            if (abs(sample8) > max_silence)
                break;
        }
        skip_head = i;
        skip_head = (skip_head > pad_head) ? skip_head - pad_head : 0;
        
        /* clip the end */
        for (i=datalen-1; i>skip_head; i--)
        {
            sample8 = databuf[i] - 0x80;
            if (abs(sample8) > max_silence)
                break;
        }
        skip_tail = datalen - 1 - i;
        skip_tail = (skip_tail > pad_tail) ? skip_tail - pad_tail : 0;
    }
    else if (bps == 2) /* 16 bit samples */
    {

        /* clip the start */
        for (i=0; i<datalen; i+=2)
        {
            sample16 = *(short *)(databuf + i);
            if (abs(sample16) > max_silence)
                break;
        }
        skip_head = i;
        skip_head = (skip_head > 2 * pad_head) ?
                     skip_head - 2 * pad_head : 0;

        /* clip the end */
        for (i=datalen-2; i>skip_head; i-=2)
        {
            sample16 = *(short *)(databuf + i);
            if (abs(sample16) > max_silence)
                break;
        }
        skip_tail = datalen - 2 - i;
        skip_tail = (skip_tail > 2 * pad_tail) ?
                     skip_tail - 2 * pad_tail : 0;
    }

    /* update the size in the headers */
    Write32(pBuf+4, Read32(pBuf+4) - skip_head - skip_tail);
    Write32(pBuf+datapos-4, datalen - skip_head - skip_tail);

    pFile = fopen(argv[1], "wb");
    if (pFile == NULL)
    {
        printf("Error opening file %s for writing\n", argv[1]);
        return -1;
    }

    /* write the new file */
    fwrite(pBuf, 1, datapos, pFile); /* write header */
    fwrite(pBuf + datapos + skip_head, 1, datalen - skip_head - skip_tail, pFile);
    fclose(pFile);

    free(pBuf);
    return 0;
}

/*
RIFF Chunk (12 bytes in length total) 
0 - 3  "RIFF" (ASCII Characters)
4 - 7  Total Length Of Package To Follow (Binary, little endian)
8 - 11  "WAVE" (ASCII Characters)


FORMAT Chunk (24 or 26 bytes in length total) Byte Number
12 - 15  "fmt_" (ASCII Characters)
16 - 19  Length Of FORMAT Chunk (Binary, 0x10 or 0x12 seen)
20 - 21  Always 0x01
22 - 23  Channel Numbers (Always 0x01=Mono, 0x02=Stereo)
24 - 27 Sample Rate (Binary, in Hz)
28 - 31 Bytes Per Second
32 - 33 Bytes Per Sample: 1=8 bit Mono, 2=8 bit Stereo or 16 bit Mono, 4=16 bit Stereo
34 - 35 Bits Per Sample
 

DATA Chunk Byte Number
36 - 39 "data" (ASCII Characters)
40 - 43  Length Of Data To Follow
44 - end
 Data (Samples)
*/