--- sourceforge.net/trunk/rdesktop/rdpsnd_sun.c 2003/10/04 12:24:56 477 +++ sourceforge.net/trunk/rdesktop/rdpsnd_sun.c 2003/10/15 14:01:32 499 @@ -36,7 +36,8 @@ static BOOL swapaudio; static short samplewidth; -static struct audio_packet { +static struct audio_packet +{ struct stream s; uint16 tick; uint8 index; @@ -48,23 +49,23 @@ { char *dsp_dev = getenv("AUDIODEV"); - if ( dsp_dev == NULL ) + if (dsp_dev == NULL) { - dsp_dev="/dev/audio"; + dsp_dev = "/dev/audio"; } - if ((g_dsp_fd = open(dsp_dev, O_WRONLY|O_NONBLOCK)) == -1) + if ((g_dsp_fd = open(dsp_dev, O_WRONLY | O_NONBLOCK)) == -1) { perror(dsp_dev); return False; } /* Non-blocking so that user interface is responsive */ - 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); + queue_lo = queue_hi = 0; reopened = True; - + return True; } @@ -72,7 +73,7 @@ wave_out_close(void) { /* Ack all remaining packets */ - while ( queue_lo != queue_hi ) + while (queue_lo != queue_hi) { rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index); free(packet_queue[queue_lo].s.data); @@ -80,12 +81,12 @@ } /* Flush the audiobuffer */ - ioctl(g_dsp_fd,I_FLUSH,FLUSHW); + ioctl(g_dsp_fd, I_FLUSH, FLUSHW); close(g_dsp_fd); } BOOL -wave_out_format_supported(WAVEFORMATEX *pwfx) +wave_out_format_supported(WAVEFORMATEX * pwfx) { if (pwfx->wFormatTag != WAVE_FORMAT_PCM) return False; @@ -98,7 +99,7 @@ } BOOL -wave_out_set_format(WAVEFORMATEX *pwfx) +wave_out_set_format(WAVEFORMATEX * pwfx) { audio_info_t info; int test = 1; @@ -111,21 +112,21 @@ if (pwfx->wBitsPerSample == 8) { info.play.encoding = AUDIO_ENCODING_LINEAR8; - samplewidth = 1; } else if (pwfx->wBitsPerSample == 16) { info.play.encoding = AUDIO_ENCODING_LINEAR; - samplewidth = 2; /* Do we need to swap the 16bit values? (Are we BigEndian) */ swapaudio = !(*(uint8 *) (&test)); } - if (pwfx->nChannels == 1 ) - { + samplewidth = pwfx->wBitsPerSample / 8; + + if (pwfx->nChannels == 1) + { info.play.channels = AUDIO_CHANNELS_MONO; } - else if (pwfx->nChannels == 2 ) + else if (pwfx->nChannels == 2) { info.play.channels = AUDIO_CHANNELS_STEREO; samplewidth *= 2; @@ -136,6 +137,7 @@ info.play.samples = 0; info.play.eof = 0; info.play.error = 0; + reopened = True; if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) { @@ -148,6 +150,42 @@ } void +wave_out_volume(uint16 left, uint16 right) +{ + audio_info_t info; + uint balance; + uint volume; + + if (ioctl(g_dsp_fd, AUDIO_GETINFO, &info) == -1) + { + perror("AUDIO_GETINFO"); + return; + } + + volume = (left > right) ? left : right; + + if (volume / AUDIO_MID_BALANCE != 0) + { + balance = + AUDIO_MID_BALANCE - (left / (volume / AUDIO_MID_BALANCE)) + + (right / (volume / AUDIO_MID_BALANCE)); + } + else + { + balance = AUDIO_MID_BALANCE; + } + + info.play.gain = volume / (65536 / AUDIO_MAX_GAIN); + info.play.balance = balance; + + if (ioctl(g_dsp_fd, AUDIO_SETINFO, &info) == -1) + { + perror("AUDIO_SETINFO"); + return; + } +} + +void wave_out_write(STREAM s, uint16 tick, uint8 index) { struct audio_packet *packet = &packet_queue[queue_hi]; @@ -158,12 +196,13 @@ error("No space to queue audio packet\n"); return; } - + queue_hi = next_hi; packet->s = *s; packet->tick = tick; packet->index = index; + packet->s.p += 4; /* we steal the data buffer from s, give it a new one */ s->data = malloc(s->size); @@ -188,7 +227,7 @@ while (1) { - if ( reopened ) + if (reopened) { /* Device was just (re)openend */ samplecnt = 0; @@ -207,26 +246,26 @@ out = &packet->s; /* Swap the current packet, but only once */ - if ( swapaudio && ! swapped ) + if (swapaudio && !swapped) { - for ( i = 0; i < out->end - out->p; i+=2 ) + for (i = 0; i < out->end - out->p; i += 2) { swap = *(out->p + i); - *(out->p + i ) = *(out->p + i + 1); + *(out->p + i) = *(out->p + i + 1); *(out->p + i + 1) = swap; - swapped = True; } + swapped = True; } - if ( sentcompletion ) + if (sentcompletion) { sentcompletion = False; - numsamples = (out->end - out->p)/samplewidth; + numsamples = (out->end - out->p) / samplewidth; } - len=0; + len = 0; - if ( out->end != out->p ) + if (out->end != out->p) { len = write(g_dsp_fd, out->p, out->end - out->p); if (len == -1) @@ -248,7 +287,7 @@ } /* Ack the packet, if we have played at least 70% */ - if ( info.play.samples >= samplecnt+((numsamples*7)/10) ) + if (info.play.samples >= samplecnt + ((numsamples * 7) / 10)) { samplecnt += numsamples; rdpsnd_send_completion(packet->tick, packet->index);