-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesd_a.cpp
272 lines (227 loc) · 5.64 KB
/
esd_a.cpp
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
/*
TiMidity++ -- MIDI to WAVE converter and player
Copyright (C) 1999 Masanao Izumo <mo@goice.co.jp>
Copyright (C) 1995 Tuukka Toivonen <tt@cgs.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
esd_a.c by Avatar <avatar@deva.net>
based on linux_a.c
Functions to play sound through EsounD
*/
#ifdef AU_ESD
#ifdef ORIG_TIMPP
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#include <errno.h>
#include <esd.h>
#ifdef ORIG_TIMPP
#include "timidity.h"
#else
#include "config.h"
#endif
#include "common.h"
#include "output.h"
#include "controls.h"
#ifdef ORIG_TIMPP
#include "timer.h"
#endif
#include "instrum.h"
#include "playmidi.h"
#ifdef ORIG_TIMPP
#include "miditrace.h"
#endif
#ifdef ORIG_TIMPP
static int acntl(int request, void *arg);
#endif
static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
static void close_output(void);
static void output_data(int32 *buf, uint32 count);
static int driver_output_data(unsigned char *buf, uint32 count);
static void flush_output(void);
static void purge_output(void);
static int output_count(uint32 ct);
/* export the playback mode */
#define dpm esd_play_mode
#ifdef ORIG_TIMPP
PlayMode dpm = {
DEFAULT_RATE,
PE_16BIT|PE_SIGNED,
PF_PCM_STREAM|PF_CAN_TRACE|PF_BUFF_FRAGM_OPT,
-1,
{0}, /* default: get all the buffer fragments you can */
"Enlightened sound daemon", 'e',
"/dev/dsp",
open_output,
close_output,
output_data,
acntl
};
#else
PlayMode dpm = {
DEFAULT_RATE,
PE_16BIT|PE_SIGNED,
-1,
{0}, /* default: get all the buffer fragments you can */
"Enlightened sound daemon", 'e',
"/dev/dsp",
open_output,
close_output,
output_data,
driver_output_data,
flush_output,
purge_output,
output_count
};
#endif
#define PE_ALAW 0x20
/*************************************************************************/
/* We currently only honor the PE_MONO bit, and the sample rate. */
static int open_output(void)
{
int fd, /*tmp, i,*/ warnings = 0;
/* int include_enc, exclude_enc; */
esd_format_t esdformat;
#if 0
include_enc = 0;
exclude_enc = PE_ULAW|PE_ALAW|PE_BYTESWAP; /* They can't mean these */
if(dpm.encoding & PE_16BIT)
include_enc |= PE_SIGNED;
else
exclude_enc |= PE_SIGNED;
dpm.encoding = validate_encoding(dpm.encoding, include_enc, exclude_enc);
#endif
/* Open the audio device */
esdformat = (dpm.encoding & PE_16BIT) ? ESD_BITS16 : ESD_BITS8;
esdformat |= (dpm.encoding & PE_MONO) ? ESD_MONO : ESD_STEREO;
fd = esd_play_stream_fallback(esdformat,dpm.rate,NULL,"timidity");
if(fd < 0)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s",
dpm.name, strerror(errno));
return -1;
}
dpm.fd = fd;
return warnings;
}
static int driver_output_data(unsigned char *buf, uint32 count)
{
return write(dpm.fd, buf, count);
}
#ifdef ORIG_TMPP
static int output_data(char *buf, uint32 count)
{
int n;
while(count > 0)
{
if((n = write(dpm.fd, buf, count)) == -1)
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"%s: %s", dpm.name, strerror(errno));
if(errno == EWOULDBLOCK)
{
/* It is possible to come here because of bug of the
* sound driver.
*/
continue;
}
return -1;
}
buf += n;
count -= n;
}
return 0;
}
#endif
#ifndef ORIG_TIMPP
static int output_count(uint32 ct)
{
int samples = -1;
int samples_queued = 0, samples_sent = (int)ct;
extern int b_out_count();
samples = samples_sent = b_out_count();
if (samples_sent) {
/* samples_queued is PM_REQ_GETFILLED */
#if 0
if (snd_pcm_playback_status(handle, &playback_status) == 0)
samples_queued = playback_status.queue;
#endif
samples -= samples_queued;
}
if (!(dpm.encoding & PE_MONO)) samples >>= 1;
if (dpm.encoding & PE_16BIT) samples >>= 1;
return samples;
}
static void output_data(int32 *buf, uint32 count)
{
int ocount;
if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
ocount = (int)count;
if (ocount) {
if (dpm.encoding & PE_16BIT)
{
/* Convert data to signed 16-bit PCM */
s32tos16(buf, count);
ocount *= 2;
}
else
{
/* Convert to 8-bit unsigned and write out. */
s32tou8(buf, count);
}
}
b_out(dpm.id_character, dpm.fd, (int *)buf, ocount);
}
static void flush_output(void)
{
output_data(0, 0);
esd_audio_flush();
}
static void purge_output(void)
{
b_out(dpm.id_character, dpm.fd, 0, -1);
}
#endif
static void close_output(void)
{
if(dpm.fd == -1)
return;
close(dpm.fd);
dpm.fd = -1;
}
#ifdef ORIG_TIMPP
static int acntl(int request, void *arg)
{
switch(request)
{
case PM_REQ_DISCARD:
esd_audio_flush();
return 0;
case PM_REQ_RATE: {
/* not implemented yet */
break;
}
}
return -1;
}
#endif
#endif /* AU_ESD */