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

Author Topic: Sound Class help please  (Read 3578 times)

0 Members and 1 Guest are viewing this topic.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Sound Class help please
« on: April 19, 2012, 10:38:35 am »
Hi, I am a new at programming.. I hope you can help such simple questions :) Oh well we all gotta start somewhere.

I know this isn't working.. and IU have tried many other thigns. this code is ment to explain what I want to do with it or how I think it should work...

#ifndef SFX_H
#define SFX_H

#include <SFML/Audio.hpp>
#include "GeneralHeaders.h"

class sfx{
        sf::Sound sfxERROR;
        sf::Sound sxfHIT;
        sf::SoundBuffer Buffer;
public:
        sfx();
        void sError();
        void sHit();
};

#endif
 


#include "sfx.h"
using namespace std;
using namespace sf;

sfx::sfx(){
        if (!Buffer.LoadFromFile("Resource/Sound/error.ogg"))
        {
                cout << "File not Found - Press enter to Continue..." << endl;
                cin.ignore(10000, '\n');
        }
        sfxERROR.GetBuffer(Buffer);

        if (!Buffer.LoadFromFile("Resource/Sound/hit.ogg.ogg"))
        {
                cout << "File not Found - Press enter to Continue..." << endl;
                cin.ignore(10000, '\n');
        }
        sfxHIT.GetBuffer(Buffer);


}

void sfx::sError()
{
        sfxERROR.Play();
}

void sfx::sHit()
{
        sxfHIT.Play();
}
 

int main(){

sfx SoundTest;

SountTest.sError();
SoundTest.sHit();

cin.ignore(10000, '\n');
}
 

Thanks in advance... I would also like ot be able to call this object global some how so I can call a soudn from anywhere in my progrema....

The idea is to have a class, and the constructor load into a variable all the sounds I need, and then have a simple way to play them from anywhere...

Am i completely about his wrong.. suggestions would help..

basically I have a game it is pretty simple and just want to add spots to play sounds..
« Last Edit: April 19, 2012, 10:45:20 am by aNewHobby »
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: Sound Class help please
« Reply #1 on: April 19, 2012, 10:50:29 am »
also tried this

#include "sfx.h"
using namespace std;
using namespace sf;

sfx::sfx(){}

void sfx::sError()
{
        SoundBuffer Buffer;
        if (!Buffer.LoadFromFile("Resource/Sound/error.ogg"))
        {
                cout << "File not Found - Press enter to Continue..." << endl;
                cin.ignore(10000, '\n');
        }

        // Display sound informations
        std::cout << "error.ogg :" << std::endl;
        std::cout << " " << Buffer.GetDuration()      << " sec"           << std::endl;
        std::cout << " " << Buffer.GetSampleRate()    << " samples / sec" << std::endl;
        std::cout << " " << Buffer.GetChannelsCount() << " channels"      << std::endl;

        sf::Sound sfxERROR(Buffer);
        sfxERROR.Play();
}

void sfx::sHit()
{
        SoundBuffer Buffer2;
        if (!Buffer2.LoadFromFile("Resource/Sound/hit.ogg"))
        {
                cout << "File not Found - Press enter to Continue..." << endl;
                cin.ignore(10000, '\n');
        }

        // Display sound informations
        std::cout << "hit.ogg :" << std::endl;
        std::cout << " " << Buffer2.GetDuration()      << " sec"           << std::endl;
        std::cout << " " << Buffer2.GetSampleRate()    << " samples / sec" << std::endl;
        std::cout << " " << Buffer2.GetChannelsCount() << " channels"      << std::endl;

        sf::Sound sxfHIT(Buffer2);
        sxfHIT.Play();
}
 

If i just put the code in main they play..
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Sound Class help please
« Reply #2 on: April 19, 2012, 01:39:28 pm »
You need to read about scopes and lifetimes of variables.

You crate the Buffer with in the constructor and you want to play it in another function.
But the buffer is a resource and holds the information on how to play a sound. If you now create it in a function and then leave this function, the buffer gets destroyed and the resource free, thus when you try to play the sound, there is no resource to access.

Define the variable as member of the the class and it should work.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: Sound Class help please
« Reply #3 on: April 19, 2012, 03:37:30 pm »
Define the variable as member of the the class and it should work.

Isn't that what I am doing in the 1st example...

#ifndef SFX_H
#define SFX_H

#include <SFML/Audio.hpp>
#include "GeneralHeaders.h"

class sfx{
        sf::SoundBuffer Buffer;
public:
        sf::Sound sfxERROR;
        sf::Sound sxfHIT;
        sfx();
        void sError();
};

#endif

#include "sfx.h"
using namespace std;
using namespace sf;

sfx::sfx(){
/*
        if (!Buffer.LoadFromFile("Resource/Sound/error.ogg"))
        {
                cout << "File not Found - Press enter to Continue..." << endl;
                cin.ignore(10000, '\n');
        }
        sfxERROR.ResetBuffer();*/

}

void sfx::sError()
{
        if (!Buffer.LoadFromFile("Resource/Sound/error.ogg"))
        {
                cout << "File not Found - Press enter to Continue..." << endl;
                cin.ignore(10000, '\n');
        }

        // Display sound informations
        std::cout << "error.ogg :" << std::endl;
        std::cout << " " << Buffer.GetDuration()      << " sec"           << std::endl;
        std::cout << " " << Buffer.GetSampleRate()    << " samples / sec" << std::endl;
        std::cout << " " << Buffer.GetChannelsCount() << " channels"      << std::endl;

        sfxERROR.GetBuffer(); //????
        sfxERROR.Play();
}

but I can not work out how to do the Sound thing.. like the buffer test thing works.. as it shows all the info from the buffer... I just can not work out how to put it into the variable?
I have tried

sfxERRROR.getbuffer();
sfxERRROR.getbuffer(Buffer);

Sound sfxERROR(Buffer) doesn't work as it is declared in the class....

Any ideas?

Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sound Class help please
« Reply #4 on: April 19, 2012, 03:45:01 pm »
SetBuffer
Laurent Gomila - SFML developer

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Re: Sound Class help please
« Reply #5 on: April 19, 2012, 03:45:55 pm »
SetBuffer

sfxERROR.SetBuffer(Buffer);

Thanks guys!
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Sound Class help please
« Reply #6 on: April 19, 2012, 06:04:18 pm »
Isn't that what I am doing in the 1st example...

Oh you actually did, I was kinda confused because of the diffrent examples. ;D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything