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

Author Topic: sf::Sound derivative not playing  (Read 2709 times)

0 Members and 1 Guest are viewing this topic.

natchos

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
    • Email
sf::Sound derivative not playing
« on: May 19, 2014, 07:21:16 pm »
I am trying to make an sound manager, from where I can play sounds through a system of IDs, for this I created a child class(Called ironically Sound_Struct). The problem I'm running into is that the sound I have loaded into it doesn't play. I have checked the file itself(through use of sf::Sound), aswell as the sf::SoundBuffer vector where I load/store the buffers. Here is the code, if someone could help that would be wonderful.

SoundManager.hpp
#pragma once
#include <SFML\Audio.hpp>
#include <list>
#include <string>
#include <iomanip>

class Sound_Struct : public sf::Sound
{
public:
        Sound_Struct(sf::SoundBuffer S_BUFF_SRC, std::string n_designation, int n_id);
        Sound_Struct(std::string n_designation, int n_id);
        void set_ID(int n_id){this->m_id = n_id;}
        int get_ID(){return this->m_id;}
        void set_Name(std::string n_designation){this->m_designation = n_designation;}
        std::string get_Name(){return this->m_designation;}
private:
        std::string m_designation;
        int m_id;
};

struct Music_Struct : public sf::Music
{
public:
        Music_Struct(std::string m_filename, std::string n_designation, int n_id);
        void set_ID(int n_id){this->m_id = n_id;}
        int get_ID(){return this->m_id;}
        void set_Name(std::string n_designation){this->m_designation = n_designation;}
        std::string get_Name(){return this->m_designation;}
private:
        std::string m_designation;
        int m_id;
};
class Sound_Manager
{
public:

        Sound_Manager();
        bool Add_Sound_Buffer(std::string m_filename); //Adds an sound_buffer into the Sound_Buffer list. Used for initial setup.

        bool Add_Sound(std::string m_filename, std::string m_designation, int m_id);
        void Add_Sound(sf::SoundBuffer m_S_BUFF, std::string m_designation, int m_id);
        void Add_Sound(sf::Sound* m_Sound, std::string m_designation, int m_id);
        void Add_Sound(int S_BUFF_ID, std::string m_designation, int m_id);
        void Add_Music(std::string m_filename, std::string m_designation, int m_id);
        void Delete_Sound(int m_id);
        void Delete_Sound(std::string m_designation);

        sf::SoundBuffer getBuffer(int ID);

        void Play_Sound(std::string m_designation);
        void Play_Sound(int m_id);
        void Play_Music(std::string m_designation);
        void Play_Music(int m_id);
        void Pause_All();
        void Resume_Previous();

        int no_Of_Sounds()
        {
                return this->Sounds.size();
        };
        int no_Of_Buffers()
        {
                return this->Sound_Buffers.size();
        };
public:
        std::list<Sound_Struct> Sounds; //Since sounds are given unique IDs, and likely change during runtime these are placed in a list
        std::vector<sf::SoundBuffer> Sound_Buffers; //Unchanging after startup, so placed in a vector
        std::vector<Music_Struct*> Songs; //Unchanging after startup, so placed in a vector
};

SoundManager.cpp (only including the functions that are relevant)
Sound_Struct::Sound_Struct(sf::SoundBuffer S_BUFF_SRC, std::string n_designation, int n_id) : sf::Sound(S_BUFF_SRC)
{
        this->m_designation = n_designation;
        this->m_id = n_id;
}
Sound_Manager::Sound_Manager()
{
        this->Songs.clear();
        this->Sounds.clear();
        this->Sound_Buffers.clear();
}
bool Sound_Manager::Add_Sound_Buffer(std::string m_filename)
{
        sf::SoundBuffer S_BUFF;
        if(S_BUFF.loadFromFile(m_filename))
        {
                this->Sound_Buffers.push_back(S_BUFF);
                return true;
        }
        else
                return false;
}
bool Sound_Manager::Add_Sound(std::string m_filename, std::string m_designation, int m_id)
{
        sf::SoundBuffer S_BUFF;
        if(S_BUFF.loadFromFile(m_filename))
        {
                this->Sound_Buffers.push_back(S_BUFF);
                Sound_Struct ne_Sound(S_BUFF,m_designation,m_id);
                this->Sounds.push_back(ne_Sound);
                return true;
        }
        else
                return false;
}
void Sound_Manager::Add_Sound(sf::SoundBuffer m_S_BUFF, std::string m_designation, int m_id)
{
        this->Sound_Buffers.push_back(m_S_BUFF);
        Sound_Struct ne_Sound(m_S_BUFF, m_designation, m_id);
        this->Sounds.push_back(ne_Sound);
}
void Sound_Manager::Play_Sound(std::string m_designation)
{
        std::list<Sound_Struct>::iterator iter;
        for(iter = this->Sounds.begin(); iter != this->Sounds.end(); iter++)
        {
                if(iter->get_Name() == m_designation)
                        iter->play();
        }
}
void Sound_Manager::Play_Sound(int m_id)
{
        std::list<Sound_Struct>::iterator iter;
        for(iter = this->Sounds.begin(); iter != this->Sounds.end(); iter++)
        {
                if(iter->get_ID() == m_id)
                        iter->play();
        }
}

The actual loading/playing is done in the main file

        if(!Miss_Lisa.Add_Sound_Buffer("Sounds/Laser_Shot.wav"))
        {
                return;
        }
        Miss_Lisa.Add_Sound(0,"Laser_Shot",1);
//The sound can now be played by calling
Miss_Lisa.play_Sound(1)
« Last Edit: May 19, 2014, 07:24:18 pm by natchos »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Sound derivative not playing
« Reply #1 on: May 19, 2014, 07:42:38 pm »
I made a small "Jukebox" class a while ago that I believe does something pretty close to what you want. I'll see if I can find the time to post it to the wiki later tonight and add a link here.. Gimme a couple of hours.

Edit: Ok, so I put it up on the wiki here: https://github.com/SFML/SFML/wiki/Source:-Jukebox
I hope it's useful to you.
Feel free to comment on/criticize the code.
« Last Edit: May 19, 2014, 09:21:01 pm by Jesper Juhl »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: sf::Sound derivative not playing
« Reply #2 on: May 20, 2014, 08:03:28 am »
If you run a soind example that comes with SFML do you hear the sound?

Also deriving might not be the best to do, a composition (object within the class) should turn out as a better design.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything