[Vorbis] libao on Debian

arunchandra1954 arunchandra1954 at gmail.com
Mon Jun 3 08:50:00 PDT 2013


Hi,

I've just installed  libao-1.1.0 on a new Debian install.

I also have alsa 1.0.27.1 installed.

Debian is running under VirtualBox 4.2.12 under OSX 10.6.8, on a MacBookPro.

libao does not work in stereo for the sampling rate of 44100, but it 
does for 8000, 11025, 22050and 48000.

When I send it two channel output at a sampling rate of 44100, the 
result includes what sounds like skips, sudden changes from left to 
right speakers, an other incongruities.

Question: Does anyone know why this one sampling rate does not work?

I've included a copy of the code that I've used to test the sound 
output.  I compiled it with:

     gcc -W -Wall -g -o sound sound.c -lao -lasound -lm

Thanks very much for any help on this.

Arun Chandra




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ao/ao.h>


int main(void)
{
     int sr = 44100 ;        // samples per second
     int nchan = 1 ;            // number of channels of sound
     double dur = 3 ;        // duration in seconds
     char *sound ;
     int nBytes ;
     extern char *Sine(int sr, int nchan, double dur, int *nBytes) ;

     int r, id ;
     ao_device *device ;
     ao_sample_format fmt ;

     sound = Sine(sr, nchan, dur, &nBytes) ;        // create sine tone(s)


     ao_initialize() ;
     if ( ( id = ao_default_driver_id() < 0 ) ) {
         fprintf(stderr,"Error in ao_default_driver(): id = %d\n", id) ;
         exit(EXIT_FAILURE) ;
     }

     fmt.bits = 16 ;    // bits per sample
     fmt.rate = sr ;    // sampling rate
     fmt.channels = nchan ;    // channel count
     fmt.byte_format = AO_FMT_NATIVE ;
     fmt.matrix = "L,R" ;

     if ( ( device = ao_open_live(id, &fmt, NULL) ) == NULL ) {
         fprintf(stderr,"Error in ao_open_live()\n") ;
         exit(EXIT_FAILURE) ;
     }

     if ( ( r = ao_play(device, sound, nBytes) ) == 0 ) {
         fprintf(stderr,"Error in ao_play()\n") ;
         exit(EXIT_FAILURE) ;
     }

     if ( ( r = ao_close(device) ) != 1 ) {
         fprintf(stderr,"Error in ao_close()\n") ;
         exit(EXIT_FAILURE) ;
     }

     ao_shutdown() ;

     return 0 ;
}


char *Sine(int sr, int nchan, double dur, int *nBytes)
{
     double f1, f2 ;
     double db, A ;
     double ph1, ph2, phInc1, phInc2 ;
     int frames, i ;
     short *sbuffer ;
     char *cbuffer ;

     f1 = 1000 ;            // frequency in Hertz
     f2 = 1.5 * f1 ;        // perfect fifth higher
     ph1 = ph2 = 0 ;
     phInc1 = ( 2 * M_PI * f1 ) / sr ;    // phase increments
     phInc2 = ( 2 * M_PI * f2 ) / sr ;

     db = 90 ;                        // amplitude in decibels
     A = pow(10.0, db/20.0) / 2 ;    // linear amplitude

     frames = dur * sr ;
     *nBytes = frames * nchan * sizeof(short) ;

     sbuffer = (short *)malloc(*nBytes) ;

     if ( nchan == 1 ) {        // mono sound
         for ( i = 0 ; i < frames ; i++ ) {
             sbuffer[i] = (short)(A * sin(ph1)) ;
             ph1 += phInc1 ;
         }
     } else {                // stereo sound
         for ( i = 0 ; i < frames ; i++ ) {
             sbuffer[2*i] = (short)(A * sin(ph1)) ;
             sbuffer[2*i+1] = (short)(A * sin(ph2)) ;
             ph1 += phInc1 ;
             ph2 += phInc2 ;
         }
     }

     cbuffer = (char *)malloc(*nBytes) ;
     memcpy(cbuffer, (char *)sbuffer, *nBytes) ;

     free(sbuffer) ;

     return cbuffer ;
}



More information about the Vorbis mailing list