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
|
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include <sndfile.h>
static void print_usage(const char* name)
{
fprintf(stdout, "usage: %s\n", name);
}
#define BUFFER_LEN 16384
static float in_data[BUFFER_LEN];
static float out_data[BUFFER_LEN];
///////////////////////////////////////////////////////////////////////////////
void downmix(float* in, int len)
{
int i, j;
// Downmix stereo to mono
for (i = 0, j = 0; i < len; i+=2, j++) {
in[j] = (in[i] + in[i + 1]) / 2.0;
}
}
void process(float *in, float *out, const SF_INFO *info, int len)
{
int i;
// Call processing here
for (int i = 0; i < len / 2; i++) {
out[i * 2] = in[i];
out[i * 2 + 1] = -in[i];
}
}
int main(int argc, char** argv)
{
const char* my_name = argv[0];
const char* in_file_nm = NULL;
const char* out_file_nm = NULL;
float shift = 0.0;
int ch;
SF_INFO sf_info;
SNDFILE *in_file, *out_file;
int read_channels;
int read_block_sz = BUFFER_LEN;
int read_count;
while ((ch = getopt(argc, argv, "f:i:")) != -1) {
switch (ch) {
case 'i':
in_file_nm = optarg;
break;
case 'f':
shift = atof(optarg);
break;
default:
print_usage(my_name);
return 1;
}
}
if (optind == argc) {
print_usage(my_name);
return 1;
}
out_file_nm = argv[optind];
fprintf(stderr, "input: %s -> %s, shift: %f hz\n", in_file_nm, out_file_nm, shift);
memset(&sf_info, 0, sizeof(sf_info));
if (!(in_file = sf_open(in_file_nm, SFM_READ, &sf_info))) {
printf ("Unable to open input file %s.\n", in_file_nm);
puts (sf_strerror (NULL));
return 1;
}
if (sf_info.channels > 2)
{
printf ("Expected stereo or mono input file") ;
sf_close (in_file);
return 1;
}
read_channels = sf_info.channels;
sf_info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
sf_info.channels = 2;
if (!(out_file = sf_open(out_file_nm, SFM_WRITE, &sf_info))) {
printf ("Unable to open output file %s.\n", out_file_nm);
puts (sf_strerror (NULL)) ;
sf_close(in_file);
return 1 ;
}
while ((read_count = (int)sf_readf_float(in_file, in_data, BUFFER_LEN / 2))) {
if (read_channels == 2)
downmix(in_data, BUFFER_LEN);
process(in_data, out_data, &sf_info, BUFFER_LEN);
sf_writef_float(out_file, out_data, read_count);
}
sf_close(in_file) ;
sf_close(out_file) ;
return 0;
}
|