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

Author Topic: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++  (Read 5005 times)

0 Members and 1 Guest are viewing this topic.

AndyOrange

  • Newbie
  • *
  • Posts: 13
    • View Profile
Greetings all,

due to real time demans in a current project I need to read an mp3-file (now that the license is free) into a buffer from where to play it, intending to use sf::Sound and sf::SoundBuffer. However, the callback of sfml which calls the "read" methode seems to only work with sf::Music and thus only offers reading from files while playing (worse, it seems to automatically stop playing when macCount bytes are read...). That is a no go in my case -- any idea how to combine a custom player/reader/opener with sf::Sound and sf::SoundBuffer such that I can preallocate the buffer (unique_ptr for instance) and play from memory?

Thanks a lot for help,
Andy

P.S.: I tried using sf::Sound::loadFromFile and sf::Music::openFromFile, only the latter worked.


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Everything should work, readers are the only source of audio data, for both sf::Sound and sf::Music. If there's a problem with sf::Sound and your custom reader, then post more details about the issue (and about your reader).
Laurent Gomila - SFML developer

AndyOrange

  • Newbie
  • *
  • Posts: 13
    • View Profile
Greetings and thanks for your reply. If I understand things correctly, the read function is called periodically. hence, when intending to work with sf::Sound and intending to read the whole file into memory, I first have to read the complete file in the open method, I guess. When I use sf::music, read gets called and delivers sound snippets as expected. When I use sf:Sound, it gets called exactly once and maxCount is just far to small to reference the whole file. However, I have no access to the caller of read and to for instance set maxCount to the complete size of the file I read...

I think about posting some snippet...

atb,

Andy

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
You don't have to do anything, if your MP3 reader is correctly implemented, it should work as expected. Can you post it, or relevant parts (if there's too much code)?

I think you might have something wrong in the check function.
Laurent Gomila - SFML developer

AndyOrange

  • Newbie
  • *
  • Posts: 13
    • View Profile
I am basically using this code and unlegacying it a little:

https://github.com/hansiC/sfml-soundfileReader-example

Most important changes concern the swap sf::music for sf::Sound as follows in the main routine:


        std::shared_ptr<sf::Sound> m_soundSource = std::make_shared<sf::Sound>();
        std::shared_ptr<sf::SoundBuffer> m_soundBuffer = std::make_shared<sf::SoundBuffer>();

        if (!m_soundBuffer->loadFromFile(filename)) {
                ...
        }
        m_soundSource->setBuffer(*m_soundBuffer);
        m_soundSource->setVolume(0.8f);

 


and later in the loop:

togglePlayPause(*m_soundSource);
 

Best regards and thanks  alot for your assistance!
Andy

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
You'll have to ask the author of this code, or debug it yourself. There's very little chance that the problem is caused by SFML, since all the built-in readers work fine.
Laurent Gomila - SFML developer

AndyOrange

  • Newbie
  • *
  • Posts: 13
    • View Profile
Can you hint at a place in sourcCode where I can debug into some reader and it's read() metohd for instance when opening a wav file? Might help to compare debugging to a working implementation with sf::Sound...

Thanks,
Andy

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
It's really easy, you have all the built-in readers in src/SFML/Audio. For example, here is the WAV reader (and yes, it is probably the simplest one, and doesn't depend on any 3rd party library).
Laurent Gomila - SFML developer

AndyOrange

  • Newbie
  • *
  • Posts: 13
    • View Profile
Marvellous, thanks a lot!

 

anything