-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathsndfile_decode.cpp
62 lines (50 loc) · 1.44 KB
/
sndfile_decode.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
/* Decode audio file using libsndfile and write decoded samples to stdout.
* Output format:
* - two channels (front left, front right)
* - samples in interleaved format (L R L R ...)
* - samples are 32-bit floats
* - sample rate is 44100
*
* Usage:
* ./sndfile_decode cool_song.wav > cool_song_samples
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sndfile.h>
int main(int argc, char** argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s input_file > output_file\n", argv[0]);
exit(1);
}
const int out_channels = 2, out_samples = 512, sample_rate = 44100;
SF_INFO sinfo;
memset(&sinfo, 0, sizeof(sinfo));
SNDFILE* sfile = sf_open(argv[1], SFM_READ, &sinfo);
if (!sfile) {
fprintf(stderr, "sf_open()\n");
exit(1);
}
if (sinfo.channels != out_channels) {
fprintf(stderr, "unsupported # of channels\n");
exit(1);
}
if (sinfo.samplerate != sample_rate) {
fprintf(stderr, "unsupported sample rate\n");
exit(1);
}
float buffer[out_samples * out_channels] = {};
for (;;) {
sf_count_t ret = sf_readf_float(sfile, buffer, 512);
if (ret == 0) {
break;
}
if (write(STDOUT_FILENO, buffer, sizeof(buffer)) != sizeof(buffer)) {
fprintf(stderr, "write(stdout)\n");
exit(1);
}
}
sf_close(sfile);
return 0;
}