-
Notifications
You must be signed in to change notification settings - Fork 5
/
rec.cpp
83 lines (81 loc) · 2.04 KB
/
rec.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
75
76
77
78
79
80
81
82
83
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
int main(int argc, char *argv[])
{
bool stereo = false;
int a = 1;
while (a < argc && argv[a][0] == '-' && argv[a][1] != 0) {
switch (argv[a][1]) {
case 's':
stereo = true;
break;
default:
fprintf(stderr, "%s: unknown option %c\n", argv[0], argv[a][1]);
exit(1);
}
a++;
}
FILE *f;
if (strcmp(argv[a], "-") == 0) {
f = stdout;
} else {
f = fopen(argv[a], "wb");
if (f == NULL) {
perror("fopen");
exit(1);
}
}
int fd = open("/dev/dsp", O_RDONLY);
if (fd < 0) {
perror("open");
exit(1);
}
int sndparam = AFMT_S16_LE;
if (ioctl(fd, SNDCTL_DSP_SETFMT, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit(1);
}
if (sndparam != AFMT_S16_LE) {
perror("ioctl: SNDCTL_DSP_SETFMT");
exit(1);
}
sndparam = stereo;
if (ioctl(fd, SNDCTL_DSP_STEREO, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_STEREO");
exit(1);
}
if (sndparam != stereo) {
fprintf(stderr, "rec: Error, cannot set the channel number to %d\n", stereo);
exit(1);
}
int sample_rate = 11025;
sndparam = sample_rate;
if (ioctl(fd, SNDCTL_DSP_SPEED, &sndparam) == -1) {
perror("ioctl: SNDCTL_DSP_SPEED");
exit(1);
}
if ((10*abs(sndparam-sample_rate)) > sample_rate) {
perror("ioctl: SNDCTL_DSP_SPEED");
exit(1);
}
if (sndparam != sample_rate) {
fprintf(stderr, "Warning: Sampling rate is %u, requested %u\n", sndparam, sample_rate);
}
for (;;) {
char buf[4096];
int n = read(fd, buf, sizeof(buf));
if (n == 0) {
fprintf(stderr, "rec: eof on input\n");
break;
}
fwrite(buf, 1, n, f);
}
close(fd);
fclose(f);
return 0;
}