#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(); } bool Music::isPaused() const { return (_music.getStatus() != sf::Music::Playing); } void Music::setVolume(int volume) { _music.setVolume(volume); } void Music::setOffset(const microsec& offset) { //_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; } void Music::moveOffset(const microsec& delta) { const auto offset = fetchOffset(); const auto result = offset + delta; if (result < 0) { setOffset(0); } else if (result > getDuration()) { setOffset(_music.getDuration().asMicroseconds()); pause(); } else { setOffset(result); } } microsec Music::getDuration() const { return _music.getDuration().asMicroseconds(); }