-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeInitialSilence.cpp
74 lines (58 loc) · 1.89 KB
/
removeInitialSilence.cpp
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
#include <stdio.h>
#include <sndfile.h>
#include <stdlib.h>
#include <string.h>
#define READ_SAMPLES 8192
#define EPSILON ((double)0.01)
void removeSilence(const char *fname) {
const char *prefix = "trimmed-";
char *fname_new = (char *)malloc(strlen(fname) + strlen(prefix) + 1);
sprintf(fname_new, "%s%s", prefix, fname);
SF_INFO fileInfo;
fileInfo.format = 0;
SNDFILE *fr = sf_open(fname, SFM_READ, &fileInfo);
SNDFILE *fw = sf_open(fname_new, SFM_WRITE, &fileInfo);
free(fname_new);
if (!fr || !fw) {
sf_close(fr); sf_close(fw);
printf("Error: couldn't open %s.\n", fname);
throw;
}
double *buffer = (double*)malloc(sizeof(double) * fileInfo.channels * READ_SAMPLES);
if (!buffer) {
sf_close(fr); sf_close(fw);
printf("Error: can't allocate enough memory.\n");
throw;
}
sf_count_t framesRead = 0;
bool soundStarted = false;
int trimmedFrames = 0;
while (true) {
framesRead = sf_readf_double(fr, buffer, READ_SAMPLES);
if (framesRead == 0)
break;
int i = 0;
while (!soundStarted && i < framesRead/2) {
if (buffer[2*i] > EPSILON || buffer[2*i+1] > EPSILON) {
soundStarted = true;
break;
}
i++;
}
trimmedFrames += i;
if (soundStarted)
sf_writef_double(fw, buffer + 2*i, framesRead - 2*i);
}
free(buffer);
sf_close(fr); sf_close(fw);
printf("%s: trimmed %i frames\n", fname, trimmedFrames);
}
int main(int argc, char **argv) {
if (argc < 2) {
printf("Usage: %s SOUNDFILE1 ... SOUNDFILEN\n\tRemoves initial silence from supplied sound files.\n\tFor each input file, a new one is created, called trimmed-OLDNAME.\n", argv[0]);
} else {
for (int i = 1; i < argc; i++) {
removeSilence(argv[i]);
}
}
}