Async 1.8.0
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

Public Types

enum  Mode { MODE_NONE , MODE_RD , MODE_WR , MODE_RDWR }
 The different modes to open a device in. More...
 

Public Member Functions

size_t readBlocksize (void)
 Find out what the read (recording) blocksize is set to.
 
size_t writeBlocksize (void)
 Find out what the write (playback) blocksize is set to.
 
 AudioIO (const std::string &dev_name, size_t channel)
 Constructor.
 
 ~AudioIO (void)
 Destructor.
 
bool isFullDuplexCapable (void)
 Check if the audio device is capable of full duplex operation.
 
bool open (Mode mode)
 Open the audio device in the specified mode.
 
void close (void)
 Close the adio device.
 
Mode mode (void) const
 Find out how many samples there are in the output buffer.
 
void setGain (float gain)
 Set the gain to use.
 
float gain (void) const
 Return the gain.
 
int sampleRate (void) const
 Return the sample rate.
 
size_t channel (void) const
 Return the audio channel used.
 
void resumeOutput (void)
 Resume audio output to the sink.
 
void allSamplesFlushed (void)
 The registered sink has flushed all samples.
 
- Public Member Functions inherited from Async::AudioSource
 AudioSource (void)
 Default constuctor.
 
virtual ~AudioSource (void)
 Destructor.
 
bool registerSink (AudioSink *sink, bool managed=false)
 Register an audio sink to provide samples to.
 
void unregisterSink (void)
 Unregister the previously registered audio sink.
 
bool isRegistered (void) const
 Check if an audio sink has been registered.
 
AudioSinksink (void) const
 Get the registered audio sink.
 
bool sinkManaged (void) const
 Check if the sink is managed or not.
 
void handleAllSamplesFlushed (void)
 The registered sink has flushed all samples.
 
- Public Member Functions inherited from Async::AudioSink
 AudioSink (void)
 Default constuctor.
 
virtual ~AudioSink (void)
 Destructor.
 
bool registerSource (AudioSource *source)
 Register an audio source to provide samples to this sink.
 
void unregisterSource (void)
 Unregister the previously registered audio source.
 
bool isRegistered (void) const
 Check if an audio source has been registered.
 
AudioSourcesource (void) const
 Get the registered audio source.
 
virtual int writeSamples (const float *samples, int count)
 Write samples into this audio sink.
 
virtual void flushSamples (void)
 Tell the sink to flush the previously written samples.
 

Static Public Member Functions

static void setSampleRate (int rate)
 Set the sample rate used when doing future opens.
 
static void setBlocksize (size_t size)
 Set the blocksize used when opening audio devices.
 
static void setBlockCount (size_t count)
 Set the block count used when opening audio devices.
 
static void setChannels (size_t channels)
 Set the number of channels used when doing future opens.
 

Friends

class AudioDevice
 

Additional Inherited Members

- Protected Member Functions inherited from Async::AudioSource
int sinkWriteSamples (const float *samples, int len)
 
void sinkFlushSamples (void)
 
bool setHandler (AudioSource *handler)
 Setup another source to handle the outgoing audio.
 
AudioSourcehandler (void) const
 
void clearHandler (void)
 Clear a handler that was previously setup with setHandler.
 
- Protected Member Functions inherited from Async::AudioSink
void sourceResumeOutput (void)
 Tell the source that we are ready to accept more samples.
 
void sourceAllSamplesFlushed (void)
 Tell the source that all samples have been flushed.
 
bool setHandler (AudioSink *handler)
 Setup another sink to handle the incoming audio.
 
void clearHandler (void)
 Clear a handler that was previously setup with setHandler.
 
AudioSinkhandler (void) const
 

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 <AsyncAudioIO.h>
#include <AsyncAudioSink.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("alsa:default", 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);
// Create a sine generator
sine_gen = new AudioGenerator;
sine_gen->setFq(1000);
sine_gen->setPower(-24);
// Create an audio device for the sine generator
AudioIO *sine_io = new AudioIO("alsa:default", 1);
sine_io->open(AudioIO::MODE_WR);
sine_gen->registerSink(sine_io, true);
// Enable the sine generator
sine_gen->enable(true);
}
~MyClass(void)
{
delete audio_io;
delete sine_gen;
}
// AudioSink functions
int writeSamples(const float *samples, int count)
{
// Just loop incoming samples back to the audio device
// We could do some processing here if we wanted to.
return sinkWriteSamples(samples, count);
}
void flushSamples(void)
{
}
// AudioSource functions
void resumeOutput(void)
{
}
void allSamplesFlushed(void)
{
}
private:
AudioIO * audio_io;
AudioGenerator * sine_gen;
};
int main(int argc, char **argv)
{
MyClass my_class;
app.exec();
}
An audio generator.
Contains a class for handling audio input/output to an audio device.
This file contains the base class for an audio sink.
This file contains the base class for an audio source.
The core class for writing asyncronous cpp applications.
A class for generating periodic audio signals.
void setFq(float tone_fq)
Set the audio frequency.
A class for handling audio input/output to an audio device.
bool open(Mode mode)
Open the audio device in the specified mode.
The base class for an audio sink.
void sourceAllSamplesFlushed(void)
Tell the source that all samples have been flushed.
bool registerSource(AudioSource *source)
Register an audio source to provide samples to this sink.
virtual int writeSamples(const float *samples, int count)
Write samples into this audio sink.
void sourceResumeOutput(void)
Tell the source that we are ready to accept more samples.
virtual void flushSamples(void)
Tell the sink to flush the previously written samples.
The base class for an audio source.
virtual void allSamplesFlushed(void)
The registered sink has flushed all samples.
void sinkFlushSamples(void)
virtual void resumeOutput(void)
Resume audio output to the sink.
int sinkWriteSamples(const float *samples, int len)
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
Namespace for the asynchronous programming classes.
Examples
AsyncAudioContainer_demo.cpp, AsyncAudioFsf_demo.cpp, AsyncAudioIO_demo.cpp, and AsyncAudioLADSPAPlugin_demo.cpp.

Definition at line 134 of file AsyncAudioIO.h.

Member Enumeration Documentation

◆ Mode

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 140 of file AsyncAudioIO.h.

Constructor & Destructor Documentation

◆ AudioIO()

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

Constructor.

Parameters
dev_nameThe name of the device to use
channelThe channel number (zero is the first channel)

◆ ~AudioIO()

Async::AudioIO::~AudioIO ( void )

Destructor.

Member Function Documentation

◆ allSamplesFlushed()

void Async::AudioIO::allSamplesFlushed ( void )
inlinevirtual

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 322 of file AsyncAudioIO.h.

◆ channel()

size_t 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 304 of file AsyncAudioIO.h.

◆ close()

void Async::AudioIO::close ( void )

Close the adio device.

◆ gain()

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

Return the gain.

Returns
Returns the gain

Definition at line 292 of file AsyncAudioIO.h.

Referenced by setGain().

◆ isFullDuplexCapable()

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

◆ mode()

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 275 of file AsyncAudioIO.h.

◆ open()

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

Open the audio device in the specified mode.

Parameters
modeThe mode to open the audio device in. See Async::AudioIO::Mode for more information
Returns
Returns true on success or else false on failure
Examples
AsyncAudioFsf_demo.cpp, AsyncAudioIO_demo.cpp, and AsyncAudioLADSPAPlugin_demo.cpp.

◆ readBlocksize()

size_t Async::AudioIO::readBlocksize ( void )

Find out what the read (recording) blocksize is set to.

Returns
Returns the currently set blocksize in samples per channel

◆ resumeOutput()

void Async::AudioIO::resumeOutput ( void )
inlinevirtual

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 313 of file AsyncAudioIO.h.

◆ sampleRate()

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

Return the sample rate.

Returns
Returns the sample rate

Definition at line 298 of file AsyncAudioIO.h.

◆ setBlockCount()

static void Async::AudioIO::setBlockCount ( size_t count)
static

Set the block count used when opening audio devices.

Parameters
countThe block count to use

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.

◆ setBlocksize()

static void Async::AudioIO::setBlocksize ( size_t size)
static

Set the blocksize used when opening audio devices.

Parameters
sizeThe blocksize, in samples per channel, to use

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.

◆ setChannels()

static void Async::AudioIO::setChannels ( size_t channels)
static

Set the number of channels used when doing future opens.

Parameters
channelsThe 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.

◆ setGain()

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

Set the gain to use.

Parameters
gainThe 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 286 of file AsyncAudioIO.h.

References gain().

◆ setSampleRate()

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

Set the sample rate used when doing future opens.

Parameters
rateThe 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.

Examples
AsyncAudioFsf_demo.cpp.

◆ writeBlocksize()

size_t Async::AudioIO::writeBlocksize ( void )

Find out what the write (playback) blocksize is set to.

Returns
Returns the currently set blocksize in samples per channel

Friends And Related Symbol Documentation

◆ AudioDevice

friend class AudioDevice
friend

Definition at line 360 of file AsyncAudioIO.h.


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