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

Author Topic: sf::Time / sf::Audio Operations [Solved]  (Read 3623 times)

0 Members and 1 Guest are viewing this topic.

Kain Nobel

  • Newbie
  • *
  • Posts: 38
    • View Profile
sf::Time / sf::Audio Operations [Solved]
« on: January 16, 2022, 12:11:29 pm »
Hello and good day everybody!

What I'm trying to accomplish is fading in and fading out of sf::Audio related members utilizing the sf::Time functionality. The current code I have does not use the sf::Time (yet), only generic floats. I don't know how to calculate the volume increments I need based on sf::seconds, sf::milliseconds or sf::microseconds.

Here is the sample header and source code, with much noise (no pun intended) omitted. I made sure to comment each and every line so you can follow along with what I'm trying to do.

GameAudio.hpp
Code: [Select]
#ifndef GAME_AUDIO_H
#define GAME_AUDIO_H

#include <Game.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System/Time.hpp>

class GameAudio {
    public:
        // Construct and Destruct
        GameAudio();
        virtual ~GameAudio();
        // Public Functions
        void fadeBGM(float volume, float duration);
        void update();
// Public Member Variables
        sf::Music m_BGM;        // Background Music
    protected:

    private:
        // Private Member Variables
        float     m_BGMFadeV    = 0.0; // BGM Fade Volume
        float     m_BGMFadeD    = 0.0; // BGM Fade Duration
// Private functions
        void updateBGM();
        // ...Need to add limited vector for sound queue
};

#endif // GAME_AUDIO_H

GameAudio.cpp
Code: [Select]
#include <Game.hpp>
#include <GameAudio.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System/Time.hpp>

/*----------------------------------------------------------------------------*/
// ** GameAudio.fadeBGM(volume, duration, mode)
/*----------------------------------------------------------------------------*/
void GameAudio::fadeBGM(float volume, float duration) {
    // Set BGM fade volume and duration
    m_BGMFadeV = volume;
    m_BGMFadeD = duration;
}
/*----------------------------------------------------------------------------*/
// ** GameAudio.update()
/*----------------------------------------------------------------------------*/
void GameAudio::update() {
/* ...Misc code... */
    updateBGM();
/* ...Misc code... */
}
/*----------------------------------------------------------------------------*/
// ** GameAudio.updateBGM()
/*----------------------------------------------------------------------------*/
void GameAudio::updateBGM() {
    // If fading BGM in
    if (m_BGMFadeV > m_BGM.getVolume()) {
        // Get new volume value
        float value = (m_BGM.getVolume() + 0.1);
        // Roll BGM in
        m_BGM.setVolume(value);
// If over limit
if (m_BGMFadeV < m_BGM.getVolume()) {
// Sync volume to stop process
m_BGMFadeV = m_BGM.getVolume();
// End function
return;
}
        // Print to console
        cout << "Fade BGM (Old Volume = " << m_BGM.getVolume() << ", ";
        cout << "New Volume = " << value << ")" << endl;
    // If fading BGM out
    } else if (m_BGMFadeV < m_BGM.getVolume()) {
        // Get new volume value
        float value = (m_BGM.getVolume() - 0.1);
        // Roll BGM out
        m_BGM.setVolume(value);
// If under limit
if (m_BGMFadeV > m_BGM.getVolume()) {
// Sync volume to stop process
m_BGMFadeV = m_BGM.getVolume();
// End function
return;
}
        // Print to console
        cout << "Fade BGM (Old Volume = " << m_BGM.getVolume() << ", ";
        cout << "New Volume = " << value << ")" << endl;
    }
}

This code does work for fading in and fading out but it uses generic floats and is frame based. I'd like to implement sf::Time and do something like GameAudio.fadeBGM(float volume, sf::seconds(3.0)) and make it time based. Obviously, I know I need to change the 2nd argument to use an sf::Time but how do I calculate something like m_BGM.setVolume(incremental volume change in seconds)?

Thank you to anybody who can help.
« Last Edit: January 18, 2022, 10:55:56 am by Kain Nobel »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Time / sf::Audio Operations
« Reply #1 on: January 18, 2022, 07:09:40 am »
You'd take a sf::Clock which gives you a way to track the passing of time. Then you can pass in the elapsed time to your fade function and multiple the fade value over time, with the elapsed time.

currenFadeValue = fadeValueOverTime * time.asSeconds()

Hope this helps
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kain Nobel

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: sf::Time / sf::Audio Operations [Solved]
« Reply #2 on: January 18, 2022, 10:57:04 am »
Cool, thank you! Now I've got the smooth roll in / roll out that I was looking for ;)

 

anything