©  Jan Newmarch 2017

Jan Newmarch, Linux Sound Programming, 10.1007/978-1-4842-2496-0_11

11. libao

Jan Newmarch

(1)Oakleigh, Victoria, Australia

According to the libao documentation ( www.xiph.org/ao/doc/overview.html ), “libao is designed to make it easy to do simple audio output using various audio devices and libraries. For this reason, complex audio control features are missing and wi ll probably never be added. However, if you just want to be able to open whatever audio device is available and play sound, libao should be just fine.”

Resources

Check out the following:

libao

libao is an extremely minimal library; it basically just plays audio data. It does not decode any of the standard file formats: no WAV, MP3, Vorbis, and so on, support. You have to configure the format parameters of bits, channels, rates, and byte formats and then send the appropriate data to the device. Its primary use is to output PCM data and can be used once a codec has been decoded or to play simple sounds like sine waves.

The following is a simple example from the libao site to play a sine tone for one second:

/*
 *
 * ao_example.c
 *
 *     Written by Stan Seibert - July 2001
 *
 * Legal Terms:
 *
 *     This source file is released into the public domain.  It is
 *     distributed without any warranty; without even the implied
 *     warranty * of merchantability or fitness for a particular
 *     purpose.
 *
 * Function:
 *
 *     This program opens the default driver and plays a 440 Hz tone for
 *     one second.
 *
 * Compilation command line (for Linux systems):
 *
 *     gcc -lao -ldl -lm -o ao_example ao_example.c
 *
 */


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


#define BUF_SIZE 4096

int main(int argc, char **argv)
{
        ao_device *device;
        ao_sample_format format;
        int default_driver;
        char *buffer;
        int buf_size;
        int sample;
        float freq = 440.0;
        int i;


        /* -- Initialize -- */

        fprintf(stderr, "libao example program ");

        ao_initialize();

        /* -- Setup for default driver -- */

        default_driver = ao_default_driver_id();

        memset(&format, 0, sizeof(format));
        format.bits = 16;
        format.channels = 2;
        format.rate = 44100;
        format.byte_format = AO_FMT_LITTLE;


        /* -- Open driver -- */
        device = ao_open_live(default_driver, &format, NULL /* no options */);
        if (device == NULL) {
                fprintf(stderr, "Error opening device. ");
                return 1;
        }


        /* -- Play some stuff -- */
        buf_size = format.bits/8 * format.channels * format.rate;
        buffer = calloc(buf_size,
                        sizeof(char));


        for (i = 0; i < format.rate; i++) {
                sample = (int)(0.75 * 32768.0 *
                        sin(2 * M_PI * freq * ((float) i/format.rate)));


                /* Put the same stuff in left and right channel */
                buffer[4*i] = buffer[4*i+2] = sample & 0xff;
                buffer[4*i+1] = buffer[4*i+3] = (sample >> 8) & 0xff;
        }
        ao_play(device, buffer, buf_size);


        /* -- Close and shutdown -- */
        ao_close(device);


        ao_shutdown();

  return (0);
}

Conclusion

libao is not complex; it is a basic library to play sounds to whatever device is available. It will suit cases where you have the sound in a known PCM format.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset