/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpsnd_sun.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /sourceforge.net/trunk/rdesktop/rdpsnd_sun.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 491 - (show annotations)
Mon Oct 13 16:09:45 2003 UTC (20 years, 7 months ago) by stargo
File MIME type: text/plain
File size: 6129 byte(s)
Volume control for OSS & SUN
Ignore first 4 bytes of audio-packet (clicking noise)

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

  ViewVC Help
Powered by ViewVC 1.1.26