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

Author Topic: Sound which is repeating.  (Read 4085 times)

0 Members and 3 Guests are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Sound which is repeating.
« on: April 02, 2015, 10:01:01 pm »
Hi!
Sorry if my question is stupid and if I've missed something in the doc but :

I've created a custom audio stream like explained in the tutorial :

void Stream::load(const sf::SoundBuffer& buffer)
        {
            m_file = nullptr;
            // 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
            SoundStream::initialize(buffer.getChannelCount(), buffer.getSampleRate());
        }
        ////////////////////////////////////////////////////////////
        bool Stream::openFromFile(const std::string& filename)
        {
            // First stop the music if it was already running
            m_file = new priv::SoundFile();
            stop();

            // Open the underlying sound file
            if (!m_file->openRead(filename))
                return false;

            // Perform common initializations
            initialize();

            return true;
        }


        ////////////////////////////////////////////////////////////
        bool Stream::openFromMemory(const void* data, std::size_t sizeInBytes)
        {
            m_file = new priv::SoundFile();
            // First stop the music if it was already running
            stop();

            // Open the underlying sound file
            if (!m_file->openRead(data, sizeInBytes))
                return false;

            // Perform common initializations
            initialize();

            return true;
        }


        ////////////////////////////////////////////////////////////
        bool Stream::openFromStream(InputStream& stream)
        {
            m_file = new priv::SoundFile();
            // First stop the music if it was already running
            stop();

            // Open the underlying sound file
            if (!m_file->openRead(stream))
                return false;

            // Perform common initializations
            initialize();

            return true;
        }
        bool Stream::onGetData(Chunk& data)
        {
            Lock lock(m_mutex);
            if (m_file == nullptr) {
                // number of samples to stream every time the function is called;
                // in a more robust implementation, it would rather be a fixed
                // amount of time rather than an arbitrary number of samples
                const int samplesToStream = 50000;

                // 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;
                }
            } else {
                // Fill the chunk parameters
                data.samples     = &m_samples[0];
                data.sampleCount = m_file->read(&m_samples[0], m_samples.size());

                // Check if we have reached the end of the audio file
                return data.sampleCount == m_samples.size();
            }
        }

        void Stream::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());
        }
        void Stream::initialize()
        {
            // Compute the music duration
            m_duration = seconds(static_cast<float>(m_file->getSampleCount()) / m_file->getSampleRate() / m_file->getChannelCount());

            // Resize the internal buffer so that it can contain 1 second of audio samples
            m_samples.resize(m_file->getSampleRate() * m_file->getChannelCount());

            // Initialize the stream
            SoundStream::initialize(m_file->getChannelCount(), m_file->getSampleRate());
        }
 

I've also created a class to play the song but I want to use only one class for playing sounds and musics :

Player::Player(SoundBuffer& buffer) {
             Stream* stream = new Stream();
             stream->load(buffer);
             this->stream = stream;
        }
        void Player::setAudioStream(SoundStream* stream) {
             this->stream = stream;
        }
        sf::SoundStream* Player::getAudioStream() {
            return stream;
        }
        void Player::play(bool loop) {
             stream->setLoop(loop);
             stream->play();
        }
        void Player::stop() {
             stream->stop();
        }
        void Player::pause() {
             stream->pause();
        }
        Player::~Player() {
            delete stream;
        }
 

The problem is that the sound is playing repeatively :

int main (int argv,char* argc[]) {
    sf::SoundBuffer buffer;
    buffer.loadFromFile("sounds/ballhurtbar.wav");
    odfaeg::audio::Stream stream;
    stream.load(buffer);
    odfaeg::audio::Player player;
    player.setAudioStream(&stream);
    player.play(false);
    const auto delay = sf::milliseconds(1);
    do {
        sf::sleep(delay);
    } while(stream.getStatus() == odfaeg::audio::Stream::Playing);
    return 0;
 

Isn't the sound supposed to be stopped when the onGetData method return false ?

Or I've I missunderstand something ?
« Last Edit: April 02, 2015, 10:15:04 pm by Lolilolight »

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Sound which is repeating.
« Reply #1 on: April 02, 2015, 10:20:35 pm »
Erf, wrong file, code block was still keeping my old project file so I've changed the wrong file.  >:(

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Sound which is repeating.
« Reply #2 on: April 02, 2015, 11:54:40 pm »
@Lolilolight : If I may make a suggestion; could you please, the next time you run into a problem, delay 48 hours before posting on the forum about it and use that time to research the problem yourself? Please.

And when that is now said, a few comments on the actual code you posted:

...
            m_file = new priv::SoundFile();
...
            delete stream;
Yuck. Naked 'new's and especially naked delete's.  You really should learn about and embrace RAII and smart pointers (unique_ptr & shared_ptr and their friends make_unique & make_shared).

int main (int argv,char* argc[])
I'm guessing this is just a typo, but it's a funny one. If you start using the argc and argv variables in your code you are going to have some nasty surprises since you mixed them up. argc is the first one and is a count of arguments, argv is the second one and is an array of pointers to arguments - you've got them "back asswards".
« Last Edit: April 03, 2015, 10:46:05 am by Jesper Juhl »

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Sound which is repeating.
« Reply #3 on: April 03, 2015, 03:20:42 pm »
Yes I have still to change some classes for using RAII. :/