Skip to content

Commit

Permalink
Add an option to stop playback instating of pausing.
Browse files Browse the repository at this point in the history
Fixes: #185
  • Loading branch information
cameronwhite committed Apr 24, 2016
1 parent 587eb24 commit 4d27cb0
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 37 deletions.
38 changes: 28 additions & 10 deletions source/app/powertabeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,9 +585,9 @@ void PowerTabEditor::startStopPlayback(bool from_measure_start)
myPlaybackWidget->setPlaybackMode(true);

const ScoreLocation &location = getLocation();
myMidiPlayer.reset(new MidiPlayer(
*mySettingsManager, location.getScore(), location.getSystemIndex(),
location.getPositionIndex(), myPlaybackWidget->getPlaybackSpeed()));
myMidiPlayer.reset(
new MidiPlayer(*mySettingsManager, location,
myPlaybackWidget->getPlaybackSpeed()));

connect(myMidiPlayer.get(), SIGNAL(playbackSystemChanged(int)), this,
SLOT(moveCaretToSystem(int)));
Expand Down Expand Up @@ -1879,6 +1879,11 @@ void PowerTabEditor::createCommands()
startStopPlayback(/* from_measure_start */ true);
});

myStopCommand =
new Command(tr("Stop"), "Playback.Stop", Qt::ALT + Qt::Key_Space, this);
connect(myStopCommand, &QAction::triggered, this,
&PowerTabEditor::stopPlayback);

myRewindCommand = new Command(tr("Rewind"), "Playback.Rewind",
Qt::ALT + Qt::Key_Left, this);
connect(myRewindCommand, &QAction::triggered, this,
Expand Down Expand Up @@ -2668,6 +2673,7 @@ void PowerTabEditor::createMenus()
myPlaybackMenu = menuBar()->addMenu(tr("Play&back"));
myPlaybackMenu->addAction(myPlayPauseCommand);
myPlaybackMenu->addAction(myPlayFromStartOfMeasureCommand);
myPlaybackMenu->addAction(myStopCommand);
myPlaybackMenu->addAction(myRewindCommand);
myPlaybackMenu->addAction(myMetronomeCommand);

Expand Down Expand Up @@ -2869,8 +2875,9 @@ void PowerTabEditor::createTabArea()
connect(myTabWidget, SIGNAL(currentChanged(int)), this,
SLOT(switchTab(int)));

myPlaybackWidget = new PlaybackWidget(*myPlayPauseCommand, *myRewindCommand,
*myMetronomeCommand, this);
myPlaybackWidget =
new PlaybackWidget(*myPlayPauseCommand, *myRewindCommand,
*myStopCommand, *myMetronomeCommand, this);

connect(myPlaybackWidget, &PlaybackWidget::activeVoiceChanged, this,
&PowerTabEditor::updateActiveVoice);
Expand Down Expand Up @@ -3025,13 +3032,15 @@ void PowerTabEditor::updateCommands()
{
// Disable editing during playback.
if (myIsPlaying)
{
enableEditing(false);
myPlayPauseCommand->setEnabled(true);
myRewindCommand->setEnabled(true);
myMetronomeCommand->setEnabled(true);

myPlayPauseCommand->setEnabled(true);
myRewindCommand->setEnabled(true);
myMetronomeCommand->setEnabled(true);
myStopCommand->setEnabled(myIsPlaying);

if (myIsPlaying)
return;
}

ScoreLocation location = getLocation();
const Score &score = location.getScore();
Expand Down Expand Up @@ -3303,6 +3312,15 @@ void PowerTabEditor::rewindPlaybackToStart()
startStopPlayback();
}

void PowerTabEditor::stopPlayback()
{
assert(myIsPlaying);
const ScoreLocation start_location = myMidiPlayer->getStartLocation();

startStopPlayback();
getCaret().moveToLocation(start_location);
}

void PowerTabEditor::toggleMetronome()
{
auto settings = mySettingsManager->getWriteHandle();
Expand Down
3 changes: 3 additions & 0 deletions source/app/powertabeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ private slots:

/// Moves the caret back to the start, and restarts playback if necessary.
void rewindPlaybackToStart();
/// Stops playback and returns to the initial position.
void stopPlayback();
/// Toggles the metronome on or off.
void toggleMetronome();
/// Sets the current voice that is being edited.
Expand Down Expand Up @@ -433,6 +435,7 @@ private slots:
QMenu *myPlaybackMenu;
Command *myPlayPauseCommand;
Command *myPlayFromStartOfMeasureCommand;
Command *myStopCommand;
Command *myRewindCommand;
Command *myMetronomeCommand;

Expand Down
14 changes: 8 additions & 6 deletions source/audio/midiplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@

static const int METRONOME_CHANNEL = 9;

MidiPlayer::MidiPlayer(SettingsManager &settings_manager, const Score &score,
int start_system, int start_pos, int speed)
MidiPlayer::MidiPlayer(SettingsManager &settings_manager,
const ScoreLocation &start_location, int speed)
: mySettingsManager(settings_manager),
myScore(score),
myStartLocation(start_system, start_pos),
myScore(start_location.getScore()),
myStartLocation(start_location),
myIsPlaying(false),
myPlaybackSpeed(speed)
{
Expand Down Expand Up @@ -119,7 +119,9 @@ void MidiPlayer::run()

bool started = false;
int beat_duration = Midi::BEAT_DURATION_120_BPM;
SystemLocation current_location = myStartLocation;
const SystemLocation start_location(myStartLocation.getSystemIndex(),
myStartLocation.getPositionIndex());
SystemLocation current_location = start_location;

for (auto event = events.begin(); event != events.end(); ++event)
{
Expand All @@ -133,7 +135,7 @@ void MidiPlayer::run()
// instrument changes. Tempo changes are tracked above.
if (!started)
{
if (event->getLocation() < myStartLocation)
if (event->getLocation() < start_location)
{
if (event->isProgramChange())
device.sendMessage(event->getData());
Expand Down
11 changes: 7 additions & 4 deletions source/audio/midiplayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,27 @@

#include <atomic>
#include <QThread>
#include <score/systemlocation.h>
#include <score/scorelocation.h>

class MidiFile;
class MidiOutputDevice;
class Score;
class SettingsManager;
class SystemLocation;

class MidiPlayer : public QThread
{
Q_OBJECT

public:
MidiPlayer(SettingsManager &settings_manager, const Score &score,
int start_system, int start_pos, int speed);
MidiPlayer(SettingsManager &settings_manager,
const ScoreLocation &start_location, int speed);
~MidiPlayer();

void changePlaybackSpeed(int new_speed);

const ScoreLocation &getStartLocation() const { return myStartLocation; }

signals:
// These signals are used to move the caret when a position change is
// necessary
Expand All @@ -56,7 +59,7 @@ class MidiPlayer : public QThread

SettingsManager &mySettingsManager;
const Score &myScore;
SystemLocation myStartLocation;
ScoreLocation myStartLocation;
std::atomic<bool> myIsPlaying;
std::atomic<bool> myMetronomeEnabled;
/// The current playback speed (percent).
Expand Down
36 changes: 23 additions & 13 deletions source/widgets/playback/playbackwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ class PercentageValidator : public QValidator
QDoubleValidator myNumberValidator;
};

PlaybackWidget::PlaybackWidget(const QAction &playPauseCommand,
const QAction &rewindCommand,
const QAction &metronomeCommand, QWidget *parent)
PlaybackWidget::PlaybackWidget(const QAction &play_pause_command,
const QAction &rewind_command,
const QAction &stop_command,
const QAction &metronome_command,
QWidget *parent)
: QWidget(parent),
ui(new Ui::PlaybackWidget),
myVoices(new QButtonGroup(this))
Expand All @@ -77,25 +79,32 @@ PlaybackWidget::PlaybackWidget(const QAction &playPauseCommand,

ui->rewindToStartButton->setIcon(
style()->standardIcon(QStyle::SP_MediaSkipBackward));
connect(&rewindCommand, &QAction::changed, [&]() {
connect(&rewind_command, &QAction::changed, [&]() {
ui->rewindToStartButton->setToolTip(
tr("Click to move playback to the beginning of the score%1.")
.arg(getShortcutHint(rewindCommand)));
.arg(getShortcutHint(rewind_command)));
});

ui->playPauseButton->setIcon(style()->standardIcon(QStyle::SP_MediaPlay));
connect(&playPauseCommand, &QAction::changed, [&]() {
connect(&play_pause_command, &QAction::changed, [&]() {
ui->playPauseButton->setToolTip(
tr("Click to start or stop playback%1.")
.arg(getShortcutHint(playPauseCommand)));
tr("Click to start or pause playback%1.")
.arg(getShortcutHint(play_pause_command)));
});

ui->stopButton->setIcon(style()->standardIcon(QStyle::SP_MediaStop));
connect(&stop_command, &QAction::changed, [&]() {
ui->stopButton->setToolTip(
tr("Click to stop playback and return to the initial "
"location%1.").arg(getShortcutHint(stop_command)));
});

ui->metronomeToggleButton->setIcon(
style()->standardIcon(QStyle::SP_MediaVolume));
connect(&metronomeCommand, &QAction::changed, [&]() {
connect(&metronome_command, &QAction::changed, [&]() {
ui->metronomeToggleButton->setToolTip(
tr("Click to toggle whether the metronome is turned on%1.")
.arg(getShortcutHint(metronomeCommand)));
.arg(getShortcutHint(metronome_command)));
});

ui->zoomComboBox->setValidator(new PercentageValidator(this));
Expand All @@ -106,9 +115,10 @@ PlaybackWidget::PlaybackWidget(const QAction &playPauseCommand,
SIGNAL(playbackSpeedChanged(int)));
connect(ui->filterComboBox, SIGNAL(currentIndexChanged(int)), this,
SIGNAL(activeFilterChanged(int)));
connectButtonToAction(ui->playPauseButton, &playPauseCommand);
connectButtonToAction(ui->metronomeToggleButton, &metronomeCommand);
connectButtonToAction(ui->rewindToStartButton, &rewindCommand);
connectButtonToAction(ui->playPauseButton, &play_pause_command);
connectButtonToAction(ui->metronomeToggleButton, &metronome_command);
connectButtonToAction(ui->rewindToStartButton, &rewind_command);
connectButtonToAction(ui->stopButton, &stop_command);

connect(ui->zoomComboBox, &QComboBox::currentTextChanged,
[=](const QString &text) {
Expand Down
7 changes: 4 additions & 3 deletions source/widgets/playback/playbackwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ class PlaybackWidget : public QWidget
Q_OBJECT

public:
explicit PlaybackWidget(const QAction &playPauseCommand,
const QAction &rewindCommand,
const QAction &metronomeCommand, QWidget *parent);
explicit PlaybackWidget(const QAction &play_pause_command,
const QAction &rewind_command,
const QAction &stop_command,
const QAction &metronome_command, QWidget *parent);
~PlaybackWidget();

/// Reload any settings for the given score.
Expand Down
9 changes: 8 additions & 1 deletion source/widgets/playback/playbackwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>907</width>
<width>933</width>
<height>38</height>
</rect>
</property>
Expand Down Expand Up @@ -133,6 +133,13 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="stopButton">
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="metronomeToggleButton">
<property name="text">
Expand Down

0 comments on commit 4d27cb0

Please sign in to comment.