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 |
#include <stropts.h> |
#include <stropts.h> |
30 |
|
|
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 swapaudio; |
static BOOL g_reopened; |
36 |
static short samplewidth; |
static BOOL g_swapaudio; |
37 |
|
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; |
47 |
BOOL |
BOOL |
48 |
wave_out_open(void) |
wave_out_open(void) |
49 |
{ |
{ |
50 |
char *dsp_dev = "/dev/audio"; |
char *dsp_dev = getenv("AUDIODEV"); |
51 |
|
|
52 |
|
if (dsp_dev == NULL) |
53 |
|
{ |
54 |
|
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 |
|
g_reopened = True; |
68 |
|
|
69 |
return True; |
return True; |
70 |
} |
} |
71 |
|
|
72 |
void |
void |
73 |
wave_out_close(void) |
wave_out_close(void) |
74 |
{ |
{ |
75 |
ioctl(g_dsp_fd,I_FLUSH,FLUSHW); |
/* Ack all remaining packets */ |
76 |
|
while (queue_lo != queue_hi) |
77 |
|
{ |
78 |
|
rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); |
79 |
|
free(packet_queue[queue_lo].s.data); |
80 |
|
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
81 |
|
} |
82 |
|
|
83 |
|
/* Flush the audiobuffer */ |
84 |
|
ioctl(g_dsp_fd, I_FLUSH, FLUSHW); |
85 |
close(g_dsp_fd); |
close(g_dsp_fd); |
86 |
} |
} |
87 |
|
|
88 |
BOOL |
BOOL |
89 |
wave_out_format_supported(WAVEFORMATEX *pwfx) |
wave_out_format_supported(WAVEFORMATEX * pwfx) |
90 |
{ |
{ |
91 |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) |
92 |
return False; |
return False; |
99 |
} |
} |
100 |
|
|
101 |
BOOL |
BOOL |
102 |
wave_out_set_format(WAVEFORMATEX *pwfx) |
wave_out_set_format(WAVEFORMATEX * pwfx) |
103 |
{ |
{ |
104 |
audio_info_t info; |
audio_info_t info; |
105 |
int test = 1; |
int test = 1; |
106 |
|
|
107 |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
ioctl(g_dsp_fd, AUDIO_DRAIN, 0); |
108 |
swapaudio = False; |
g_swapaudio = False; |
109 |
AUDIO_INITINFO(&info); |
AUDIO_INITINFO(&info); |
110 |
|
|
111 |
|
|
112 |
if (pwfx->wBitsPerSample == 8) |
if (pwfx->wBitsPerSample == 8) |
113 |
{ |
{ |
114 |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
|
samplewidth=1; |
|
115 |
} |
} |
116 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
117 |
{ |
{ |
118 |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
119 |
swapaudio = !(*(uint8 *) (&test)); |
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
120 |
samplewidth=2; |
g_swapaudio = !(*(uint8 *) (&test)); |
121 |
} |
} |
122 |
|
|
123 |
if (pwfx->nChannels == 1 ) |
g_samplewidth = pwfx->wBitsPerSample / 8; |
124 |
{ |
|
125 |
|
if (pwfx->nChannels == 1) |
126 |
|
{ |
127 |
info.play.channels = AUDIO_CHANNELS_MONO; |
info.play.channels = AUDIO_CHANNELS_MONO; |
128 |
} |
} |
129 |
else if (pwfx->nChannels == 2 ) |
else if (pwfx->nChannels == 2) |
130 |
{ |
{ |
131 |
info.play.channels = AUDIO_CHANNELS_STEREO; |
info.play.channels = AUDIO_CHANNELS_STEREO; |
132 |
samplewidth*=2; |
g_samplewidth *= 2; |
133 |
} |
} |
134 |
|
|
135 |
info.play.sample_rate = pwfx->nSamplesPerSec; |
info.play.sample_rate = pwfx->nSamplesPerSec; |
137 |
info.play.samples = 0; |
info.play.samples = 0; |
138 |
info.play.eof = 0; |
info.play.eof = 0; |
139 |
info.play.error = 0; |
info.play.error = 0; |
140 |
|
g_reopened = True; |
141 |
|
|
142 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
143 |
{ |
{ |
150 |
} |
} |
151 |
|
|
152 |
void |
void |
153 |
|
wave_out_volume(uint16 left, uint16 right) |
154 |
|
{ |
155 |
|
audio_info_t info; |
156 |
|
uint balance; |
157 |
|
uint volume; |
158 |
|
|
159 |
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
160 |
|
{ |
161 |
|
perror("AUDIO_GETINFO"); |
162 |
|
return; |
163 |
|
} |
164 |
|
|
165 |
|
volume = (left > right) ? left : right; |
166 |
|
|
167 |
|
if (volume / AUDIO_MID_BALANCE != 0) |
168 |
|
{ |
169 |
|
balance = |
170 |
|
AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + |
171 |
|
(right / (volume / AUDIO_MID_BALANCE)); |
172 |
|
} |
173 |
|
else |
174 |
|
{ |
175 |
|
balance = AUDIO_MID_BALANCE; |
176 |
|
} |
177 |
|
|
178 |
|
info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); |
179 |
|
info.play.balance = balance; |
180 |
|
|
181 |
|
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
182 |
|
{ |
183 |
|
perror("AUDIO_SETINFO"); |
184 |
|
return; |
185 |
|
} |
186 |
|
} |
187 |
|
|
188 |
|
void |
189 |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
190 |
{ |
{ |
191 |
struct audio_packet *packet = &packet_queue[queue_hi]; |
struct audio_packet *packet = &packet_queue[queue_hi]; |
196 |
error("No space to queue audio packet\n"); |
error("No space to queue audio packet\n"); |
197 |
return; |
return; |
198 |
} |
} |
199 |
|
|
200 |
queue_hi = next_hi; |
queue_hi = next_hi; |
201 |
|
|
202 |
packet->s = *s; |
packet->s = *s; |
203 |
packet->tick = tick; |
packet->tick = tick; |
204 |
packet->index = index; |
packet->index = index; |
205 |
|
packet->s.p += 4; |
206 |
|
|
207 |
/* we steal the data buffer from s, give it a new one */ |
/* we steal the data buffer from s, give it a new one */ |
208 |
s->data = malloc(s->size); |
s->data = malloc(s->size); |
222 |
STREAM out; |
STREAM out; |
223 |
static BOOL swapped = False; |
static BOOL swapped = False; |
224 |
static BOOL sentcompletion = True; |
static BOOL sentcompletion = True; |
225 |
static int samplecnt; |
static uint32 samplecnt = 0; |
226 |
static int numsamples; |
static uint32 numsamples; |
227 |
|
|
228 |
while (1) |
while (1) |
229 |
{ |
{ |
230 |
|
if (g_reopened) |
231 |
|
{ |
232 |
|
/* Device was just (re)openend */ |
233 |
|
samplecnt = 0; |
234 |
|
swapped = False; |
235 |
|
sentcompletion = True; |
236 |
|
g_reopened = False; |
237 |
|
} |
238 |
|
|
239 |
if (queue_lo == queue_hi) |
if (queue_lo == queue_hi) |
240 |
{ |
{ |
241 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
245 |
packet = &packet_queue[queue_lo]; |
packet = &packet_queue[queue_lo]; |
246 |
out = &packet->s; |
out = &packet->s; |
247 |
|
|
248 |
if ( swapaudio && ! swapped ) |
/* Swap the current packet, but only once */ |
249 |
|
if (g_swapaudio && !swapped) |
250 |
{ |
{ |
251 |
for ( i = 0; i < out->end - out->p; i+=2 ) |
for (i = 0; i < out->end - out->p; i += 2) |
252 |
{ |
{ |
253 |
swap = *(out->p + i); |
swap = *(out->p + i); |
254 |
*(out->p + i ) = *(out->p + i + 1); |
*(out->p + i) = *(out->p + i + 1); |
255 |
*(out->p + i + 1) = swap; |
*(out->p + i + 1) = swap; |
|
swapped = True; |
|
256 |
} |
} |
257 |
|
swapped = True; |
258 |
} |
} |
259 |
|
|
260 |
if ( sentcompletion ) |
if (sentcompletion) |
261 |
{ |
{ |
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
|
|
{ |
|
|
perror("AUDIO_GETINFO"); |
|
|
return; |
|
|
} |
|
|
samplecnt=info.play.samples; |
|
262 |
sentcompletion = False; |
sentcompletion = False; |
263 |
numsamples = (out->end-out->p)/samplewidth; |
numsamples = (out->end - out->p) / g_samplewidth; |
264 |
} |
} |
265 |
|
|
266 |
len=0; |
len = 0; |
267 |
|
|
268 |
if ( out->end - out->p != 0 ) |
if (out->end != out->p) |
269 |
{ |
{ |
270 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(g_dsp_fd, out->p, out->end - out->p); |
271 |
if (len == -1) |
if (len == -1) |
285 |
perror("AUDIO_GETINFO"); |
perror("AUDIO_GETINFO"); |
286 |
return; |
return; |
287 |
} |
} |
288 |
if ( info.play.samples >= samplecnt+(numsamples)*0.9 ) |
|
289 |
|
/* Ack the packet, if we have played at least 70% */ |
290 |
|
if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) |
291 |
{ |
{ |
292 |
|
samplecnt += numsamples; |
293 |
rdpsnd_send_completion(packet->tick, packet->index); |
rdpsnd_send_completion(packet->tick, packet->index); |
294 |
free(out->data); |
free(out->data); |
295 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |