Skip to content

Commit

Permalink
playback: check if we need to restart playback stream on play()
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia committed Aug 3, 2024
1 parent 48e88c0 commit 7a09492
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
33 changes: 26 additions & 7 deletions app/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ Player * Player::instance()

void Player::play()
{
PaError ret = Pa_IsStreamActive(m_stream);
if (ret == 0) {
fputs("Playback stream stopped, restarting...", stderr);
bool succ = setupAndStartStream();
if (!succ) {
fputs("Not able to restart playback stream", stderr);
return;
}
}
m_isPlaying = true;
if (mf_onIsPlayingChanged) mf_onIsPlayingChanged(m_isPlaying);
}
Expand Down Expand Up @@ -208,13 +217,7 @@ Player::Player()
m_opl = opl_create();

Pa_Initialize();
Pa_OpenDefaultStream(&m_stream, 0, 2, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified,
+[](const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) -> int {
return ((Player *)userData)->streamCallback(inputBuffer, outputBuffer, framesPerBuffer);
}, this);
Pa_StartStream(m_stream);
setupAndStartStream();
}

Player::~Player()
Expand Down Expand Up @@ -297,6 +300,22 @@ std::tuple<double, tml_message *> Player::renderToBuffer(float *buffer, tml_mess
return std::make_tuple(playbackEnd, curMsg);
}

bool Player::setupAndStartStream()
{
Pa_OpenDefaultStream(&m_stream, 0, 2, paFloat32, SAMPLE_RATE, paFramesPerBufferUnspecified,
+[](const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) -> int {
return ((Player *)userData)->streamCallback(inputBuffer, outputBuffer, framesPerBuffer);
}, this);
PaError err = Pa_StartStream(m_stream);
if (err != paNoError) {
// TODO: logging?
return false;
}
return true;
}

int Player::streamCallback(const void *inputBuffer, void *outputBuffer, unsigned long numFrames)
{
if (m_tinyMidiLoader && m_isPlaying) {
Expand Down
1 change: 1 addition & 0 deletions app/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Player
std::tuple<double, tml_message *> renderToBuffer(float * buffer, tml_message * startMsg, double startMs, int sampleCount);

static Player * m_player_instance;
bool setupAndStartStream();
int streamCallback(const void *inputBuffer, void *outputBuffer, unsigned long numFrames);

float m_volume = 1;
Expand Down
2 changes: 1 addition & 1 deletion app/playlistmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ class PlaylistManager : public QObject
void totalCountChanged(int count);

private:
int m_currentIndex;
int m_currentIndex = -1;
PlaylistModel m_model;
};

0 comments on commit 7a09492

Please sign in to comment.