Ok, let's see...
Oscillator.h
#ifndef __OSCILLATOR__
#define __OSCILLATOR__
#include <SFML/Audio.hpp>
#include <iostream>
#include <math.h>
#define NSAMPLES 44100
class Oscillator
{
public :
Oscillator (float frequency, float amplitude);
void setTone(float frequency, float amplitude);
private :
sf::SoundBuffer buffer;
sf::Sound sound;
sf::Int16 samples[NSAMPLES];
};
#endif
Oscillator.cpp
#include "Oscillator.h"
Oscillator::Oscillator(float frequency, float amplitude) {
setTone( frequency, amplitude);
sound.SetLoop(true);
sound.Play();
}
void Oscillator::setTone(float frequency, float amplitude) {
for (int i = 0; i < NSAMPLES; i++) {
samples[i] = ( amplitude * sin(frequency * (2.0f * 3.1415f) * i / 44100));
}
buffer.LoadFromSamples(samples, NSAMPLES, 1, 44100);
sound.SetBuffer(buffer);
}
main.cpp
#include <SFML/Audio.hpp>
#include "Oscillator.h"
int main() {
Oscillator *o = new Oscillator(440, 10000);
sf::Sleep(2.0f);
o->setTone(880,10000); // but we still hear 440 after this...
sf::Sleep(2.0f);
return 0;
}
SetTone works the first time it's called, in the constructor. But samples won't change the second time it's called, after two seconds.
EDIT: I guess this is not the right way of doing this. I should have a look at soundstream...