project-kyoku/tools/src/music.cpp

59 lines
1.3 KiB
C++
Raw Normal View History

#include "tools/music.h"
Music::Music() :
_sfml_music_offset(0),
_previous_frame_offset(0),
_absolute_offset(0)
{}
bool Music::openFromFile(const std::string& filepath)
{
return _music.openFromFile(filepath);
}
void Music::play()
{
_music.play();
_sfml_music_offset = _offset_interpolator.restart().asMicroseconds();
}
void Music::pause()
{
_music.pause();
}
void Music::stop()
{
_music.stop();
}
void Music::setVolume(int volume)
{
_music.setVolume(volume);
}
void Music::setOffset(const microsec& offset)
{
2021-08-12 21:10:52 +02:00
//_previous_frame_offset += (offset - _absolute_offset);
_music.setPlayingOffset(sf::microseconds(offset));
}
microsec Music::fetchOffset()
{
if (_music.getStatus() != sf::Music::Status::Playing)
return _absolute_offset;
const auto interpolator_timestamp = _offset_interpolator.getElapsedTime().asMicroseconds();
const auto sfml_new_offset = _music.getPlayingOffset().asMicroseconds();
_absolute_offset += (interpolator_timestamp - _previous_frame_offset);
_previous_frame_offset = interpolator_timestamp;
if (sfml_new_offset != _sfml_music_offset)
{
_absolute_offset = ((_absolute_offset + sfml_new_offset) / 2);
_sfml_music_offset = sfml_new_offset;
}
return _absolute_offset;
}