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

Author Topic: [SOLVED]Can't play several sounds at the same time. :/  (Read 6947 times)

0 Members and 1 Guest are viewing this topic.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
[SOLVED]Can't play several sounds at the same time. :/
« on: January 03, 2015, 05:16:24 pm »
Hi!

I've created a custom stream class which inherits from your SoundStream class.

I pass my custom stream to a player class (because sf::sound can only take a constant reference to a soundstream instance)

Code: [Select]
||=== Build: Debug in ODFAEG-DEMO (compiler: GNU GCC Compiler) ===|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp||In member function ‘virtual void MyAppli::onLoad()’:|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|97|erreur: no matching function for call to ‘sf::Sound::setBuffer(odfaeg::audio::Stream&)’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|97|note: candidate is:|
/usr/include/SFML/Audio/Sound.hpp|126|note: void sf::Sound::setBuffer(const sf::SoundBuffer&)|
/usr/include/SFML/Audio/Sound.hpp|126|note:   no known conversion for argument 1 from ‘odfaeg::audio::Stream’ to ‘const sf::SoundBuffer&’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|100|erreur: ‘class sf::Sound’ has no member named ‘setAudioStream’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp||In member function ‘virtual void MyAppli::onInit()’:|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|307|erreur: no matching function for call to ‘sf::Sound::play(bool)’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|307|note: candidate is:|
/usr/include/SFML/Audio/Sound.hpp|89|note: void sf::Sound::play()|
/usr/include/SFML/Audio/Sound.hpp|89|note:   candidate expects 0 arguments, 1 provided|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp||In member function ‘virtual void MyAppli::onUpdate(sf::Event&)’:|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|433|erreur: ‘class sf::Sound’ has no member named ‘isPlaying’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp||In member function ‘virtual void MyAppli::onExec()’:|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|452|erreur: ‘class sf::Sound’ has no member named ‘isPlaying’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|453|erreur: no matching function for call to ‘sf::Sound::play(bool)’|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|453|note: candidate is:|
/usr/include/SFML/Audio/Sound.hpp|89|note: void sf::Sound::play()|
/usr/include/SFML/Audio/Sound.hpp|89|note:   candidate expects 0 arguments, 1 provided|
/home/laurent/Développement/Projets-c++/ODFAEG-DEMO/application.cpp|490|erreur: ‘class sf::Sound’ has no member named ‘isPlaying’|
||=== Build failed: 7 error(s), 0 warning(s) (0 minute(s), 5 second(s)) ===|

The player class doesn't do anything more than calling the methods play, stop, etc... of the SoundStream class.

It's fine but.

When I load another sound buffer with a second player, and when I play it, the first sound is stoped, so, the two sounds are not played at the same time.  :-\

Is it a bug, if it's already a reported issue, sorry about that. :/
But in the tutorial it is said that we can play serveral sounds at the same time, so, I don't understand that.
« Last Edit: January 03, 2015, 09:34:54 pm by Lolilolight »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Can't play several sounds at the same time. :/
« Reply #1 on: January 03, 2015, 05:37:56 pm »
You describe an execution problem and you post compiler errors... ??

And you know what I'm going to say... please provide a complete and minimal example that reproduces the problem. We tell you everytime you post here, we'll start to believe that you do it on purpose...
Laurent Gomila - SFML developer

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Can't play several sounds at the same time. :/
« Reply #2 on: January 03, 2015, 06:07:06 pm »
First of all, why I get this error ?

My class inherits from your sf::SoundStream class, so it should accept a reference onto a derived instance.

Quote
And you know what I'm going to say... please provide a complete and minimal example that reproduces the problem. We tell you everytime you post here, we'll start to believe that you do it on purpose..

No, I don't do it in purpose, here is the code which cause problems :

#include "../../../include/odfaeg/Audio/stream.h"
#include "SoundFile.hpp"
using namespace sf;
namespace odfaeg {
    namespace audio {
        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();
            }
        }
        bool Stream::isFinished() {
            return m_currentSample == m_samples.size() - 1;
        }
        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());
        }
        bool Stream::stereo() {
            return m_file->getChannelCount() > 1;
        }
        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());
        }
    }
}
 
#include "../../../include/odfaeg/Audio/player.h"
#include <iostream>
using namespace sf;
namespace odfaeg {
    namespace audio {
        Player::Player() {
            stream = nullptr;
            inPause = true;
        }
        Player::Player(SoundBuffer& buffer) {
            Stream* stream = new Stream();
            stream->load(buffer);
            this->stream = stream;
            inPause = true;
        }
        bool Player::stereo() {
            return stream->stereo();
        }
        void Player::setAudioStream(Stream* stream) {
            this->stream = stream;
        }
        Stream* Player::getAudioStream() {
            return stream;
        }
        void Player::play(bool loop) {
            inPause = false;
            stream->setLoop(true);
            stream->play();
        }
        void Player::stop() {
            inPause = true;
            stream->stop();
        }
        void Player::pause() {
            inPause = true;
            stream->pause();
        }
        bool Player::isPlaying() {
            return !inPause && !stream->isFinished();
        }
        void Player::setPitch(float pitch) {
            stream->setPitch(pitch);
        }
        void Player::setVolume(float volume) {
            stream->setVolume(volume);
        }
        void Player::setPosition(float x, float y, float z) {
            stream->setPosition(x, y, z);
        }
        void Player::setPosition(sf::Vector3f position) {
            stream->setPosition(position);
        }
        void Player::setRelativeToListener(bool relative) {
            stream->setRelativeToListener(relative);
        }
        void Player::setMinDistance(float minDistance) {
            stream->setMinDistance(minDistance);
        }
        void Player::setAttenuation(float attenuation) {
            stream->setAttenuation(attenuation);
        }
        float Player::getPitch() const {
            return stream->getPitch();
        }
        float Player::getVolume() const {
            return stream->getVolume();
        }
        sf::Vector3f Player::getPosition() const {
            return stream->getPosition();
        }
        bool Player::isRelativeToListener() const {
            return stream->isRelativeToListener();
        }
        float Player::getMinDistance() const {
            return stream->getMinDistance();
        }
        float Player::getAttenuation() const {
            return stream->getAttenuation();
        }
        Player::~Player() {
            delete stream;
        }
    }
}
 

The class Player is made to play my custom stream.

« Last Edit: January 03, 2015, 06:33:14 pm by Lolilolight »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: Can't play several sounds at the same time. :/
« Reply #3 on: January 03, 2015, 06:37:05 pm »
In what way is this code minimal? In what way is this code complete? Do you really believe we want to spend our nights reading wall of codes?

If something is not clear in the response you get here, instead of barfing code all over the place, ask further information.

I can't believe that after 1172 posts you still don't know the rules of this forum... It's becoming insulting that every time you simply ignore our messages...
SFML / OS X developer

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Can't play several sounds at the same time. :/
« Reply #4 on: January 03, 2015, 06:53:03 pm »
I think I'll spend nights at checking at the code so if you don't want.

I can't make it more minimalist. :/
The stream and the player are the only things needed to play a sound.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Can't play several sounds at the same time. :/
« Reply #5 on: January 03, 2015, 07:09:32 pm »
The code is not complete because we can't copy-paste it into ideone or Visual Studio and run it ourselves.  That automatically means we can't do anything but guess what the problem might be.

The code is probably not minimal because most of the functions you've shown in Player and Stream are probably not relevant.  If you made an actual minimal example with just a main() function calling methods on Player/Stream, I doubt you would need that whole interface to reproduce the problem you're describing.  I also don't see why Player is necessary; Stream is the one actually playing sounds.

You've also failed to state what problem you're having.  Since your initial post contains compiler errors and very vague descriptions of runtime errors, which can't possibly be the same issue, we don't even know which problem you're trying to ask for help with.

An actual minimal and complete example that we could help you with would look more like this:
#include <SFML/Audio.hpp>

int main()
{
    sf::SoundBuffer buffer1;
    if (!buffer1.loadFromFile("sound1.wav"))
        return -1;

    sf::SoundBuffer buffer2;
    if (!buffer2.loadFromFile("sound2.wav"))
        return -1;

    sf::Sound sound1, sound2;
    sound1.setBuffer(buffer1);
    sound2.setBuffer(buffer2);

    sound1.play();
    sound2.play(); // I never hear this sound...

    return 0;
}
Provided you attached the sound files and told us some details about your sound card/audio drivers.



Please, at least try to make a help request that the SFML team can work with.  There's really nothing they can do with what you've posted so far, even if they had infinite patience.

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Can't play several sounds at the same time. :/
« Reply #6 on: January 03, 2015, 07:24:31 pm »
I've found the problem, the problem is that the class soundstream doesn't use a list like the soundbuffer class.

So I need to make a list by myself, for my custom stream. :/

I thougth I could reuse the class sound but it attempts a soundbuffer  but it doesn't seems that it's possible because of the compilation error.

I was just confounding the soundbuffer and the soundstream class, which looks like to be very similar.

« Last Edit: January 03, 2015, 07:26:56 pm by Lolilolight »

Lolilolight

  • Hero Member
  • *****
  • Posts: 1232
    • View Profile
Re: Can't play several sounds at the same time. :/
« Reply #7 on: January 03, 2015, 09:21:48 pm »
After some research, it seems the cause is the sound spacialization, when I desactivate it, it works, I'll try to post a minimal code here, because, I don't really understand why I heard nothing.

PS : I've got it! Then min distance was too slow. :/
« Last Edit: January 03, 2015, 09:34:41 pm by Lolilolight »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: [SOLVED]Can't play several sounds at the same time. :/
« Reply #8 on: January 03, 2015, 10:39:09 pm »
Next time you have a problem could you please spend a minimum of 48hrs working on it before posting?
Seriously, that would make me (and I think, others) very happy.
Thank you in advance.

 

anything