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; |
114 |
samplewidth=1; |
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 |
|
samplewidth = 2; |
120 |
|
/* Do we need to swap the 16bit values? (Are we BigEndian) */ |
121 |
swapaudio = !(*(uint8 *) (&test)); |
swapaudio = !(*(uint8 *) (&test)); |
|
samplewidth=2; |
|
122 |
} |
} |
123 |
|
|
124 |
if (pwfx->nChannels == 1 ) |
if (pwfx->nChannels == 1 ) |
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; |
183 |
STREAM out; |
STREAM out; |
184 |
static BOOL swapped = False; |
static BOOL swapped = False; |
185 |
static BOOL sentcompletion = True; |
static BOOL sentcompletion = True; |
186 |
static int samplecnt; |
static uint32 samplecnt = 0; |
187 |
static int numsamples; |
static uint32 numsamples; |
188 |
|
|
189 |
while (1) |
while (1) |
190 |
{ |
{ |
191 |
|
if ( reopened ) |
192 |
|
{ |
193 |
|
/* Device was just (re)openend */ |
194 |
|
samplecnt = 0; |
195 |
|
swapped = False; |
196 |
|
sentcompletion = True; |
197 |
|
reopened = False; |
198 |
|
} |
199 |
|
|
200 |
if (queue_lo == queue_hi) |
if (queue_lo == queue_hi) |
201 |
{ |
{ |
202 |
g_dsp_busy = 0; |
g_dsp_busy = 0; |
206 |
packet = &packet_queue[queue_lo]; |
packet = &packet_queue[queue_lo]; |
207 |
out = &packet->s; |
out = &packet->s; |
208 |
|
|
209 |
|
/* Swap the current packet, but only once */ |
210 |
if ( swapaudio && ! swapped ) |
if ( swapaudio && ! swapped ) |
211 |
{ |
{ |
212 |
for ( i = 0; i < out->end - out->p; i+=2 ) |
for ( i = 0; i < out->end - out->p; i+=2 ) |
220 |
|
|
221 |
if ( sentcompletion ) |
if ( sentcompletion ) |
222 |
{ |
{ |
|
if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) |
|
|
{ |
|
|
perror("AUDIO_GETINFO"); |
|
|
return; |
|
|
} |
|
|
samplecnt=info.play.samples; |
|
223 |
sentcompletion = False; |
sentcompletion = False; |
224 |
numsamples = (out->end-out->p)/samplewidth; |
numsamples = (out->end - out->p)/samplewidth; |
225 |
} |
} |
226 |
|
|
227 |
len=0; |
len=0; |
228 |
|
|
229 |
if ( out->end - out->p != 0 ) |
if ( out->end != out->p ) |
230 |
{ |
{ |
231 |
len = write(g_dsp_fd, out->p, out->end - out->p); |
len = write(g_dsp_fd, out->p, out->end - out->p); |
232 |
if (len == -1) |
if (len == -1) |
246 |
perror("AUDIO_GETINFO"); |
perror("AUDIO_GETINFO"); |
247 |
return; |
return; |
248 |
} |
} |
249 |
if ( info.play.samples >= samplecnt+(numsamples)*0.9 ) |
|
250 |
|
/* Ack the packet, if we have played at least 70% */ |
251 |
|
if ( info.play.samples >= samplecnt+((numsamples*7)/10) ) |
252 |
{ |
{ |
253 |
|
samplecnt += numsamples; |
254 |
rdpsnd_send_completion(packet->tick, packet->index); |
rdpsnd_send_completion(packet->tick, packet->index); |
255 |
free(out->data); |
free(out->data); |
256 |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |
queue_lo = (queue_lo + 1) % MAX_QUEUE; |