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

Author Topic: Trouble with setPlayingOffset()  (Read 5361 times)

0 Members and 1 Guest are viewing this topic.

tangor

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Trouble with setPlayingOffset()
« on: September 15, 2015, 07:22:06 pm »
If I set setPlayingOffset() over more than 50% of .flac file, music restarts playing.

For example: music.setPlayingOffset(sf::seconds(200); (music duration is equal 300 seconds). This causes playing music from the beginning.

If I open .ogg file, everything works well- I can set every value which is less than music.getDuration();

Here is code:
void playMusic(std::string bob)
{
    // Load an ogg music file
    sf::Music music;
       
       
    if (!music.openFromFile(bob))
        return;

    // Display music informations
    std::cout << "orchestral.ogg :" << std::endl;
    std::cout << " " << music.getDuration().asSeconds() << " seconds"       << std::endl;
    std::cout << " " << music.getSampleRate()           << " samples / sec" << std::endl;
    std::cout << " " << music.getChannelCount()         << " channels"      << std::endl;

    // Play it
    music.play();

        music.setPlayingOffset(sf::seconds(200));
    // Loop while the music is playing
    while (music.getStatus() == sf::Music::Playing)
    {
        // Leave some CPU time for other processes
        sf::sleep(sf::milliseconds(100));

        // Display the playing position
        std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << music.getPlayingOffset().asSeconds() << " sec   ";
        std::cout << std::flush;
    }
    std::cout << std::endl;
}

 

I check it for several .flac files, which duration is between 2-4minutes. In each is the same bug.

I use SFML for visual studio 2010 32-bit

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Trouble with setPlayingOffset()
« Reply #1 on: September 15, 2015, 10:27:55 pm »
music.setPlayingOffset(sf::seconds(200); (music duration is equal 300 seconds).
...
I check it for several .flac files, which duration is between 2-4minutes. In each is the same bug.
Are you sure about the 300s? Because 200s = 3.33min, so a 2min file won't work.
Can you provide any of your FLAC files, because I can't reproduce this with mine.


However there does seem to be a bug, because if the offset is larger than the actual audio length, it will just keep counting from the offset and start the music from the beginning.

Here's an actual complete example code:
#include <SFML/Audio.hpp>
#include <iostream>
#include <iomanip>

void playMusic(std::string bob)
{
    // Load an ogg music file
    sf::Music music;

    if (!music.openFromFile(bob))
        return;

    // Display music informations
    std::cout << bob << " :" << std::endl;
    std::cout << " " << music.getDuration().asSeconds() << " seconds"       << std::endl;
    std::cout << " " << music.getSampleRate()           << " samples / sec" << std::endl;
    std::cout << " " << music.getChannelCount()         << " channels"      << std::endl;

    // Play it
    music.play();

    music.setPlayingOffset(sf::seconds(200));
    // Loop while the music is playing
    while (music.getStatus() == sf::Music::Playing)
    {
        // Leave some CPU time for other processes
        sf::sleep(sf::milliseconds(100));

        // Display the playing position
        std::cout << "\rPlaying... " << std::fixed << std::setprecision(2) << music.getPlayingOffset().asSeconds() << " sec   ";
        std::cout << std::flush;
    }
    std::cout << std::endl;
}

int main()
{
    playMusic("music.flac");
}
 

With the output being somewhere along the lines of:


And indeed the the offset gets never validated but gets just applied.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Trouble with setPlayingOffset()
« Reply #2 on: September 16, 2015, 11:00:24 pm »
Laurent: Was that just overlooked in the initial implementation or was there a specific reason to not check it?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trouble with setPlayingOffset()
« Reply #3 on: September 16, 2015, 11:13:34 pm »
Quote
Laurent: Was that just overlooked in the initial implementation or was there a specific reason to not check it?
I think it's a mistake that should be fixed.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Trouble with setPlayingOffset()
« Reply #4 on: September 16, 2015, 11:20:55 pm »
I opened an issue: #966
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ief015

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Trouble with setPlayingOffset()
« Reply #5 on: January 06, 2016, 10:59:05 am »
I'm having the same issue OP is getting. For some reference, here's the offending function in my app:

void onMouseUp(int x, int y, unsigned b)
{

        if (b != sf::Mouse::Left)
                return;
       
        if (mouseHoverProgressBar)
        {
                if (playMode == PLAY_FILE_BUFFER)
                {
                        ... irrelevant code here
                }
                else if (playMode == PLAY_FILE_STREAM)
                {
                        sf::Time t = audioStream->getDuration() * (static_cast<float>(x) / rw->getSize().x);
                        audioStream->setPlayingOffset(t); // <<< PROBLEMS HAPPEN IN HERE SOMEWHERE.
                        // audioStream is an instance of a child class of sf::Music.

                        //printf(">>>seeking=%f | duration=%f\n",t.asSeconds(),audioStream->getDuration().asSeconds());
                }
        }

}
 

The program was working flawlessly using sfml 2.2, before upgrading it to 2.3.2 today.

That print statement I have being printed out displays exactly what the OP was having trouble with, for instance on a stereo 200s flac file, seeking to anything above 100s will cause the song to repeat. Seeking to say, something like 99s will bring me very near the end of the song.

In this print example, I seek to just almost half way through the audio, displays that it's only a few minutes through the song, but the last few seconds of the audio is playing instead.
>>>seeking=176.484055 | duration=365.533325

I also tried using an 8 channel flac file, and as I've expected and you've probably guessed, the song fails to seek properly after 1/8th (12.5%) of the duration, and setting the offset to 1/16th of the duration will bring me half way through the song. So, there's some sort of flawed math happening during seeking that's likely not taking into account of the number of channels.

If I were to guess, this line probably needs to factor in the number of channels in the source file.

I've also tested my program against .ogg and .wav files and they did not share the same issue. Only .flac files have this problem.
« Last Edit: January 06, 2016, 11:14:40 am by ief015 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: Trouble with setPlayingOffset()
« Reply #6 on: January 07, 2016, 11:38:25 pm »
Nice catch. Really seems like the channel count should be taken into account. Feel free to open an issue on GitHub.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/