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