Async 1.5.0
AsyncAudioIO_demo.cpp

An example of how to use the Async::AudioIO class

#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)
{
}
// AudioSource functions
void resumeOutput(void)
{
}
void allSamplesFlushed(void)
{
}
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
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.