Async 1.5.0
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
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

int readBlocksize (void)
 Find out what the read (recording) blocksize is set to.
 
int writeBlocksize (void)
 Find out what the write (playback) blocksize is set to.
 
 AudioIO (const std::string &dev_name, int 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.
 
int 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.
 
virtual void resumeOutput (void)
 Resume audio output to the sink.
 
- 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 (int size)
 Set the blocksize used when opening audio devices.
 
static void setBlockCount (int count)
 Set the block count used when opening audio devices.
 
static void setChannels (int channels)
 Set the number of channels used when doing future opens.
 

Friends

class AudioDevice
 

Additional Inherited Members

- Protected Member Functions inherited from Async::AudioSource
virtual void allSamplesFlushed (void)
 The registered sink has flushed all samples.
 
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 <iostream>
#include <cmath>
#include <AsyncAudioIO.h>
#include <AsyncAudioSink.h>
using namespace std;
using namespace Async;
class SineGenerator : public Async::AudioSource
{
public:
explicit SineGenerator(const string& audio_dev, int channel)
: audio_io(audio_dev, channel), pos(0), fq(0.0), level(0.0),
sample_rate(0)
{
sample_rate = audio_io.sampleRate();
audio_io.registerSource(this);
}
~SineGenerator(void)
{
enable(false);
}
void setFq(double tone_fq)
{
fq = tone_fq;
}
void setLevel(int level_percent)
{
level = level_percent / 100.0;
}
void enable(bool enable)
{
if (enable == (audio_io.mode() != AudioIO::MODE_NONE))
{
return;
}
if (enable && (fq != 0))
{
if (audio_io.open(AudioIO::MODE_WR))
{
pos = 0;
writeSamples();
}
else
{
printf("Failed to open audio device\n");
}
}
else
{
audio_io.close();
}
}
void resumeOutput(void)
{
if (audio_io.mode() != AudioIO::MODE_NONE)
{
writeSamples();
}
}
void allSamplesFlushed(void)
{
}
private:
static const int BLOCK_SIZE = 256;
AudioIO audio_io;
unsigned pos;
double fq;
double level;
int sample_rate;
void writeSamples(void)
{
int written;
do {
float buf[BLOCK_SIZE];
for (int i=0; i<BLOCK_SIZE; ++i)
{
buf[i] = level * sin(2 * M_PI * fq * (pos+i) / sample_rate);
}
written = sinkWriteSamples(buf, BLOCK_SIZE);
pos += written;
} while (written != 0);
}
};
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);
SineGenerator *sine_gen = new SineGenerator("alsa:default", 1);
sine_gen->setFq(1000);
sine_gen->setLevel(2);
sine_gen->enable(true);
}
~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)
{
MyClass my_class;
app.exec();
}
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 handling audio input/output to an audio device.
Definition: AsyncAudioIO.h:135
bool open(Mode mode)
Open the audio device in the specified mode.
The base class for an audio sink.
bool registerSource(AudioSource *source)
Register an audio source to provide samples to this sink.
The base class for an audio source.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
Namespace for the asynchronous programming classes.
Examples
AsyncAudioIO_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,
int  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()

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

◆ readBlocksize()

int 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 ( int  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 ( int  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 ( int  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.

◆ writeBlocksize()

int 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 Function 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: