2 * This is a simple program which demonstrates use of mmapped DMA buffer
3 * of the sound driver directly from application program.
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.
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).
12 * This program requires version 3.5-beta7 or later of OSS
13 * (3.8-beta16 or later in FreeBSD and BSD/OS).
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 $";
26 #include <sys/types.h>
28 #include <sys/soundcard.h>
34 int fd, sz, fsz, tmp, nfrag;
39 unsigned char data[500000], *dp = data;
46 struct audio_buf_info info;
48 int frag = 0xffff000c; /* Max # fragments of 2^13=8k bytes */
53 if ((fd=open("/dev/dsp", O_RDWR, 0))==-1)
56 * Then setup sampling parameters. Just sampling rate in this case.
60 ioctl(fd, SNDCTL_DSP_SPEED, &tmp);
63 * Load some test data.
67 if ((sd=open("smpl", O_RDONLY, 0))!=-1)
69 sl = read(sd, data, sizeof(data));
70 printf("%d bytes read from file.\n", sl);
75 if (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
77 warn("sorry but your sound driver is too old");
82 * Check that the device has capability to do this. Currently just
83 * CS4231 based cards will work.
85 * The application should also check for DSP_CAP_MMAP bit but this
86 * version of driver doesn't have it yet.
88 /* ioctl(fd, SNDCTL_DSP_SETSYNCRO, 0); */
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
96 if (!(caps & DSP_CAP_TRIGGER) ||
97 !(caps & DSP_CAP_MMAP))
98 errx(1, "sorry but your soundcard can't do this");
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.
106 ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &frag);
109 * Compute total size of the buffer. It's important to use this value
113 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
116 sz = info.fragstotal * info.fragsize;
121 * IMPORTANT NOTE!!!!!!!!!!!
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:
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).
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
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);
148 * op contains now a pointer to the DMA buffer
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.
159 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &tmp);
162 * It might be usefull to write some data to the buffer before starting.
165 tmp = PCM_ENABLE_OUTPUT;
166 ioctl(fd, SNDCTL_DSP_SETTRIGGER, &tmp);
169 * The machine is up and running now. Use SNDCTL_DSP_GETOPTR to get the
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.
182 struct count_info count;
186 FD_SET(fd, &writeset);
191 select(fd+1, &writeset, &writeset, NULL, NULL);
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.
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
201 * The ptr field is the DMA pointer inside the buffer area (in bytes from
202 * the beginning of total buffer area).
205 if (ioctl(fd, SNDCTL_DSP_GETOPTR, &count)==-1)
207 if (count.ptr < 0 ) count.ptr = 0;
208 nfrag += count.blocks;
213 printf("\rTotal: %09d, Fragment: %03d, Ptr: %06d",
214 count.bytes, nfrag, count.ptr);
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.
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.
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.
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.
243 count.ptr = ((count.ptr+16)/fsz )*fsz;
245 printf(" memcpy(%6d, %4d)", (dp-data), fsz);
250 * Set few bytes in the beginning of next fragment too.
253 if ((count.ptr+fsz+16) < sz) /* Last fragment? */
257 memcpy(op+count.ptr, dp, (fsz+extra));
259 if (dp > (data+sl-fsz))