-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecord.c
More file actions
97 lines (83 loc) · 2 KB
/
Copy pathrecord.c
File metadata and controls
97 lines (83 loc) · 2 KB
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
#include <stdlib.h>
#include <fcntl.h>
#include <linux/soundcard.h>
#include <errno.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <memory.h>
int rate = 8000;
char* dsp = "/dev/dsp";
int wide = 0;
void initialize(int fd) {
int arg = 0;
if(!wide) arg = AFMT_S8;
else arg = AFMT_S16_LE;
if(ioctl(fd, SNDCTL_DSP_SETFMT, &arg) == -1) {
perror("SNDCTL_DSP_SETFMT");
exit(EXIT_FAILURE);
}
arg = 1;
if(ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg) == -1) {
perror("SNDCTL_DSP_STEREO");
exit(EXIT_FAILURE);
}
if(arg != 1) {
fprintf(stderr, "Unable to set mono mode\n");
exit(EXIT_FAILURE);
}
arg = rate;
if(ioctl(fd, SNDCTL_DSP_SPEED, &arg) == -1) {
perror("SNDCTL_DSP_SPEED");
exit(EXIT_FAILURE);
}
if(arg != rate) {
fprintf(stderr, "Unable to set rate\n");
exit(EXIT_FAILURE);
}
}
void usage(char* cmd) {
fprintf(stderr, "usage: %s [-h] [-d dev] [-o outfile] [-r rate]\n"
"\t -h : Display this message\n"
"\t -d : Specify device (e.g. /dev/dsp)\n"
"\t -o : Set output file (default stdout)\n"
"\t -r : Set rate\n\n", cmd);
}
int main(int argc, char* argv[]) {
FILE* outfile = stdout;
int fd = 0;
int i;
char* buffer = NULL;
short* sbuffer = NULL;
while((i = getopt(argc, argv, "r:d:ho:w"))) switch(i) {
case 'h': usage(argv[0]); return EXIT_SUCCESS;
case 'r': rate = atoi(optarg); break;
case 'd': dsp = strdup(optarg); break;
case 'w': wide = 1; break;
case 'o':
outfile = fopen(optarg, "w");
if(!outfile) perror(optarg);
break;
case -1: goto done;
default: exit(EXIT_FAILURE);
}
done:
buffer = malloc(rate);
sbuffer = malloc(2*rate);
fd = open(dsp, O_RDONLY);
if(fd < 0) { perror(dsp); exit(EXIT_FAILURE); }
initialize(fd);
if(wide) {
while(read(fd, sbuffer, 2*rate) == 2*rate) {
for(i = 0; i < rate; i++) {
buffer[i] = sbuffer[i] / 0x0100;
}
fwrite(buffer, 1, rate, outfile);
}
} else {
while(read(fd, buffer, rate) == rate) {
fwrite(buffer, 1, rate, outfile);
}
}
return EXIT_SUCCESS;
}