diff --git a/src/player.cpp b/src/player.cpp index 2963ba0..346894b 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -52,7 +52,7 @@ using std::endl; #include #include - +#include #include using filter_map_t = std::unordered_map; @@ -774,7 +774,6 @@ bool ConsolePlayer::createSidEmu (SIDEMUS emu, const SidTuneInfo *tuneInfo) return false; } - bool ConsolePlayer::open (void) { if ((m_state & ~playerFast) == playerRestart) @@ -810,22 +809,19 @@ bool ConsolePlayer::open (void) return false; } + const bool isNTSC = ( + (m_engCfg.defaultC64Model == SidConfig::NTSC) && + (m_engCfg.forceC64Model || (tuneInfo->clockSpeed() != SidTuneInfo::CLOCK_PAL)) + ) || + (tuneInfo->clockSpeed() == SidTuneInfo::CLOCK_NTSC); + #ifdef FEAT_FILTER_DISABLE m_engine.filter(0, m_filter.enabled); m_engine.filter(1, m_filter.enabled); m_engine.filter(2, m_filter.enabled); #endif #ifdef FEAT_REGS_DUMP_SID - if ( - ( - (m_engCfg.defaultC64Model == SidConfig::NTSC) && - (m_engCfg.forceC64Model || (tuneInfo->clockSpeed() != SidTuneInfo::CLOCK_PAL)) - ) || - (tuneInfo->clockSpeed() == SidTuneInfo::CLOCK_NTSC) - ) - m_freqTable = freqTableNtsc; - else - m_freqTable = freqTablePal; + m_freqTable = isNTSC ? freqTableNtsc : freqTablePal; #endif // Start the player. Do this by fast // forwarding to the start position @@ -884,13 +880,25 @@ bool ConsolePlayer::open (void) // Update display menu(); - updateDisplay(); + + // Update display at 50/60Hz + int delay = isNTSC ? 16 : 20; + m_thread = new std::thread([this](int delay) + { + while (m_state != playerStopped) + { + if (m_state == playerRunning) + updateDisplay(); + std::this_thread::sleep_for(std::chrono::milliseconds(delay)); + } + }, delay); + return true; } void ConsolePlayer::close () { - m_engine.stop(); + stop(); if (m_state == playerExit) { // Natural finish emuflush (); @@ -917,6 +925,12 @@ void ConsolePlayer::close () cerr << endl; #endif } + + if (m_thread) + { + m_thread->join(); + delete m_thread; + } } // Flush any hardware sid fifos so all music is played @@ -946,8 +960,6 @@ bool ConsolePlayer::play() uint_least32_t frames = 0; if (m_state == playerRunning) { - updateDisplay(); - // Fill buffer short *buffer = m_driver.selected->buffer(); // getBufSize returns the number of frames @@ -1064,7 +1076,6 @@ uint_least32_t ConsolePlayer::getBufSize() } -// External Timer Event void ConsolePlayer::updateDisplay() { #ifdef FEAT_NEW_SONLEGTH_DB diff --git a/src/player.h b/src/player.h index 399d06f..d5416ee 100644 --- a/src/player.h +++ b/src/player.h @@ -41,6 +41,8 @@ #include #include +#include +#include #ifdef HAVE_TSID # if HAVE_TSID > 1 @@ -146,7 +148,9 @@ class ConsolePlayer sidplayfp m_engine; SidConfig m_engCfg; SidTune m_tune; - player_state_t m_state; + + std::atomic m_state; + const char* m_outfile; std::string m_filename; @@ -184,6 +188,8 @@ class ConsolePlayer int m_precision; int m_buffer_size; + std::thread *m_thread = nullptr; + struct m_filter_t { // Filter parameter for reSID @@ -213,7 +219,7 @@ class ConsolePlayer struct m_timer_t { // secs uint_least32_t start; - uint_least32_t current; + std::atomic current; uint_least32_t stop; uint_least32_t length; bool valid;