Commit e0b1b6a0 authored by Sandro Andrade's avatar Sandro Andrade

Refactor MidiPlayer and midisequencer state integration

parent ef90e36e
...@@ -43,7 +43,8 @@ MidiSequencer::MidiSequencer(QObject *parent) : ...@@ -43,7 +43,8 @@ MidiSequencer::MidiSequencer(QObject *parent) :
QObject(parent), QObject(parent),
m_tick(0), m_tick(0),
m_song(0), m_song(0),
m_eventSchedulingMode(FROM_ENGINE) m_eventSchedulingMode(FROM_ENGINE),
m_state(StoppedState)
{ {
qmlRegisterType<MidiSequencer>("org.kde.minuet", 1, 0, "MidiSequencer"); qmlRegisterType<MidiSequencer>("org.kde.minuet", 1, 0, "MidiSequencer");
// MidiClient configuration // MidiClient configuration
...@@ -156,7 +157,6 @@ void MidiSequencer::openFile(const QString &fileName) ...@@ -156,7 +157,6 @@ void MidiSequencer::openFile(const QString &fileName)
m_song = new Song(); m_song = new Song();
m_eventSchedulingMode = FROM_ENGINE; m_eventSchedulingMode = FROM_ENGINE;
m_smfReader->readFromFile(fileName); m_smfReader->readFromFile(fileName);
emit tempoChanged(6.0e7f / m_song->initialTempo());
m_song->sort(); m_song->sort();
m_midiSequencerOutputThread->setSong(m_song); m_midiSequencerOutputThread->setSong(m_song);
} }
...@@ -201,7 +201,7 @@ MidiSequencer::EventSchedulingMode MidiSequencer::schedulingMode() const ...@@ -201,7 +201,7 @@ MidiSequencer::EventSchedulingMode MidiSequencer::schedulingMode() const
void MidiSequencer::resetMidiPlayer() void MidiSequencer::resetMidiPlayer()
{ {
setPlaybackLabel(QStringLiteral("00:00.00")); setPlaybackLabel(QStringLiteral("00:00.00"));
emit stateChanged(MidiSequencer::StoppedState); setState(StoppedState);
} }
int MidiSequencer::pitch() const int MidiSequencer::pitch() const
...@@ -224,6 +224,11 @@ QString MidiSequencer::playbackLabel() const ...@@ -224,6 +224,11 @@ QString MidiSequencer::playbackLabel() const
return m_playbackLabel; return m_playbackLabel;
} }
MidiSequencer::State MidiSequencer::state() const
{
return m_state;
}
void MidiSequencer::play() void MidiSequencer::play()
{ {
if (m_song && !m_song->isEmpty() && !m_midiSequencerOutputThread->isRunning()) { if (m_song && !m_song->isEmpty() && !m_midiSequencerOutputThread->isRunning()) {
...@@ -232,7 +237,7 @@ void MidiSequencer::play() ...@@ -232,7 +237,7 @@ void MidiSequencer::play()
m_midiSequencerOutputThread->setSong(m_song); m_midiSequencerOutputThread->setSong(m_song);
} }
m_midiSequencerOutputThread->start(); m_midiSequencerOutputThread->start();
emit stateChanged(MidiSequencer::PlayingState); setState(PlayingState);
} }
} }
...@@ -242,7 +247,7 @@ void MidiSequencer::pause() ...@@ -242,7 +247,7 @@ void MidiSequencer::pause()
m_midiSequencerOutputThread->stop(); m_midiSequencerOutputThread->stop();
m_midiSequencerOutputThread->setPosition(m_queue->getStatus().getTickTime()); m_midiSequencerOutputThread->setPosition(m_queue->getStatus().getTickTime());
} }
emit stateChanged(MidiSequencer::PausedState); setState(PausedState);
} }
void MidiSequencer::stop() void MidiSequencer::stop()
...@@ -251,15 +256,15 @@ void MidiSequencer::stop() ...@@ -251,15 +256,15 @@ void MidiSequencer::stop()
m_midiSequencerOutputThread->resetPosition(); m_midiSequencerOutputThread->resetPosition();
emit allNotesOff(); emit allNotesOff();
setPlaybackLabel(QStringLiteral("00:00.00")); setPlaybackLabel(QStringLiteral("00:00.00"));
emit stateChanged(MidiSequencer::StoppedState); setState(StoppedState);
} }
void MidiSequencer::setPitch(int pitch) void MidiSequencer::setPitch(int pitch)
{ {
if (m_midiSequencerOutputThread->pitch() != pitch) { if (m_midiSequencerOutputThread->pitch() != pitch) {
m_midiSequencerOutputThread->setPitch(pitch); m_midiSequencerOutputThread->setPitch(pitch);
emit pitchChanged();
emit allNotesOff(); emit allNotesOff();
emit pitchChanged(pitch);
} }
} }
...@@ -267,7 +272,7 @@ void MidiSequencer::setVolume(unsigned int volume) ...@@ -267,7 +272,7 @@ void MidiSequencer::setVolume(unsigned int volume)
{ {
if (m_midiSequencerOutputThread->volume() != volume) { if (m_midiSequencerOutputThread->volume() != volume) {
m_midiSequencerOutputThread->setVolume(volume); m_midiSequencerOutputThread->setVolume(volume);
emit volumeChanged(volume); emit volumeChanged();
} }
} }
...@@ -280,17 +285,23 @@ void MidiSequencer::setTempo(unsigned int tempo) ...@@ -280,17 +285,23 @@ void MidiSequencer::setTempo(unsigned int tempo)
queueTempo.setTempoFactor(tempoFactor); queueTempo.setTempoFactor(tempoFactor);
m_queue->setTempo(queueTempo); m_queue->setTempo(queueTempo);
m_client->drainOutput(); m_client->drainOutput();
emit tempoChanged(queueTempo.getRealBPM()); emit tempoChanged();
} }
void MidiSequencer::setPlaybackLabel(QString playbackLabel) void MidiSequencer::setPlaybackLabel(QString playbackLabel)
{ {
if (m_playbackLabel != playbackLabel) { if (m_playbackLabel != playbackLabel) {
m_playbackLabel = playbackLabel; m_playbackLabel = playbackLabel;
emit playbackLabelChanged(m_playbackLabel); emit playbackLabelChanged();
} }
} }
void MidiSequencer::setState(State state)
{
m_state = state;
emit stateChanged();
}
void MidiSequencer::setSong(Song *song) void MidiSequencer::setSong(Song *song)
{ {
delete m_song; delete m_song;
......
...@@ -46,6 +46,8 @@ class MidiSequencer : public QObject ...@@ -46,6 +46,8 @@ class MidiSequencer : public QObject
Q_PROPERTY(unsigned int volume READ volume WRITE setVolume NOTIFY volumeChanged) Q_PROPERTY(unsigned int volume READ volume WRITE setVolume NOTIFY volumeChanged)
Q_PROPERTY(unsigned int tempo READ tempo WRITE setTempo NOTIFY tempoChanged) Q_PROPERTY(unsigned int tempo READ tempo WRITE setTempo NOTIFY tempoChanged)
Q_PROPERTY(QString playbackLabel READ playbackLabel WRITE setPlaybackLabel NOTIFY playbackLabelChanged) Q_PROPERTY(QString playbackLabel READ playbackLabel WRITE setPlaybackLabel NOTIFY playbackLabelChanged)
Q_ENUMS(State)
Q_PROPERTY(State state READ state NOTIFY stateChanged)
public: public:
explicit MidiSequencer(QObject *parent = 0); explicit MidiSequencer(QObject *parent = 0);
...@@ -61,29 +63,29 @@ public: ...@@ -61,29 +63,29 @@ public:
PlayingState, PlayingState,
PausedState PausedState
}; };
Q_ENUMS(State)
void subscribeTo(const QString &portName); void subscribeTo(const QString &portName);
void openFile(const QString &fileName); void openFile(const QString &fileName);
void appendEvent(drumstick::SequencerEvent *ev, unsigned long tick); void appendEvent(drumstick::SequencerEvent *ev, unsigned long tick);
QStringList availableOutputPorts() const; QStringList availableOutputPorts() const;
EventSchedulingMode schedulingMode() const; EventSchedulingMode schedulingMode() const;
int pitch() const; int pitch() const;
unsigned int volume() const; unsigned int volume() const;
unsigned int tempo() const; unsigned int tempo() const;
QString playbackLabel() const; QString playbackLabel() const;
State state() const;
Q_SIGNALS: Q_SIGNALS:
void noteOn(int chan, int pitch, int vel); void noteOn(int chan, int pitch, int vel);
void noteOff(int chan, int pitch, int vel); void noteOff(int chan, int pitch, int vel);
void allNotesOff(); void allNotesOff();
void pitchChanged(int pitch); void pitchChanged();
void volumeChanged(unsigned int volume); void volumeChanged();
void tempoChanged(unsigned int tempo); void tempoChanged();
void playbackLabelChanged(QString playbackLabel); void playbackLabelChanged();
void stateChanged(State state); void stateChanged();
public Q_SLOTS: public Q_SLOTS:
void play(); void play();
void pause(); void pause();
...@@ -92,6 +94,7 @@ public Q_SLOTS: ...@@ -92,6 +94,7 @@ public Q_SLOTS:
void setVolume(unsigned int volume); void setVolume(unsigned int volume);
void setTempo(unsigned int tempo); void setTempo(unsigned int tempo);
void setPlaybackLabel(QString playbackLabel); void setPlaybackLabel(QString playbackLabel);
void setState(State state);
void setSong(Song *song); void setSong(Song *song);
void clearSong(); void clearSong();
...@@ -138,6 +141,7 @@ private: ...@@ -138,6 +141,7 @@ private:
EventSchedulingMode m_eventSchedulingMode; EventSchedulingMode m_eventSchedulingMode;
QString m_currentSubscribedPort; QString m_currentSubscribedPort;
QString m_playbackLabel; QString m_playbackLabel;
State m_state;
}; };
#endif // MIDISEQUENCER_H #endif // MIDISEQUENCER_H
......
...@@ -110,6 +110,12 @@ Item { ...@@ -110,6 +110,12 @@ Item {
value: sequencer.playbackLabel value: sequencer.playbackLabel
} }
Binding {
target: midiPlayer
property: "sequencerState"
value: sequencer.state
}
Component.onCompleted: { Component.onCompleted: {
minuetMenu.breadcrumbPressed.connect(exerciseView.clearExerciseGrid) minuetMenu.breadcrumbPressed.connect(exerciseView.clearExerciseGrid)
minuetMenu.breadcrumbPressed.connect(rhythmAnswerView.resetAnswers) minuetMenu.breadcrumbPressed.connect(rhythmAnswerView.resetAnswers)
...@@ -122,8 +128,6 @@ Item { ...@@ -122,8 +128,6 @@ Item {
sequencer.noteOff.connect(pianoView.noteOff) sequencer.noteOff.connect(pianoView.noteOff)
sequencer.allNotesOff.connect(pianoView.allNotesOff) sequencer.allNotesOff.connect(pianoView.allNotesOff)
sequencer.stateChanged.connect(midiPlayer.stateChanged)
exerciseView.answerHoverEnter.connect(pianoView.noteMark) exerciseView.answerHoverEnter.connect(pianoView.noteMark)
exerciseView.answerHoverExit.connect(pianoView.noteUnmark) exerciseView.answerHoverExit.connect(pianoView.noteUnmark)
exerciseView.answerClicked.connect(rhythmAnswerView.answerClicked) exerciseView.answerClicked.connect(rhythmAnswerView.answerClicked)
......
...@@ -29,18 +29,12 @@ Rectangle { ...@@ -29,18 +29,12 @@ Rectangle {
property alias volume: volumeSlider.value property alias volume: volumeSlider.value
property alias tempo: tempoSlider.value property alias tempo: tempoSlider.value
property alias playbackLabel: playbackLabelText.text property alias playbackLabel: playbackLabelText.text
property int sequencerState
signal playActivated signal playActivated
signal pauseActivated signal pauseActivated
signal stopActivated signal stopActivated
function stateChanged(state) {
if (state == MidiSequencer.PlayingState)
item12.state = "Pause"
else
item12.state = "Play"
}
height: childrenRect.height + 15 height: childrenRect.height + 15
anchors { left: parent.left; bottom: parent.bottom } anchors { left: parent.left; bottom: parent.bottom }
color: "black" color: "black"
...@@ -92,40 +86,26 @@ Rectangle { ...@@ -92,40 +86,26 @@ Rectangle {
width: item1.width width: item1.width
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
font.pointSize: 24 font.pointSize: 24
text: "TESTE"
color: "#008000" color: "#008000"
} }
MultimediaButton { MultimediaButton {
id: item12 id: item12
width: playbackLabelText.contentWidth / 2 width: playbackLabelText.contentWidth / 2
anchors.horizontalCenterOffset: -30 anchors.horizontalCenterOffset: -30
anchors { top: playbackLabelText.bottom; horizontalCenter: playbackLabelText.horizontalCenter;} anchors { top: playbackLabelText.bottom; horizontalCenter: playbackLabelText.horizontalCenter }
state: "Play" text: (sequencerState != MidiSequencer.PlayingState) ? i18n("Play"):i18n("Pause")
states: [ source: (sequencerState != MidiSequencer.PlayingState) ? "../images/multimedia-play.png":"../images/multimedia-pause.png"
State { onActivated: {
name: "Play" if (sequencerState == MidiSequencer.StoppedState || sequencerState == MidiSequencer.PausedState)
PropertyChanges { playActivated()
target: item12; else
text: i18n("Play") pauseActivated()
onActivated: playActivated(); }
source: "../images/multimedia-play.png"
}
},
State {
name: "Pause"
PropertyChanges {
target: item12;
text: i18n("Pause")
onActivated: pauseActivated();
source: "../images/multimedia-pause.png"
}
}
]
} }
MultimediaButton { MultimediaButton {
width: playbackLabelText.contentWidth / 2 width: playbackLabelText.contentWidth / 2
anchors.horizontalCenterOffset: +30 anchors.horizontalCenterOffset: +30
anchors { top: playbackLabelText.bottom; horizontalCenter: playbackLabelText.horizontalCenter;} anchors { top: playbackLabelText.bottom; horizontalCenter: playbackLabelText.horizontalCenter }
source: "../images/multimedia-stop.png" source: "../images/multimedia-stop.png"
text: i18n("Stop") text: i18n("Stop")
onActivated: stopActivated() onActivated: stopActivated()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment