Skip to content

Commit

Permalink
playback: support 'replay' mode
Browse files Browse the repository at this point in the history
  • Loading branch information
BLumia committed Aug 14, 2024
1 parent a69ef28 commit 6eebc7f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
9 changes: 6 additions & 3 deletions player/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ QDebug& operator<<(QDebug& out, const std::string& str)
Player::Player(QObject *parent)
: QObject{parent}
, m_isPlaying(false)
, m_restartAfterFinished(false)
{
Pa_Initialize();
setupAndStartStream();
Expand Down Expand Up @@ -92,9 +93,11 @@ int Player::streamCallback(const void *inputBuffer, void *outputBuffer, unsigned
// api like get_current_order() will return as it rewinded to the beginning of the song
// but read() will still return 0 frame and won't write to the buffer...
seek(0);
m_isPlaying = false;
emit playbackStatusChanged();
emit endOfSongReached();
if (!m_restartAfterFinished) {
m_isPlaying = false;
emit playbackStatusChanged();
emit endOfSongReached();
}
}
} else {
std::fill(buffer, buffer + numFrames * 2, 0);
Expand Down
3 changes: 3 additions & 0 deletions player/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Player : public QObject
Q_PROPERTY(int repeatCount READ repeatCount WRITE setRepeatCount NOTIFY repeatCountChanged)
Q_PROPERTY(int gain READ gain WRITE setGain NOTIFY gainChanged)
Q_PROPERTY(bool isPlaying READ isPlaying NOTIFY playbackStatusChanged)
Q_PROPERTY(bool restartAfterFinished MEMBER m_restartAfterFinished NOTIFY restartAfterFinishedChanged)

Q_INVOKABLE bool load(const QUrl &filename);
Q_INVOKABLE void play();
Expand Down Expand Up @@ -91,6 +92,7 @@ class Player : public QObject
void playbackStatusChanged(); // can be emitted even if it's not changed
void repeatCountChanged();
void gainChanged();
void restartAfterFinishedChanged();

private:
void updateCachedState();
Expand All @@ -103,5 +105,6 @@ class Player : public QObject
CachedPlaybackState m_cachedState;
PlaybackOptions m_options;
bool m_isPlaying;
bool m_restartAfterFinished;
};

29 changes: 29 additions & 0 deletions widget/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ MainWindow::MainWindow(QWidget *parent)
ui->muteButton->setIcon(QIcon::fromTheme(iconText));
});

connect(this, &MainWindow::repeatModeChanged, this, [this](RepeatMode mode){
switch (mode) {
case DisableRepeat:
m_player->setRepeatCount(1);
m_player->setProperty("restartAfterFinished", false);
ui->playbackModeButton->setText("Single");
break;
case Repeat:
m_player->setRepeatCount(0);
ui->playbackModeButton->setText("Repeat");
break;
case Replay:
m_player->setRepeatCount(1);
m_player->setProperty("restartAfterFinished", true);
ui->playbackModeButton->setText("Replay");
break;
}
});

connect(ui->playbackSlider, &QSlider::valueChanged, this, [this](int value){
m_player->seek(ui->playbackSlider->value());
QToolTip::showText(QCursor::pos(), QString::number(value), nullptr);
Expand Down Expand Up @@ -222,3 +241,13 @@ void MainWindow::on_muteButton_clicked()
m_player->setGain(isMuted ? ui->volumeSlider->value() : std::numeric_limits<std::int32_t>::min());
}


void MainWindow::on_playbackModeButton_clicked()
{
RepeatMode targetMode = FirstRepeatMode;
if (m_repeatMode != LastRepeatMode) {
targetMode = static_cast<RepeatMode>(m_repeatMode + 1);
}
setProperty("repeatMode", targetMode);
}

16 changes: 16 additions & 0 deletions widget/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ class MainWindow : public QMainWindow
Q_OBJECT

public:
enum RepeatMode {
DisableRepeat,
Repeat,
Replay,
FirstRepeatMode = DisableRepeat,
LastRepeatMode = Replay,
};
Q_ENUM(RepeatMode)

Q_PROPERTY(RepeatMode repeatMode MEMBER m_repeatMode NOTIFY repeatModeChanged)

MainWindow(QWidget *parent = nullptr);
~MainWindow();

Expand All @@ -26,6 +37,9 @@ class MainWindow : public QMainWindow
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;

signals:
void repeatModeChanged(RepeatMode mode);

private slots:
void on_instrumentsBtn_clicked();
void on_messageBtn_clicked();
Expand All @@ -34,11 +48,13 @@ private slots:
void on_actionAbout_triggered();
void on_filterEdit_textChanged(const QString &arg1);
void on_muteButton_clicked();
void on_playbackModeButton_clicked();

private:
Ui::MainWindow *ui;
Player * m_player = nullptr;
PlaylistManager * m_playlistManager = nullptr;
InstrumentsModel * m_instrumentsModel = nullptr;
QSortFilterProxyModel * m_playlistFilderModel = nullptr;
RepeatMode m_repeatMode = Repeat;
};
11 changes: 9 additions & 2 deletions widget/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,1,0,0">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0,1,0,0">
<item>
<widget class="QPushButton" name="playButton">
<property name="text">
<string>Play</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="playbackModeButton">
<property name="text">
<string>Repeat</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
Expand Down Expand Up @@ -256,7 +263,7 @@
<x>0</x>
<y>0</y>
<width>600</width>
<height>24</height>
<height>20</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
Expand Down

0 comments on commit 6eebc7f

Please sign in to comment.