I am trying to extend the sinewave playing example from the wiki into a dtmf generator. Here is the header file Dtmf.hpp
#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>
class Dtmf
{
private:
sf::Int16* raw;
unsigned _sample_rate;
unsigned _amp;
double freqs[2];
sf::SoundBuffer* buffer;
sf::Sound* sound;
void genBuffer();
public:
Dtmf(double, double,unsigned,unsigned);
~Dtmf();
unsigned getSampleRate();
double getFreq(int);
void play();
};
The implementation of Dtmf
#include "Dtmf.hpp"
Dtmf::Dtmf(double freq1, double freq2, unsigned sample_rate=44100, unsigned amp=15)
{
_sample_rate = sample_rate;
_amp = amp;
freqs[0] = freq1;
freqs[1] = freq2;
raw = new sf::Int16[_sample_rate];
buffer = new sf::SoundBuffer;
sound = new sf::Sound;
genBuffer();
}
Dtmf::~Dtmf()
{
delete raw;
delete buffer;
delete sound;
}
unsigned Dtmf::getSampleRate()
{
return _sample_rate;
}
double Dtmf::getFreq(int index)
{
return freqs[index];
}
void Dtmf::genBuffer()
{
const double TWO_PI = 6.28318;
double f1inc = freqs[0] / _sample_rate;
double f2inc = freqs[1] / _sample_rate;
unsigned i;
double f1=0;
double f2=0;
for(i=0;i<_sample_rate;i++)
{
raw[i] = _amp * ( sin( f1 * TWO_PI ) + sin( f2 * TWO_PI ) );
f1 += f1inc; f2 += f2inc;
}
if(!buffer->loadFromSamples(raw,_sample_rate,1,_sample_rate))
{
std::cerr << "Error loading buffer" << std::endl;
return;
}
sound->setBuffer(*buffer);
sound->setLoop(true);
}
void Dtmf::play()
{
sound->play();
sf::sleep(sf::milliseconds(500));
sound->stop();
}
And test.cpp
#include "Dtmf.hpp"
int main()
{
Dtmf dialTone = Dtmf(350., 440.,44100,15);
std::cout << dialTone.getSampleRate() << std::endl;
std::cout << "Freqs :" << dialTone.getFreq(0) << " and " << dialTone.getFreq(1) << std::endl;
dialTone.play();
}
The code compiles and links just fine, however no sound is generated.
Here is a working example of what I'm trying to accomplish without the OOP encapsulation.
#include <SFML/Audio.hpp>
#include <cmath>
#include <iostream>
int main() {
const unsigned SAMPLES = 44100;
const unsigned SAMPLE_RATE = 44100;
const unsigned AMPLITUDE = 15000;
sf::Int16 raw[SAMPLES];
const double TWO_PI = 6.28318;
const double xincrement = 440./44100;
const double yincrement = 350./44100;
double x = 0;
double y = 0;
for (unsigned i = 0; i < SAMPLES; i++) {
raw[i] = AMPLITUDE * ( sin( x * TWO_PI ) + sin( y * TWO_PI ) );
x += xincrement;
y += yincrement;
}
sf::SoundBuffer Buffer;
if (!Buffer.loadFromSamples(raw, SAMPLES, 1, SAMPLE_RATE)) {
std::cerr << "Loading failed!" << std::endl;
return 1;
}
sf::Sound Sound;
Sound.setBuffer(Buffer);
Sound.setLoop(true);
Sound.play();
while (1) {
sf::sleep(sf::milliseconds(100));
}
return 0;
}
Any hints or suggestions on correcting the problem are appreciated.
This is the SConstruct file that I am using for compilation
env = Environment()
target = 'test'
sources = ['test.cpp','Dtmf.cpp']
libs = ['sfml-audio','sfml-system']
object_list = []
for source in sources:
object_list.extend(Object(source,CCFLAGS='-g'))
env.Program(target,object_list,LIBS = libs,CCFLAGS='-g')