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