Skip to content

Latest commit

 

History

History
460 lines (403 loc) · 13.8 KB

drv_alsa.c

File metadata and controls

460 lines (403 loc) · 13.8 KB
 
Nov 10, 2019
Nov 10, 2019
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
/* MikMod sound library
(c) 1998, 1999, 2000 Miodrag Vallat and others - see file AUTHORS for
complete list.
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.
*/
/*==============================================================================
$Id$
Driver for Advanced Linux Sound Architecture (ALSA)
==============================================================================*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h"
#ifdef DRV_ALSA
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_MEMORY_H
#include <memory.h>
#endif
#ifdef MIKMOD_DYNAMIC
#include <dlfcn.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <alsa/asoundlib.h>
#if defined(SND_LIB_VERSION) && (SND_LIB_VERSION >= 0x20000)
#undef DRV_ALSA
#endif
#if defined(SND_LIB_VERSION) && (SND_LIB_VERSION < 0x600)
#error ALSA Version too old. Please upgrade your Linux distribution.
#endif
#endif /* DRV_ALSA */
#ifdef DRV_ALSA
#ifdef MIKMOD_DYNAMIC
/* runtime link with libasound */
#ifndef HAVE_RTLD_GLOBAL
#define RTLD_GLOBAL (0)
#endif
static int (*alsa_pcm_subformat_mask_malloc)(snd_pcm_subformat_mask_t **);
static const char * (*alsa_strerror)(int);
static int (*alsa_pcm_resume)(snd_pcm_t *);
static int (*alsa_pcm_prepare)(snd_pcm_t *);
static int (*alsa_pcm_hw_params_any)(snd_pcm_t *, snd_pcm_hw_params_t *);
static int (*alsa_pcm_hw_params)(snd_pcm_t *, snd_pcm_hw_params_t *);
static int (*alsa_pcm_hw_params_current)(snd_pcm_t *, snd_pcm_hw_params_t *);
static int (*alsa_pcm_hw_params_set_access)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t);
static int (*alsa_pcm_hw_params_set_format)(snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t);
static int (*alsa_pcm_hw_params_set_rate_near)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
static int (*alsa_pcm_hw_params_set_channels_near)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *);
static int (*alsa_pcm_hw_params_set_buffer_time_near)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
static int (*alsa_pcm_hw_params_set_period_time_near)(snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
static int (*alsa_pcm_hw_params_get_buffer_size)(const snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
static int (*alsa_pcm_hw_params_get_period_size)(const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
static int (*alsa_pcm_sw_params_sizeof)(void);
static int (*alsa_pcm_hw_params_sizeof)(void);
static int (*alsa_pcm_open)(snd_pcm_t**, const char *, int, int);
static int (*alsa_pcm_close)(snd_pcm_t*);
static int (*alsa_pcm_drain)(snd_pcm_t*);
static int (*alsa_pcm_drop)(snd_pcm_t*);
static int (*alsa_pcm_start)(snd_pcm_t *);
static snd_pcm_sframes_t (*alsa_pcm_writei)(snd_pcm_t*,const void*,snd_pcm_uframes_t);
static void* libasound = NULL;
#else
/* compile-time link with libasound */
#define alsa_pcm_subformat_mask_malloc snd_pcm_subformat_mask_malloc
#define alsa_strerror snd_strerror
#define alsa_pcm_hw_params_any snd_pcm_hw_params_any
#define alsa_pcm_hw_params snd_pcm_hw_params
#define alsa_pcm_hw_params_current snd_pcm_hw_params_current
#define alsa_pcm_hw_params_set_access snd_pcm_hw_params_set_access
#define alsa_pcm_hw_params_set_format snd_pcm_hw_params_set_format
#define alsa_pcm_hw_params_set_rate_near snd_pcm_hw_params_set_rate_near
#define alsa_pcm_hw_params_set_channels_near snd_pcm_hw_params_set_channels_near
#define alsa_pcm_hw_params_set_buffer_time_near snd_pcm_hw_params_set_buffer_time_near
#define alsa_pcm_hw_params_set_period_time_near snd_pcm_hw_params_set_period_time_near
#define alsa_pcm_hw_params_get_buffer_size snd_pcm_hw_params_get_buffer_size
#define alsa_pcm_hw_params_get_period_size snd_pcm_hw_params_get_period_size
#define alsa_pcm_resume snd_pcm_resume
#define alsa_pcm_prepare snd_pcm_prepare
#define alsa_pcm_close snd_pcm_close
#define alsa_pcm_drain snd_pcm_drain
#define alsa_pcm_drop snd_pcm_drop
#define alsa_pcm_start snd_pcm_start
#define alsa_pcm_open snd_pcm_open
#define alsa_pcm_writei snd_pcm_writei
#endif /* MIKMOD_DYNAMIC */
#if defined(MIKMOD_DEBUG)
# define dbgprint fprintf
#elif defined (__GNUC__) && !(defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
# define dbgprint(f, fmt, args...) do {} while (0)
#else
# define dbgprint(f, ...) do {} while (0)
#endif
static BOOL enabled = 0;
static snd_pcm_t *pcm_h = NULL;
static SBYTE *audiobuffer = NULL;
static snd_pcm_sframes_t period_size;
static int bytes_written = 0, bytes_played = 0;
static int global_frame_size;
#ifdef MIKMOD_DYNAMIC
static int ALSA_Link(void)
{
if (libasound) return 0;
/* load libasound.so */
libasound = dlopen("libasound.so.2",RTLD_LAZY|RTLD_GLOBAL);
if (!libasound) libasound = dlopen("libasound.so",RTLD_LAZY|RTLD_GLOBAL);
if (!libasound) return 1;
if (!(alsa_pcm_subformat_mask_malloc = dlsym(libasound,"snd_pcm_subformat_mask_malloc"))) return 1;
if (!(alsa_strerror = dlsym(libasound,"snd_strerror"))) return 1;
if (!(alsa_pcm_prepare = dlsym(libasound,"snd_pcm_prepare"))) return 1;
if (!(alsa_pcm_sw_params_sizeof = dlsym(libasound,"snd_pcm_sw_params_sizeof"))) return 1;
if (!(alsa_pcm_hw_params_sizeof = dlsym(libasound,"snd_pcm_hw_params_sizeof"))) return 1;
if (!(alsa_pcm_resume = dlsym(libasound,"snd_pcm_resume"))) return 1;
if (!(alsa_pcm_hw_params_any = dlsym(libasound,"snd_pcm_hw_params_any"))) return 1;
if (!(alsa_pcm_hw_params = dlsym(libasound,"snd_pcm_hw_params"))) return 1;
if (!(alsa_pcm_hw_params_current = dlsym(libasound,"snd_pcm_hw_params_current"))) return 1;
if (!(alsa_pcm_hw_params_set_access = dlsym(libasound,"snd_pcm_hw_params_set_access"))) return 1;
if (!(alsa_pcm_hw_params_set_format = dlsym(libasound,"snd_pcm_hw_params_set_format"))) return 1;
if (!(alsa_pcm_hw_params_set_rate_near = dlsym(libasound,"snd_pcm_hw_params_set_rate_near"))) return 1;
if (!(alsa_pcm_hw_params_set_channels_near = dlsym(libasound,"snd_pcm_hw_params_set_channels_near"))) return 1;
if (!(alsa_pcm_hw_params_set_buffer_time_near = dlsym(libasound,"snd_pcm_hw_params_set_buffer_time_near"))) return 1;
if (!(alsa_pcm_hw_params_set_period_time_near = dlsym(libasound,"snd_pcm_hw_params_set_period_time_near"))) return 1;
if (!(alsa_pcm_hw_params_get_buffer_size = dlsym(libasound,"snd_pcm_hw_params_get_buffer_size"))) return 1;
if (!(alsa_pcm_hw_params_get_period_size = dlsym(libasound,"snd_pcm_hw_params_get_period_size"))) return 1;
if (!(alsa_pcm_open = dlsym(libasound,"snd_pcm_open"))) return 1;
if (!(alsa_pcm_close = dlsym(libasound,"snd_pcm_close"))) return 1;
if (!(alsa_pcm_drain = dlsym(libasound,"snd_pcm_drain"))) return 1;
if (!(alsa_pcm_drop = dlsym(libasound,"snd_pcm_drop"))) return 1;
if (!(alsa_pcm_start = dlsym(libasound,"snd_pcm_start"))) return 1;
if (!(alsa_pcm_writei = dlsym(libasound,"snd_pcm_writei"))) return 1;
return 0;
}
static void ALSA_Unlink(void)
{
alsa_pcm_subformat_mask_malloc = NULL;
alsa_strerror = NULL;
alsa_pcm_resume = NULL;
alsa_pcm_prepare = NULL;
alsa_pcm_hw_params_any = NULL;
alsa_pcm_hw_params = NULL;
alsa_pcm_hw_params_current = NULL;
alsa_pcm_hw_params_set_access = NULL;
alsa_pcm_hw_params_set_format = NULL;
alsa_pcm_hw_params_set_rate_near = NULL;
alsa_pcm_hw_params_set_channels_near = NULL;
alsa_pcm_hw_params_set_buffer_time_near = NULL;
alsa_pcm_hw_params_set_period_time_near = NULL;
alsa_pcm_hw_params_get_buffer_size = NULL;
alsa_pcm_hw_params_get_period_size = NULL;
alsa_pcm_close = NULL;
alsa_pcm_drain = NULL;
alsa_pcm_drop = NULL;
alsa_pcm_start = NULL;
alsa_pcm_open = NULL;
alsa_pcm_writei = NULL;
if (libasound) {
dlclose(libasound);
libasound = NULL;
}
}
/* This is done to override the identifiers expanded
* in the macros provided by the ALSA includes which are
* not available.
* */
#define snd_strerror alsa_strerror
#define snd_pcm_sw_params_sizeof alsa_pcm_sw_params_sizeof
#define snd_pcm_hw_params_sizeof alsa_pcm_hw_params_sizeof
#endif /* MIKMOD_DYNAMIC */
static void ALSA_CommandLine(CHAR *cmdline)
{
/* no options */
}
static BOOL ALSA_IsThere(void)
{
snd_pcm_subformat_mask_t *ptr = NULL;
BOOL retval;
#ifdef MIKMOD_DYNAMIC
if (ALSA_Link()) return 0;
#endif
retval = (alsa_pcm_subformat_mask_malloc(&ptr) == 0) && (ptr != NULL);
free(ptr);
#ifdef MIKMOD_DYNAMIC
ALSA_Unlink();
#endif
return retval;
}
static int ALSA_Init_internal(void)
{
snd_pcm_format_t pformat;
unsigned int btime = 250000; /* 250ms */
unsigned int ptime = 50000; /* 50ms */
snd_pcm_uframes_t psize;
snd_pcm_uframes_t bsize;
unsigned int rate, channels;
snd_pcm_hw_params_t * hwparams;
int err;
/* setup playback format structure */
pformat = (md_mode&DMODE_16BITS)? SND_PCM_FORMAT_S16 : SND_PCM_FORMAT_U8;
channels = (md_mode&DMODE_STEREO)?2:1;
rate = md_mixfreq;
#define MIKMOD_ALSA_DEVICE "default"
if ((err = alsa_pcm_open(&pcm_h, MIKMOD_ALSA_DEVICE, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0) {
_mm_errno = MMERR_OPENING_AUDIO;
goto END;
}
snd_pcm_hw_params_alloca(&hwparams);
err = alsa_pcm_hw_params_any(pcm_h, hwparams);
if (err < 0) {
_mm_errno = MMERR_ALSA_NOCONFIG;
goto END;
}
err = alsa_pcm_hw_params_set_access(pcm_h, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
if (!err) err = alsa_pcm_hw_params_set_format(pcm_h, hwparams, pformat);
if (!err) err = alsa_pcm_hw_params_set_rate_near(pcm_h, hwparams, &rate, NULL);
if (!err) err = alsa_pcm_hw_params_set_channels_near(pcm_h, hwparams, &channels);
if (!err) err = alsa_pcm_hw_params_set_buffer_time_near(pcm_h, hwparams, &btime, NULL);
if (!err) err = alsa_pcm_hw_params_set_period_time_near(pcm_h, hwparams, &ptime, NULL);
if (!err) err = alsa_pcm_hw_params(pcm_h, hwparams);
if (err < 0) {
_mm_errno = MMERR_ALSA_SETPARAMS;
goto END;
}
if (rate != md_mixfreq) {
_mm_errno = MMERR_ALSA_SETRATE;
goto END;
}
if (!(md_mode&DMODE_STEREO) && channels != 1) {
_mm_errno = MMERR_ALSA_SETCHANNELS;
goto END;
}
if ((md_mode&DMODE_STEREO) && channels != 2) {
_mm_errno = MMERR_ALSA_SETCHANNELS;
goto END;
}
err = alsa_pcm_hw_params_current(pcm_h, hwparams);
if (!err) err = alsa_pcm_hw_params_get_buffer_size(hwparams, &bsize);
if (!err) err = alsa_pcm_hw_params_get_period_size(hwparams, &psize, NULL);
if (err < 0) {
_mm_errno = MMERR_ALSA_BUFFERSIZE;
goto END;
}
period_size = psize;
global_frame_size = channels * ((md_mode&DMODE_16BITS)? 2 : 1);
if (!(audiobuffer=(SBYTE*)_mm_malloc(period_size * global_frame_size))) {
_mm_errno = MMERR_OUT_OF_MEMORY;
goto END;
}
/* sound device is ready to work */
if (!VC_Init()) {
enabled = 1;
return 0;
}
END:
alsa_pcm_close(pcm_h);
pcm_h = NULL;
return 1;
}
static int ALSA_Init(void)
{
#ifdef MIKMOD_DYNAMIC
if (ALSA_Link()) {
_mm_errno=MMERR_DYNAMIC_LINKING;
return 1;
}
#endif
return ALSA_Init_internal();
}
static void ALSA_Exit_internal(void)
{
enabled = 0;
VC_Exit();
if (pcm_h) {
alsa_pcm_drain(pcm_h);
alsa_pcm_close(pcm_h);
pcm_h = NULL;
}
_mm_free(audiobuffer);
}
static void ALSA_Exit(void)
{
ALSA_Exit_internal();
#ifdef MIKMOD_DYNAMIC
ALSA_Unlink();
#endif
}
/* Underrun and suspend recovery - from alsa-lib:test/pcm.c
*/
static int xrun_recovery(snd_pcm_t *handle, int err)
{
if (err == -EPIPE) { /* under-run */
err = alsa_pcm_prepare(handle);
if (err < 0)
dbgprint(stderr, "Can't recover from underrun, prepare failed: %s\n", snd_strerror(err));
return 0;
}
else if (err == -ESTRPIPE) {
while ((err = alsa_pcm_resume(handle)) == -EAGAIN)
sleep(1); /* wait until the suspend flag is released */
if (err < 0) {
err = alsa_pcm_prepare(handle);
if (err < 0)
dbgprint(stderr, "Can't recover from suspend, prepare failed: %s\n", snd_strerror(err));
}
return 0;
}
return err;
}
static void ALSA_Update(void)
{
int err;
if (!enabled) return;
if (bytes_written == 0 || bytes_played == bytes_written) {
bytes_written = VC_WriteBytes(audiobuffer,period_size * global_frame_size);
bytes_played = 0;
}
while (bytes_played < bytes_written)
{
err = alsa_pcm_writei(pcm_h, &audiobuffer[bytes_played], (bytes_written - bytes_played) / global_frame_size);
if (err == -EAGAIN)
continue;
if (err < 0) {
if ((err = xrun_recovery(pcm_h, err)) < 0) {
_mm_errno = MMERR_ALSA_PCM_RECOVER;
enabled = 0;
dbgprint(stderr, "Write error: %s\n", alsa_strerror(err));
}
break;
}
bytes_played += err * global_frame_size;
}
}
static int ALSA_PlayStart(void)
{
int err;
if (pcm_h == NULL) return 1;
err = alsa_pcm_prepare(pcm_h);
if (err == 0)
err = alsa_pcm_start(pcm_h);
if (err < 0) {
enabled = 0;
_mm_errno = MMERR_ALSA_PCM_START;
return 1;
}
return VC_PlayStart();
}
static void ALSA_PlayStop(void)
{
VC_PlayStop();
if (pcm_h) alsa_pcm_drop(pcm_h);
}
static int ALSA_Reset(void)
{
ALSA_Exit_internal();
return ALSA_Init_internal();
}
MIKMODAPI MDRIVER drv_alsa = {
NULL,
"ALSA",
"Advanced Linux Sound Architecture (ALSA) driver v1.10",
0,255,
"alsa",
ALSA_CommandLine,
ALSA_IsThere,
VC_SampleLoad,
VC_SampleUnload,
VC_SampleSpace,
VC_SampleLength,
ALSA_Init,
ALSA_Exit,
ALSA_Reset,
VC_SetNumVoices,
ALSA_PlayStart,
ALSA_PlayStop,
ALSA_Update,
NULL,
VC_VoiceSetVolume,
VC_VoiceGetVolume,
VC_VoiceSetFrequency,
VC_VoiceGetFrequency,
VC_VoiceSetPanning,
VC_VoiceGetPanning,
VC_VoicePlay,
VC_VoiceStop,
VC_VoiceStopped,
VC_VoiceGetPosition,
VC_VoiceRealVolume
};
#else
MISSING(drv_alsa);
#endif
/* ex:set ts=8: */