File: drv_nas.c

package info (click to toggle)
libmikmod 3.3.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,188 kB
  • sloc: ansic: 36,299; sh: 5,013; makefile: 548; javascript: 1
file content (448 lines) | stat: -rw-r--r-- 10,766 bytes parent folder | download | duplicates (5)
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
/*** drv_nas.c --- MikMod sound library NAS output driver  -*- C -*- */
/** $Id$ */

/*** Copyright (C) 2004 Ivan Shmakov */

/** This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Library General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  */

/** This program is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.  */

/** You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
    02111-1307, USA.  */

/*** Code: */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "mikmod_internals.h"

#ifdef DRV_NAS

/* !!! HACK: avoid BOOL typedef clash */
#define BOOL NAS_BOOL
#include <audio/audiolib.h>
#undef BOOL /* HACK !!! */

#include <errno.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#define MALLOC_VAR(x)    ((x = (typeof (x))MikMod_malloc       (sizeof (*(x)))))
#define MALLOC_ARY(x, y) ((x = (typeof (x))MikMod_malloc ((y) * sizeof (*(x)))))

/* slow network configuration by-default: */
#define NAS_DEFAULT_BUFFER 5000
#define NAS_DEFAULT_LOW_WM 75

struct my_info {
  AuServer *server;
  AuDeviceID device;
  AuFlowID sole_flow;
  AuEventHandlerRec *handler;
  /* buffer */
  struct {
    char *ptr;
    size_t size, pend, want;
  } buf;
  int stopped_p;
};

struct my_info *nas_info = NULL;

/* command line params.: */
static char *audioserver = NULL;
static long buffer_msec = NAS_DEFAULT_BUFFER;
static long buffer_low = NAS_DEFAULT_LOW_WM;

static AuDeviceID
nas_find_device (AuServer *s, int channels)
{
  int i, max = AuServerNumDevices (s);
  for (i = 0; i < max; i++) {
    if ((AuDeviceKind (AuServerDevice (s, i))
         == AuComponentKindPhysicalOutput)
        && AuDeviceNumTracks (AuServerDevice (s, i)) == channels)
      return AuDeviceIdentifier (AuServerDevice (s, i));
  }
  return AuNone;
}

/*** Data Sending and Event Handling */

static void
nas_send_data (struct my_info *i)
{
  AuServer *s = i->server;
  AuFlowID flow = i->sole_flow;
  char *ptr = i->buf.ptr;
  const size_t bytes = i->buf.want;
  const size_t pending = i->buf.pend;

  i->buf.want = 0;
  if (bytes < pending) {
    AuWriteElement (s, flow, 0, bytes, ptr, AuFalse, NULL);
    memmove (ptr, ptr + bytes, pending - bytes);
    i->buf.pend -= bytes;
  } else {
    AuWriteElement (s, flow, 0, pending, ptr,
                    bytes > pending ? AuTrue : AuFalse,
                    NULL);
    i->buf.pend = 0;
  }
}

static AuBool
nas_event_handler (AuServer *s, AuEvent *ev, AuEventHandlerRec *handler)
{
  struct my_info *i = (struct my_info *)(handler->data);
  switch (ev->type) {
  case AuEventTypeElementNotify:
    {
      AuElementNotifyEvent *eev = (AuElementNotifyEvent *)ev;
      switch (eev->kind) {
      case AuElementNotifyKindLowWater:
        /* FIXME: am I right? */
        i->buf.want = (size_t)(eev->num_bytes);
        break;
      case AuElementNotifyKindState:
        switch (eev->cur_state) {
        case AuStatePause:
          i->stopped_p = 0;
          if (eev->reason != AuReasonUser)
            i->buf.want = (size_t)(eev->num_bytes);
          break;
        case AuStateStop:
          i->stopped_p = 1;
          break;
        }
        break;
      }
    }
    break;
  }

  return AuTrue;
}

static void
nas_check_event (AuServer *s)
{
  AuEvent e;

  AuNextEvent (s, AuTrue, &e);
  AuDispatchEvent (s, &e);
}

/*** Initialization */

static int
nas_init_flow (struct my_info *i)
{
  AuServer *s = i->server;
  AuDeviceID device;
  AuFlowID flow;
  AuElement elt[2];
  unsigned char format;
  const int rate = md_mixfreq;
  const int chans = (md_mode & DMODE_STEREO) ? 2 : 1;
  const AuInt32 samples = (AuInt32)1 * rate * buffer_msec / 1000;
  const AuInt32 samples_low = samples * buffer_low / 100;
  AuEventHandlerRec *handler;

  if ((device = nas_find_device (s, chans)) == AuNone
      || (flow = AuCreateFlow (s, NULL)) == AuNone) {
    _mm_errno = MMERR_OPENING_AUDIO;
    return -1;
  }

  format = (md_mode & DMODE_16BITS)
    ? ((((char) *(short *)"x") == 'x') /* FIXME: too ugly? */
       ? AuFormatLinearSigned16LSB
       : AuFormatLinearSigned16MSB)
    : AuFormatLinearUnsigned8;

  AuMakeElementImportClient (&elt[0], /* element */
                             rate, format, chans, /* format, etc. */
                             AuTrue, /* discard */
                             samples, samples_low, /* buf size */
                             0, NULL); /* actions */
  AuMakeElementExportDevice (&elt[1], /* element */
                             0, device,
                             rate, AuUnlimitedSamples,
                             0, NULL); /* actions */
  AuSetElements (s, flow, AuTrue, 2, elt, NULL);

  /* FIXME: check for errors? */
  handler
    = AuRegisterEventHandler (s,
                              AuEventHandlerIDMask, 0, flow,
                              nas_event_handler, (AuPointer)i);

  {
    const size_t bufsz
      = samples * chans * AuSizeofFormat (format);
    if ((MALLOC_ARY (i->buf.ptr, bufsz)) == NULL)
      return -1;
    i->buf.size = bufsz;
    i->buf.pend = i->buf.want = 0;
  }

  i->device = device;
  i->sole_flow = flow;
  i->handler = handler;

  i->stopped_p = 1;

  return 0;
}

static int
nas_init_server (struct my_info *i)
{
  AuServer *s;

  if (!(s = AuOpenServer (audioserver, 0, NULL, 0, NULL, NULL))) {
    _mm_errno = MMERR_OPENING_AUDIO;
    return -1;
  }

  i->server = s;
  if (nas_init_flow (i) < 0) {
    /* _mm_errno should be set in nas_init_flow */
    AuCloseServer (s);
    return -1;
  }

  return 0;
}

static int
NAS_init (void)
{
  struct my_info *i;

  /* FIXME: assert here? */
  if (nas_info != NULL) {
    _mm_errno = MMERR_OPENING_AUDIO;
    return 1;
  }

  if (MALLOC_VAR (i) == NULL)
    return 1;

  if (nas_init_server (i) < 0
      || VC_Init () != 0) {
    MikMod_free (i);
    return 1;
  }

  nas_info = i;
  return 0;
}

/*** Deinitialization & Reset */

static void
NAS_exit (void)
{
  struct my_info *i = nas_info;
  AuServer *s;

  if (i == NULL)
    return;

  VC_Exit ();

  s = i->server;

  AuUnregisterEventHandler (s, i->handler);
  AuDestroyFlow (s, i->sole_flow, NULL);
  AuCloseServer (s);
  MikMod_free (i->buf.ptr);
  MikMod_free (i);

  nas_info = NULL;
}

static int
NAS_reset (void)
{
  NAS_exit ();
  return NAS_init ();
}

/*** Flow Control */

static int
NAS_flow_start (void)
{
  struct my_info *i = nas_info;

  i->buf.pend = i->buf.want = 0;
  AuStartFlow (i->server, i->sole_flow, NULL);
  while (i->stopped_p)
    nas_check_event (i->server);

  if (VC_PlayStart () != 0)
    return 1;

  return 0;
}

static void
NAS_flow_stop (void)
{
  struct my_info *i = nas_info;

  VC_PlayStop ();

  while (!i->stopped_p) {
    nas_check_event (i->server);
	if (i->buf.want > 0)
		nas_send_data (i);
  }
  i->buf.pend = i->buf.want = 0;
}

static void
NAS_flow_pause (void)
{
  struct my_info *i = nas_info;
  AuPauseFlow (i->server, i->sole_flow, NULL);
  /* nas_check_event (i->server); */
}

/*** Update routine */

static void
NAS_update (void)
{
  struct my_info *i = nas_info;
  const size_t bufsz = i->buf.size;

  if (i->stopped_p) {
    /* underrun? */
    AuStartFlow (i->server, i->sole_flow, NULL);
    while (i->stopped_p)
      nas_check_event (i->server);
  }

  if (i->buf.pend < bufsz) {
    size_t bytes
      = (size_t)VC_WriteBytes ((SBYTE *)(i->buf.ptr + i->buf.pend),
                               (ULONG)(i->buf.size  - i->buf.pend));
    i->buf.pend += bytes;
  }

  while ((i->buf.want == 0) && !i->stopped_p)
    nas_check_event (i->server);
  if (!i->stopped_p)
    nas_send_data (i);
}

/*** Miscellaneous API routines */

static void
NAS_cmd_line (const CHAR *s)
{
  CHAR *p;

  if ((p = MD_GetAtom ("audioserver", s, 0))) {
    MikMod_free (audioserver);
    audioserver = p;
  }

  if ((p = MD_GetAtom ("buffer", s, 0))) {
    long v = atol (p);
    MikMod_free (p);
    buffer_msec = (v < 50 || v > 10000) ? NAS_DEFAULT_BUFFER : v;
  }

  if ((p = MD_GetAtom ("buffer_low", s, 0))) {
    long v = atol (p);
    MikMod_free (p);
    buffer_low = (v < 10 || v > 90) ? NAS_DEFAULT_LOW_WM : v;
  }
}

static BOOL
NAS_there_p (void)
{
  AuServer *s;

  if (!(s = AuOpenServer (NULL, 0, NULL, 0, NULL, NULL)))
    return 0;
  AuCloseServer (s);

  return 1;
}

/*** MDRIVER record */

MIKMODAPI MDRIVER drv_nas = {
  NULL,                         /* next */
  "Network Audio System",       /* Name */
  "Network Audio System driver v0.1", /* Version */
  0, 255,                       /* NB: 0 hardware voices? */
  "nas",                        /* Alias */
  "audioserver:t::NAS server name\n"
  "buffer:r:50:10000:Audio buffer size, ms\n"
  "buffer_low:r:10:90:Audio buffer low water mark, percents\n",
  NAS_cmd_line,                 /* CommandLine */
  NAS_there_p,                  /* IsPresent */
  VC_SampleLoad,                /* SampleLoad */
  VC_SampleUnload,              /* SampleUnload */
  VC_SampleSpace,               /* FreeSampleSpace */
  VC_SampleLength,              /* RealSampleLength */
  NAS_init,                     /* Init */
  NAS_exit,                     /* Exit */
  NAS_reset,                    /* Reset */
  VC_SetNumVoices,              /* SetNumVoices */
  NAS_flow_start,               /* PlayStart */
  NAS_flow_stop,                /* PlayStop */
  NAS_update,                   /* Update */
  NAS_flow_pause,               /* Pause */
  VC_VoiceSetVolume,            /* VoiceSetVolume */
  VC_VoiceGetVolume,            /* VoiceGetVolume */
  VC_VoiceSetFrequency,         /* VoiceSetFrequency */
  VC_VoiceGetFrequency,         /* VoiceGetFrequency */
  VC_VoiceSetPanning,           /* VoiceSetPanning */
  VC_VoiceGetPanning,           /* VoiceGetPanning */
  VC_VoicePlay,                 /* VoicePlay */
  VC_VoiceStop,                 /* VoiceStop */
  VC_VoiceStopped,              /* VoiceStopped */
  VC_VoiceGetPosition,          /* VoiceGetPosition */
  VC_VoiceRealVolume            /* VoiceRealVolume */
};

#else /* DRV_NAS */
MISSING(drv_nas);
#endif /* DRV_NAS */

/*** Emacs stuff */
/** Local variables: */
/** fill-column: 72 */
/** indent-tabs-mode: nil */
/** mode: outline-minor */
/** outline-regexp: "[/]\\*\\*\\*" */
/** End: */
/*** drv_nas.c ends here */