project-kyoku/src/tools/musicsfml.cpp

59 lines
1.3 KiB
C++
Raw Normal View History

#include "musicsfml.h"
MusicSFML::MusicSFML() :
_sfml_music_offset(0),
_previous_frame_offset(0),
_absolute_offset(0)
{}
bool MusicSFML::openFromFile(const std::string& filepath)
{
return _music.openFromFile(filepath);
}
void MusicSFML::play()
{
_music.play();
_sfml_music_offset = _offset_interpolator.restart().asMicroseconds();
}
void MusicSFML::pause()
{
_music.pause();
}
void MusicSFML::stop()
{
_music.stop();
}
void MusicSFML::setVolume(int volume)
{
_music.setVolume(volume);
}
void MusicSFML::setOffset(const microsec& offset)
{
2021-08-12 15:10:52 -04:00
//_previous_frame_offset += (offset - _absolute_offset);
_music.setPlayingOffset(sf::microseconds(offset));
}
microsec MusicSFML::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;
}