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

Author Topic: Audio cuts out or doesn't play at all [c++]  (Read 4030 times)

0 Members and 2 Guests are viewing this topic.

MrSnappingTurtle

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Audio cuts out or doesn't play at all [c++]
« on: December 20, 2014, 10:02:54 pm »
Hi,
I am trying to set up my audio system, however it seems to cut out early or sometimes not play at all. Do you guys see anything wrong with my set up?

#ifndef SOUNDMANAGER_H
#define SOUNDMANAGER_H
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <vector>
class SoundManager
{
public:
    SoundManager();
    void playSound(const std::string&);
    void update(const sf::Time&);
private:
   
    //------------------Buffers-----------------------
    //Pistol
    sf::SoundBuffer bufferPistolOne_;
    sf::SoundBuffer bufferPistolTwo_;
    sf::SoundBuffer bufferPistolThree_;
   
    //Rifle
    sf::SoundBuffer bufferRifleOne_;
    sf::SoundBuffer bufferRifleTwo_;
    sf::SoundBuffer bufferRifleThree_;
    sf::SoundBuffer bufferRifleFour_;
   
    //Grass footsteps
    sf::SoundBuffer bufferGrassOne_;
    sf::SoundBuffer bufferGrassTwo_;
    sf::SoundBuffer bufferGrassThree_;
    sf::SoundBuffer bufferGrassFour_;
    sf::SoundBuffer bufferGrassFive_;
    sf::SoundBuffer bufferGrassSix_;
    sf::SoundBuffer bufferGrassSeven_;
   
    std::vector<sf::Sound> vSounds_;
   
   
   
   
};

#endif

 


#include "SoundManager.h"
#include <iostream>
SoundManager::SoundManager()
{
    //----------------Loads all of the sounds--------------
    //Pistol
    if(!bufferPistolOne_.loadFromFile("sounds/gun/pistol/pistol_1.ogg"))
        std::cout << "Failed to load: sounds/gun/pistol/pistol_1.ogg" << std::endl;
   
    if(!bufferPistolTwo_.loadFromFile("sounds/gun/pistol/pistol_2.ogg"))
        std::cout << "Failed to load: sounds/gun/pistol/pistol_2.ogg" << std::endl;

    if(!bufferPistolThree_.loadFromFile("sounds/gun/pistol/pistol_3.ogg"))
        std::cout << "Failed to load: sounds/gun/pistol/pistol_3.ogg" << std::endl;

    //Rifle
    if(!bufferRifleOne_.loadFromFile("sounds/gun/rifle/rifle_1.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_1.ogg" << std::endl;

    if(!bufferRifleTwo_.loadFromFile("sounds/gun/rifle/rifle_2.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_2.ogg" << std::endl;
   
    if(!bufferRifleThree_.loadFromFile("sounds/gun/rifle/rifle_3.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_3.ogg" << std::endl;

    if(!bufferRifleFour_.loadFromFile("sounds/gun/rifle/rifle_4.ogg"))
        std::cout << "Failed to load: sounds/gun/rifle/rifle_4.ogg" << std::endl;

   
    //Grass
    if(!bufferGrassOne_.loadFromFile("sounds/footstep/grass/grass_1.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_1.ogg" << std::endl;

    if(!bufferGrassTwo_.loadFromFile("sounds/footstep/grass/grass_2.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_2.ogg" << std::endl;

    if(!bufferGrassThree_.loadFromFile("sounds/footstep/grass/grass_3.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_3.ogg" << std::endl;

    if(!bufferGrassFour_.loadFromFile("sounds/footstep/grass/grass_4.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_4.ogg" << std::endl;

    if(!bufferGrassFive_.loadFromFile("sounds/footstep/grass/grass_5.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_5.ogg" << std::endl;

    if(!bufferGrassSix_.loadFromFile("sounds/footstep/grass/grass_6.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_6.ogg" << std::endl;

    if(!bufferGrassSeven_.loadFromFile("sounds/footstep/grass/grass_7.ogg"))
        std::cout << "Failed to load: sounds/footstep/grass/grass_7.ogg" << std::endl;


}
void SoundManager::update(const sf::Time& dT)
{
    for(auto iSound = vSounds_.begin(); iSound != vSounds_.end();)
        if(iSound->getStatus() == sf::Sound::Stopped)
            iSound = vSounds_.erase(iSound);
        else
            ++iSound;
   
}
void SoundManager::playSound(const std::string& type)
{
    if(type == "pistol")
    {
        sf::Sound sound;
        int random = std::rand() % 3;
        if(random == 0)
            sound = sf::Sound(bufferPistolOne_);
        else if(random == 1)
            sound = sf::Sound(bufferPistolTwo_);
        else
            sound = sf::Sound(bufferPistolThree_);
       
        vSounds_.push_back(sound);
        vSounds_.at(vSounds_.size() - 1).play();
    }
    else if(type == "rifle")
    {
        sf::Sound sound;
        int random = std::rand() % 4;
        if(random == 0)
            sound = sf::Sound(bufferRifleOne_);
        else if(random == 1)
            sound = sf::Sound(bufferRifleTwo_);
        else if(random == 2)
            sound = sf::Sound(bufferRifleThree_);
        else
            sound = sf::Sound(bufferRifleFour_);
       
        vSounds_.push_back(sound);
        vSounds_.at(vSounds_.size() - 1).play();
    }
    else if(type == "grass")
    {

        sf::Sound sound;
        int random = std::rand() % 7;
        if(random == 0)
            sound = sf::Sound(bufferGrassOne_);
        else if(random == 1)
            sound = sf::Sound(bufferGrassTwo_);
        else if(random == 2)
            sound = sf::Sound(bufferGrassThree_);
        else if(random == 3)
            sound = sf::Sound(bufferGrassFour_);
        else if(random == 4)
            sound = sf::Sound(bufferGrassFive_);
        else if(random == 5)
            sound = sf::Sound(bufferGrassSix_);
        else
            sound = sf::Sound(bufferGrassSeven_);
   
       
        vSounds_.push_back(sound);
        vSounds_.at(vSounds_.size() - 1).play();
    }
}
 


Thanks! :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Audio cuts out or doesn't play at all [c++]
« Reply #1 on: December 20, 2014, 10:08:03 pm »
Everything looks good.

What's the maximum number of sounds that are played simultaneously? Have you tried with WAV souds instead of OGG/Vorbis?

By the way, your update functions is quite inefficient. You'd better use the erase-remove idiom.
Laurent Gomila - SFML developer

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Audio cuts out or doesn't play at all [c++]
« Reply #2 on: December 21, 2014, 04:18:28 am »
I have hit this problem before and, luckily for you, I found the answer!  ;)

Your sounds are stored in a std::vector. Elements in a vector can often be re-arranged automatically, especially when using erase(). This can interfere with the sf::Sound object, which stops it.
The solution is to use storage that gets left alone, such as a std::list.

Also, use .back() rather than .at(.size() - 1)
« Last Edit: December 21, 2014, 04:21:05 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

MrSnappingTurtle

  • Newbie
  • *
  • Posts: 21
    • View Profile
    • Email
Re: Audio cuts out or doesn't play at all [c++]
« Reply #3 on: December 21, 2014, 06:32:15 am »

The solution is to use storage that gets left alone, such as a std::list.

Also, use .back() rather than .at(.size() - 1)

Thanks! That fixed it!

Quote
By the way, your update functions is quite inefficient. You'd better use the erase-remove idiom.

Cool, I didn't know that those were actually more efficient. I just thought they were convenient.

EDIT: I actually can't use std::remove  because it will end up causing the same problem we just fixed because it reorganizes the list. I had to revert that part.
« Last Edit: December 21, 2014, 08:02:42 am by MrSnappingTurtle »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Audio cuts out or doesn't play at all [c++]
« Reply #4 on: December 21, 2014, 09:39:23 am »
Ok, if your code works then I'd say don't touch it. But in case you want to optimize it or make it less error-prone anyway, here are two strategies that you could follow:

1. Since sounds cannot be copied nor moved around, storing (smart) pointers to dynamically allocated instances would solve all the issues that you have with the way you store them; this would relax constraints on what type of container you use, and what operations you can do on it.

2. Instead of constantly creating and deleting sounds, you could just reuse those that are stopped.
Laurent Gomila - SFML developer

 

anything