31 |
#define MAX_QUEUE 10 |
#define MAX_QUEUE 10 |
32 |
|
|
33 |
int g_dsp_fd; |
int g_dsp_fd; |
34 |
BOOL g_dsp_busy; |
BOOL g_dsp_busy = False; |
35 |
static BOOL reopened; |
static BOOL g_reopened; |
36 |
static BOOL swapaudio; |
static BOOL g_swapaudio; |
37 |
static short samplewidth; |
static short g_samplewidth; |
38 |
|
|
39 |
static struct audio_packet { |
static struct audio_packet |
40 |
|
{ |
41 |
struct stream s; |
struct stream s; |
42 |
uint16 tick; |
uint16 tick; |
43 |
uint8 index; |
uint8 index; |
49 |
{ |
{ |
50 |
char *dsp_dev = getenv("AUDIODEV"); |
char *dsp_dev = getenv("AUDIODEV"); |
51 |
|
|
52 |
if ( dsp_dev == NULL ) |
if (dsp_dev == NULL) |
53 |
{ |
{ |
54 |
dsp_dev="/dev/audio"; |
dsp_dev = "/dev/audio"; |
55 |
} |
} |
56 |
|
|
57 |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY|O_NONBLOCK)) == -1) |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) |
58 |
{ |
{ |
59 |
perror(dsp_dev); |
perror(dsp_dev); |
60 |
return False; |
return False; |
61 |
} |
} |
62 |
|
|
63 |
/* Non-blocking so that user interface is responsive */ |
/* Non-blocking so that user interface is responsive */ |
64 |
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); |
65 |
|
|
66 |
queue_lo = queue_hi = 0; |
queue_lo = queue_hi = 0; |
67 |
reopened = True; |
g_reopened = True; |
68 |
|
|
69 |
return True; |
return True; |
70 |
} |
} |
71 |
|
|
73 |
wave_out_close(void) |
wave_out_close(void) |
74 |
{ |
{ |
75 |
/* Ack all remaining packets */ |
/* Ack all remaining packets */ |
76 |
while ( queue_lo != queue_hi ) |
while (queue_lo != queue_hi) |
77 |
{ |
{ |
78 |
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
79 |
free(packet_queue[queue_lo].s.data); |
free(packet_queue[queue_lo].s.data); |
80 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
81 |
} |
} |
82 |
|
|
83 |
|
#if defined I_FLUSH && defined FLUSHW |
84 |
/* Flush the audiobuffer */ |
/* Flush the audiobuffer */ |
85 |
ioctl(g_dsp_fd,I_FLUSH,FLUSHW); |
ioctl(g_dsp_fd, I_FLUSH, FLUSHW); |
86 |
|
#endif |
87 |
close(g_dsp_fd); |
close(g_dsp_fd); |
88 |
} |
} |
89 |
|
|
90 |
BOOL |
BOOL |
91 |
wave_out_format_supported(WAVEFORMATEX *pwfx) |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
92 |
{ |
{ |
93 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
94 |
return False; |
return False; |
101 |
} |
} |
102 |
|
|
103 |
BOOL |
BOOL |
104 |
wave_out_set_format(WAVEFORMATEX *pwfx) |
wave_out_set_format(WAVEFORMATEX * pwfx) |
105 |
{ |
{ |
106 |
audio_info_t info; |
audio_info_t info; |
107 |
int test = 1; |
int test = 1; |
108 |
|
|
109 |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
110 |
swapaudio = False; |
g_swapaudio = False; |
111 |
AUDIO_INITINFO(&info); |
AUDIO_INITINFO(&info); |
112 |
|
|
113 |
|
|
119 |
{ |
{ |
120 |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
121 |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
122 |
swapaudio = !(*(uint8 *) (&test)); |
g_swapaudio = !(*(uint8 *) (&test)); |
123 |
} |
} |
124 |
|
|
125 |
samplewidth = pwfx->wBitsPerSample/8; |
g_samplewidth = pwfx->wBitsPerSample / 8; |
126 |
|
|
127 |
if (pwfx->nChannels == 1 ) |
if (pwfx->nChannels == 1) |
128 |
{ |
{ |
129 |
info.play.channels = AUDIO_CHANNELS_MONO; |
info.play.channels = 1; |
130 |
} |
} |
131 |
else if (pwfx->nChannels == 2 ) |
else if (pwfx->nChannels == 2) |
132 |
{ |
{ |
133 |
info.play.channels = AUDIO_CHANNELS_STEREO; |
info.play.channels = 2; |
134 |
samplewidth *= 2; |
g_samplewidth *= 2; |
135 |
} |
} |
136 |
|
|
137 |
info.play.sample_rate = pwfx->nSamplesPerSec; |
info.play.sample_rate = pwfx->nSamplesPerSec; |
139 |
info.play.samples = 0; |
info.play.samples = 0; |
140 |
info.play.eof = 0; |
info.play.eof = 0; |
141 |
info.play.error = 0; |
info.play.error = 0; |
142 |
reopened = True; |
g_reopened = True; |
143 |
|
|
144 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
145 |
{ |
{ |
166 |
|
|
167 |
volume = (left > right) ? left : right; |
volume = (left > right) ? left : right; |
168 |
|
|
169 |
if ( volume/AUDIO_MID_BALANCE != 0 ) |
if (volume / AUDIO_MID_BALANCE != 0) |
170 |
{ |
{ |
171 |
balance = AUDIO_MID_BALANCE - (left/(volume/AUDIO_MID_BALANCE)) + (right/(volume/AUDIO_MID_BALANCE)); |
balance = |
172 |
|
AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + |
173 |
|
(right / (volume / AUDIO_MID_BALANCE)); |
174 |
} |
} |
175 |
else |
else |
176 |
{ |
{ |
177 |
balance = AUDIO_MID_BALANCE; |
balance = AUDIO_MID_BALANCE; |
178 |
} |
} |
179 |
|
|
180 |
info.play.gain = volume/(65536/AUDIO_MAX_GAIN); |
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
181 |
info.play.balance = balance; |
info.play.balance = balance; |
182 |
|
|
183 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
198 |
error("No space to queue audio packet\n"); |
error("No space to queue audio packet\n"); |
199 |
return; |
return; |
200 |
} |
} |
201 |
|
|
202 |
queue_hi = next_hi; |
queue_hi = next_hi; |
203 |
|
|
204 |
packet->s = *s; |
packet->s = *s; |
229 |
|
|
230 |
while (1) |
while (1) |
231 |
{ |
{ |
232 |
if ( reopened ) |
if (g_reopened) |
233 |
{ |
{ |
234 |
/* Device was just (re)openend */ |
/* Device was just (re)openend */ |
235 |
samplecnt = 0; |
samplecnt = 0; |
236 |
swapped = False; |
swapped = False; |
237 |
sentcompletion = True; |
sentcompletion = True; |
238 |
reopened = False; |
g_reopened = False; |
239 |
} |
} |
240 |
|
|
241 |
if (queue_lo == queue_hi) |
if (queue_lo == queue_hi) |
248 |
out = &packet->s; |
out = &packet->s; |
249 |
|
|
250 |
/* Swap the current packet, but only once */ |
/* Swap the current packet, but only once */ |
251 |
if ( swapaudio && ! swapped ) |
if (g_swapaudio && !swapped) |
252 |
{ |
{ |
253 |
for ( i = 0; i < out->end - out->p; i += 2 ) |
for (i = 0; i < out->end - out->p; i += 2) |
254 |
{ |
{ |
255 |
swap = *(out->p + i); |
swap = *(out->p + i); |
256 |
*(out->p + i) = *(out->p + i + 1); |
*(out->p + i) = *(out->p + i + 1); |
259 |
swapped = True; |
swapped = True; |
260 |
} |
} |
261 |
|
|
262 |
if ( sentcompletion ) |
if (sentcompletion) |
263 |
{ |
{ |
264 |
sentcompletion = False; |
sentcompletion = False; |
265 |
numsamples = (out->end - out->p)/samplewidth; |
numsamples = (out->end - out->p) / g_samplewidth; |
266 |
} |
} |
267 |
|
|
268 |
len=0; |
len = 0; |
269 |
|
|
270 |
if ( out->end != out->p ) |
if (out->end != out->p) |
271 |
{ |
{ |
272 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(g_dsp_fd, out->p, out->end - out->p); |
273 |
if (len == -1) |
if (len == -1) |
289 |
} |
} |
290 |
|
|
291 |
/* Ack the packet, if we have played at least 70% */ |
/* Ack the packet, if we have played at least 70% */ |
292 |
if ( info.play.samples >= samplecnt+((numsamples*7)/10) ) |
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
293 |
{ |
{ |
294 |
samplecnt += numsamples; |
samplecnt += numsamples; |
295 |
rdpsnd_send_completion(packet->tick, packet->index); |
rdpsnd_send_completion(packet->tick, packet->index); |