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

Author Topic: Accessing Musics Samples  (Read 9637 times)

0 Members and 2 Guests are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Accessing Musics Samples
« on: April 19, 2012, 06:05:51 pm »
Hello everybody,
my questions is if it is possible to access the samples of a music file in real time. I'm not sure if it's clear what I mean, so I'll explain what I want to do. I would like to get the samples of a music file currently being processed, so I can do some sound manipulation in real time (while it's being played). I know this is possible with SoundBuffers, but I want to use bigger files (i.e. SoundStreams / sf::Music). Is there any way I can do this with SFML? And if not is there any way I can do this using as much of SFML convenience as possible?
Thanks in advance :)
Foaly

I am on a Windows machine using the current SFML2 rc btw.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Accessing Musics Samples
« Reply #1 on: April 19, 2012, 06:20:04 pm »
class MyMusic : public sf::Music
{
protected:

    virtual bool onGetData(sf::SoundStream::Chunk& data)
    {
        bool result = sf::Music::onGetData(data);
        // ... do something with data.samples ...
        return result;
    }
};
(SFML 2)

But with this code, you'll process samples as they are loaded into the music's internal buffer, which is slightly before they are played.
Laurent Gomila - SFML developer

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: Accessing Musics Samples
« Reply #2 on: April 19, 2012, 06:46:06 pm »
Ok, perfect! Thank you for the *really* quick reply! :D
First of all I wanted to implement a beat detection algorithm i found here: http://www.flipcode.com/misc/BeatDetectionAlgorithms.pdf
It uses a window of 1024 samples to stay in real time. How could I get 1024 samples at a time, while still staying in sync with the playing? (right now the sampleCount in onGetData is 88200, which is like 2 seconds)
thanks again :)
Foaly

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Accessing Musics Samples
« Reply #3 on: April 19, 2012, 07:31:02 pm »
I guess there's nothing much you can do with the current API.

You could try to modify the internal sample size: at line 145 of Music.cpp, you can try:
m_samples.resize(1024);
1024 is not a lot so I don't know if the music will still play smoothly. And it will still not be perfect, you'll still be 1024 * 2 samples before what's played (SFML streams are triple buffered).
I don't know if true real-time is achievable without specialized audio APIs (or even hardware?), to be honest I have never thought about this kind of use case.
Laurent Gomila - SFML developer

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Accessing Musics Samples
« Reply #4 on: June 17, 2012, 03:08:21 pm »
I am also interested in this question.
What I'm trying to do is to create bars visualization of a playing music file. For that I'm intending to use kissfft in order to get the frequency spectrum of the current sounds.

I thought about doing it in onGetData but the long interval between calls does not suit my needs too.
I'm still trying. If I'm able to do it I'll tell you.

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Accessing Musics Samples
« Reply #5 on: June 17, 2012, 06:12:15 pm »
After some fiddling I was able to draw the waveform of the loaded chunk.
Since the chunk is much bigger than the time window of a frame I divide the chunk by framerate and draw the waves sequentially.

The problem is it seems that there is a delay between loading a chunk and playing it so if I start drawing the chunk when it loads I draw ahead of time. Do I have to access another buffer of samples?

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Accessing Musics Samples
« Reply #6 on: June 17, 2012, 10:12:04 pm »
I resorted to using SoundBuffer and loading the whole file to memory for now. It works and the synchronization is good.

If I'll find a way to do it with a stream in the near future I'll post here.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Accessing Musics Samples
« Reply #7 on: June 17, 2012, 10:58:33 pm »
Quote
If I'll find a way to do it with a stream in the near future I'll post here.
I don't think you can get correct synchronization, there's no way to know when a chunk of samples will be played -- except by looking at the source code and adding one or two ugly hacks to your code.
Laurent Gomila - SFML developer

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Accessing Musics Samples
« Reply #8 on: June 17, 2012, 11:01:54 pm »
Thanks.
For this functionality I would have to use another library in conjunction with SFML then. It is ok I guess. Any idea where I should look first? LibSndFile? Portaudio? I don't really know them.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Accessing Musics Samples
« Reply #9 on: June 18, 2012, 08:27:04 am »
I have no idea. SFML uses libsndfile + OpenAL, so I guess you won't have better results with these libraries; unless you choose a different strategy, but I don't even know if there's a good alternative.

I don't know other audio libraries, except FModEx which is an audio engine and has many many features.
Laurent Gomila - SFML developer

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Accessing Musics Samples
« Reply #10 on: June 18, 2012, 10:35:38 pm »
Ok. Thanks for your responsiveness.

I'll post my findings if I succeed.

Phanoo

  • Full Member
  • ***
  • Posts: 136
    • View Profile
Re: Accessing Musics Samples
« Reply #11 on: August 06, 2012, 12:03:04 pm »
Look at portaudio for real time applications

SoleSoul

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Accessing Musics Samples
« Reply #12 on: August 06, 2012, 12:33:50 pm »
Ok, thanks.

 

anything