Skip to content

Commit

Permalink
Implemented volume and balance messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodmg committed Nov 25, 2023
1 parent 280036d commit 0040bd0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
37 changes: 33 additions & 4 deletions audiosourcecoordinator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,36 @@ AudioSourceCoordinator::AudioSourceCoordinator(QObject *parent, PlayerView *play

}

void AudioSourceCoordinator::setSource(int source)
void AudioSourceCoordinator::setSource(int newSource)
{
// TODO
if(currentSource >= 0) {
// deactivate old source
sources[currentSource]->deactivate();

// disconnect slots
disconnect(view, &PlayerView::positionChanged, sources[currentSource], &AudioSource::handleSeek);
disconnect(view, &PlayerView::previousClicked, sources[currentSource], &AudioSource::handlePrevious);
disconnect(view, &PlayerView::playClicked, sources[currentSource], &AudioSource::handlePlay);
disconnect(view, &PlayerView::pauseClicked, sources[currentSource], &AudioSource::handlePause);
disconnect(view, &PlayerView::stopClicked, sources[currentSource], &AudioSource::handleStop);
disconnect(view, &PlayerView::nextClicked, sources[currentSource], &AudioSource::handleNext);
disconnect(view, &PlayerView::openClicked, sources[currentSource], &AudioSource::handleOpen);
disconnect(view, &PlayerView::shuffleClicked, sources[currentSource], &AudioSource::handleShuffle);
disconnect(view, &PlayerView::repeatClicked, sources[currentSource], &AudioSource::handleRepeat);

disconnect(sources[currentSource], &AudioSource::playbackStateChanged, view, &PlayerView::setPlaybackState);
disconnect(sources[currentSource], &AudioSource::positionChanged, view, &PlayerView::setPosition);
disconnect(sources[currentSource], &AudioSource::dataEmitted, view, &PlayerView::setSpectrumData);
disconnect(sources[currentSource], &AudioSource::metadataChanged, view, &PlayerView::setMetadata);
disconnect(sources[currentSource], &AudioSource::durationChanged, view, &PlayerView::setDuration);
disconnect(sources[currentSource], &AudioSource::eqEnabledChanged, view, &PlayerView::setEqEnabled);
disconnect(sources[currentSource], &AudioSource::plEnabledChanged, view, &PlayerView::setPlEnabled);
disconnect(sources[currentSource], &AudioSource::shuffleEnabledChanged, view, &PlayerView::setShuffleEnabled);
disconnect(sources[currentSource], &AudioSource::repeatEnabledChanged, view, &PlayerView::setRepeatEnabled);
disconnect(sources[currentSource], &AudioSource::messageSet, view, &PlayerView::setMessage);
disconnect(sources[currentSource], &AudioSource::messageClear, view, &PlayerView::clearMessage);
}

currentSource = source;
currentSource = newSource;
// connect slots to new source
connect(view, &PlayerView::positionChanged, sources[currentSource], &AudioSource::handleSeek);
connect(view, &PlayerView::previousClicked, sources[currentSource], &AudioSource::handlePrevious);
Expand Down Expand Up @@ -58,11 +77,21 @@ void AudioSourceCoordinator::setSource(int source)
void AudioSourceCoordinator::setVolume(int volume)
{
system_audio->setVolume(volume);
view->setMessage(QString("VOLUME: %1%").arg(volume), 500);
}

void AudioSourceCoordinator::setBalance(int balance)
{
system_audio->setBalance(balance);
QString message;
if(balance == 0) {
message = "BALANCE: CENTER";
} else {
message = QString("BALANCE: %1% %2")
.arg(abs(balance))
.arg(balance < 0 ? "LEFT" : "RIGHT");
}
view->setMessage(message, 500);
}

void AudioSourceCoordinator::addSource(AudioSource *source, bool activate)
Expand Down
24 changes: 15 additions & 9 deletions audiosourcefile.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include <QTime>
#include <QApplication>
#include <QFileDialog>

#include "audiosourcefile.h"
#include "util.h"

//#define DISPLAY_BUFFERING


AudioSourceFile::AudioSourceFile(QObject *parent, PlaylistModel *playlistModel)
: AudioSource{parent}
Expand Down Expand Up @@ -144,13 +145,14 @@ void AudioSourceFile::handleMediaStatusChanged(MediaPlayer::MediaStatus status)
break;
case MediaPlayer::BufferingMedia:
case MediaPlayer::BufferedMedia:
setStatusInfo(tr("Buffering %1%").arg(qRound(m_player->bufferProgress() * 100.)));
#ifdef DISPLAY_BUFFERING
setStatusInfo(tr("Buffering %1%").arg(qRound(m_player->bufferProgress())));
#endif
break;
case MediaPlayer::StalledMedia:
setStatusInfo(tr("Stalled %1%").arg(qRound(m_player->bufferProgress() * 100.)));
setStatusInfo(tr("Stalled %1%").arg(qRound(m_player->bufferProgress())));
break;
case MediaPlayer::EndOfMedia:
//QApplication::alert(this);
handleNext();
break;
case MediaPlayer::InvalidMedia:
Expand All @@ -162,10 +164,14 @@ void AudioSourceFile::handleMediaStatusChanged(MediaPlayer::MediaStatus status)
void AudioSourceFile::handleBufferingProgress(float progress)
{

if (m_player->mediaStatus() == MediaPlayer::StalledMedia)
setStatusInfo(tr("Stalled %1%").arg(qRound(progress * 100.)));
else
setStatusInfo(tr("Buffering %1%").arg(qRound(progress * 100.)));
if (m_player->mediaStatus() == MediaPlayer::StalledMedia) {
setStatusInfo(tr("Stalled %1%").arg(qRound(progress)));
}
else {
#ifdef DISPLAY_BUFFERING
setStatusInfo(tr("Buffering %1%").arg(qRound(progress)));
#endif
}
}

void AudioSourceFile::handleMediaError()
Expand All @@ -181,7 +187,7 @@ void AudioSourceFile::setStatusInfo(const QString &info)
m_statusInfo = info;

if (!m_statusInfo.isEmpty())
emit messageSet(QString("%1 | %2").arg(m_statusInfo), 5000);
emit messageSet(QString("%1").arg(m_statusInfo), 1000);
else
emit messageClear();
}
Expand Down
4 changes: 2 additions & 2 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ MainWindow::MainWindow(QWidget *parent)
connect(controlButtons, &ControlButtonsWidget::nextClicked, player, &PlayerView::nextClicked);
connect(controlButtons, &ControlButtonsWidget::previousClicked, player, &PlayerView::previousClicked);
connect(controlButtons, &ControlButtonsWidget::openClicked, player, &PlayerView::showPlaylistClicked);
//TOSO connect(controlButtons, &ControlButtonsWidget::repeatClicked, player, &PlayerView::repeatButtonClicked);
//TODO connect(controlButtons, &ControlButtonsWidget::shuffleClicked, player, &PlayerView::shuffleButtonClicked);
connect(controlButtons, &ControlButtonsWidget::repeatClicked, player, &PlayerView::repeatClicked);
connect(controlButtons, &ControlButtonsWidget::shuffleClicked, player, &PlayerView::shuffleClicked);
connect(controlButtons, &ControlButtonsWidget::logoClicked, this, &MainWindow::showShutdownModal);

// Prepare player main view
Expand Down
9 changes: 7 additions & 2 deletions playerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ PlayerView::PlayerView(QWidget *parent, PlaylistModel *playlistModel) :
spectrumLayout->setSpacing(0);
ui->spectrumContainer->setLayout(spectrumLayout);

// Setup message functionality
messageTimer = new QTimer(this);
connect(messageTimer, &QTimer::timeout, this, &PlayerView::clearMessage);
}

PlayerView::~PlayerView()
Expand Down Expand Up @@ -265,13 +268,15 @@ void PlayerView::setRepeatEnabled(bool enabled)

void PlayerView::setMessage(QString message, qint64 timeout)
{
//ui->songInfoLabel->setText(message);
// TODO timeout
ui->songInfoLabel->setText(message);
messageTimer->setSingleShot(true);
messageTimer->start(timeout);
}

void PlayerView::clearMessage()
{
ui->songInfoLabel->setText(m_trackInfo);
messageTimer->stop();
}

/////////////
Expand Down
10 changes: 4 additions & 6 deletions playerview.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QMediaMetaData>
#include <QAudioOutput>
#include <QWidget>
#include <QTimer>

QT_BEGIN_NAMESPACE
class QAbstractItemView;
Expand Down Expand Up @@ -62,10 +63,13 @@ public slots:
void repeatClicked();
void menuClicked();

void showPlaylistClicked();

private:
Ui::PlayerView *ui;
void scale();
SpectrumWidget *spectrum = nullptr;
QTimer *messageTimer = nullptr;

QString m_trackInfo;
QString m_statusInfo;
Expand All @@ -82,12 +86,6 @@ public slots:
private slots:
void handleBalanceChanged();

//////////


signals:
void showPlaylistClicked();

};

#endif // PLAYERVIEW_H

0 comments on commit 0040bd0

Please sign in to comment.