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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* This is a simple program which demonstrates use of mmapped DMA buffer
* of the sound driver directly from application program.
*
* This sample program works (currently) only with Linux, FreeBSD and BSD/OS
* (FreeBSD and BSD/OS require OSS version 3.8-beta16 or later.
*
* Note! Don't use mmapped DMA buffers (direct audio) unless you have
* very good reasons to do it. Programs using this feature will not
* work with all soundcards. GUS (GF1) is one of them (GUS MAX works).
*
* This program requires version 3.5-beta7 or later of OSS
* (3.8-beta16 or later in FreeBSD and BSD/OS).
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/soundcard.h>
#include <sys/time.h>
main()
{
int fd, sz, fsz, i, tmp, n, l, have_data=0, nfrag;
int caps, idx;
int sd, sl=0, sp;
unsigned char data[500000], *dp = data;
struct buffmem_desc imemd, omemd;
caddr_t buf;
struct timeval tim;
unsigned char *op;
struct audio_buf_info info;
int frag = 0xffff000c; /* Max # periods of 2^13=8k bytes */
fd_set writeset;
close(0);
if ((fd=open("/dev/dsp", O_RDWR, 0))==-1)
{
perror("/dev/dsp");
exit(-1);
}
/*
* Then setup sampling parameters. Just sampling rate in this case.
*/
tmp = 48000;
ioctl(fd, SNDCTL_DSP_SPEED, &tmp);
printf("Speed set to %d\n", tmp);
/*
* Load some test data.
*/
sl = sp = 0;
if ((sd=open("smpl", O_RDONLY, 0))!=-1)
{
sl = read(sd, data, sizeof(data));
printf("%d bytes read from file.\n", sl);
close(sd);
}
else perror("smpl");
if (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps)==-1)
{
perror("/dev/dsp");
fprintf(stderr, "Sorry but your sound driver is too old\n");
exit(-1);
}
/*
* Check that the device has capability to do this. Currently just
* CS4231 based cards will work.
*
* The application should also check for DSP_CAP_MMAP bit but this
* version of driver doesn't have it yet.
*/
/* ioctl(fd, SNDCTL_DSP_SETSYNCRO, 0); */
/*
* You need version 3.5-beta7 or later of the sound driver before next
* two lines compile. There is no point to modify this program to
* compile with older driver versions since they don't have working
* mmap() support.
*/
if (!(caps & DSP_CAP_TRIGGER) ||
!(caps & DSP_CAP_MMAP))
{
fprintf(stderr, "Sorry but your soundcard can't do this\n");
exit(-1);
}
/*
* Select the period size. This is propably important only when
* the program uses select(). Period size defines how often
* select call returns.
*/
ioctl(fd, SNDCTL_DSP_SETPERIOD, &frag);
/*
* Compute total size of the buffer. It's important to use this value
* in mmap() call.
*/
if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)==-1)
{
perror("GETOSPACE");
exit(-1);
}
sz = info.fragstotal * info.fragsize;
fsz = info.fragsize;
printf( "info.fragstotal = %i\n", info.fragstotal );
printf( "info.fragsize = %i\n", info.fragsize );
printf( "info.periods = %i\n", info.periods );
printf( "info.bytes = %i\n", info.bytes );
/*
* Call mmap().
*
* IMPORTANT NOTE!!!!!!!!!!!
*
* Full duplex audio devices have separate input and output buffers.
* It is not possible to map both of them at the same mmap() call. The buffer
* is selected based on the prot argument in the following way:
*
* - PROT_READ (alone) selects the input buffer.
* - PROT_WRITE (alone) selects the output buffer.
* - PROT_WRITE|PROT_READ together select the output buffer. This combination
* is required in BSD to make the buffer accessible. With just PROT_WRITE
* every attempt to access the returned buffer will result in segmentation/bus
* error. PROT_READ|PROT_WRITE is also permitted in Linux with OSS version
* 3.8-beta16 and later (earlier versions don't accept it).
*
* Non duplex devices have just one buffer. When an application wants to do both
* input and output it's recommended that the device is closed and re-opened when
* switching between modes. PROT_READ|PROT_WRITE can be used to open the buffer
* for both input and output (with OSS 3.8-beta16 and later) but the result may be
* unpredictable.
*/
#if 1
if ((buf=mmap(NULL, sz, PROT_WRITE, MAP_FILE|MAP_SHARED, fd, 0))==(caddr_t)-1)
{
perror("mmap (write)");
exit(-1);
}
printf("mmap (out) returned %08x\n", buf);
#else
buf=data;
#endif
op=buf;
/*
* op contains now a pointer to the DMA buffer
*/
/*
* Then it's time to start the engine. The driver doesn't allow read() and/or
* write() when the buffer is mapped. So the only way to start operation is
* to togle device's enable bits. First set them off. Setting them on enables
* recording and/or playback.
*/
tmp = 0;
ioctl(fd, SNDCTL_DSP_SETTRIGGER, &tmp);
printf("Trigger set to %08x\n", tmp);
/*
* It might be usefull to write some data to the buffer before starting.
*/
tmp = PCM_ENABLE_OUTPUT;
ioctl(fd, SNDCTL_DSP_SETTRIGGER, &tmp);
printf("Trigger set to %08x\n", tmp);
/*
* The machine is up and running now. Use SNDCTL_DSP_GETOPTR to get the
* buffer status.
*
* NOTE! The driver empties each buffer fragmen after they have been
* played. This prevents looping sound if there are some performance problems
* in the application side. For similar reasons it recommended that the
* application uses some amout of play ahead. It can rewrite the unplayed
* data later if necessary.
*/
nfrag = 0;
for (idx=0; idx<40; idx++)
{
struct count_info count;
int p, l, extra;
FD_ZERO(&writeset);
FD_SET(fd, &writeset);
tim.tv_sec = 10;
tim.tv_usec= 0;
select(fd+1, NULL, &writeset, NULL, NULL);
/*
* SNDCTL_DSP_GETOPTR (and GETIPTR as well) return three items. The
* bytes field returns number of bytes played since start. It can be used
* as a real time clock.
*
* The blocks field returns number of period transitions (interrupts) since
* previous GETOPTR call. It can be used as a method to detect underrun
* situations.
*
* The ptr field is the DMA pointer inside the buffer area (in bytes from
* the beginning of total buffer area).
*/
if (ioctl(fd, SNDCTL_DSP_GETOPTR, &count)==-1)
{
perror("GETOPTR");
exit(-1);
}
nfrag += count.blocks;
#ifdef VERBOSE
printf("Total: %09d, Period: %03d, Ptr: %06d",
count.bytes, nfrag, count.ptr);
fflush(stdout);
#endif
/*
* Caution! This version doesn't check for bounds of the DMA
* memory area. It's possible that the returned pointer value is not aligned
* to period boundaries. It may be several samples behind the boundary
* in case there was extra delay between the actual hardware interrupt and
* the time when DSP_GETOPTR was called.
*
* Don't just call memcpy() with length set to 'period_size' without
* first checking that the transfer really fits to the buffer area.
* A mistake of just one byte causes seg fault. It may be easiest just
* to align the returned pointer value to period boundary before using it.
*
* It would be very good idea to write few extra samples to next period
* too. Otherwise several (uninitialized) samples from next period
* will get played before your program gets chance to initialize them.
* Take in count the fact thaat there are other processes batling about
* the same CPU. This effect is likely to be very annoying if period
* size is decreased too much.
*/
/*
* Just a minor clarification to the above. The following line alings
* the pointer to period boundaries. Note! Don't trust that period
* size is always a power of 2. It may not be so in future.
*/
count.ptr = (count.ptr/fsz)*fsz;
#ifdef VERBOSE
printf(" memcpy(%6d, %4d)\n", (dp-data), fsz);
fflush(stdout);
#endif
/*
* Set few bytes in the beginning of next period too.
*/
if ((count.ptr+fsz+16) < sz) /* Last period? */
extra = 16;
else
extra = 0;
memcpy(op+count.ptr, dp, fsz+extra);
dp += fsz;
if (dp > (data+sl-fsz))
dp = data;
}
close(fd);
printf( ">>>> open (2)\n" ); fflush( stdout );
if ((fd=open("/dev/dsp", O_RDWR, 0))==-1)
{
perror("/dev/dsp");
exit(-1);
}
close( fd );
exit(0);
}
|