fix: Fix overlaying of music and movie audio

This commit is contained in:
Vsevolod Kremianskii 2020-11-09 09:50:12 +07:00
parent 4f6ad28edd
commit edd7c9b0bb
7 changed files with 24 additions and 13 deletions

View file

@ -63,7 +63,7 @@ void AudioPlayer::threadStart() {
auto it = remove_if(
_sounds.begin(),
_sounds.end(),
[](const shared_ptr<SoundInstance> &sound) { return sound->stopped(); });
[](const shared_ptr<SoundInstance> &sound) { return sound->isStopped(); });
_sounds.erase(it, _sounds.end());
@ -105,10 +105,9 @@ shared_ptr<SoundInstance> AudioPlayer::play(const shared_ptr<AudioStream> &strea
if (!stream) {
throw invalid_argument("Audio stream is empty");
}
bool loop = type == AudioType::Music;
float gain = getVolume(type) / 100.0f;
shared_ptr<SoundInstance> sound(new SoundInstance(stream, loop, gain));
shared_ptr<SoundInstance> sound(new SoundInstance(stream, false, gain));
lock_guard<recursive_mutex> lock(_soundsMutex);
_sounds.push_back(sound);

View file

@ -115,7 +115,7 @@ void SoundInstance::stop() {
_state = State::Stopped;
}
bool SoundInstance::stopped() const {
bool SoundInstance::isStopped() const {
return _state == State::Stopped;
}

View file

@ -37,7 +37,7 @@ public:
void update();
void stop();
bool stopped() const;
bool isStopped() const;
int duration() const;
private:

View file

@ -121,9 +121,12 @@ void Game::playVideo(const string &name) {
_video = bik.video();
if (_music && !_music->isStopped()) {
_music->stop();
}
shared_ptr<AudioStream> audio(_video->audio());
if (audio) {
AudioPlayer::instance().play(audio, AudioType::Movie);
_movieAudio = AudioPlayer::instance().play(audio, AudioType::Movie);
}
}
@ -148,12 +151,12 @@ string Game::getMainMenuMusic() const {
}
void Game::playMusic(const string &resRef) {
if (_music) {
if (_musicResRef == resRef) return;
if (_music && !_music->isStopped()) {
_music->stop();
}
if (!resRef.empty()) {
_music = ::playMusic(resRef);
}
_musicResRef = resRef;
}
void Game::loadMainMenu() {
@ -390,6 +393,8 @@ void Game::update() {
if (_video->isFinished()) {
_video.reset();
}
} else if (!_musicResRef.empty() && (!_music || _music->isStopped())) {
_music = ::playMusic(_musicResRef);
}
if (!_nextModule.empty()) {

View file

@ -155,7 +155,6 @@ private:
std::unique_ptr<ObjectFactory> _objectFactory;
GameScreen _screen { GameScreen::MainMenu };
Party _party;
std::shared_ptr<audio::SoundInstance> _music;
uint32_t _ticks { 0 };
bool _quit { false };
std::shared_ptr<video::Video> _video;
@ -186,6 +185,14 @@ private:
// END GUI
// Audio
std::string _musicResRef;
std::shared_ptr<audio::SoundInstance> _music;
std::shared_ptr<audio::SoundInstance> _movieAudio;
// END Audio
// Globals/locals
std::map<std::string, bool> _globalBooleans;

View file

@ -437,7 +437,7 @@ void Dialog::update(float dt) {
endCurrentEntry();
}
} else if (endOnAudioStop) {
bool stopped = _currentVoice && _currentVoice->stopped();
bool stopped = _currentVoice && _currentVoice->isStopped();
if (stopped) {
endCurrentEntry();
}

View file

@ -67,7 +67,7 @@ void Program::initOptions() {
("soundvol", po::value<int>()->default_value(kDefaultSoundVolume), "sound volume in percents")
("movievol", po::value<int>()->default_value(kDefaultMovieVolume), "movie volume in percents")
("port", po::value<int>()->default_value(kDefaultMultiplayerPort), "multiplayer port number")
("debug", po::value<int>()->default_value(0), "debug level (0-3)");
("debug", po::value<int>()->default_value(0), "debug log level (0-3)");
_cmdLineOpts.add(_commonOpts).add_options()
("help", "print this message")