Skip to content

Commit

Permalink
Refactor working, some details missing
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodmg committed Nov 25, 2023
1 parent beb891b commit 280036d
Show file tree
Hide file tree
Showing 12 changed files with 283 additions and 361 deletions.
2 changes: 1 addition & 1 deletion audiosource.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AudioSource : public QObject
signals:
void playbackStateChanged(MediaPlayer::PlaybackState state);
void positionChanged(qint64 progress);
void dataEmitted(const QByteArray& data);
void dataEmitted(const QByteArray& data, QAudioFormat format);
void metadataChanged(QMediaMetaData metadata);
void durationChanged(qint64 duration);
void eqEnabledChanged(bool enabled);
Expand Down
51 changes: 42 additions & 9 deletions audiosourcecoordinator.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,58 @@
#include "audiosourcecoordinator.h"
#include "audiosourcefile.h"

AudioSourceCoordinator::AudioSourceCoordinator(QObject *parent, PlayerView *playerView)
: QObject{parent}
{
system_audio = new SystemAudioControl(this);
view = playerView;

// TODO instantiate sources
// TODO setSource

/*
view->setVolume(system_audio->getVolume());
connect(system_audio, &SystemAudioControl::volumeChanged, view, &PlayerView::setVolume);
connect(view, &PlayerView::volumeChanged, this, &AudioSourceCoordinator::setVolume);

view->setBalance(system_audio->getBalance());
connect(system_audio, &SystemAudioControl::balanceChanged, view, &PlayerView::setBalance);
connect(view, &PlayerView::balanceChanged, this, &AudioSourceCoordinator::setBalance);
*/

}

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

// disconnect slots
}

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

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

// activate new source
sources[currentSource]->activate();
}

void AudioSourceCoordinator::setVolume(int volume)
Expand All @@ -40,3 +64,12 @@ void AudioSourceCoordinator::setBalance(int balance)
{
system_audio->setBalance(balance);
}

void AudioSourceCoordinator::addSource(AudioSource *source, bool activate)
{
// Instantiate sources
sources.append(source);
if(activate) {
setSource(sources.length() - 1);
}
}
4 changes: 3 additions & 1 deletion audiosourcecoordinator.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class AudioSourceCoordinator : public QObject
public:
explicit AudioSourceCoordinator(QObject *parent = nullptr, PlayerView *playerView = nullptr);

void addSource(AudioSource *source, bool activate = false);

signals:
void sourceChanged(int source);

Expand All @@ -22,7 +24,7 @@ public slots:

private:
QList<AudioSource*> sources;
int currentSource = 0;
int currentSource = -1;

SystemAudioControl *system_audio = nullptr;
PlayerView *view = nullptr;
Expand Down
97 changes: 91 additions & 6 deletions audiosourcefile.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <QTime>
#include <QApplication>
#include <QFileDialog>

#include "audiosourcefile.h"
#include "util.h"
Expand All @@ -22,23 +23,23 @@ AudioSourceFile::AudioSourceFile(QObject *parent, PlaylistModel *playlistModel)
m_playlist = m_playlistModel->playlist();

connect(m_playlist, &QMediaPlaylist::currentIndexChanged, this,
&PlayerView::playlistPositionChanged);
&AudioSourceFile::handlePlaylistPositionChanged);
connect(m_playlist, &QMediaPlaylist::mediaAboutToBeRemoved, this,
&PlayerView::playlistMediaRemoved);
&AudioSourceFile::handlePlaylistMediaRemoved);

connect(m_player, &MediaPlayer::playbackStateChanged, this, &AudioSourceFile::playbackStateChanged);

// Emit data for spectrum analyzer
connect(m_player, &MediaPlayer::newData, this, &AudioSourceFile::dataEmitted);
connect(m_player, &MediaPlayer::newData, this, &AudioSourceFile::handleSpectrumData);

}

void AudioSourceFile::activate()
{
emit playbackStateChanged(m_player->playbackState());
emit positionChanged(0);
// emit metadataChanged(...) TODO get current playlist file meta
// emit durationChanged(...)
emit positionChanged(m_player->position());
emit metadataChanged(m_player->metaData());
emit durationChanged(m_player->duration());
emit eqEnabledChanged(false);
emit plEnabledChanged(true);
emit shuffleEnabledChanged(shuffleEnabled);
Expand Down Expand Up @@ -184,3 +185,87 @@ void AudioSourceFile::setStatusInfo(const QString &info)
else
emit messageClear();
}

void AudioSourceFile::handlePlaylistPositionChanged(int)
{
m_player->setSource(m_playlist->currentQueueMedia());

if (shouldBePlaying) {
m_player->play();
}
}

void AudioSourceFile::handlePlaylistMediaRemoved(int from, int to)
{
// Stop only if currently playing file was removed
int playingIndex = m_playlist->currentIndex();
if(playingIndex >= from && playingIndex <= to) {
shouldBePlaying = false;
m_player->stop();
m_player->clearSource();
}
}

void AudioSourceFile::jump(const QModelIndex &index)
{
if (index.isValid()) {
m_playlist->setCurrentIndex(index.row());
shouldBePlaying = true;
m_player->play();
}
}

void AudioSourceFile::open()
{

/*
QList<QUrl> urls;
urls << QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first())
<< QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::DownloadLocation).first())
<< QUrl::fromLocalFile(QStandardPaths::standardLocations(QStandardPaths::MusicLocation).first());
QFileDialog fileDialog(this);
QString filters = audioFileFilters().join(" ");
fileDialog.setNameFilter("Audio (" + filters + ")");
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
fileDialog.setFileMode(QFileDialog::ExistingFiles);
fileDialog.setWindowTitle(tr("Open Files"));
fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MusicLocation)
.value(0, QDir::homePath()));
fileDialog.setOption(QFileDialog::ReadOnly, true);
fileDialog.setOption(QFileDialog::DontUseNativeDialog, true);
fileDialog.setViewMode(QFileDialog::Detail);
fileDialog.setSidebarUrls(urls);
#ifdef IS_EMBEDDED
fileDialog.setWindowState(Qt::WindowFullScreen);
#endif
if (fileDialog.exec() == QDialog::Accepted)
addToPlaylist(fileDialog.selectedUrls());
*/
}

void AudioSourceFile::addToPlaylist(const QList<QUrl> &urls)
{
const int previousMediaCount = m_playlist->mediaCount();
for (auto &url : urls) {
if (isPlaylist(url))
m_playlist->load(url);
else
m_playlist->addMedia(url);
}
if (m_playlist->mediaCount() > previousMediaCount) {
// Start playing only if not already playing
if(m_player->playbackState() == MediaPlayer::PlaybackState::StoppedState) {
auto index = m_playlistModel->index(previousMediaCount, 0);
jump(index);
}
}
}

void AudioSourceFile::handleSpectrumData(const QByteArray& data)
{
emit dataEmitted(data, m_player->format());
}
7 changes: 7 additions & 0 deletions audiosourcefile.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ public slots:

void jump(const QModelIndex &index);

void open();
void addToPlaylist(const QList<QUrl> &urls);

private slots:
void handleMetaDataChanged();
void handleMediaStatusChanged(MediaPlayer::MediaStatus status);
void handleBufferingProgress(float progress);
void handleMediaError();
void handlePlaylistPositionChanged(int);
void handlePlaylistMediaRemoved(int, int);
void handleSpectrumData(const QByteArray& data);


private:
MediaPlayer *m_player = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int main(int argc, char *argv[])
QList<QUrl> urls;
for (auto &a : parser.positionalArguments())
urls.append(QUrl::fromUserInput(a, QDir::currentPath()));
window.player->addToPlaylist(urls);
//window.player->addToPlaylist(urls);
}

#ifdef IS_EMBEDDED
Expand Down
12 changes: 8 additions & 4 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,26 @@ MainWindow::MainWindow(QWidget *parent)
playlist = new PlaylistView(this, m_playlistModel);
playlist->setAttribute(Qt::WidgetAttribute::WA_StyledBackground, true);

coordinator = new AudioSourceCoordinator(this, player);
fileSource = new AudioSourceFile(this, m_playlistModel);
coordinator->addSource(fileSource, true);

controlButtons = new ControlButtonsWidget(this);

// Connect events
connect(player, &PlayerView::showPlaylistClicked, this, &MainWindow::showPlaylist);
connect(playlist, &PlaylistView::showPlayerClicked, this, &MainWindow::showPlayer);
connect(playlist, &PlaylistView::songSelected, player, &PlayerView::jump);
connect(playlist, &PlaylistView::addSelectedFilesClicked, player, &PlayerView::addToPlaylist);
connect(playlist, &PlaylistView::songSelected, fileSource, &AudioSourceFile::jump);
connect(playlist, &PlaylistView::addSelectedFilesClicked, fileSource, &AudioSourceFile::addToPlaylist);

connect(controlButtons, &ControlButtonsWidget::playClicked, player, &PlayerView::playClicked);
connect(controlButtons, &ControlButtonsWidget::pauseClicked, player, &PlayerView::pauseClicked);
connect(controlButtons, &ControlButtonsWidget::stopClicked, player, &PlayerView::stopClicked);
connect(controlButtons, &ControlButtonsWidget::nextClicked, player, &PlayerView::nextClicked);
connect(controlButtons, &ControlButtonsWidget::previousClicked, player, &PlayerView::previousClicked);
connect(controlButtons, &ControlButtonsWidget::openClicked, player, &PlayerView::showPlaylistClicked);
connect(controlButtons, &ControlButtonsWidget::repeatClicked, player, &PlayerView::repeatButtonClicked);
connect(controlButtons, &ControlButtonsWidget::shuffleClicked, player, &PlayerView::shuffleButtonClicked);
//TOSO connect(controlButtons, &ControlButtonsWidget::repeatClicked, player, &PlayerView::repeatButtonClicked);
//TODO connect(controlButtons, &ControlButtonsWidget::shuffleClicked, player, &PlayerView::shuffleButtonClicked);
connect(controlButtons, &ControlButtonsWidget::logoClicked, this, &MainWindow::showShutdownModal);

// Prepare player main view
Expand Down
4 changes: 4 additions & 0 deletions mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <QMainWindow>
#include <QStackedLayout>
#include <QProcess>
#include "audiosourcecoordinator.h"
#include "audiosourcefile.h"
#include "controlbuttonswidget.h"
#include "playerview.h"
#include "playlistview.h"
Expand All @@ -22,6 +24,8 @@ class MainWindow : public QMainWindow
PlayerView *player;
ControlButtonsWidget *controlButtons;
PlaylistView *playlist;
AudioSourceCoordinator *coordinator;
AudioSourceFile *fileSource;

public slots:
void showPlayer();
Expand Down
Loading

0 comments on commit 280036d

Please sign in to comment.