aboutsummaryrefslogtreecommitdiff
path: root/oss.c
blob: 4210799eb78f0ab432796134bb5de7da28652db5 (plain)
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/*
 * QEMU OSS Audio output driver
 * 
 * Copyright (c) 2003 Vassili Karpov (malc)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>

#include "vl.h"

/* http://www.df.lth.se/~john_e/gems/gem002d.html */
/* http://www.multi-platforms.com/Tips/PopCount.htm */
static inline uint32_t popcount (uint32_t u)
{
  u = ((u&0x55555555) + ((u>>1)&0x55555555));
  u = ((u&0x33333333) + ((u>>2)&0x33333333));
  u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));
  u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));
  u = ( u&0x0000ffff) + (u>>16);
  return u;
}

static inline uint32_t lsbindex (uint32_t u)
{
  return popcount ((u&-u)-1);
}

#define MIN(a, b) ((a)>(b)?(b):(a))
#define MAX(a, b) ((a)<(b)?(b):(a))

#define DEREF(x) (void)x
#define log(...) fprintf (stderr, "oss: " __VA_ARGS__)
#define ERRFail(...) do {                                       \
    int _errno = errno;                                         \
    fprintf (stderr, "oss: " __VA_ARGS__);                      \
    fprintf (stderr, "system error: %s\n", strerror (_errno));  \
    abort ();                                                   \
} while (0)
#define Fail(...) do {                          \
    fprintf (stderr, "oss: " __VA_ARGS__);      \
    fprintf (stderr, "\n");                     \
    abort ();                                   \
} while (0)

#ifdef DEBUG_OSS
#define lwarn(...) fprintf (stderr, "oss: " __VA_ARGS__)
#define linfo(...) fprintf (stderr, "oss: " __VA_ARGS__)
#define ldebug(...) fprintf (stderr, "oss: " __VA_ARGS__)
#else
#define lwarn(...)
#define linfo(...)
#define ldebug(...)
#endif


#define IOCTL(args) do {                        \
  int ret = ioctl args;                         \
  if (-1 == ret) {                              \
    ERRFail (#args);                            \
  }                                             \
  ldebug ("ioctl " #args " = %d\n", ret);       \
} while (0)

static int audio_fd = -1;
static int freq;
static int conf_nfrags = 4;
static int conf_fragsize;
static int nfrags;
static int fragsize;
static int bufsize;
static int nchannels;
static int fmt;
static int rpos;
static int wpos;
static int atom;
static int live;
static int leftover;
static int bytes_per_second;
static void *buf;
static enum {DONT, DSP, TID} estimate = TID;

static void (*copy_fn)(void *, void *, int);

static void copy_no_conversion (void *dst, void *src, int size)
{
    memcpy (dst, src, size);
}

static void copy_u16_to_s16 (void *dst, void *src, int size)
{
    int i;
    uint16_t *out, *in;

    out = dst;
    in = src;

    for (i = 0; i < size / 2; i++) {
        out[i] = in[i] + 0x8000;
    }
}

static void pab (struct audio_buf_info *abinfo)
{
    DEREF (abinfo);

    ldebug ("fragments %d, fragstotal %d, fragsize %d, bytes %d\n"
            "rpos %d, wpos %d, live %d\n",
            abinfo->fragments,
            abinfo->fragstotal,
            abinfo->fragsize,
            abinfo->bytes,
            rpos, wpos, live);
}

void AUD_reset (int rfreq, int rnchannels, audfmt_e rfmt)
{
    int fmt_;
    int bits16;

    if (-1 == audio_fd) {
        AUD_open (rfreq, rnchannels, rfmt);
        return;
    }

    switch (rfmt) {
    case AUD_FMT_U8:
        bits16 = 0;
        fmt_ = AFMT_U8;
        copy_fn = copy_no_conversion;
        atom = 1;
        break;

    case AUD_FMT_S8:
        Fail ("can not play 8bit signed");

    case AUD_FMT_S16:
        bits16 = 1;
        fmt_ = AFMT_S16_LE;
        copy_fn = copy_no_conversion;
        atom = 2;
        break;

    case AUD_FMT_U16:
        bits16 = 1;
        fmt_ = AFMT_S16_LE;
        copy_fn = copy_u16_to_s16;
        atom = 2;
        break;

    default:
        abort ();
    }

    if ((fmt_ == fmt) && (bits16 + 1 == nchannels) && (rfreq == freq))
        return;
    else {
        AUD_open (rfreq, rnchannels, rfmt);
    }
}

void AUD_open (int rfreq, int rnchannels, audfmt_e rfmt)
{
    int fmt_;
    int mmmmssss;
    struct audio_buf_info abinfo;
    int _fmt;
    int _freq;
    int _nchannels;
    int bits16;

    bits16 = 0;

    switch (rfmt) {
    case AUD_FMT_U8:
        bits16 = 0;
        fmt_ = AFMT_U8;
        copy_fn = copy_no_conversion;
        atom = 1;
        break;

    case AUD_FMT_S8:
        Fail ("can not play 8bit signed");

    case AUD_FMT_S16:
        bits16 = 1;
        fmt_ = AFMT_S16_LE;
        copy_fn = copy_no_conversion;
        atom = 2;
        break;

    case AUD_FMT_U16:
        bits16 = 1;
        fmt_ = AFMT_S16_LE;
        copy_fn = copy_u16_to_s16;
        atom = 2;
        break;

    default:
        abort ();
    }

    if (buf) {
        free (buf);
        buf = 0;
    }

    if (-1 != audio_fd)
        close (audio_fd);

    audio_fd = open ("/dev/dsp", O_WRONLY | O_NONBLOCK);
    if (-1 == audio_fd) {
        ERRFail ("can not open /dev/dsp");
    }

    _fmt = fmt_;
    _freq = rfreq;
    _nchannels = rnchannels;

    IOCTL ((audio_fd, SNDCTL_DSP_RESET, 1));
    IOCTL ((audio_fd, SNDCTL_DSP_SAMPLESIZE, &_fmt));
    IOCTL ((audio_fd, SNDCTL_DSP_CHANNELS, &_nchannels));
    IOCTL ((audio_fd, SNDCTL_DSP_SPEED, &_freq));
    IOCTL ((audio_fd, SNDCTL_DSP_NONBLOCK));

    /* from oss.pdf:

    The argument to this call is an integer encoded as 0xMMMMSSSS (in
    hex). The 16 least significant bits determine the fragment
    size. The size is 2^SSSS. For examp le SSSS=0008 gives fragment
    size of 256 bytes (2^8). The minimum is 16 bytes (SSSS=4) and the
    maximum is total_buffer_size/2. Some devices or processor
    architectures may require larger fragments - in this case the
    requested fragment size is automatically increased.

    So ahem... 4096 = 2^12, and grand total 0x0004000c
    */

    mmmmssss = (conf_nfrags << 16) | conf_fragsize;
    IOCTL ((audio_fd, SNDCTL_DSP_SETFRAGMENT, &mmmmssss));

    linfo ("_fmt = %d, fmt = %d\n"
           "_channels = %d, rnchannels = %d\n"
           "_freq = %d, freq = %d\n",
           _fmt, fmt_,
           _nchannels, rnchannels,
           _freq, rfreq);

    if (_fmt != fmt_) {
        Fail ("format %d != %d", _fmt, fmt_);
    }

    if (_nchannels != rnchannels) {
        Fail ("channels %d != %d", _nchannels, rnchannels);
    }

    if (_freq != rfreq) {
        Fail ("freq %d != %d", _freq, rfreq);
    }

    IOCTL ((audio_fd, SNDCTL_DSP_GETOSPACE, &abinfo));

    nfrags = abinfo.fragstotal;
    fragsize = abinfo.fragsize;
    freq = _freq;
    fmt = _fmt;
    nchannels = rnchannels;
    atom <<= nchannels >>  1;
    bufsize = nfrags * fragsize;

    bytes_per_second = (freq << (nchannels >> 1)) << bits16;

    linfo ("bytes per second %d\n", bytes_per_second);

    linfo ("fragments %d, fragstotal %d, fragsize %d, bytes %d, bufsize %d\n",
           abinfo.fragments,
           abinfo.fragstotal,
           abinfo.fragsize,
           abinfo.bytes,
           bufsize);

    if (NULL == buf) {
        buf = malloc (bufsize);
        if (NULL == buf) {
            abort ();
        }
    }

    rpos = 0;
    wpos = 0;
    live = 0;
}

int AUD_write (void *in_buf, int size)
{
    int to_copy, temp;
    uint8_t *in, *out;

    to_copy = MIN (bufsize - live, size);

    temp = to_copy;

    in = in_buf;
    out = buf;

    while (temp) {
        int copy;

        copy = MIN (temp, bufsize - wpos);
        copy_fn (out + wpos, in, copy);

        wpos += copy;
        if (wpos == bufsize) {
            wpos = 0;
        }

        temp -= copy;
        in += copy;
        live += copy;
    }

    return to_copy;
}

void AUD_run (void)
{
    int res;
    int bytes;
    struct audio_buf_info abinfo;

    if (0 == live)
        return;

    res = ioctl (audio_fd, SNDCTL_DSP_GETOSPACE, &abinfo);

    if (-1 == res) {
        int err;

        err = errno;
        lwarn ("SNDCTL_DSP_GETOSPACE failed with %s\n", strerror (err));
    }

    bytes = abinfo.bytes;
    bytes = MIN (live, bytes);
#if 0
    bytes = (bytes / fragsize) * fragsize;
#endif

    while (bytes) {
        int left, play, written;

        left = bufsize - rpos;
        play = MIN (left, bytes);
        written = write (audio_fd, (void *) ((uint32_t) buf + rpos), play);

        if (-1 == written) {
            if (EAGAIN == errno || EINTR == errno) {
                return;
            }
            else {
                ERRFail ("write audio");
            }
        }

        play = written;
        live -= play;
        rpos += play;
        bytes -= play;

        if (rpos == bufsize) {
            rpos = 0;
        }
    }
}

static int get_dsp_bytes (void)
{
    int res;
    struct count_info info;

    res = ioctl (audio_fd, SNDCTL_DSP_GETOPTR, &info);
    if (-1 == res) {
        int err;

        err = errno;
        lwarn ("SNDCTL_DSP_GETOPTR failed with %s\n", strerror (err));
        return -1;
    }
    else {
        ldebug ("bytes %d\n", info.bytes);
        return info.bytes;
    }
}

void AUD_adjust_estimate (int _leftover)
{
    leftover = _leftover;
}

int AUD_get_free (void)
{
    int free, elapsed;

    free = bufsize - live;

    if (0 == free)
        return 0;

    elapsed = free;
    switch (estimate) {
    case DONT:
        break;

    case DSP:
        {
            static int old_bytes;
            int bytes;

            bytes = get_dsp_bytes ();
            if (bytes <= 0)
                return free;

            elapsed = bytes - old_bytes;
            old_bytes = bytes;
            ldebug ("dsp elapsed %d bytes\n", elapsed);
            break;
        }

    case TID:
        {
            static uint64_t old_ticks;
            uint64_t ticks, delta;
            uint64_t ua_elapsed;
            uint64_t al_elapsed;

            ticks = cpu_get_ticks ();
            delta = ticks - old_ticks;
            old_ticks = ticks;

            ua_elapsed = (delta * bytes_per_second) / ticks_per_sec;
            al_elapsed = ua_elapsed & ~3ULL;

            ldebug ("tid elapsed %llu bytes\n", ua_elapsed);

            if (al_elapsed > (uint64_t) INT_MAX)
                elapsed = INT_MAX;
            else
                elapsed = al_elapsed;

            elapsed += leftover;
        }
    }

    if (elapsed > free) {
        lwarn ("audio can not keep up elapsed %d free %d\n", elapsed, free);
        return free;
    }
    else {
        return elapsed;
    }
}

int AUD_get_live (void)
{
    return live;
}

int AUD_get_buffer_size (void)
{
    return bufsize;
}

void AUD_init (void)
{
    int fsp;
    int _fragsize = 4096;

    DEREF (pab);

    fsp = _fragsize;
    if (0 != (fsp & (fsp - 1))) {
        Fail ("fragment size %d is not power of 2", fsp);
    }

    conf_fragsize = lsbindex (fsp);
}