Async::AudioIO Class Reference

A class for handling audio input/output to an audio device. More...

#include <AsyncAudioIO.h>

Inheritance diagram for Async::AudioIO:

Async::AudioSource Async::AudioSink

List of all members.

Public Types

Public Member Functions

Static Public Member Functions

Friends


Detailed Description

A class for handling audio input/output to an audio device.

Author:
Tobias Blomberg
Date:
2003-03-23
This is a class for handling audio input and output to an audio device. For now, the AudioIO class only works with 16 bit stereo samples. An example usage is shown below.

Multiple AudioIO objects can use the same audio device as long as the device name is exactly the same.

#include <iostream>
#include <AsyncCppApplication.h>
#include <AsyncAudioIO.h>
#include <AsyncAudioSink.h>
#include <AsyncAudioSource.h>

using namespace std;
using namespace Async;


class MyClass : public Async::AudioSink, public Async::AudioSource
{
  public:
    MyClass(void)
    {
        // Create a new audio IO object
      audio_io = new AudioIO("/dev/dsp", 0);
      
        // Open it for both reading and writing
      audio_io->open(AudioIO::MODE_RDWR);
      
        // Register this object as the audio source for sound output
      audio_io->registerSource(this);
      
        // Register the audio device as the audio source for this object
      registerSource(audio_io);
    }
    
    ~MyClass(void)
    {
      delete audio_io;
    }

      // AudioSink functions
    int writeSamples(const float *samples, int count)
    {
        // Just loop incoming samples back to the audio device
      return sinkWriteSamples(samples, count);
    }
    
    void flushSamples(void)
    {
      sinkFlushSamples();
    }

      // AudioSource functions
    void resumeOutput(void)
    {
      sourceResumeOutput();
    }
    
    void allSamplesFlushed(void)
    {
      sourceAllSamplesFlushed();
    }

  private:
    AudioIO *audio_io;
    
};

int main(int argc, char **argv)
{
  CppApplication app;
  MyClass my_class;
  app.exec();
}
Examples:

AsyncAudioIO_demo.cpp.

Definition at line 135 of file AsyncAudioIO.h.


Member Enumeration Documentation

The different modes to open a device in.

Enumerator:
MODE_NONE  No mode. The same as close.
MODE_RD  Read.
MODE_WR  Write.
MODE_RDWR  Both read and write.

Definition at line 141 of file AsyncAudioIO.h.


Constructor & Destructor Documentation

Async::AudioIO::AudioIO ( const std::string &  dev_name,
int  channel 
)

Constructor.

Parameters:
dev_name The name of the device to use
channel The channel number (zero is the first channel)

Async::AudioIO::~AudioIO ( void   ) 

Destructor.


Member Function Documentation

static void Async::AudioIO::setSampleRate ( int  rate  )  [static]

Set the sample rate used when doing future opens.

Parameters:
rate The sampling rate to use
Use this function to set the sample rate used when opening audio devices. This is a global setting so all sound cards will be affected. Already opened sound cards will not be affected.

static int Async::AudioIO::setBlocksize ( int  size  )  [static]

Set the blocksize used when opening audio devices.

Parameters:
size The blocksize, in samples per channel, to use
Returns:
Returns the blocksize actually set
Use this function to set the block size used when opening audio devices. The block size is the size of the blocks used when reading and writing audio to/from the sound card. Smaller blocks give less delay but could cause choppy audio if the computer is too slow. The blocksize is set as samples per channel. For example, a blocksize of 256 samples at 8kHz sample rate will give a delay of 256/8000 = 32ms. This is a global setting so all sound cards will be affected. Already opened sound cards will not be affected.

static int Async::AudioIO::blocksize ( void   )  [static]

Find out what the blocksize is set to.

Returns:
Returns the currently set blocksize in samples per channel

static int Async::AudioIO::setBufferCount ( int  count  )  [static]

Set the buffer count used when opening audio devices.

Parameters:
count The buffer count to use
Returns:
Returns the buffer count actually set
Use this function to set the buffer count used when opening audio devices. The buffer count is the maximum number of blocks the driver will buffer when reading and writing audio to/from the sound card. Lower numbers give less delay but could cause choppy audio if the computer is too slow. This is a global setting so all sound cards will be affected. Already opened sound cards will not be affected.

static void Async::AudioIO::setChannels ( int  channels  )  [static]

Set the number of channels used when doing future opens.

Parameters:
channels The number of channels to use
Use this function to set the number of channels used when opening audio devices. This is a global setting so all sound cards will be affected. Already opened sound cards will not be affected.

bool Async::AudioIO::isFullDuplexCapable ( void   ) 

Check if the audio device is capable of full duplex operation.

Returns:
Return true if the device is capable of full duplex or false if it is not

bool Async::AudioIO::open ( Mode  mode  ) 

Open the audio device in the specified mode.

Parameters:
mode The mode to open the audio device in. See Async::AudioIO::Mode for more information
Returns:
Returns true on success or else false on failure

void Async::AudioIO::close ( void   ) 

Close the adio device.

Mode Async::AudioIO::mode ( void   )  const [inline]

Find out how many samples there are in the output buffer.

Returns:
Returns the number of samples in the output buffer on success or -1 on failure.
This function can be used to find out how many samples there are in the output buffer at the moment. This can for example be used to find out how long it will take before the output buffer has been flushed.

Definition at line 272 of file AsyncAudioIO.h.

void Async::AudioIO::setGain ( float  gain  )  [inline]

Set the gain to use.

Parameters:
gain The new gain to set
This function will setup the gain to use for this audio stream. The default gain is 1.0, that is no amplification or attenuation. A value < 1.0 will attenuate the audio stream and a value > 1.0 will result in an amplification of the audio stream.

Definition at line 283 of file AsyncAudioIO.h.

float Async::AudioIO::gain ( void   )  const [inline]

Return the gain.

Returns:
Returns the gain

Definition at line 289 of file AsyncAudioIO.h.

int Async::AudioIO::sampleRate ( void   )  const [inline]

Return the sample rate.

Returns:
Returns the sample rate

Definition at line 295 of file AsyncAudioIO.h.

int Async::AudioIO::channel ( void   )  const [inline]

Return the audio channel used.

Returns:
Returns the audio channel that was given to the constructor

Definition at line 301 of file AsyncAudioIO.h.

void Async::AudioIO::resumeOutput ( void   )  [inline, virtual]

Resume audio output to the sink.

This function will be called when the registered audio sink is ready to accept more samples. This function is normally only called from a connected sink object.

Reimplemented from Async::AudioSource.

Definition at line 310 of file AsyncAudioIO.h.

void Async::AudioIO::allSamplesFlushed ( void   )  [inline, virtual]

The registered sink has flushed all samples.

This function will be called when all samples have been flushed in the registered sink. This function is normally only called from a connected sink object.

Reimplemented from Async::AudioSource.

Definition at line 319 of file AsyncAudioIO.h.


Friends And Related Function Documentation

friend class AudioDevice [friend]

Definition at line 368 of file AsyncAudioIO.h.


The documentation for this class was generated from the following file:

Generated on Wed Jan 7 23:13:18 2009 for Async by  doxygen 1.5.6