/
drv_esd.c
453 lines (380 loc) · 9.88 KB
/
drv_esd.c
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
/* 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: drv_esd.c,v 1.1.1.1 2004/06/01 12:16:17 raph Exp $
Driver for the Enlightened sound daemon (EsounD)
==============================================================================*/
/*
You should set the hostname of the machine running esd in the environment
variable 'ESPEAKER'. If this variable is not set, localhost is used.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "mikmod_internals.h"
#ifdef DRV_ESD
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef MIKMOD_DYNAMIC
#include <dlfcn.h>
#endif
#include <errno.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <esd.h>
#ifdef MIKMOD_DYNAMIC
/* runtime link with libesd */
/* note that since we only need the network part of EsounD, we don't need to
load libasound.so or libaudiofile.so as well */
static int(*esd_closestream)(int);
static int(*esd_playstream)(esd_format_t,int,const char*,const char*);
static void* libesd=NULL;
#ifndef HAVE_RTLD_GLOBAL
#define RTLD_GLOBAL (0)
#endif
#else /* MIKMOD_DYNAMIC */
/* compile-time link with libesd */
#define esd_closestream esd_close
#define esd_playstream esd_play_stream
#endif
#ifdef HAVE_SETENV
#define SETENV setenv("ESD_NO_SPAWN","1",0)
#else
static char ESDENV[] = "ESD_NO_SPAWN=1";
#define SETENV putenv(ESDENV)
#endif
static int sndfd=-1;
static esd_format_t format;
static CHAR *espeaker=NULL;
#ifdef MIKMOD_DYNAMIC
/* straight from audio.c in esd sources */
esd_format_t esd_audio_format=ESD_BITS16|ESD_STEREO;
int esd_audio_rate=ESD_DEFAULT_RATE;
static int ESD_Link(void)
{
if (libesd) return 0;
/* load libesd.so */
libesd=dlopen("libesd.so.0",RTLD_LAZY|RTLD_GLOBAL);
if (!libesd) libesd=dlopen("libesd.so",RTLD_LAZY|RTLD_GLOBAL);
if (!libesd) return 1;
/* resolve function references */
if (!(esd_closestream=dlsym(libesd,"esd_close"))) return 1;
if (!(esd_playstream=dlsym(libesd,"esd_play_stream"))) return 1;
return 0;
}
static void ESD_Unlink(void)
{
esd_closestream=NULL;
esd_playstream=NULL;
if (libesd) {
dlclose(libesd);
libesd=NULL;
}
}
#endif
/* A simple chunk fifo buffer, as we cannot query to ask how much free space
there is with esd, we keep a fifo filled and try to write the entire fifo
non blocking, stopping when we get -EAGAIN */
#define AUDIO_BUF_SIZE (ESD_BUF_SIZE * 4)
static SBYTE audiobuffer[AUDIO_BUF_SIZE];
static int audiobuffer_start, audiobuffer_end, audiobuffer_empty;
static void audiobuffer_init(void)
{
audiobuffer_empty = 1;
audiobuffer_start = audiobuffer_end = 0;
}
/* Get a free chunk of the fifo, the caller is expected to use all of it, so
all of it gets marked filled */
static SBYTE *audiobuffer_get_free_chunk(int *size)
{
int end = audiobuffer_end;
if (audiobuffer_empty)
{
audiobuffer_empty = 0;
*size = AUDIO_BUF_SIZE;
}
else if (audiobuffer_end > audiobuffer_start)
{
*size = AUDIO_BUF_SIZE - audiobuffer_end;
audiobuffer_end = 0;
}
else
{
*size = audiobuffer_start - audiobuffer_end;
audiobuffer_end = audiobuffer_start;
}
return audiobuffer + end;
}
/* Get a filled chunk from the fifo, the caller must call audiobuffer_mark_free
to tell the fifo how much of the chunk it has consumed */
static SBYTE *audiobuffer_get_filled_chunk(int *size)
{
if (audiobuffer_empty)
{
*size = 0;
}
else if (audiobuffer_end > audiobuffer_start)
{
*size = audiobuffer_end - audiobuffer_start;
}
else
{
*size = AUDIO_BUF_SIZE - audiobuffer_start;
}
return audiobuffer + audiobuffer_start;
}
/* Tell the fifo to mark size bytes as free starting from the current head of
the fifo */
static void audiobuffer_mark_free(int size)
{
audiobuffer_start = (audiobuffer_start + size) % AUDIO_BUF_SIZE;
if (audiobuffer_start == audiobuffer_end)
{
audiobuffer_empty = 1;
audiobuffer_start = audiobuffer_end = 0;
}
}
/* I hope to have this function integrated into libesd someday...*/
static ssize_t esd_writebuf(int fd, const void *buffer, size_t count)
{
ssize_t res;
res = write(fd, buffer, count);
if (res < 0 && errno == EAGAIN)
return 0;
return res;
}
static void ESD_CommandLine(CHAR *cmdline)
{
CHAR *ptr=MD_GetAtom("machine",cmdline,0);
if (ptr) {
if (espeaker) free(espeaker);
espeaker=ptr;
}
}
static BOOL ESD_IsThere(void)
{
BOOL retval;
int fd;
#ifdef MIKMOD_DYNAMIC
if (ESD_Link()) return 0;
#endif
/* Try to esablish a connection with default esd settings, but we don't
want esdlib to spawn esd if esd is not running ! */
if (SETENV)
retval=0;
else {
if ((fd=esd_playstream(ESD_BITS16|ESD_STEREO|ESD_STREAM|ESD_PLAY,
ESD_DEFAULT_RATE,espeaker,"libmikmod"))<0)
retval=0;
else {
esd_closestream(fd);
retval=1;
}
}
#ifdef MIKMOD_DYNAMIC
ESD_Unlink();
#endif
return retval;
}
static int ESD_Init_internal(void)
{
format=(md_mode&DMODE_16BITS?ESD_BITS16:ESD_BITS8)|
(md_mode&DMODE_STEREO?ESD_STEREO:ESD_MONO)|ESD_STREAM|ESD_PLAY;
if (md_mixfreq > ESD_DEFAULT_RATE)
md_mixfreq = ESD_DEFAULT_RATE;
/* make sure we can open an esd stream with our parameters */
if (!(SETENV)) {
if ((sndfd=esd_playstream(format,md_mixfreq,espeaker,"libmikmod"))<0) {
_mm_errno=MMERR_OPENING_AUDIO;
return 1;
}
fcntl(sndfd, F_SETFL, fcntl(sndfd, F_GETFL) | O_NONBLOCK);
} else {
_mm_errno=MMERR_OUT_OF_MEMORY;
return 1;
}
/* Initialize the audiobuffer */
audiobuffer_init();
return VC_Init();
}
static int ESD_Init(void)
{
#ifdef MIKMOD_DYNAMIC
if (ESD_Link()) {
_mm_errno=MMERR_DYNAMIC_LINKING;
return 1;
}
#endif
return ESD_Init_internal();
}
static void ESD_Exit_internal(void)
{
VC_Exit();
if (sndfd>=0) {
esd_closestream(sndfd);
sndfd=-1;
signal(SIGPIPE,SIG_DFL);
}
}
static void ESD_Exit(void)
{
ESD_Exit_internal();
#ifdef MIKMOD_DYNAMIC
ESD_Unlink();
#endif
}
typedef ULONG (*VC_WriteBytesFunc)(SBYTE*, ULONG);
static void ESD_Update_internal(VC_WriteBytesFunc WriteBytes)
{
static time_t losttime;
if (sndfd>=0) {
SBYTE *chunk;
int size;
ssize_t written;
/* Fill fifo */
chunk = audiobuffer_get_free_chunk(&size);
while (size)
{
WriteBytes(chunk, size);
chunk = audiobuffer_get_free_chunk(&size);
}
/* And write untill fifo empty, or we would block */
chunk = audiobuffer_get_filled_chunk(&size);
while (size)
{
written = esd_writebuf(sndfd, chunk, size);
if (written < 0)
{
/* if we lost our connection with esd, clean up
and work as the nosound driver until we can
reconnect */
esd_closestream(sndfd);
sndfd = -1;
signal(SIGPIPE, SIG_DFL);
losttime = time(NULL);
break;
}
/* Stop if the buffer is full */
if (written == 0)
break;
audiobuffer_mark_free(written);
chunk = audiobuffer_get_filled_chunk(&size);
}
} else {
/* an alarm would be better, but then the library user could not use
alarm(2) himself... */
if (time(NULL)-losttime>=5) {
losttime=time(NULL);
/* Attempt to reconnect every 5 seconds */
if (!(SETENV))
if ((sndfd=esd_playstream(format,md_mixfreq,espeaker,"libmikmod"))>=0) {
/* reconnected, clear fifo (contains old sound) and recurse
to play sound */
audiobuffer_init();
fcntl(sndfd, F_SETFL, fcntl(sndfd, F_GETFL) | O_NONBLOCK);
ESD_Update_internal(WriteBytes);
}
}
}
}
static void ESD_Update(void)
{
ESD_Update_internal(VC_WriteBytes);
}
static void ESD_Pause(void)
{
ESD_Update_internal(VC_SilenceBytes);
}
static int ESD_PlayStart(void)
{
if (sndfd<0)
if (!(SETENV)) {
if ((sndfd=esd_playstream(format,md_mixfreq,espeaker,"libmikmod"))<0) {
_mm_errno=MMERR_OPENING_AUDIO;
return 1;
}
fcntl(sndfd, F_SETFL, fcntl(sndfd, F_GETFL) | O_NONBLOCK);
}
/* since the default behaviour of SIGPIPE on most Unices is to kill the
program, we'll prefer handle EPIPE ourselves should the esd die - recent
esdlib use a do-nothing handler, which prevents us from receiving EPIPE,
so we override this */
signal(SIGPIPE,SIG_IGN);
/* silence buffers */
VC_SilenceBytes(audiobuffer,ESD_BUF_SIZE);
esd_writebuf(sndfd,audiobuffer,ESD_BUF_SIZE);
audiobuffer_init();
return VC_PlayStart();
}
static void ESD_PlayStop(void)
{
if (sndfd>=0) {
/* silence buffers */
VC_SilenceBytes(audiobuffer,ESD_BUF_SIZE);
esd_writebuf(sndfd,audiobuffer,ESD_BUF_SIZE);
audiobuffer_init();
signal(SIGPIPE,SIG_DFL);
}
VC_PlayStop();
}
static int ESD_Reset(void)
{
ESD_Exit_internal();
return ESD_Init_internal();
}
MIKMODAPI MDRIVER drv_esd={
NULL,
"Enlightened sound daemon",
/* use the same version number as the EsounD release it works best with */
"Enlightened sound daemon (EsounD) driver v0.2.38",
0,255,
"esd",
ESD_CommandLine,
ESD_IsThere,
VC_SampleLoad,
VC_SampleUnload,
VC_SampleSpace,
VC_SampleLength,
ESD_Init,
ESD_Exit,
ESD_Reset,
VC_SetNumVoices,
ESD_PlayStart,
ESD_PlayStop,
ESD_Update,
ESD_Pause,
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_esd);
#endif
/* ex:set ts=4: */