File: wvdsp.cc

package info (click to toggle)
wvstreams 4.0.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,420 kB
  • ctags: 6,518
  • sloc: cpp: 52,544; sh: 5,770; ansic: 810; makefile: 461; tcl: 114; perl: 18
file content (472 lines) | stat: -rw-r--r-- 10,013 bytes parent folder | download
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 *
 * One more attempt at making a decent stream for Linux /dev/dsp.
 * See wvdsp.h.
 * 
 * "See all that stuff in there, Homer?  I guess that's why _your_ robot
 * never worked!"
 */
#include "wvdsp.h"
#include "wvfork.h"
#include <sys/ioctl.h>
#include <sys/soundcard.h>
#include <fcntl.h>
#include <sched.h>
#include <time.h>

#define DO_RATEADJ 0

static const char *AUDIO_DEVICE = "/dev/dsp";

static int msec_lat(int frags, int frag_bits, int srate)
{
    return frags * (1<<frag_bits) * 1000 / srate;
}

WvDsp::WvDsp(int msec_latency, int srate, int bits, bool stereo,
	     bool readable, bool writable, bool _realtime, bool _oss)
    : log("DSP", WvLog::Debug2), rcircle(102400), wcircle(102400),
	 inrate(bits/8 * (stereo ? 2 : 1), srate, srate), 
	outrate(bits/8 * (stereo ? 2 : 1), srate, srate)
{
    is_realtime = _realtime;

    int mode = 0;
    
    outrate.orate_n = srate;
   
    assert(msec_latency >= 0);
    assert(srate >= 8000);
    assert(srate <= 48000);
    assert(bits == 8 || bits == 16);
    assert(readable || writable);
    
#if DO_RATEADJ
    // the record clock should be the same as the playback clock, so it's
    // best to match our output rate to the input rate.  Of course, we can
    // only do this if we'll be inputting anything.
    if (readable)
	outrate.match_rate = &inrate;
#endif
    
    if (readable && writable)
	mode = O_RDWR;
    else if (readable)
	mode = O_RDONLY;
    else if (writable)
	mode = O_WRONLY;

    // we do O_NONBLOCK in case someone is currently using the dsp
    // device and the particular one we're using can't be shared.
    if ((fd = open(AUDIO_DEVICE, mode | O_NONBLOCK)) < 0)
    {
    	seterr(errno);
	return;
    }
    
    // now get rid of O_NONBLOCK, so write() and read() on our fd don't return
    // until we get our data.  Since select() doesn't work anyway with some
    // drivers, we'll have to cheat.
    fcntl(fd, F_SETFL, mode);
    
    // set the fragment size appropriately for the desired latency
    num_frags = 5;
    int frag_size_bits = 7; // log2 of fragment size
    int lat;
    if (msec_latency > 1000)
	msec_latency = 1000; // don't be _too_ ridiculous...
    while (msec_latency > (lat = msec_lat(num_frags, frag_size_bits, srate)))
    {
	if (frag_size_bits < 14 && msec_latency >= 2*lat)
	    frag_size_bits++;
	else
	    num_frags++;
    }
    
    log(WvLog::Debug, "With %s %s-bit frags, latency will be about %s ms.\n",
                      num_frags, frag_size_bits, lat);
    
    frag_size = (1 << frag_size_bits);
    if (!setioctl(SNDCTL_DSP_SETFRAGMENT, (num_frags << 16) | frag_size_bits))
	seterr("can't set frag size!");
    
    if (bits == 16)
    {
	if (!setioctl(SNDCTL_DSP_SETFMT, AFMT_S16_NE))
	    seterr("can't set sample size!");
    }
    else if (bits == 8)
    {
	if (!setioctl(SNDCTL_DSP_SETFMT, AFMT_S8))
	    seterr("can't set sample size!");
    }
	
    if (!setioctl(SNDCTL_DSP_CHANNELS, stereo ? 2 : 1))
	seterr("can't set number of channels!");
    
    if (!setioctl(SNDCTL_DSP_SPEED, srate))
	seterr("can't set sampling rate!");

    // in fact, the OSS docs say we're not allowed to have separate processes
    // doing reads and writes at the same time.  Unfortunately, ALSA seems
    // to _need_ that for decent real-time performance.  But you can toggle
    // it here :)
    if (_oss)
    {
	// start the read/writer process
	subproc(readable, writable);
    }
    else
    {
	// start the reader process
	if (readable) subproc(true, false);
	
	// start the writer process
	if (writable) subproc(false, true);
    }

    rloop.nowrite();
    wloop.noread();
    realtime(); // actually necessary, but a bit dangerous...
}


WvDsp::~WvDsp()
{
    close();
}


bool WvDsp::pre_select(SelectInfo &si)
{
    bool ret = false;

/*
    size_t rleft = rcircle.used(), wleft = wcircle.used();

    if (rleft > 2*frag_size)
	log("read circle is filling! (%s = %s)\n", rleft, rleft/frag_size);
    if (wleft > 3*frag_size)
	log("write circle is filling! (%s = %s; %s)\n", 
	    wleft, wleft/frag_size, ospace());
*/

    if (si.wants.readable)
    {
	rloop.drain();
	if (rcircle.used())
	    return true;
	else
	    ret |= rloop.pre_select(si);
    }
    
    if (si.wants.writable)
	return true;
    
    return ret;
}


bool WvDsp::post_select(SelectInfo &si)
{
    bool ret = false;
    
    if (si.wants.readable)
    {
	if (rcircle.used())
	    return true;
	else
	    ret |= rloop.post_select(si);
    }
    
    return ret;
}


size_t WvDsp::uread(void *buf, size_t len)
{
    if (len == 0)
        return 0;
    size_t avail = rcircle.used();
    
    // transfer from the magic circle into our rbuf, using the rate adjuster.
    {
	WvDynBuf silly;
	void *data = silly.alloc(avail);
	size_t got = rcircle.get(data, avail);
	silly.unalloc(avail - got);
#if DO_RATEADJ
	inrate.encode(silly, rbuf);
#else
	rbuf.merge(silly);
#endif	
    }
    
    avail = rbuf.used();
    if (avail < len)
	len = avail;
    
    rbuf.move(buf, len);
    return len;
}


size_t WvDsp::uwrite(const void *buf, size_t len)
{
    static time_t last_dump;
    
    if (len == 0)
        return 0;
    
    if (last_dump < time(NULL) - 1)
    {
	log(WvLog::Debug, "writer rates: %s/%s; reader rates: %s/%s\n",
	    outrate.getirate(), outrate.getorate(),
	    inrate.getirate(), inrate.getorate());
	last_dump = time(NULL);
    }
   
#if DO_RATEADJ
    outrate.flushmembuf(buf, len, wbuf);
#else
    wbuf.put(buf, len);
#endif
    
    size_t howmuch = wcircle.left();
    
    if (howmuch > wbuf.used())
	howmuch = wbuf.used();
    
    buf = wbuf.get(howmuch);
    wcircle.put(buf, howmuch);
    wloop.write("", 1);
    
    return len; // never let WvStreams buffer anything
}


bool WvDsp::isok() const
{
    return (fd >= 0);
}


void WvDsp::close()
{
    if (fd >= 0)
	::close(fd);
    fd = -1;

    // this should wake up the subprocess(es) and ask them to die.
    rloop.close();
    wloop.close();
}


bool WvDsp::setioctl(int ctl, int param)
{
    return ioctl(fd, ctl, &param) >= 0;
}


// set realtime scheduling priority
void WvDsp::realtime()
{
    if (is_realtime)
    {
        struct sched_param sch;
        memset(&sch, 0, sizeof(sch));
        sch.sched_priority = 1;
        if (sched_setscheduler(getpid(), SCHED_FIFO, &sch) < 0)
            seterr("can't set scheduler priority!");
    }
}


void WvDsp::subproc(bool reading, bool writing)
{
    intTable fds(4);
    fds.add(new int(rloop.getrfd()), true);
    fds.add(new int(rloop.getwfd()), true);
    fds.add(new int(wloop.getrfd()), true);
    fds.add(new int(wloop.getwfd()), true);
    
    pid_t pid = wvfork(fds);
    if (pid < 0)
    {
	seterr(errno);
	return;
    }
    else if (pid > 0) // parent
	return;

    // otherwise, this is the child

    char buf[10240];
 
    realtime();
 
    rloop.noread();
    wloop.nowrite();
 
    if (!reading)
	rloop.close();
    if (!writing)
	wloop.close();
 
    while (isok() && (rloop.isok() || wloop.isok()))
    {
	if (reading)
	{
	    size_t len = do_uread(buf, sizeof(buf));
	    if (len)
	    {
		rcircle.put(buf, len);
		rloop.write("", 1);
	    }
	}

	if (writing)
	{
            wloop.drain();
            size_t avail;
            while ((avail = wcircle.used()) >= frag_size)
            {
                if (avail > frag_size)
                    avail = frag_size;
                size_t len = wcircle.get(buf, avail);
                do_uwrite(buf, len);
	    }
            if (!reading)
                wloop.select(-1);
	}
    }

    _exit(0);
}


size_t WvDsp::ispace()
{
    audio_buf_info info;
    
    if (ioctl(fd, SNDCTL_DSP_GETISPACE, &info) < 0)
    {
	log(WvLog::Error, "error in GETISPACE\n");
	return 0;
    }
    
    return info.fragments;
}


size_t WvDsp::ospace()
{
    audio_buf_info info;
    
    if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) < 0)
    {
	log(WvLog::Error, "error in GETOSPACE\n");
	return 0;
    }
    
    return num_frags - info.fragments;
}


size_t WvDsp::do_uread(void *buf, size_t len)
{
    if (!len) return 0;

    if (len < frag_size)
        log(WvLog::Warning, "reading less than frag size: %s/%s\n", len, frag_size);

    size_t i, i2;
    
    if (len > frag_size)
	len = frag_size;
    
    if ((i = ispace()) > 1)
    {
        if (i > num_frags * 2)
        {
            log("resetting: frag count is broken! (%s)\n", i);
            ioctl(fd, SNDCTL_DSP_RESET, NULL);
        }
        else
        {
            i2 = i;
            while (i2-- > 1)
            {
                char buf2[frag_size];
                ::read(fd, buf2, frag_size);
            }
            //log("inbuf is filling up! (%s waiting)\n", i);
        }
    }
    
    // note: ALSA drivers sometimes read zero bytes even with stuff in the
    // buffer (sigh).  So that's not EOF in this case.
    int ret = ::read(fd, buf, len);
    if (ret < 0)
    {
	if (errno != EAGAIN)
	    seterr(errno);
	return 0;
    }

    if (ret && ret < (int)len && ret < (int)frag_size)
	log("inbuf underflow (%s/%s)!\n", ret, len);

    return ret;
}


size_t WvDsp::do_uwrite(const void *buf, size_t len)
{
    if (!len) return 0;
    
    if (len < frag_size)
        log(WvLog::Warning, "writing less than frag size: %s/%s\n",
	    len, frag_size);
    
    int o = ospace(), o2;
    
    if (o < 2)
    {
	o2 = o;
	while (o2++ < 2)
	{
	    char buf2[frag_size];
	    memset(buf2, 0, sizeof(buf2));
	    ::write(fd, buf2, frag_size);
	}
	//log("outbuf is almost empty! (%s waiting)\n", o);
    }

    if (o >= (int)num_frags-1)
    {
	//log("outbuf overflowing (%s): skipping write.\n", o);
	return len;
    }
    
    size_t ret = ::write(fd, buf, len);
    if (ret < 0)
    {
        log("Error: %s\n", errno);
	if (errno != EAGAIN)
	    seterr(errno);
	return len; // avoid using WvStreams buffer
    }

/*
    if (ret < len)
	log("outbuf overflow (%s/%s)!\n", ret, len);
*/

    return len; // avoid using WvStreams buffer
}