#include <iostream>
#include <cmath>
using namespace std;
{
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();
}
}
{
if (audio_io.mode() != AudioIO::MODE_NONE)
{
writeSamples();
}
}
{
}
private:
static const int BLOCK_SIZE = 256;
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);
}
pos += written;
} while (written != 0);
}
};
{
public:
MyClass(void)
{
audio_io =
new AudioIO(
"alsa:default", 0);
audio_io->open(AudioIO::MODE_RDWR);
audio_io->registerSource(this);
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;
}
{
}
{
}
{
}
{
}
private:
};
int main(int argc, char **argv)
{
MyClass my_class;
}
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.
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.