/[rdesktop]/sourceforge.net/trunk/rdesktop/rdpsnd_libao.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /sourceforge.net/trunk/rdesktop/rdpsnd_libao.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1043 by stargo, Tue Feb 7 16:21:31 2006 UTC revision 1351 by stargo, Sun Dec 24 13:53:23 2006 UTC
# Line 3  Line 3 
3     Sound Channel Process Functions - libao-driver     Sound Channel Process Functions - libao-driver
4     Copyright (C) Matthew Chapman 2003     Copyright (C) Matthew Chapman 2003
5     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003     Copyright (C) GuoJunBo guojunbo@ict.ac.cn 2003
6     Copyright (C) Michael Gernoth mike@zerfleddert.de 2005     Copyright (C) Michael Gernoth mike@zerfleddert.de 2005-2006
7    
8     This program is free software; you can redistribute it and/or modify     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by     it under the terms of the GNU General Public License as published by
# Line 21  Line 21 
21  */  */
22    
23  #include "rdesktop.h"  #include "rdesktop.h"
24    #include "rdpsnd.h"
25    #include "rdpsnd_dsp.h"
26  #include <unistd.h>  #include <unistd.h>
27  #include <fcntl.h>  #include <fcntl.h>
28  #include <errno.h>  #include <errno.h>
29  #include <ao/ao.h>  #include <ao/ao.h>
30  #include <sys/time.h>  #include <sys/time.h>
31    
32  #define MAX_QUEUE       10  #define WAVEOUTLEN      16
 #define WAVEOUTBUF      64  
33    
34  int g_dsp_fd;  static ao_device *o_device = NULL;
35  ao_device *o_device = NULL;  static int default_driver;
36  int default_driver;  static BOOL reopened;
37  int g_samplerate;  static char *libao_device = NULL;
 int g_channels;  
 BOOL g_dsp_busy = False;  
 static BOOL g_reopened;  
 static short g_samplewidth;  
38    
39  static struct audio_packet  void libao_play(void);
40    
41    void
42    libao_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv)
43    {
44    }
45    
46    void
47    libao_check_fds(fd_set * rfds, fd_set * wfds)
48  {  {
49          struct stream s;          if (o_device == NULL)
50          uint16 tick;                  return;
51          uint8 index;  
52  } packet_queue[MAX_QUEUE];          if (!rdpsnd_queue_empty())
53  static unsigned int queue_hi, queue_lo;                  libao_play();
54    }
55    
56  BOOL  BOOL
57  wave_out_open(void)  libao_open(void)
58  {  {
59          ao_sample_format format;          ao_sample_format format;
60    
61          ao_initialize();          ao_initialize();
62          default_driver = ao_default_driver_id();  
63            if (libao_device)
64            {
65                    default_driver = ao_driver_id(libao_device);
66            }
67            else
68            {
69                    default_driver = ao_default_driver_id();
70            }
71    
72          format.bits = 16;          format.bits = 16;
73          format.channels = 2;          format.channels = 2;
         g_channels = 2;  
74          format.rate = 44100;          format.rate = 44100;
75          g_samplerate = 44100;          format.byte_format = AO_FMT_NATIVE;
76          format.byte_format = AO_FMT_LITTLE;  
77    
78          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
79          if (o_device == NULL)          if (o_device == NULL)
# Line 68  wave_out_open(void) Line 81  wave_out_open(void)
81                  return False;                  return False;
82          }          }
83    
84          g_dsp_fd = 0;          reopened = True;
         queue_lo = queue_hi = 0;  
   
         g_reopened = True;  
85    
86          return True;          return True;
87  }  }
88    
89  void  void
90  wave_out_close(void)  libao_close(void)
91  {  {
92          /* Ack all remaining packets */          /* Ack all remaining packets */
93          while (queue_lo != queue_hi)          while (!rdpsnd_queue_empty())
94          {          {
95                  rdpsnd_send_completion(packet_queue[queue_lo].tick, packet_queue[queue_lo].index);                  rdpsnd_queue_next(0);
                 free(packet_queue[queue_lo].s.data);  
                 queue_lo = (queue_lo + 1) % MAX_QUEUE;  
96          }          }
97    
98          if (o_device != NULL)          if (o_device != NULL)
99                  ao_close(o_device);                  ao_close(o_device);
100    
101          ao_shutdown();          o_device = NULL;
 }  
102    
103  BOOL          ao_shutdown();
 wave_out_format_supported(WAVEFORMATEX * pwfx)  
 {  
         if (pwfx->wFormatTag != WAVE_FORMAT_PCM)  
                 return False;  
         if ((pwfx->nChannels != 1) && (pwfx->nChannels != 2))  
                 return False;  
         if ((pwfx->wBitsPerSample != 8) && (pwfx->wBitsPerSample != 16))  
                 return False;  
         /* The only common denominator between libao output drivers is a sample-rate of  
            44100, we need to upsample 22050 to it */  
         if ((pwfx->nSamplesPerSec != 44100) && (pwfx->nSamplesPerSec != 22050))  
                 return False;  
   
         return True;  
104  }  }
105    
106  BOOL  BOOL
107  wave_out_set_format(WAVEFORMATEX * pwfx)  libao_set_format(WAVEFORMATEX * pwfx)
108  {  {
109          ao_sample_format format;          ao_sample_format format;
110    
111          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
112          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
         g_channels = pwfx->nChannels;  
113          format.rate = 44100;          format.rate = 44100;
114          g_samplerate = pwfx->nSamplesPerSec;          format.byte_format = AO_FMT_NATIVE;
         format.byte_format = AO_FMT_LITTLE;  
   
         g_samplewidth = pwfx->wBitsPerSample / 8;  
115    
116          if (o_device != NULL)          if (o_device != NULL)
117                  ao_close(o_device);                  ao_close(o_device);
# Line 133  wave_out_set_format(WAVEFORMATEX * pwfx) Line 122  wave_out_set_format(WAVEFORMATEX * pwfx)
122                  return False;                  return False;
123          }          }
124    
125          g_reopened = True;          if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
   
         return True;  
 }  
   
 void  
 wave_out_volume(uint16 left, uint16 right)  
 {  
         warning("volume changes not supported with libao-output\n");  
 }  
   
 void  
 wave_out_write(STREAM s, uint16 tick, uint8 index)  
 {  
         struct audio_packet *packet = &packet_queue[queue_hi];  
         unsigned int next_hi = (queue_hi + 1) % MAX_QUEUE;  
   
         if (next_hi == queue_lo)  
126          {          {
127                  error("No space to queue audio packet\n");                  return False;
                 return;  
128          }          }
129    
130          queue_hi = next_hi;          reopened = True;
   
         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);  
131    
132          if (!g_dsp_busy)          return True;
                 wave_out_play();  
133  }  }
134    
135  void  void
136  wave_out_play(void)  libao_play(void)
137  {  {
138          struct audio_packet *packet;          struct audio_packet *packet;
139          STREAM out;          STREAM out;
140          char outbuf[WAVEOUTBUF];          int len;
         int offset, len, i;  
141          static long prev_s, prev_us;          static long prev_s, prev_us;
142          unsigned int duration;          unsigned int duration;
143          struct timeval tv;          struct timeval tv;
144          int next_tick;          int next_tick;
145    
146          if (g_reopened)          if (reopened)
147          {          {
148                  g_reopened = False;                  reopened = False;
149                  gettimeofday(&tv, NULL);                  gettimeofday(&tv, NULL);
150                  prev_s = tv.tv_sec;                  prev_s = tv.tv_sec;
151                  prev_us = tv.tv_usec;                  prev_us = tv.tv_usec;
152          }          }
153    
154          if (queue_lo == queue_hi)          /* We shouldn't be called if the queue is empty, but still */
155          {          if (rdpsnd_queue_empty())
                 g_dsp_busy = 0;  
156                  return;                  return;
         }  
157    
158          packet = &packet_queue[queue_lo];          packet = rdpsnd_queue_current_packet();
159          out = &packet->s;          out = &packet->s;
160    
161          if (((queue_lo + 1) % MAX_QUEUE) != queue_hi)          next_tick = rdpsnd_queue_next_tick();
         {  
                 next_tick = packet_queue[(queue_lo + 1) % MAX_QUEUE].tick;  
         }  
         else  
         {  
                 next_tick = (packet->tick + 65535) % 65536;  
         }  
   
         len = 0;  
162    
163          if (g_samplerate == 22050)          len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
164          {          ao_play(o_device, (char *) out->p, len);
165                  /* Resample to 44100 */          out->p += len;
                 for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - g_samplewidth))) && (out->p < out->end);  
                      i++)  
                 {  
                         /* On a stereo-channel we must make sure that left and right  
                            does not get mixed up, so we need to expand the sample-  
                            data with channels in mind: 1234 -> 12123434  
                            If we have a mono-channel, we can expand the data by simply  
                            doubling the sample-data: 1234 -> 11223344 */  
                         if (g_channels == 2)  
                                 offset = ((i * 2) - (i & 1)) * g_samplewidth;  
                         else  
                                 offset = (i * 2) * g_samplewidth;  
   
                         memcpy(&outbuf[offset], out->p, g_samplewidth);  
                         memcpy(&outbuf[g_channels * g_samplewidth + offset], out->p, g_samplewidth);  
   
                         out->p += g_samplewidth;  
                         len += 2 * g_samplewidth;  
                 }  
         }  
         else  
         {  
                 len = (WAVEOUTBUF > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTBUF;  
                 memcpy(outbuf, out->p, len);  
                 out->p += len;  
         }  
   
         ao_play(o_device, outbuf, len);  
166    
167          gettimeofday(&tv, NULL);          gettimeofday(&tv, NULL);
168    
# Line 261  wave_out_play(void) Line 183  wave_out_play(void)
183                                 (packet->tick + duration) % 65536, next_tick % 65536));                                 (packet->tick + duration) % 65536, next_tick % 65536));
184                  }                  }
185    
186                  /* Until all drivers are using the windows sound-ticks, we need to                  rdpsnd_queue_next(duration);
187                     substract the 50 ticks added later by rdpsnd.c */          }
188                  rdpsnd_send_completion(((packet->tick + duration) % 65536) - 50, packet->index);  }
189                  free(out->data);  
190                  queue_lo = (queue_lo + 1) % MAX_QUEUE;  struct audio_driver *
191    libao_register(char *options)
192    {
193            static struct audio_driver libao_driver;
194    
195            libao_driver.name = xstrdup("libao");
196            libao_driver.description = xstrdup("libao output driver, default device: system dependent");
197    
198            libao_driver.add_fds = libao_add_fds;
199            libao_driver.check_fds = libao_check_fds;
200    
201            libao_driver.wave_out_open = libao_open;
202            libao_driver.wave_out_close = libao_close;
203            libao_driver.wave_out_format_supported = rdpsnd_dsp_resample_supported;
204            libao_driver.wave_out_set_format = libao_set_format;
205            libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;
206    
207            libao_driver.need_byteswap_on_be = 1;
208            libao_driver.need_resampling = 1;
209    
210            if (options)
211            {
212                    libao_device = xstrdup(options);
213          }          }
214    
215          g_dsp_busy = 1;          return &libao_driver;
         return;  
216  }  }

Legend:
Removed from v.1043  
changed lines
  Added in v.1351

  ViewVC Help
Powered by ViewVC 1.1.26