Hello !
I am building a music visualizer. I already made the analyser, but I load the music using a buffer with the function LoadFromFile. Here is what it looks like :
Now I would like to use the internal input of my sound card for the sound input, to be able to display this animation live during parties and live dj set.
So I begun looking at the Audio Recording part of SFML, and made this little code to test it :
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
using namespace std;
void drawSamples(const sf::Int16* samples, sf::Uint64 samplecount, sf::RenderWindow &window){
sf::VertexArray Lines;
Lines.setPrimitiveType(sf::LineStrip);
for(int i = 0;i < samplecount;i++){
Lines.append(sf::Vertex(sf::Vector2f(i,samples[i]/100+window.getSize().y/2),sf::Color::White));
}
window.draw(Lines);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1440,900,32),"TEST RECORDER");
sf::SoundBufferRecorder recorder;
sf::SoundBuffer buffer;
sf::Event event;
//Make sure there is something in the buffer
recorder.start();
sf::sleep(sf::milliseconds(1));
recorder.stop();
while(window.isOpen()){
while(window.pollEvent(event)){
switch (event.type)
{
case sf::Event::EventType::Closed:
window.close();
break;
}
}
recorder.stop();
buffer = recorder.getBuffer();
window.clear(sf::Color::Black);
const sf::Int16* samples = buffer.getSamples();
sf::Uint64 samplecount = buffer.getSampleCount();
drawSamples(samples, samplecount, window);
window.display();
recorder.start();
}
return 0;
}
When I run it, I find it really slow. I would like to hit almost 60fps to be able to run it smoothly on a video projector.
Where do you think this slowness comes from ? Is it because the recording is taking a lot of time ? Is it because of the the fact that I copy the buffer every time ?
There is another part in this problem. To achieve a very good looking results like the one on the video I use a bufferSize of 2^13 (8192) integer. I guess if I want to run my program with a live sound source I need to be able to get that data fast enough so that it won’t interfere with the displaying.
What should I use ? I feel like I should use a custom recorder that is fast enough, but I also feel like this should use an audio stream because I want to load some data that cannot be loaded directly in memory (because it doesn’t exist yet…). I would like to know if recording with a SoundBufferRecorder is a good solution to provide data for an audio stream.
I hope my problem appears clear to you, and I say thanks in advance to anyone willing to help me !
Thanks,
Louis