-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
94 lines (86 loc) · 1.98 KB
/
main.c
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
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
static int verbose=0;
static int dspfd=-1;
static char const *dspdev="/dev/dsp";
static char const *me="dsprec";
void dspopen ()
{
if (dspfd < 0) {
dspfd = open(dspdev, O_RDWR);
if (dspfd < 0) {
fprintf (stderr, "%s: can't open %s: %s\n", me, dspdev, strerror(errno));
exit (1);
}
}
}
void setrate (int newrate)
{
dspopen();
if (ioctl (dspfd, SNDCTL_DSP_SPEED, &newrate) < 0) {
fprintf (stderr, "%s: setrate(%d): %s\n", me, newrate, strerror(errno));
}
}
void setchannels (int channels)
{
dspopen();
if (ioctl (dspfd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
fprintf (stderr, "%s: setchannels(%d): %s\n", me, channels, strerror(errno));
}
}
void cat ()
{
char buf[4410];
int n;
dspopen();
while ((n=read (dspfd, buf, sizeof(buf))))
write (STDOUT_FILENO, buf, n);
}
void setbits (int bits)
{
int fmt;
if (bits == 8)
fmt = AFMT_S8;
else if (bits == 16)
fmt = AFMT_S16_LE;
else {
fprintf (stderr, "%s: setbits(%d): unrecognized word size\n", me, bits);
return;
}
dspopen();
if (ioctl (dspfd, SNDCTL_DSP_SETFMT, &fmt) < 0) {
fprintf (stderr, "%s: setbits(%d): %s\n", me, bits, strerror(errno));
}
}
int main(int argc, char** argv)
{
int a;
me=argv[0];
for(a=1; a<argc; a++) {
if (!strcmp (argv[a], "-v"))
verbose=1;
else if(a+1<argc && !strcmp (argv[a], "-f")) {
if (dspfd >= 0)
close (dspfd);
dspfd = -1;
dspdev = argv[++a];
}
else if(a+1<argc && !strcmp (argv[a], "rate"))
setrate (atoi (argv[++a]));
else if(a+1<argc && !strcmp (argv[a], "bits"))
setbits (atoi (argv[++a]));
else if(a+1<argc && !strcmp (argv[a], "channels"))
setchannels (atoi (argv[++a]));
else
fprintf(stderr, "%s: unrecognized command line option: %s\n",
argv[0], argv[a]);
}
cat();
exit(0);
}