SFML community forums

Help => Audio => Topic started by: shadowmouse on December 22, 2015, 09:29:16 pm

Title: Playing a sound wave
Post by: shadowmouse on December 22, 2015, 09:29:16 pm
I have a function that allows a user input maths function (such as y = 2 *sin x) to be evaluated for whatever value of x. Is it possible to play this wave via SFML? The only way I've seen it done is via a callback that would pass the time value to the evaluation function, and then the result would be passed back to the library (done using PortAudio). As far as I know, you can't do that with SFML, so is there some other way to do it?
Title: Re: Playing a sound wave
Post by: Mario on December 22, 2015, 09:53:56 pm
What exactly are you trying to do? Do you want to modulate a sound's amplitude over time based on the graph? Or do you want to directly use the graph results for your samples?

I'm in the process of writing a tutorial for simple audio processing with SFML, although that might take a bit till it's done.
Title: Re: Playing a sound wave
Post by: shadowmouse on December 22, 2015, 10:19:26 pm
Either, as long as they produce the result of playing back an entered sound wave. My original plan was to change the amplitude over time based on the graph function, but I had also thought of precalculating the amplitudes and storing them in an array then playing it at a set rate.
Title: Re: Playing a sound wave
Post by: Hapax on December 22, 2015, 10:25:59 pm
You can play a wave created from a maths function by sampling the function and storing them in a sf::SoundBuffer.
Title: Re: Playing a sound wave
Post by: shadowmouse on December 22, 2015, 10:46:29 pm
That is a very good point. Thank you.
Title: Re: Playing a sound wave
Post by: Mario on December 22, 2015, 10:51:29 pm
If you'd like to modify the amplitude, you could just play a static/constant sinus wave and update it's volume while updating the graph.
Title: Re: Playing a sound wave
Post by: Laurent on December 23, 2015, 08:01:44 am
Volume controls the amplitude, and pitch controls the frequency. So you could even change the whole sine without touching the sampled data.