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