/[rdesktop]/jpeg/rdesktop/trunk/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 /jpeg/rdesktop/trunk/rdpsnd_libao.c

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

revision 1260 by stargo, Sun Sep 17 15:25:10 2006 UTC revision 1345 by ossman_, Thu Dec 7 11:54:29 2006 UTC
# Line 29  Line 29 
29  #include <ao/ao.h>  #include <ao/ao.h>
30  #include <sys/time.h>  #include <sys/time.h>
31    
32  #define WAVEOUTBUF      16  #define WAVEOUTLEN      16
33    
34  static ao_device *o_device = NULL;  static ao_device *o_device = NULL;
35  static int default_driver;  static int default_driver;
 static int samplerate;  
 static int audiochannels;  
36  static BOOL reopened;  static BOOL reopened;
 static short samplewidth;  
37  static char *libao_device = NULL;  static char *libao_device = NULL;
38    
39  BOOL  BOOL
40  libao_open(void)  libao_open(void)
41  {  {
42          ao_sample_format format;          ao_sample_format format;
         static int warned = 0;  
43    
44          if (!warned && libao_device)          ao_initialize();
45    
46            if (libao_device)
47          {          {
48                  warning("device-options not supported for libao-driver\n");                  default_driver = ao_driver_id(libao_device);
49                  warned = 1;          }
50            else
51            {
52                    default_driver = ao_default_driver_id();
53          }          }
   
         ao_initialize();  
         default_driver = ao_default_driver_id();  
54    
55          format.bits = 16;          format.bits = 16;
56          format.channels = 2;          format.channels = 2;
         audiochannels = 2;  
57          format.rate = 44100;          format.rate = 44100;
58          samplerate = 44100;          format.byte_format = AO_FMT_NATIVE;
59          format.byte_format = AO_FMT_LITTLE;  
60    
61          o_device = ao_open_live(default_driver, &format, NULL);          o_device = ao_open_live(default_driver, &format, NULL);
62          if (o_device == NULL)          if (o_device == NULL)
# Line 68  libao_open(void) Line 65  libao_open(void)
65          }          }
66    
67          g_dsp_fd = 0;          g_dsp_fd = 0;
         rdpsnd_queue_init();  
68    
69          reopened = True;          reopened = True;
70    
# Line 81  libao_close(void) Line 77  libao_close(void)
77          /* Ack all remaining packets */          /* Ack all remaining packets */
78          while (!rdpsnd_queue_empty())          while (!rdpsnd_queue_empty())
79          {          {
80                  rdpsnd_send_completion(rdpsnd_queue_current_packet()->tick,                  rdpsnd_queue_next(0);
                                        rdpsnd_queue_current_packet()->index);  
                 rdpsnd_queue_next();  
81          }          }
82    
83          if (o_device != NULL)          if (o_device != NULL)
# Line 93  libao_close(void) Line 87  libao_close(void)
87  }  }
88    
89  BOOL  BOOL
 libao_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;  
 }  
   
 BOOL  
90  libao_set_format(WAVEFORMATEX * pwfx)  libao_set_format(WAVEFORMATEX * pwfx)
91  {  {
92          ao_sample_format format;          ao_sample_format format;
93    
94          format.bits = pwfx->wBitsPerSample;          format.bits = pwfx->wBitsPerSample;
95          format.channels = pwfx->nChannels;          format.channels = pwfx->nChannels;
         audiochannels = pwfx->nChannels;  
96          format.rate = 44100;          format.rate = 44100;
97          samplerate = pwfx->nSamplesPerSec;          format.byte_format = AO_FMT_NATIVE;
         format.byte_format = AO_FMT_LITTLE;  
   
         samplewidth = pwfx->wBitsPerSample / 8;  
98    
99          if (o_device != NULL)          if (o_device != NULL)
100                  ao_close(o_device);                  ao_close(o_device);
# Line 132  libao_set_format(WAVEFORMATEX * pwfx) Line 105  libao_set_format(WAVEFORMATEX * pwfx)
105                  return False;                  return False;
106          }          }
107    
108            if (rdpsnd_dsp_resample_set(44100, pwfx->wBitsPerSample, pwfx->nChannels) == False)
109            {
110                    return False;
111            }
112    
113          reopened = True;          reopened = True;
114    
115          return True;          return True;
# Line 142  libao_play(void) Line 120  libao_play(void)
120  {  {
121          struct audio_packet *packet;          struct audio_packet *packet;
122          STREAM out;          STREAM out;
123          char outbuf[WAVEOUTBUF];          int len;
         int offset, len, i;  
124          static long prev_s, prev_us;          static long prev_s, prev_us;
125          unsigned int duration;          unsigned int duration;
126          struct timeval tv;          struct timeval tv;
# Line 168  libao_play(void) Line 145  libao_play(void)
145    
146          next_tick = rdpsnd_queue_next_tick();          next_tick = rdpsnd_queue_next_tick();
147    
148          len = 0;          len = (WAVEOUTLEN > (out->end - out->p)) ? (out->end - out->p) : WAVEOUTLEN;
149            ao_play(o_device, (char *) out->p, len);
150          if (samplerate == 22050)          out->p += len;
         {  
                 /* Resample to 44100 */  
                 for (i = 0; (i < ((WAVEOUTBUF / 4) * (3 - 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 (audiochannels == 2)  
                                 offset = ((i * 2) - (i & 1)) * samplewidth;  
                         else  
                                 offset = (i * 2) * samplewidth;  
   
                         memcpy(&outbuf[offset], out->p, samplewidth);  
                         memcpy(&outbuf[audiochannels * samplewidth + offset], out->p, samplewidth);  
   
                         out->p += samplewidth;  
                         len += 2 * 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);  
151    
152          gettimeofday(&tv, NULL);          gettimeofday(&tv, NULL);
153    
# Line 221  libao_play(void) Line 168  libao_play(void)
168                                 (packet->tick + duration) % 65536, next_tick % 65536));                                 (packet->tick + duration) % 65536, next_tick % 65536));
169                  }                  }
170    
171                  rdpsnd_send_completion(((packet->tick + duration) % 65536), packet->index);                  rdpsnd_queue_next(duration);
                 rdpsnd_queue_next();  
172          }          }
173    
174          g_dsp_busy = 1;          g_dsp_busy = 1;
175          return;          return;
176  }  }
177    
178    static struct audio_driver libao_driver = {
179            .name = "libao",
180            .description = "libao output driver, default device: system dependent",
181    
182            .wave_out_open = libao_open,
183            .wave_out_close = libao_close,
184            .wave_out_format_supported = rdpsnd_dsp_resample_supported,
185            .wave_out_set_format = libao_set_format,
186            .wave_out_volume = rdpsnd_dsp_softvol_set,
187            .wave_out_play = libao_play,
188    
189            .need_byteswap_on_be = 1,
190            .need_resampling = 1,
191    };
192    
193  struct audio_driver *  struct audio_driver *
194  libao_register(char *options)  libao_register(char *options)
195  {  {
         static struct audio_driver libao_driver;  
   
         libao_driver.wave_out_write = rdpsnd_queue_write;  
         libao_driver.wave_out_open = libao_open;  
         libao_driver.wave_out_close = libao_close;  
         libao_driver.wave_out_format_supported = libao_format_supported;  
         libao_driver.wave_out_set_format = libao_set_format;  
         libao_driver.wave_out_volume = rdpsnd_dsp_softvol_set;  
         libao_driver.wave_out_play = libao_play;  
         libao_driver.name = xstrdup("libao");  
         libao_driver.description = xstrdup("libao output driver");  
         libao_driver.need_byteswap_on_be = 0;  
         libao_driver.next = NULL;  
   
196          if (options)          if (options)
197          {          {
198                  libao_device = xstrdup(options);                  libao_device = xstrdup(options);

Legend:
Removed from v.1260  
changed lines
  Added in v.1345

  ViewVC Help
Powered by ViewVC 1.1.26