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

Author Topic: music sound lowered  (Read 2012 times)

0 Members and 1 Guest are viewing this topic.

djarkan

  • Newbie
  • *
  • Posts: 12
    • View Profile
music sound lowered
« on: May 08, 2022, 02:46:55 pm »
hi

when i play a sound the music volume is lowered and after few seconds upping to the old volume

music.hpp
#ifndef    __music__
#define    __music__

#include <SFML/Audio.hpp>
#include <string>

class music{
public:
    bool initMusic();
    void setMusicVolume(const float volume);
    void playSong();
    void stopSong();

    sf::Music m_music;

private:
    bool loadSong(std::string title);
    void setMusicLoop(bool loop);


};
#endif

music.cpp
#include "music.hpp"

bool music::loadSong(std::string title)
{
    return m_music.openFromFile(title);
}

void music::setMusicVolume(const float volume)
{
    m_music.setVolume(volume);
}

void music::setMusicLoop(bool loop)
{
    m_music.setLoop(loop);
}

bool music::initMusic()
{
    bool flag = loadSong("A Theme.flac");
    setMusicLoop(true);
    return flag;
}

void music::playSong()
{
    m_music.play();
}

void music::stopSong()
{
    m_music.stop();
}
 

sound.hpp

#ifndef    __sound__
#define    __sound__

#include <SFML/Audio.hpp>
#include <string>
#include <vector>
#include <memory>

class sound{
public:
 //   sound();
    void initSounds();
    void playSound(const int thatSound);
    void setSoundVolume(const float volume);

private:
    bool loadFileSounds();
    void assignSoundsToBuffers();

    std::array<sf::SoundBuffer, 15> m_soundBuffer;
    std::array<sf::Sound, 15> m_sound;
};
#endif

sound.cpp

#include "sound.hpp"
#include <array>
#include <iostream>

bool sound::loadFileSounds()
{
    bool flag{true};
    bool flag1 = m_soundBuffer[0].loadFromFile("sound\\harddrop.ogg");
    flag1 = m_soundBuffer[1].loadFromFile("sound\\rotation.ogg");
    flag1 = m_soundBuffer[2].loadFromFile("sound\\softdrop.ogg");
    flag1 = m_soundBuffer[3].loadFromFile("sound\\ouverturemenu.ogg");
    flag1 = m_soundBuffer[4].loadFromFile("sound\\levelchgt.ogg");
    flag1 = m_soundBuffer[5].loadFromFile("sound\\complitedlines.ogg");
    flag1 = m_soundBuffer[6].loadFromFile("sound\\departfinpartie.ogg");
}

void sound::assignSoundsToBuffers()
{
    for(auto i = 0; i < 15; ++i){
        m_sound[i].setBuffer(m_soundBuffer[i]);
    }
}

void sound::initSounds()
{
    loadFileSounds();
    assignSoundsToBuffers();
}

void sound::setSoundVolume(const float volume)
{
    for(auto i = 0; i < 15; ++i){
        m_sound[i].setVolume(volume);
    }
}

void sound::playSound(const int thatSound)
{
    m_sound[thatSound].play();
}
 

both volume are initialized once
m_jsonSetup.loadJsonFile("setup.json");
    gameSound.initSounds();
    gameMusic.initMusic();
    setSetUp();
    setSoundVolume(m_setup.soundVolume);
    setMusicVolume(m_setup.musicVolume);

thx
« Last Edit: May 08, 2022, 03:04:24 pm by djarkan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: music sound lowered
« Reply #1 on: May 09, 2022, 03:21:43 pm »
Please describe your problem or question clearly.

With the few words posted here, it's not even clear if you're looking for a solution or are reporting an issue with your code. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

djarkan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: music sound lowered
« Reply #2 on: May 09, 2022, 06:03:14 pm »
the music begins
and when a soud is played, the music "volume" is lowered .
after few seconds the musoic "volume" up to the normal.
the set music volume is only set at the beginning
so i don t understand why the music is low then re-up

 

fallahn

  • Sr. Member
  • ****
  • Posts: 492
  • Buns.
    • View Profile
    • Trederia
Re: music sound lowered
« Reply #3 on: May 10, 2022, 10:55:24 am »
It's an automatic gain control applied by OpenAL to try to make sure that the total volume doesn't exceed an internal limit and start to distort. It can even vary between speaker setups, I've noticed the effect is much more noticeable with 2 channels than it is for surround.

I've experimented a bit and found that the best way to try and counteract the effect is to lower the gain of OpenAL sources - which is the same as setting their volume in SFML. As a starting point assuming that the internal max gain is 100, set both of your sources to 50. If you play both at once you should find that the music shouldn't get as quiet, if at all, when both sounds play, because the total gain is <= max gain.

These numbers are a rough guess, and to find the perfect amount of gain with minimal quietening effect you'll have to experiment, but it should demonstrate what I mean. A good estimation for an average gain is to take the max gain and divide it by the number of sounds you expect to be playing at any one time.

Of course the overall volume will now sound quieter. This can be compensated for by increasing the listener volume, but it also helps to set the actual level of your audio files (in an audio editor) to as loud as possible (or at the very least make sure they all have the same level), then let OpenAL/SFML attenuate as needed.

HTH

djarkan

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: music sound lowered
« Reply #4 on: May 12, 2022, 01:25:45 pm »
tested for volume set to 13 for music and sound, and it s the same

changed the music in a sound by converting flac->ogg. it s the same

« Last Edit: May 12, 2022, 02:14:54 pm by djarkan »

 

anything