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; |
35 |
|
static BOOL reopened; |
36 |
static BOOL swapaudio; |
static BOOL swapaudio; |
37 |
static short samplewidth; |
static short samplewidth; |
38 |
|
|
46 |
BOOL |
BOOL |
47 |
wave_out_open(void) |
wave_out_open(void) |
48 |
{ |
{ |
49 |
char *dsp_dev = "/dev/audio"; |
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) |
if ((g_dsp_fd = open(dsp_dev, O_WRONLY|O_NONBLOCK)) == -1) |
57 |
{ |
{ |
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; |
67 |
|
|
68 |
return True; |
return True; |
69 |
} |
} |
70 |
|
|
71 |
void |
void |
72 |
wave_out_close(void) |
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); |
ioctl(g_dsp_fd,I_FLUSH,FLUSHW); |
84 |
close(g_dsp_fd); |
close(g_dsp_fd); |
85 |
} |
} |
111 |
if (pwfx->wBitsPerSample == 8) |
if (pwfx->wBitsPerSample == 8) |
112 |
{ |
{ |
113 |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
info.play.encoding = AUDIO_ENCODING_LINEAR8; |
|
samplewidth=1; |
|
114 |
} |
} |
115 |
else if (pwfx->wBitsPerSample == 16) |
else if (pwfx->wBitsPerSample == 16) |
116 |
{ |
{ |
117 |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
info.play.encoding = AUDIO_ENCODING_LINEAR; |
118 |
|
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
119 |
swapaudio = !(*(uint8 *) (&test)); |
swapaudio = !(*(uint8 *) (&test)); |
|
samplewidth=2; |
|
120 |
} |
} |
121 |
|
|
122 |
|
samplewidth = pwfx->wBitsPerSample/8; |
123 |
|
|
124 |
if (pwfx->nChannels == 1 ) |
if (pwfx->nChannels == 1 ) |
125 |
{ |
{ |
126 |
info.play.channels = AUDIO_CHANNELS_MONO; |
info.play.channels = AUDIO_CHANNELS_MONO; |
128 |
else if (pwfx->nChannels == 2 ) |
else if (pwfx->nChannels == 2 ) |
129 |
{ |
{ |
130 |
info.play.channels = AUDIO_CHANNELS_STEREO; |
info.play.channels = AUDIO_CHANNELS_STEREO; |
131 |
samplewidth*=2; |
samplewidth *= 2; |
132 |
} |
} |
133 |
|
|
134 |
info.play.sample_rate = pwfx->nSamplesPerSec; |
info.play.sample_rate = pwfx->nSamplesPerSec; |
136 |
info.play.samples = 0; |
info.play.samples = 0; |
137 |
info.play.eof = 0; |
info.play.eof = 0; |
138 |
info.play.error = 0; |
info.play.error = 0; |
139 |
|
reopened = True; |
140 |
|
|
141 |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) |
142 |
{ |
{ |
149 |
} |
} |
150 |
|
|
151 |
void |
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) |
wave_out_write(STREAM s, uint16 tick, uint8 index) |
187 |
{ |
{ |
188 |
struct audio_packet *packet = &packet_queue[queue_hi]; |
struct audio_packet *packet = &packet_queue[queue_hi]; |
199 |
packet->s = *s; |
packet->s = *s; |
200 |
packet->tick = tick; |
packet->tick = tick; |
201 |
packet->index = index; |
packet->index = index; |
202 |
|
packet->s.p += 4; |
203 |
|
|
204 |
/* we steal the data buffer from s, give it a new one */ |
/* we steal the data buffer from s, give it a new one */ |
205 |
s->data = malloc(s->size); |
s->data = malloc(s->size); |
219 |
STREAM out; |
STREAM out; |
220 |
static BOOL swapped = False; |
static BOOL swapped = False; |
221 |
static BOOL sentcompletion = True; |
static BOOL sentcompletion = True; |
222 |
static int samplecnt; |
static uint32 samplecnt = 0; |
223 |
static int numsamples; |
static uint32 numsamples; |
224 |
|
|
225 |
while (1) |
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) |
if (queue_lo == queue_hi) |
237 |
{ |
{ |
238 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
242 |
packet = &packet_queue[queue_lo]; |
packet = &packet_queue[queue_lo]; |
243 |
out = &packet->s; |
out = &packet->s; |
244 |
|
|
245 |
|
/* Swap the current packet, but only once */ |
246 |
if ( swapaudio && ! swapped ) |
if ( swapaudio && ! swapped ) |
247 |
{ |
{ |
248 |
for ( i = 0; i < out->end - out->p; i+=2 ) |
for ( i = 0; i < out->end - out->p; i += 2 ) |
249 |
{ |
{ |
250 |
swap = *(out->p + i); |
swap = *(out->p + i); |
251 |
*(out->p + i ) = *(out->p + i + 1); |
*(out->p + i) = *(out->p + i + 1); |
252 |
*(out->p + i + 1) = swap; |
*(out->p + i + 1) = swap; |
|
swapped = True; |
|
253 |
} |
} |
254 |
|
swapped = True; |
255 |
} |
} |
256 |
|
|
257 |
if ( sentcompletion ) |
if ( sentcompletion ) |
258 |
{ |
{ |
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
|
|
{ |
|
|
perror("AUDIO_GETINFO"); |
|
|
return; |
|
|
} |
|
|
samplecnt=info.play.samples; |
|
259 |
sentcompletion = False; |
sentcompletion = False; |
260 |
numsamples = (out->end-out->p)/samplewidth; |
numsamples = (out->end - out->p)/samplewidth; |
261 |
} |
} |
262 |
|
|
263 |
len=0; |
len=0; |
264 |
|
|
265 |
if ( out->end - out->p != 0 ) |
if ( out->end != out->p ) |
266 |
{ |
{ |
267 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(g_dsp_fd, out->p, out->end - out->p); |
268 |
if (len == -1) |
if (len == -1) |
282 |
perror("AUDIO_GETINFO"); |
perror("AUDIO_GETINFO"); |
283 |
return; |
return; |
284 |
} |
} |
285 |
if ( info.play.samples >= samplecnt+(numsamples)*0.9 ) |
|
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); |
rdpsnd_send_completion(packet->tick, packet->index); |
291 |
free(out->data); |
free(out->data); |
292 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |