SFML community forums

Help => Audio => Topic started by: TheOm3ga on September 21, 2011, 02:38:00 pm

Title: Clear buffer of sf::SoundBufferRecorder
Post by: TheOm3ga on September 21, 2011, 02:38:00 pm
Hi.

I'm trying to do some pseudo-real time processing, using sf::SoundBufferRecorder. The question I have is, how do I clear empty the buffer captured by sf::SoundBufferRecorder once I have processed the data?

I've tried deleting the sf::SoundBufferRecorder using something like

Code: [Select]
recorder = sf::SoundBufferRecorder();


But it gives me an error in compile time. I can't directly modify the buffer that returns GetBuffer() because it's const.

What can I do?
Title: Clear buffer of sf::SoundBufferRecorder
Post by: Laurent on September 21, 2011, 02:43:10 pm
Why don't you create your own recorder instead? Continuously storing the data in a buffer, reading it and clearing it seems rather inefficient, compared to processing the audio data directly while it's captured.
Title: Clear buffer of sf::SoundBufferRecorder
Post by: TheOm3ga on September 21, 2011, 02:57:43 pm
I'm applying a FFT, so I need to store chunks of data in a buffer no matter what.
Title: Clear buffer of sf::SoundBufferRecorder
Post by: Laurent on September 21, 2011, 03:07:00 pm
Can you show me how you use the recorder?
Title: Clear buffer of sf::SoundBufferRecorder
Post by: TheOm3ga on September 21, 2011, 04:24:37 pm
It's very basic, I start recording at the beginning of the program, and in each iteration, if a certain amount of time has past, I check the buffer. That's when I'd like to erase the buffer.

Anyways, I've tried your suggestion of creating my own SoundRecorder

Code: [Select]
class Analyser : public sf::SoundRecorder{
    sf::Clock clock;

    bool OnStart(){
        clock.Reset();
        return true;
    }

    bool OnProcessSamples (const sf::Int16 * Samples, std::size_t SamplesCount){
        std::cout << "NumSamples: " << SamplesCount << " time: " << clock.GetElapsedTime() << std::endl;
        return true;
    }
};


And looks like the "OnProcessSamples" is getting executed every 0.1 seconds, no matter what sample rate I use. Is there a way of changing it? 0.1 is too much time, I need some more resolution.
Title: Clear buffer of sf::SoundBufferRecorder
Post by: Laurent on September 21, 2011, 04:37:56 pm
Quote
It's very basic, I start recording at the beginning of the program, and in each iteration, if a certain amount of time has past, I check the buffer. That's when I'd like to erase the buffer.

If "a certain amount of time" is less than 100 ms, then it's definitely not worth using a sf::SoundBufferRecorder. You waste more time copying the samples in the sound buffer, than processing them.

Quote
And looks like the "OnProcessSamples" is getting executed every 0.1 seconds, no matter what sample rate I use. Is there a way of changing it? 0.1 is too much time, I need some more resolution.

100 ms is currently hard-coded in the sf::SoundRecorder class, you cannot change it.
What resolution do you need?
Title: Clear buffer of sf::SoundBufferRecorder
Post by: TheOm3ga on September 21, 2011, 04:51:58 pm
I need at least half that.

What I'm trying to achieve is this: http://www.youtube.com/watch?v=1mTU0H0-Ank. This is a video of my project, currently developed using the PulseAudio API (and Gosu as 2D api). I'm looking to port it to Windows, and SFML seemed like a nice way of dealing with cross-platform audio.
Title: Clear buffer of sf::SoundBufferRecorder
Post by: Laurent on September 21, 2011, 05:02:54 pm
Nice app :)

I think I could add a way to customize the delay between two calls to OnProcessData. Seems like a reasonable feature.

In the meanwhile you can change this "100" to "50" or whatever in the source code of sf::SoundRecorder, and recompile SFML.
Title: Clear buffer of sf::SoundBufferRecorder
Post by: TheOm3ga on September 21, 2011, 05:02:59 pm
I'm looking at the source code of SoundRecorder (at https://github.com/SFML/SFML/blob/master/src/SFML/Audio/SoundRecorder.cpp) It's really easy to follow. Looks like the call to Sleep (100) is the one forcing the update interval to be 100ms.

Guess I'll copy the code to another class and change the value there. However, it would be great for future releases to add a parameter for that time, defaulting it to 100.
Title: Clear buffer of sf::SoundBufferRecorder
Post by: Laurent on September 21, 2011, 06:32:12 pm
Quote
Looks like the call to Sleep (100) is the one forcing the update interval to be 100ms.

Indeed :)

Quote
Guess I'll copy the code to another class and change the value there.

You can't do that, this class uses private stuff that you don't have in public SFML files.

Quote
However, it would be great for future releases to add a parameter for that time, defaulting it to 100.

Yep!