Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Getting a sample buffer in sync with music's own.  (Read 2383 times)

0 Members and 1 Guest are viewing this topic.

MrShedman

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Getting a sample buffer in sync with music's own.
« on: March 31, 2014, 10:55:25 pm »
I'm trying to visualize an FFT using the sf::Music class. However my visualization happens 2 or so seconds before what gets played which is what I expected.

I'ts pretty easy to do this just by using an sf::SoundBuffer on it's own but then there is a 3-4 second delay loading the file and easily 100mb+ memory usage.

Having read this thread I can't understand why I cannot simply do this:


class FFT: public sf::Music
{
public:

        app()
        {
                loadFromFile("music.ogg");
                play();
        }

        void input()
        {
                standard event stuff...
        }

        virtual bool onGetData(sf::SoundStream::Chunk& data)
        {
                bool result = sf::Music::onGetData(data);

                chunkQ.push(data);

                if (chunkQ.size() > bufferCount)
                {
                        chunkQ.pop();
                }

                chunkData = chunkQ.front();

                return result;
        }

        void render()
        {
                rendering vertex array....
        }
       
        void update()
        {
                crazy FFT shit going on here....
        }

        void run()
        {
                typical run loop....
        }

private:

        sf::SoundStream::Chunk chunkData;
        std::queue<sf::SoundStream::Chunk> chunkQ;

        window, vertex array, other stuff not important
};
 
« Last Edit: March 31, 2014, 11:00:30 pm by MrShedman »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Getting a sample buffer in sync with music's own.
« Reply #1 on: March 31, 2014, 11:04:37 pm »
Quote
I can't understand why I cannot simply do this
Who said you can't do it? What's your problem?
Laurent Gomila - SFML developer

MrShedman

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Getting a sample buffer in sync with music's own.
« Reply #2 on: March 31, 2014, 11:46:34 pm »
In the previous thread I linked there was a delay between the samples being analysed (roughly 2 seconds for a 44.1kHz sound) and the ones actually being played through the speakers. You suggested lowering the buffer size in sf::Music although pointed out there would still be a delay, only smaller and that the music might not play smoothly.

I thought I could just use a simple queue to store the 3 buffers and use the "oldest" one for FFT analysis. When new samples were loaded I would push them into the queue and just pop the "oldest" one. It doesn't work and is still roughly 2 seconds infront.

I would've thought this would solve the delay caused by the triple buffering in sf::Music, but it doesn't and I can't figure out why....I probably dun goofed somewhere.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Getting a sample buffer in sync with music's own.
« Reply #3 on: April 01, 2014, 07:46:39 am »
It sounds like it should solve the problem, indeed. I don't know why it doesn't work, check your code and especially the delays.

You can provide a complete and minimal example that reproduces the problem, so that we can test it.
Laurent Gomila - SFML developer