25 |
#include <fcntl.h> |
#include <fcntl.h> |
26 |
#include <errno.h> |
#include <errno.h> |
27 |
#include <sys/ioctl.h> |
#include <sys/ioctl.h> |
28 |
#include <sys/audio.h> |
#include <sys/audioio.h> |
29 |
|
|
30 |
|
#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) |
31 |
#include <stropts.h> |
#include <stropts.h> |
32 |
|
#endif |
33 |
|
|
34 |
#define MAX_QUEUE 10 |
#define MAX_QUEUE 10 |
35 |
|
|
36 |
int g_dsp_fd; |
int g_dsp_fd; |
37 |
BOOL g_dsp_busy; |
BOOL g_dsp_busy = False; |
38 |
static BOOL swapaudio; |
static BOOL g_reopened; |
39 |
static short samplewidth; |
static BOOL g_swapaudio; |
40 |
|
static short g_samplewidth; |
41 |
|
|
42 |
static struct audio_packet { |
static struct audio_packet |
43 |
|
{ |
44 |
struct stream s; |
struct stream s; |
45 |
uint16 tick; |
uint16 tick; |
46 |
uint8 index; |
uint8 index; |
50 |
BOOL |
BOOL |
51 |
wave_out_open(void) |
wave_out_open(void) |
52 |
{ |
{ |
53 |
char *dsp_dev = "/dev/audio"; |
char *dsp_dev = getenv("AUDIODEV"); |
54 |
|
|
55 |
|
if (dsp_dev == NULL) |
56 |
|
{ |
57 |
|
dsp_dev = "/dev/audio"; |
58 |
|
} |
59 |
|
|
60 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY|O_NONBLOCK)) == -1) |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
61 |
{ |
{ |
62 |
perror(dsp_dev); |
perror(dsp_dev); |
63 |
return False; |
return False; |
64 |
} |
} |
65 |
|
|
|
|
|
66 |
/* Non-blocking so that user interface is responsive */ |
/* Non-blocking so that user interface is responsive */ |
67 |
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL)|O_NONBLOCK); |
fcntl(g_dsp_fd, F_SETFL, fcntl(g_dsp_fd, F_GETFL) | O_NONBLOCK); |
68 |
|
|
69 |
queue_lo = queue_hi = 0; |
queue_lo = queue_hi = 0; |
70 |
|
g_reopened = True; |
71 |
|
|
72 |
return True; |
return True; |
73 |
} |
} |
74 |
|
|
75 |
void |
void |
76 |
wave_out_close(void) |
wave_out_close(void) |
77 |
{ |
{ |
78 |
ioctl(g_dsp_fd,I_FLUSH,FLUSHW); |
/* Ack all remaining packets */ |
79 |
|
while (queue_lo != queue_hi) |
80 |
|
{ |
81 |
|
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
82 |
|
free(packet_queue[queue_lo].s.data); |
83 |
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
84 |
|
} |
85 |
|
|
86 |
|
#if defined I_FLUSH && defined FLUSHW |
87 |
|
/* Flush the audiobuffer */ |
88 |
|
ioctl(g_dsp_fd, I_FLUSH, FLUSHW); |
89 |
|
#endif |
90 |
|
#if defined AUDIO_FLUSH |
91 |
|
ioctl(g_dsp_fd, AUDIO_FLUSH, NULL); |
92 |
|
#endif |
93 |
close(g_dsp_fd); |
close(g_dsp_fd); |
94 |
} |
} |
95 |
|
|
96 |
BOOL |
BOOL |
97 |
wave_out_format_supported(WAVEFORMATEX *pwfx) |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
98 |
{ |
{ |
99 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
100 |
return False; |
return False; |
107 |
} |
} |
108 |
|
|
109 |
BOOL |
BOOL |
110 |
wave_out_set_format(WAVEFORMATEX *pwfx) |
wave_out_set_format(WAVEFORMATEX * pwfx) |
111 |
{ |
{ |
112 |
audio_info_t info; |
audio_info_t info; |
|
int test = 1; |
|
113 |
|
|
114 |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
115 |
swapaudio = False; |
g_swapaudio = False; |
116 |
AUDIO_INITINFO(&info); |
AUDIO_INITINFO(&info); |
117 |
|
|
118 |
|
|
119 |
if (pwfx->wBitsPerSample == 8) |
if (pwfx->wBitsPerSample == 8) |
120 |
{ |
{ |
121 |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
|
samplewidth=1; |
|
122 |
} |
} |
123 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
124 |
{ |
{ |
125 |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
126 |
swapaudio = !(*(uint8 *) (&test)); |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
127 |
samplewidth=2; |
#ifdef B_ENDIAN |
128 |
|
g_swapaudio = 1; |
129 |
|
#else |
130 |
|
g_swapaudio = 0; |
131 |
|
#endif |
132 |
} |
} |
133 |
|
|
134 |
if (pwfx->nChannels == 1 ) |
g_samplewidth = pwfx->wBitsPerSample / 8; |
135 |
{ |
|
136 |
info.play.channels = AUDIO_CHANNELS_MONO; |
if (pwfx->nChannels == 1) |
137 |
|
{ |
138 |
|
info.play.channels = 1; |
139 |
} |
} |
140 |
else if (pwfx->nChannels == 2 ) |
else if (pwfx->nChannels == 2) |
141 |
{ |
{ |
142 |
info.play.channels = AUDIO_CHANNELS_STEREO; |
info.play.channels = 2; |
143 |
samplewidth*=2; |
g_samplewidth *= 2; |
144 |
} |
} |
145 |
|
|
146 |
info.play.sample_rate = pwfx->nSamplesPerSec; |
info.play.sample_rate = pwfx->nSamplesPerSec; |
148 |
info.play.samples = 0; |
info.play.samples = 0; |
149 |
info.play.eof = 0; |
info.play.eof = 0; |
150 |
info.play.error = 0; |
info.play.error = 0; |
151 |
|
g_reopened = True; |
152 |
|
|
153 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
154 |
{ |
{ |
161 |
} |
} |
162 |
|
|
163 |
void |
void |
164 |
|
wave_out_volume(uint16 left, uint16 right) |
165 |
|
{ |
166 |
|
audio_info_t info; |
167 |
|
uint balance; |
168 |
|
uint volume; |
169 |
|
|
170 |
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
171 |
|
{ |
172 |
|
perror("AUDIO_GETINFO"); |
173 |
|
return; |
174 |
|
} |
175 |
|
|
176 |
|
volume = (left > right) ? left : right; |
177 |
|
|
178 |
|
if (volume / AUDIO_MID_BALANCE != 0) |
179 |
|
{ |
180 |
|
balance = |
181 |
|
AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + |
182 |
|
(right / (volume / AUDIO_MID_BALANCE)); |
183 |
|
} |
184 |
|
else |
185 |
|
{ |
186 |
|
balance = AUDIO_MID_BALANCE; |
187 |
|
} |
188 |
|
|
189 |
|
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
190 |
|
info.play.balance = balance; |
191 |
|
|
192 |
|
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
193 |
|
{ |
194 |
|
perror("AUDIO_SETINFO"); |
195 |
|
return; |
196 |
|
} |
197 |
|
} |
198 |
|
|
199 |
|
void |
200 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
201 |
{ |
{ |
202 |
struct audio_packet *packet = &packet_queue[queue_hi]; |
struct audio_packet *packet = &packet_queue[queue_hi]; |
207 |
error("No space to queue audio packet\n"); |
error("No space to queue audio packet\n"); |
208 |
return; |
return; |
209 |
} |
} |
210 |
|
|
211 |
queue_hi = next_hi; |
queue_hi = next_hi; |
212 |
|
|
213 |
packet->s = *s; |
packet->s = *s; |
214 |
packet->tick = tick; |
packet->tick = tick; |
215 |
packet->index = index; |
packet->index = index; |
216 |
|
packet->s.p += 4; |
217 |
|
|
218 |
/* we steal the data buffer from s, give it a new one */ |
/* we steal the data buffer from s, give it a new one */ |
219 |
s->data = malloc(s->size); |
s->data = malloc(s->size); |
233 |
STREAM out; |
STREAM out; |
234 |
static BOOL swapped = False; |
static BOOL swapped = False; |
235 |
static BOOL sentcompletion = True; |
static BOOL sentcompletion = True; |
236 |
static int samplecnt; |
static uint32 samplecnt = 0; |
237 |
static int numsamples; |
static uint32 numsamples; |
238 |
|
|
239 |
while (1) |
while (1) |
240 |
{ |
{ |
241 |
|
if (g_reopened) |
242 |
|
{ |
243 |
|
/* Device was just (re)openend */ |
244 |
|
samplecnt = 0; |
245 |
|
swapped = False; |
246 |
|
sentcompletion = True; |
247 |
|
g_reopened = False; |
248 |
|
} |
249 |
|
|
250 |
if (queue_lo == queue_hi) |
if (queue_lo == queue_hi) |
251 |
{ |
{ |
252 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
256 |
packet = &packet_queue[queue_lo]; |
packet = &packet_queue[queue_lo]; |
257 |
out = &packet->s; |
out = &packet->s; |
258 |
|
|
259 |
if ( swapaudio && ! swapped ) |
/* Swap the current packet, but only once */ |
260 |
|
if (g_swapaudio && !swapped) |
261 |
{ |
{ |
262 |
for ( i = 0; i < out->end - out->p; i+=2 ) |
for (i = 0; i < out->end - out->p; i += 2) |
263 |
{ |
{ |
264 |
swap = *(out->p + i); |
swap = *(out->p + i); |
265 |
*(out->p + i ) = *(out->p + i + 1); |
*(out->p + i) = *(out->p + i + 1); |
266 |
*(out->p + i + 1) = swap; |
*(out->p + i + 1) = swap; |
|
swapped = True; |
|
267 |
} |
} |
268 |
|
swapped = True; |
269 |
} |
} |
270 |
|
|
271 |
if ( sentcompletion ) |
if (sentcompletion) |
272 |
{ |
{ |
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
|
|
{ |
|
|
perror("AUDIO_GETINFO"); |
|
|
return; |
|
|
} |
|
|
samplecnt=info.play.samples; |
|
273 |
sentcompletion = False; |
sentcompletion = False; |
274 |
numsamples = (out->end-out->p)/samplewidth; |
numsamples = (out->end - out->p) / g_samplewidth; |
275 |
} |
} |
276 |
|
|
277 |
len=0; |
len = 0; |
278 |
|
|
279 |
if ( out->end - out->p != 0 ) |
if (out->end != out->p) |
280 |
{ |
{ |
281 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(g_dsp_fd, out->p, out->end - out->p); |
282 |
if (len == -1) |
if (len == -1) |
296 |
perror("AUDIO_GETINFO"); |
perror("AUDIO_GETINFO"); |
297 |
return; |
return; |
298 |
} |
} |
299 |
if ( info.play.samples >= samplecnt+(numsamples)*0.9 ) |
|
300 |
|
/* Ack the packet, if we have played at least 70% */ |
301 |
|
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
302 |
{ |
{ |
303 |
|
samplecnt += numsamples; |
304 |
rdpsnd_send_completion(packet->tick, packet->index); |
rdpsnd_send_completion(packet->tick, packet->index); |
305 |
free(out->data); |
free(out->data); |
306 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |