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

Author Topic: Set Music Loop-Points (Start/End)  (Read 8457 times)

0 Members and 3 Guests are viewing this topic.

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Set Music Loop-Points (Start/End)
« on: August 03, 2010, 07:04:11 pm »
It should be possible to set loop points on music. I have got some music where it would be good if not the whole song is looped, but just a part of it. And please, if this feature will be added, it should be possible to set it with sample number and not only in seconds using float.
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Set Music Loop-Points (Start/End)
« Reply #1 on: August 03, 2010, 07:24:57 pm »
There are lots of topics about this feature. I'm currently working on it.

Quote
And please, if this feature will be added, it should be possible to set it with sample number and not only in seconds using float

May I ask why? I don't think that you loose precision with floats.
And I assume that most people will give loop points in time, not samples. But maybe I'm wrong, I didn't get much feedback yet.
Anyway, you can always calculate one from the other.
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Set Music Loop-Points (Start/End)
« Reply #2 on: August 03, 2010, 08:45:26 pm »
Quote from: "Laurent"
There are lots of topics about this feature. I'm currently working on it.

I hope that it will be in the next version and I hope it's coming soon. Otherwise I might think of using a different library or try to "solve" it with a custom audio stream...

Quote from: "Laurent"

Quote
And please, if this feature will be added, it should be possible to set it with sample number and not only in seconds using float

May I ask why? I don't think that you loose precision with floats.
And I assume that most people will give loop points in time, not samples. But maybe I'm wrong, I didn't get much feedback yet.
Anyway, you can always calculate one from the other.

I usually don't like floats and doubles very much.
To get looping a stream very precisely it would be good to pass the sample number and not a float, in my opinion.
Also I often see floats in the lib where other types (e.g. ms for the time, samples for sound) could be use. Could you maybe add support for non-float values for such things?
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Set Music Loop-Points (Start/End)
« Reply #3 on: August 03, 2010, 10:34:47 pm »
Quote
I hope that it will be in the next version and I hope it's coming soon. Otherwise I might think of using a different library or try to "solve" it with a custom audio stream...

It is already possible, you can inherit from sf::Music and hack into the virtual functions to do what you want.

Quote
To get looping a stream very precisely it would be good to pass the sample number and not a float, in my opinion.

I understand, but I still don't think that in this particular context you would loose precision with floats. If I'm wrong please provide more detailed arguments :)

Quote
Also I often see floats in the lib where other types (e.g. ms for the time, samples for sound) could be use. Could you maybe add support for non-float values for such things?

I agree that unsigned integers are better for storing time in milliseconds ;)
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Set Music Loop-Points (Start/End)
« Reply #4 on: August 03, 2010, 11:12:50 pm »
Quote from: "Laurent"

Quote
Also I often see floats in the lib where other types (e.g. ms for the time, samples for sound) could be use. Could you maybe add support for non-float values for such things?

I agree that unsigned integers are better for storing time in milliseconds ;)

So, why don't I see any possibilites of using ms (or e.g. 1/1000000 secs)? ^^

Quote from: "Laurent"

Quote
To get looping a stream very precisely it would be good to pass the sample number and not a float, in my opinion.

I understand, but I still don't think that in this particular context you would loose precision with floats. If I'm wrong please provide more detailed arguments :)

I have got a simple example:
Let's assume, the sound does have 3,333,333 samples. Loop start might be 1,111,111. If I'd calculate the seconds, it would be 1/3 or the song length (let's assume 100 secs), the the float value will be about 33.333333. When I calculate 33.333333/100*3,333,333, I would get 1,111,110.989, so will it be round down to 1,111,110?
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Set Music Loop-Points (Start/End)
« Reply #5 on: August 04, 2010, 08:45:30 am »
Quote
so will it be round down to 1,111,110?

No, we'd rather round to the nearest and get the correct value.
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Set Music Loop-Points (Start/End)
« Reply #6 on: August 04, 2010, 03:43:54 pm »
Quote from: "Laurent"
Quote
so will it be round down to 1,111,110?

No, we'd rather round to the nearest and get the correct value.

Okay. But I would still prefer integers to floats  :roll:
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Set Music Loop-Points (Start/End)
« Reply #7 on: August 04, 2010, 03:57:36 pm »
Ok, but without further arguments I'll implement what I prefer ;)
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
How to do special loop with sf::Music?
« Reply #8 on: August 11, 2010, 02:22:45 am »
Quote from: "Laurent"
Quote
I hope that it will be in the next version and I hope it's coming soon. Otherwise I might think of using a different library or try to "solve" it with a custom audio stream...

It is already possible, you can inherit from sf::Music and hack into the virtual functions to do what you want.

Looking at the source of Music.hpp I don't really understand how I can do that.
How may I get sample data from the loaded (ogg) file without directly playing the file and instead putting the samples in a custom buffer until a certain (loop) point?
Could you maybe show me a little example please?
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Set Music Loop-Points (Start/End)
« Reply #9 on: August 11, 2010, 09:15:57 am »
You don't need to do that, just let sf::Music do its job and adjust the chunks of samples that it streams.

Code: [Select]
#include <limits>

class MyMusic : public sf::Music
{
public:

    MyMusic() :
    myStart(0),
    myEnd(std::numeric_limits<std::size_t>::max()),
    myCurrentPosition(0)
    {
    }

    void SetStart(std::size_t sample)
    {
        myStart = sample;
    }

    void SetEnd(std::size_t sample)
    {
        myEnd = sample;
    }

private:

    virtual bool OnGetData(Chunk& data)
    {
        if (sf::Music::OnGetData(data))
        {
            // calculate the new position (in sample)
            std::size_t current = data.NbSamples + myCurrentPosition;
            if (current <= myEnd)
            {
                // we're still in [myStart, myEnd], update the current position
                // and leave the returned chunk unchanged
                myCurrentPosition = current;
                return true;
            }
            else
            {
                // we've reached myEnd, adjust the number of samples returned
                // and notify the base class that we need to loop
                data.NbSamples = myEnd - myCurrentPosition;
                myCurrentPosition = myEnd;
                return false;
            }
        }
        else
        {
            // base class requested to stop playback
            return false;
        }
    }

    virtual void OnSeek(float time)
    {
        // make time relative to myStart
        myCurrentPosition = myStart + TimeToSample(time);
        sf::Music::OnSeek(SampleToTime(myCurrentPosition));
    }

    std::size_t TimeToSample(float time)
    {
        return static_cast<std::size_t>(time * GetSampleRate() * GetChannelsCount());
    }

    float SampleToTime(std::size_t sample)
    {
        return static_cast<float>(sample) / GetSampleRate() / GetChannelsCount();
    }

private:

    std::size_t myStart;
    std::size_t myEnd;
    std::size_t myCurrentPosition;
};


This code works only with SFML 2. I don't think it is possible to implement the same thing in SFML 1, due to the lack of the OnSeek callback.

It also requires a minor modification in the sf::Music class (change the access of these virtual functions from private to protected); I'l officially change this as soon as possible.

I don't know if this class does everything that you want, but at least now you have some code to play with ;)
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Set Music Loop-Points (Start/End)
« Reply #10 on: August 11, 2010, 03:48:31 pm »
Quote from: "Laurent"
This code works only with SFML 2. I don't think it is possible to implement the same thing in SFML 1, due to the lack of the OnSeek callback.

It also requires a minor modification in the sf::Music class (change the access of these virtual functions from private to protected); I'l officially change this as soon as possible.

I don't know if this class does everything that you want, but at least now you have some code to play with ;)

So I either have to wait for SFML 2, or I have to use a different library, or I have to compile a pre-version of SFML 2 myself if it is already fully implemented in the version?
Please note that my previous display name was "Shy Guy".

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Set Music Loop-Points (Start/End)
« Reply #11 on: August 11, 2010, 03:52:23 pm »
Quote
or I have to compile a pre-version of SFML 2 myself if it is already fully implemented in the version?

You can do that, yes. I've already made the modification that I said previously.

Of course, you can as well use another library for audio, if you want something much more complete and high-level, there are very good libraries such as BASS or FMod.
Laurent Gomila - SFML developer

Tenry

  • Full Member
  • ***
  • Posts: 120
  • Experienced Programmer
    • View Profile
    • Simon-Burchert.com
Set Music Loop-Points (Start/End)
« Reply #12 on: August 11, 2010, 05:21:18 pm »
Quote from: "Laurent"
Of course, you can as well use another library for audio, if you want something much more complete and high-level, there are very good libraries such as BASS or FMod.

Okay. BASS is by the way my favorite audio library, it's really cool :)
Please note that my previous display name was "Shy Guy".