-
Notifications
You must be signed in to change notification settings - Fork 7
/
soundtest.c
168 lines (124 loc) · 4.6 KB
/
soundtest.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
Broadcast 2.0 multitrack audio editing
(c) 1997 Heroine Virtual
This program is distributed with the intent that it will be useful, without
any warranty. The code is being updated and has many bugs which are
constantly being elucidated so changes should not be made without notifying
the author.
*/
/* Run at startup to permanently allocate your DMA buffers */
#include <sys/soundcard.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <math.h>
int main(int argc, char *argv[]){
int dsp;
int duplexenable = 1;
audio_buf_info playinfo, recinfo;
int bufsize = 0x7FFF0000;
int fragsize;
int format = AFMT_S16_LE;
int channels = 2;
int samplerate = 44100;
if(argc < 2)
{
bufsize += 15;
}
else
{
fragsize = atol(argv[1]);
if(fragsize <= 0) fragsize = 32768;
bufsize += (long)(log(fragsize) / log(2));
}
#ifdef USE_FULLDUPLEX
printf("*** Full duplex\n");
if((dsp = open("/dev/dsp", O_RDWR)) < 0){
printf("Can't open audio device.\n");
close(dsp);
return 1;
}
if (ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &bufsize))
printf("Couldn't set buffer parameters.\n");
if(duplexenable && ioctl(dsp, SNDCTL_DSP_SETDUPLEX, 0) == -1){
printf("Couldn't enable full duplex audio.\n");
duplexenable = 0;
}
if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
printf("playback file format failed\n");
if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
printf("playback channel allocation failed\n");
if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
printf("playback sample rate set failed\n");
ioctl(dsp, SNDCTL_DSP_GETOSPACE, &playinfo);
printf(" fragments fragstotal fragsize bytes TOTAL BYTES AVAILABLE\n");
printf("Playback: %9d %10d %8d %7d %12d\n", playinfo.fragments,
playinfo.fragstotal, playinfo.fragsize, playinfo.bytes, playinfo.bytes);
if(duplexenable)
{
if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
printf("record file format failed\n");
if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
printf("record channel allocation failed\n");
if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
printf("record sample rate set failed\n");
ioctl(dsp, SNDCTL_DSP_GETISPACE, &recinfo);
printf("Record: %9d %10d %8d %7d %12d\n", recinfo.fragments,
recinfo.fragstotal, recinfo.fragsize, recinfo.bytes, recinfo.fragstotal * recinfo.fragsize);
}
close(dsp);
#endif
printf("\n*** Half duplex\n");
if((dsp = open("/dev/dsp", O_WRONLY)) < 0){
printf("Can't open audio device.\n");
close(dsp);
return 1;
}
if (ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &bufsize))
printf("Couldn't set buffer parameters.\n");
if(duplexenable && ioctl(dsp, SNDCTL_DSP_SETDUPLEX, 0) == -1){
printf("Couldn't enable full duplex audio.\n");
duplexenable = 0;
}
if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
printf("playback file format failed\n");
if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
printf("playback channel allocation failed\n");
if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
printf("playback sample rate set failed\n");
ioctl(dsp, SNDCTL_DSP_GETOSPACE, &playinfo);
printf(" fragments fragstotal fragsize bytes TOTAL BYTES AVAILABLE\n");
printf("Playback: %9d %10d %8d %7d %12d\n", playinfo.fragments,
playinfo.fragstotal, playinfo.fragsize, playinfo.bytes, playinfo.bytes);
close(dsp);
if((dsp = open("/dev/dsp", O_RDONLY)) < 0){
printf("Can't open audio device.\n");
close(dsp);
return 1;
}
if (ioctl(dsp, SNDCTL_DSP_SETFRAGMENT, &bufsize))
printf("Couldn't set buffer parameters.\n");
if(duplexenable && ioctl(dsp, SNDCTL_DSP_SETDUPLEX, 0) == -1){
printf("Couldn't enable full duplex audio.\n");
duplexenable = 0;
}
if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
printf("playback file format failed\n");
if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
printf("playback channel allocation failed\n");
if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
printf("playback sample rate set failed\n");
if(ioctl(dsp, SNDCTL_DSP_SETFMT, &format) < 0)
printf("record file format failed\n");
if(ioctl(dsp, SNDCTL_DSP_CHANNELS, &channels) < 0)
printf("record channel allocation failed\n");
if(ioctl(dsp, SNDCTL_DSP_SPEED, &samplerate) < 0)
printf("record sample rate set failed\n");
ioctl(dsp, SNDCTL_DSP_GETISPACE, &recinfo);
printf("Record: %9d %10d %8d %7d %12d\n", recinfo.fragments,
recinfo.fragstotal, recinfo.fragsize, recinfo.bytes, recinfo.fragstotal * recinfo.fragsize);
ioctl(dsp, SNDCTL_DSP_RESET);
close(dsp);
}