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
113
|
/*
Purpose: This program has been used to verify that the select() call works
* Copyright (C) 4Front Technologies, 2002-2004. Released under GPLv2/CDDL.
*
* Description:
* This program opens an audio device and then just
* copies input to output. Select is used for flow control.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <soundcard.h>
#ifdef _AIX
#include <sys/select.h>
#endif
int
main (int agrc, char *argv[])
{
int fd;
int tmp;
char buf[128 * 1024];
int have_data = 0;
int n, l;
int frag = 0x00200008; /* 32 fragments of 2^8=256 bytes */
fd_set reads, writes;
close (0);
if ((fd = open ("/dev/dsp", O_RDWR, 0)) == -1)
{
perror ("/dev/dsp open");
exit (-1);
}
ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &frag);
/*
* Set just the sampling tahe. Use the default format. We do not do any
* error checking (maybe not so good idea) because we don't care what
* the sampling rate really is.
*/
tmp = 48000;
ioctl (fd, SNDCTL_DSP_SPEED, &tmp);
while (1)
{
struct timeval time;
FD_ZERO (&reads);
FD_ZERO (&writes);
if (have_data)
FD_SET (fd, &writes);
else
FD_SET (fd, &reads);
time.tv_sec = 1;
time.tv_usec = 0;
if (select (fd + 1, &reads, &writes, NULL, &time) == -1)
{
perror ("select");
exit (-1);
}
if (FD_ISSET (fd, &reads))
{
struct audio_buf_info info;
if (ioctl (fd, SNDCTL_DSP_GETISPACE, &info) == -1)
{
perror ("select");
exit (-1);
}
n = info.bytes;
l = read (fd, buf, n);
if (l > 0)
have_data = 1;
}
if (FD_ISSET (fd, &writes))
{
int i;
struct audio_buf_info info;
if (ioctl (fd, SNDCTL_DSP_GETOSPACE, &info) == -1)
{
perror ("select");
exit (-1);
}
n = info.bytes;
printf ("Write %d\n", l);
write (fd, buf, l);
printf ("OK");
have_data = 0;
}
}
exit (0);
}
|