more_precise_music test

This commit is contained in:
Stepland 2022-02-16 01:52:32 +01:00
parent 3503399ed4
commit c224f6645a
3 changed files with 78 additions and 0 deletions

View File

@ -11,4 +11,11 @@ executable(
'mouse_events.cpp',
dependencies: deps,
include_directories: inc,
)
executable(
'more_precise_music',
'more_precise_music.cpp',
dependencies: deps + [dependency('openal')],
include_directories: inc,
)

View File

@ -0,0 +1,53 @@
#include "more_precise_music.hpp"
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
MorePreciseMusic::MorePreciseMusic(const std::filesystem::path& path) {
if (not alIsExtensionPresent("AL_SOFT_source_latency")) {
throw std::runtime_error("Error: AL_SOFT_source_latency not supported");
}
(alGetSourcedvSOFT) = reinterpret_cast<LPALGETSOURCEDVSOFT>(alGetProcAddress("alGetSourcedvSOFT"));
if (not this->openFromFile(path.string())) {
throw std::runtime_error("Could not open "+path.string());
}
}
bool MorePreciseMusic::onGetData(sf::SoundStream::Chunk& data) {
auto result = sf::Music::onGetData(data);
time_since_last_mix.restart();
ALdouble offsets[2];
alGetSourcedvSOFT(m_source, AL_SEC_OFFSET_LATENCY_SOFT, offsets);
open_al_playback_position_from_lag = sf::seconds(offsets[0]);
open_al_lag = sf::seconds(offsets[1]);
ALfloat secs = 0.f;
alGetSourcef(m_source, AL_SEC_OFFSET, &secs);
open_al_offset = sf::seconds(secs);
return result;
}
int main(int argc, char** argv) {
std::vector<std::string> args = {argv, argv+argc};
if (args.size() != 2) {
std::cout << "Usage : " << args[0] << " [file]" << std::endl;
return -1;
}
auto music = MorePreciseMusic{args[1]};
music.play();
std::cout << "getPlayingOffset, time_since_last_mix, open_al_playback_position_from_lag, open_al_lag, open_al_offset" << std::endl;
for (int i = 0; i < 5000; i++) {
//while (music.getStatus() == sf::Music::Playing) {
std::cout << music.getPlayingOffset().asMicroseconds() << ", ";
std::cout << music.time_since_last_mix.getElaspedTime().asMicroseconds() << ", ";
std::cout << music.open_al_playback_position_from_lag.asMicroseconds() << ", ";
std::cout << music.open_al_lag.asMicroseconds() << ", ";
std::cout << music.open_al_offset.asMicroseconds() << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

View File

@ -0,0 +1,18 @@
#include <filesystem>
#include <SFML/Audio.hpp>
#include "AL/al.h"
#include "AL/alext.h"
struct MorePreciseMusic : sf::Music {
MorePreciseMusic(const std::filesystem::path& path);
sf::Clock time_since_last_mix;
sf::Time open_al_playback_position_from_lag = sf::Time::Zero;
sf::Time open_al_lag = sf::Time::Zero;
sf::Time open_al_offset = sf::Time::Zero;
bool lag_measured = false;
LPALGETSOURCEDVSOFT alGetSourcedvSOFT;
protected:
bool onGetData(sf::SoundStream::Chunk& data) override;
};