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

Author Topic: In implementation of MyStream sound sometimes doesn't play  (Read 2216 times)

0 Members and 1 Guest are viewing this topic.

GetterSetter

  • Newbie
  • *
  • Posts: 7
    • View Profile
In implementation of MyStream sound sometimes doesn't play
« on: January 25, 2022, 11:38:24 am »
I got the code from https://www.sfml-dev.org/tutorials/2.5/audio-streams.php and replaced
const int samplesToStream = 50000;
to
const int samplesToStream = getSampleRate()/factor;
It worked fine until factor was 4 (or 4^k: 4,16,64...). In case if factor == 4^k, sound doesn't play and the app closes immediately with no errors
#include <SFML/Audio.hpp>
#include <vector>

// custom audio stream that plays a loaded buffer
class MyStream : public sf::SoundStream
{
public:

    void load(const sf::SoundBuffer& buffer)
    {
        // extract the audio samples from the sound buffer to our own container
        m_samples.assign(buffer.getSamples(), buffer.getSamples() + buffer.getSampleCount());

        // reset the current playing position
        m_currentSample = 0;

        // initialize the base class
        initialize(buffer.getChannelCount(), buffer.getSampleRate());
    }

private:

    virtual bool onGetData(Chunk& data)
    {
        // number of samples to stream every time the function is called;
        // in a more robust implementation, it should be a fixed
        // amount of time rather than an arbitrary number of samples
        const std::size_t samplesToStream = getSampleRate()/4;

        // set the pointer to the next audio samples to be played
        data.samples = &m_samples[m_currentSample];

        // have we reached the end of the sound?
        if (m_currentSample + samplesToStream <= m_samples.size())
        {
            // end not reached: stream the samples and continue
            data.sampleCount = samplesToStream;
            m_currentSample += samplesToStream;
            return true;
        }
        else
        {
            // end of stream reached: stream the remaining samples and stop playback
            data.sampleCount = m_samples.size() - m_currentSample;
            m_currentSample = m_samples.size();
            return false;
        }
    }

    virtual void onSeek(sf::Time timeOffset)
    {
        // compute the corresponding sample index according to the sample rate and channel count
        m_currentSample = static_cast<std::size_t>(timeOffset.asSeconds() * getSampleRate() * getChannelCount());
    }

    std::vector<sf::Int16> m_samples;
    std::size_t m_currentSample;
};

int main()
{
    // load an audio buffer from a sound file
    sf::SoundBuffer buffer;
    buffer.loadFromFile("sound.ogg");

    // initialize and play our custom stream
    MyStream stream;
    stream.load(buffer);
    stream.play();

    // let it play until it is finished
    while (stream.getStatus() == MyStream::Playing)
        sf::sleep(sf::seconds(0.1f));

    return 0;
}
 
OS: Ubuntu 20.04.3 LTS
SFML version: 2.5.1

GetterSetter

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: In implementation of MyStream sound sometimes doesn't play
« Reply #1 on: January 26, 2022, 06:42:31 pm »
Ok, I found the similiar problem on SO (i guess): https://stackoverflow.com/a/62172120
I've compiled new SFML release and the output is:
An internal OpenAL call failed in SoundStream.cpp(448).
Expression:
   alBufferData(buffer, m_format, data.samples, size, static_cast<ALsizei>(m_sampleRate))
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.
 


And the solution was:
samplesToStream -= samplesToStream % 4;
« Last Edit: January 26, 2022, 06:51:11 pm by GetterSetter »

 

anything