1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Sound Channel Process Functions - SGI/IRIX |
4 |
Copyright (C) Matthew Chapman 2003 |
5 |
Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003 |
6 |
Copyright (C) Jeremy Meng void.foo@gmail.com 2004, 2005 |
7 |
|
8 |
This program is free software; you can redistribute it and/or modify |
9 |
it under the terms of the GNU General Public License as published by |
10 |
the Free Software Foundation; either version 2 of the License, or |
11 |
(at your option) any later version. |
12 |
|
13 |
This program is distributed in the hope that it will be useful, |
14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
GNU General Public License for more details. |
17 |
|
18 |
You should have received a copy of the GNU General Public License |
19 |
along with this program; if not, write to the Free Software |
20 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
*/ |
22 |
|
23 |
#include "rdesktop.h" |
24 |
#include <errno.h> |
25 |
#include <dmedia/audio.h> |
26 |
|
27 |
/* #define IRIX_DEBUG 1 */ |
28 |
|
29 |
#define IRIX_MAX_VOL 65535 |
30 |
|
31 |
ALconfig audioconfig; |
32 |
ALport output_port; |
33 |
|
34 |
static BOOL g_swapaudio; |
35 |
static int g_snd_rate; |
36 |
static BOOL g_swapaudio; |
37 |
static int width = AL_SAMPLE_16; |
38 |
|
39 |
double min_volume, max_volume, volume_range; |
40 |
int resource, maxFillable; |
41 |
int combinedFrameSize; |
42 |
|
43 |
BOOL |
44 |
wave_out_open(void) |
45 |
{ |
46 |
ALparamInfo pinfo; |
47 |
|
48 |
#if (defined(IRIX_DEBUG)) |
49 |
fprintf(stderr, "sgi_open: begin\n"); |
50 |
#endif |
51 |
|
52 |
if (alGetParamInfo(AL_DEFAULT_OUTPUT, AL_GAIN, &pinfo) < 0) |
53 |
{ |
54 |
fprintf(stderr, "sgi_open: alGetParamInfo failed: %s\n", |
55 |
alGetErrorString(oserror())); |
56 |
} |
57 |
min_volume = alFixedToDouble(pinfo.min.ll); |
58 |
max_volume = alFixedToDouble(pinfo.max.ll); |
59 |
volume_range = (max_volume - min_volume); |
60 |
#if (defined(IRIX_DEBUG)) |
61 |
fprintf(stderr, "sgi_open: minvol = %lf, maxvol= %lf, range = %lf.\n", |
62 |
min_volume, max_volume, volume_range); |
63 |
#endif |
64 |
|
65 |
rdpsnd_queue_init(); |
66 |
|
67 |
audioconfig = alNewConfig(); |
68 |
if (audioconfig == (ALconfig) 0) |
69 |
{ |
70 |
fprintf(stderr, "sgi_open: alNewConfig failed: %s\n", alGetErrorString(oserror())); |
71 |
return False; |
72 |
} |
73 |
|
74 |
output_port = alOpenPort("rdpsnd", "w", 0); |
75 |
if (output_port == (ALport) 0) |
76 |
{ |
77 |
fprintf(stderr, "sgi_open: alOpenPort failed: %s\n", alGetErrorString(oserror())); |
78 |
return False; |
79 |
} |
80 |
|
81 |
#if (defined(IRIX_DEBUG)) |
82 |
fprintf(stderr, "sgi_open: returning\n"); |
83 |
#endif |
84 |
return True; |
85 |
} |
86 |
|
87 |
void |
88 |
wave_out_close(void) |
89 |
{ |
90 |
/* Ack all remaining packets */ |
91 |
#if (defined(IRIX_DEBUG)) |
92 |
fprintf(stderr, "sgi_close: begin\n"); |
93 |
#endif |
94 |
|
95 |
while (!rdpsnd_queue_empty()) |
96 |
{ |
97 |
/* We need to add 50 to tell windows that time has passed while |
98 |
* playing this packet */ |
99 |
rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick + 50, |
100 |
rdpsnd_queue_current_packet()->index); |
101 |
rdpsnd_queue_next(); |
102 |
} |
103 |
alDiscardFrames(output_port, 0); |
104 |
|
105 |
alClosePort(output_port); |
106 |
alFreeConfig(audioconfig); |
107 |
#if (defined(IRIX_DEBUG)) |
108 |
fprintf(stderr, "sgi_close: returning\n"); |
109 |
#endif |
110 |
} |
111 |
|
112 |
BOOL |
113 |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
114 |
{ |
115 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
116 |
return False; |
117 |
if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2)) |
118 |
return False; |
119 |
if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16)) |
120 |
return False; |
121 |
|
122 |
return True; |
123 |
} |
124 |
|
125 |
BOOL |
126 |
wave_out_set_format(WAVEFORMATEX * pwfx) |
127 |
{ |
128 |
int channels; |
129 |
int frameSize, channelCount; |
130 |
ALpv params; |
131 |
|
132 |
#if (defined(IRIX_DEBUG)) |
133 |
fprintf(stderr, "sgi_set_format: init...\n"); |
134 |
#endif |
135 |
|
136 |
g_swapaudio = False; |
137 |
if (pwfx->wBitsPerSample == 8) |
138 |
width = AL_SAMPLE_8; |
139 |
else if (pwfx->wBitsPerSample == 16) |
140 |
{ |
141 |
width = AL_SAMPLE_16; |
142 |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
143 |
#if (defined(B_ENDIAN)) |
144 |
g_swapaudio = 1; |
145 |
#else |
146 |
g_swapaudio = 0; |
147 |
#endif |
148 |
} |
149 |
|
150 |
/* Limited support to configure an opened audio port in IRIX. The |
151 |
number of channels is a static setting and can not be changed after |
152 |
a port is opened. So if the number of channels remains the same, we |
153 |
can configure other settings; otherwise we have to reopen the audio |
154 |
port, using same config. */ |
155 |
|
156 |
channels = pwfx->nChannels; |
157 |
g_snd_rate = pwfx->nSamplesPerSec; |
158 |
|
159 |
alSetSampFmt(audioconfig, AL_SAMPFMT_TWOSCOMP); |
160 |
alSetWidth(audioconfig, width); |
161 |
if (channels != alGetChannels(audioconfig)) |
162 |
{ |
163 |
alClosePort(output_port); |
164 |
alSetChannels(audioconfig, channels); |
165 |
output_port = alOpenPort("rdpsnd", "w", audioconfig); |
166 |
|
167 |
if (output_port == (ALport) 0) |
168 |
{ |
169 |
fprintf(stderr, "sgi_set_format: alOpenPort failed: %s\n", |
170 |
alGetErrorString(oserror())); |
171 |
return False; |
172 |
} |
173 |
|
174 |
} |
175 |
|
176 |
resource = alGetResource(output_port); |
177 |
maxFillable = alGetFillable(output_port); |
178 |
channelCount = alGetChannels(audioconfig); |
179 |
frameSize = alGetWidth(audioconfig); |
180 |
|
181 |
if (frameSize == 0 || channelCount == 0) |
182 |
{ |
183 |
fprintf(stderr, "sgi_set_format: bad frameSize or channelCount\n"); |
184 |
return False; |
185 |
} |
186 |
combinedFrameSize = frameSize * channelCount; |
187 |
|
188 |
params.param = AL_RATE; |
189 |
params.value.ll = (long long) g_snd_rate << 32; |
190 |
|
191 |
if (alSetParams(resource, ¶ms, 1) < 0) |
192 |
{ |
193 |
fprintf(stderr, "wave_set_format: alSetParams failed: %s\n", |
194 |
alGetErrorString(oserror())); |
195 |
return False; |
196 |
} |
197 |
if (params.sizeOut < 0) |
198 |
{ |
199 |
fprintf(stderr, "wave_set_format: invalid rate %d\n", g_snd_rate); |
200 |
return False; |
201 |
} |
202 |
|
203 |
#if (defined(IRIX_DEBUG)) |
204 |
fprintf(stderr, "sgi_set_format: returning...\n"); |
205 |
#endif |
206 |
return True; |
207 |
} |
208 |
|
209 |
void |
210 |
wave_out_volume(uint16 left, uint16 right) |
211 |
{ |
212 |
double gainleft, gainright; |
213 |
ALpv pv[1]; |
214 |
ALfixed gain[8]; |
215 |
|
216 |
#if (defined(IRIX_DEBUG)) |
217 |
fprintf(stderr, "sgi_volume: begin\n"); |
218 |
fprintf(stderr, "left='%d', right='%d'\n", left, right); |
219 |
#endif |
220 |
|
221 |
gainleft = (double) left / IRIX_MAX_VOL; |
222 |
gainright = (double) right / IRIX_MAX_VOL; |
223 |
|
224 |
gain[0] = alDoubleToFixed(min_volume + gainleft * volume_range); |
225 |
gain[1] = alDoubleToFixed(min_volume + gainright * volume_range); |
226 |
|
227 |
pv[0].param = AL_GAIN; |
228 |
pv[0].value.ptr = gain; |
229 |
pv[0].sizeIn = 8; |
230 |
if (alSetParams(AL_DEFAULT_OUTPUT, pv, 1) < 0) |
231 |
{ |
232 |
fprintf(stderr, "sgi_volume: alSetParams failed: %s\n", |
233 |
alGetErrorString(oserror())); |
234 |
return; |
235 |
} |
236 |
|
237 |
#if (defined(IRIX_DEBUG)) |
238 |
fprintf(stderr, "sgi_volume: returning\n"); |
239 |
#endif |
240 |
} |
241 |
|
242 |
void |
243 |
wave_out_play(void) |
244 |
{ |
245 |
struct audio_packet *packet; |
246 |
ssize_t len; |
247 |
unsigned int i; |
248 |
uint8 swap; |
249 |
STREAM out; |
250 |
static BOOL swapped = False; |
251 |
int gf; |
252 |
|
253 |
while (1) |
254 |
{ |
255 |
if (rdpsnd_queue_empty()) |
256 |
{ |
257 |
g_dsp_busy = False; |
258 |
return; |
259 |
} |
260 |
|
261 |
packet = rdpsnd_queue_current_packet(); |
262 |
out = &packet->s; |
263 |
|
264 |
/* Swap the current packet, but only once */ |
265 |
if (g_swapaudio && !swapped) |
266 |
{ |
267 |
for (i = 0; i < out->end - out->p; i += 2) |
268 |
{ |
269 |
swap = *(out->p + i); |
270 |
*(out->p + i) = *(out->p + i + 1); |
271 |
*(out->p + i + 1) = swap; |
272 |
} |
273 |
swapped = True; |
274 |
} |
275 |
|
276 |
len = out->end - out->p; |
277 |
|
278 |
alWriteFrames(output_port, out->p, len / combinedFrameSize); |
279 |
|
280 |
out->p += len; |
281 |
if (out->p == out->end) |
282 |
{ |
283 |
gf = alGetFilled(output_port); |
284 |
if (gf < (4 * maxFillable / 10)) |
285 |
{ |
286 |
rdpsnd_send_completion(packet->tick, packet->index); |
287 |
rdpsnd_queue_next(); |
288 |
swapped = False; |
289 |
} |
290 |
else |
291 |
{ |
292 |
#if (defined(IRIX_DEBUG)) |
293 |
/* fprintf(stderr,"Busy playing...\n"); */ |
294 |
#endif |
295 |
g_dsp_busy = True; |
296 |
usleep(10); |
297 |
return; |
298 |
} |
299 |
} |
300 |
} |
301 |
} |