SFML community forums

Help => Audio => Topic started by: AndyOrange on July 18, 2017, 08:16:56 am

Title: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: AndyOrange on July 18, 2017, 08:16:56 am
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.

Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: Laurent on July 18, 2017, 10:02:19 am
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).
Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: AndyOrange on July 18, 2017, 11:09:27 am
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
Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: Laurent on July 18, 2017, 11:18:30 am
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.
Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: AndyOrange on July 18, 2017, 11:26:46 am
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
Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: Laurent on July 18, 2017, 11:47:49 am
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.
Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: AndyOrange on July 18, 2017, 11:54:49 am
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
Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: Laurent on July 18, 2017, 12:38:01 pm
It's really easy, you have all the built-in readers in src/SFML/Audio. For example, here is the WAV reader (https://github.com/SFML/SFML/blob/master/src/SFML/Audio/SoundFileReaderWav.cpp) (and yes, it is probably the simplest one, and doesn't depend on any 3rd party library).
Title: Re: SoundFileFactory::RegisterReader not working with sf::Sound and buffer, C++
Post by: AndyOrange on July 18, 2017, 01:08:47 pm
Marvellous, thanks a lot!