Initial import from FreeBSD RELENG_4:
[games.git] / sys / i386 / isa / sound / mmap_test.c
1 /*
2  * This is a simple program which demonstrates use of mmapped DMA buffer
3  * of the sound driver directly from application program.
4  *
5  * This sample program works (currently) only with Linux, FreeBSD and BSD/OS
6  * (FreeBSD and BSD/OS require OSS version 3.8-beta16 or later.
7  *
8  * Note! Don't use mmapped DMA buffers (direct audio) unless you have
9  * very good reasons to do it. Programs using this feature will not
10  * work with all soundcards. GUS (GF1) is one of them (GUS MAX works).
11  *
12  * This program requires version 3.5-beta7 or later of OSS
13  * (3.8-beta16 or later in FreeBSD and BSD/OS).
14  */
15
16 #ifndef lint
17 static const char rcsid[] =
18   "$FreeBSD: src/sys/i386/isa/sound/mmap_test.c,v 1.6 1999/09/04 15:21:28 peter Exp $";
19 #endif /* not lint */
20
21 #include <err.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/mman.h>
28 #include <sys/soundcard.h>
29 #include <sys/time.h>
30
31 int
32 main()
33 {
34         int fd, sz, fsz, tmp, nfrag;
35         int caps;
36
37         int sd, sl=0, sp;
38
39         unsigned char data[500000], *dp = data;
40
41         caddr_t buf;
42         struct timeval tim;
43
44         unsigned char *op;
45         
46         struct audio_buf_info info;
47
48         int frag = 0xffff000c;  /* Max # fragments of 2^13=8k bytes */
49
50         fd_set writeset;
51
52         close(0);
53         if ((fd=open("/dev/dsp", O_RDWR, 0))==-1)
54                 err(1, "/dev/dsp");
55 /*
56  * Then setup sampling parameters. Just sampling rate in this case.
57  */
58
59         tmp = 8000;
60         ioctl(fd, SNDCTL_DSP_SPEED, &tmp);
61
62 /*
63  * Load some test data.
64  */
65
66   sl = sp = 0;
67   if ((sd=open("smpl", O_RDONLY, 0))!=-1)
68   {
69         sl = read(sd, data, sizeof(data));
70         printf("%d bytes read from file.\n", sl);
71         close(sd);
72   }
73   else warn("smpl");
74
75         if (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
76         {
77                 warn("sorry but your sound driver is too old");
78                 err(1, "/dev/dsp");
79         }
80
81 /*
82  * Check that the device has capability to do this. Currently just
83  * CS4231 based cards will work.
84  *
85  * The application should also check for DSP_CAP_MMAP bit but this
86  * version of driver doesn't have it yet.
87  */
88 /*      ioctl(fd, SNDCTL_DSP_SETSYNCRO, 0); */
89
90 /*
91  * You need version 3.5-beta7 or later of the sound driver before next
92  * two lines compile. There is no point to modify this program to
93  * compile with older driver versions since they don't have working
94  * mmap() support.
95  */
96         if (!(caps & DSP_CAP_TRIGGER) ||
97             !(caps & DSP_CAP_MMAP))
98                 errx(1, "sorry but your soundcard can't do this");
99
100 /*
101  * Select the fragment size. This is propably important only when
102  * the program uses select(). Fragment size defines how often
103  * select call returns.
104  */
105
106         ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
107
108 /*
109  * Compute total size of the buffer. It's important to use this value
110  * in mmap() call.
111  */
112
113         if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
114                 err(1, "GETOSPACE");
115
116         sz = info.fragstotal * info.fragsize;
117         fsz = info.fragsize;
118 /*
119  * Call mmap().
120  * 
121  * IMPORTANT NOTE!!!!!!!!!!!
122  *
123  * Full duplex audio devices have separate input and output buffers. 
124  * It is not possible to map both of them at the same mmap() call. The buffer
125  * is selected based on the prot argument in the following way:
126  *
127  * - PROT_READ (alone) selects the input buffer.
128  * - PROT_WRITE (alone) selects the output buffer.
129  * - PROT_WRITE|PROT_READ together select the output buffer. This combination
130  *   is required in BSD to make the buffer accessible. With just PROT_WRITE
131  *   every attempt to access the returned buffer will result in segmentation/bus
132  *   error. PROT_READ|PROT_WRITE is also permitted in Linux with OSS version
133  *   3.8-beta16 and later (earlier versions don't accept it).
134  *
135  * Non duplex devices have just one buffer. When an application wants to do both
136  * input and output it's recommended that the device is closed and re-opened when
137  * switching between modes. PROT_READ|PROT_WRITE can be used to open the buffer
138  * for both input and output (with OSS 3.8-beta16 and later) but the result may be
139  * unpredictable.
140  */
141
142         if ((buf=mmap(NULL, sz, PROT_WRITE | PROT_READ, MAP_FILE|MAP_SHARED, fd, 0))==(caddr_t)-1)
143                 err(1, "mmap (write)");
144         printf("mmap (out) returned %08x\n", buf);
145         op=buf;
146
147 /*
148  * op contains now a pointer to the DMA buffer
149  */
150
151 /*
152  * Then it's time to start the engine. The driver doesn't allow read() and/or
153  * write() when the buffer is mapped. So the only way to start operation is
154  * to togle device's enable bits. First set them off. Setting them on enables
155  * recording and/or playback.
156  */
157
158         tmp = 0;
159         ioctl(fd, SNDCTL_DSP_SETTRIGGER, &tmp);  
160
161 /*
162  * It might be usefull to write some data to the buffer before starting.
163  */
164
165         tmp = PCM_ENABLE_OUTPUT;
166         ioctl(fd, SNDCTL_DSP_SETTRIGGER, &tmp);
167
168 /*
169  * The machine is up and running now. Use SNDCTL_DSP_GETOPTR to get the
170  * buffer status.
171  *
172  * NOTE! The driver empties each buffer fragmen after they have been
173  * played. This prevents looping sound if there are some performance problems
174  * in the application side. For similar reasons it recommended that the
175  * application uses some amout of play ahead. It can rewrite the unplayed
176  * data later if necessary.
177  */
178
179         nfrag = 0;
180         while (1)
181         {
182                 struct count_info count;
183                 int extra;
184
185                 FD_ZERO(&writeset);
186                 FD_SET(fd, &writeset);
187
188                 tim.tv_sec = 10;
189                 tim.tv_usec= 0;
190
191                 select(fd+1, &writeset, &writeset, NULL, NULL);
192 /*
193  * SNDCTL_DSP_GETOPTR (and GETIPTR as well) return three items. The
194  * bytes field returns number of bytes played since start. It can be used
195  * as a real time clock.
196  *
197  * The blocks field returns number of fragment transitions (interrupts) since
198  * previous GETOPTR call. It can be used as a method to detect underrun 
199  * situations.
200  *
201  * The ptr field is the DMA pointer inside the buffer area (in bytes from
202  * the beginning of total buffer area).
203  */
204
205                 if (ioctl(fd, SNDCTL_DSP_GETOPTR, &count)==-1)
206                         err(1, "GETOPTR");
207                 if (count.ptr < 0 ) count.ptr = 0;
208                 nfrag += count.blocks;
209
210
211 #ifdef VERBOSE
212
213                 printf("\rTotal: %09d, Fragment: %03d, Ptr: %06d",
214                         count.bytes, nfrag, count.ptr);
215                 fflush(stdout);
216 #endif
217
218 /*
219  * Caution! This version doesn't check for bounds of the DMA
220  * memory area. It's possible that the returned pointer value is not aligned
221  * to fragment boundaries. It may be several samples behind the boundary
222  * in case there was extra delay between the actual hardware interrupt and
223  * the time when DSP_GETOPTR was called.
224  *
225  * Don't just call memcpy() with length set to 'fragment_size' without
226  * first checking that the transfer really fits to the buffer area.
227  * A mistake of just one byte causes seg fault. It may be easiest just
228  * to align the returned pointer value to fragment boundary before using it.
229  *
230  * It would be very good idea to write few extra samples to next fragment
231  * too. Otherwise several (uninitialized) samples from next fragment
232  * will get played before your program gets chance to initialize them.
233  * Take in count the fact thaat there are other processes batling about
234  * the same CPU. This effect is likely to be very annoying if fragment
235  * size is decreased too much.
236  */
237
238 /*
239  * Just a minor clarification to the above. The following line alings
240  * the pointer to fragment boundaries. Note! Don't trust that fragment
241  * size is always a power of 2. It may not be so in future.
242  */
243                 count.ptr = ((count.ptr+16)/fsz )*fsz;
244 #ifdef VERBOSE
245                 printf(" memcpy(%6d, %4d)", (dp-data), fsz);
246                 fflush(stdout);
247 #endif
248
249 /*
250  * Set few bytes in the beginning of next fragment too.
251  */
252
253                 if ((count.ptr+fsz+16) < sz)    /* Last fragment? */
254                    extra = 16;
255                 else
256                    extra = 0;
257                 memcpy(op+count.ptr, dp, (fsz+extra));
258                 dp += fsz;
259                 if (dp > (data+sl-fsz))
260                    dp = data;
261
262         }
263
264         exit(0);
265 }