Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a separate thread to update the display (#57) #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ using std::endl;
#include <sidplayfp/SidInfo.h>
#include <sidplayfp/SidTuneInfo.h>


#include <chrono>
#include <unordered_map>

using filter_map_t = std::unordered_map<std::string, double>;
Expand Down Expand Up @@ -774,7 +774,6 @@ bool ConsolePlayer::createSidEmu (SIDEMUS emu, const SidTuneInfo *tuneInfo)
return false;
}


bool ConsolePlayer::open (void)
{
if ((m_state & ~playerFast) == playerRestart)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ();
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1064,7 +1076,6 @@ uint_least32_t ConsolePlayer::getBufSize()
}


// External Timer Event
void ConsolePlayer::updateDisplay()
{
#ifdef FEAT_NEW_SONLEGTH_DB
Expand Down
10 changes: 8 additions & 2 deletions src/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

#include <string>
#include <bitset>
#include <thread>
#include <atomic>

#ifdef HAVE_TSID
# if HAVE_TSID > 1
Expand Down Expand Up @@ -146,7 +148,9 @@ class ConsolePlayer
sidplayfp m_engine;
SidConfig m_engCfg;
SidTune m_tune;
player_state_t m_state;

std::atomic<player_state_t> m_state;

const char* m_outfile;
std::string m_filename;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -213,7 +219,7 @@ class ConsolePlayer
struct m_timer_t
{ // secs
uint_least32_t start;
uint_least32_t current;
std::atomic<uint_least32_t> current;
uint_least32_t stop;
uint_least32_t length;
bool valid;
Expand Down
Loading