midisequencer.h 4.99 KB
Newer Older
1 2
/****************************************************************************
**
Sandro Andrade's avatar
Sandro Andrade committed
3
** Copyright (C) 2016 by Sandro S. Andrade <sandroandrade@kde.org>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License as
** published by the Free Software Foundation; either version 2 of
** the License or (at your option) version 3 or any later version
** accepted by the membership of KDE e.V. (or its successor approved
** by the membership of KDE e.V.), which shall act as a proxy
** defined in Section 14 of version 3 of the license.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program.  If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#ifndef MIDISEQUENCER_H
#define MIDISEQUENCER_H

26 27
#include "song.h"

Sandro Andrade's avatar
Sandro Andrade committed
28
#include <QObject>
29 30 31 32 33 34 35 36 37 38

#include <drumstick/alsaqueue.h>

namespace drumstick {
    class QSmf;
    class MidiClient;
    class MidiPort;
    class SequencerEvent;
}

39 40
class MidiSequencerOutputThread;

41 42 43 44
class MidiSequencer : public QObject
{
    Q_OBJECT

45 46 47 48
    Q_PROPERTY(int pitch READ pitch WRITE setPitch NOTIFY pitchChanged)
    Q_PROPERTY(unsigned int volume READ volume WRITE setVolume NOTIFY volumeChanged)
    Q_PROPERTY(unsigned int tempo READ tempo WRITE setTempo NOTIFY tempoChanged)
    Q_PROPERTY(QString playbackLabel READ playbackLabel WRITE setPlaybackLabel NOTIFY playbackLabelChanged)
49 50
    Q_ENUMS(State)
    Q_PROPERTY(State state READ state NOTIFY stateChanged)
51
    Q_ENUMS(PlayMode)
52

53
public:
Sandro Andrade's avatar
Sandro Andrade committed
54
    explicit MidiSequencer(QObject *parent = 0);
55 56
    virtual ~MidiSequencer();

57 58 59 60 61
    enum EventSchedulingMode {
        FROM_ENGINE = 0,
        EXPLICIT,
        DAMAGED
    };
62 63 64 65 66
    enum State {
        StoppedState = 0,
        PlayingState,
        PausedState
    };
67 68 69 70 71
    enum PlayMode {
        ScalePlayMode = 0,
        ChordPlayMode,
        RhythmPlayMode
    };
72

73
    void subscribeTo(const QString &portName);
74
    void openFile(const QString &fileName);
75
    void appendEvent(drumstick::SequencerEvent *ev, unsigned long tick);
76
    QStringList availableOutputPorts() const;
77
    EventSchedulingMode schedulingMode() const;
78

79 80 81 82
    int pitch() const;
    unsigned int volume() const;
    unsigned int tempo() const;
    QString playbackLabel() const;
83
    State state() const;
84

85
Q_SIGNALS:
86 87
    void noteOn(int chan, int pitch, int vel);
    void noteOff(int chan, int pitch, int vel);
88
    void allNotesOff();
89 90 91 92 93 94
    void pitchChanged();
    void volumeChanged();
    void tempoChanged();
    void playbackLabelChanged();
    void stateChanged();

95
public Q_SLOTS:
96 97 98
    void play();
    void pause();
    void stop();
99 100 101 102
    void setPitch(int pitch);
    void setVolume(unsigned int volume);
    void setTempo(unsigned int tempo);
    void setPlaybackLabel(QString playbackLabel);
103
    void setState(State state);
104
    void setSong(Song *song);
105
    void clearSong();
106 107
    void generateSong(QJsonArray selectedOptions);
    void setPlayMode(PlayMode playMode);
108

109
    // Slots for events generated when reading a MIDI file
110
    void SMFHeader(int format, int ntrks, int division);
111 112 113 114 115 116 117 118 119 120 121 122
    drumstick::SequencerEvent *SMFNoteOn(int chan, int pitch, int vel);
    drumstick::SequencerEvent *SMFNoteOff(int chan, int pitch, int vel);
    drumstick::SequencerEvent *SMFKeyPress(int chan, int pitch, int press);
    drumstick::SequencerEvent *SMFCtlChange(int chan, int ctl, int value);
    drumstick::SequencerEvent *SMFPitchBend(int chan, int value);
    drumstick::SequencerEvent *SMFProgram(int chan, int patch);
    drumstick::SequencerEvent *SMFChanPress(int chan, int press);
    drumstick::SequencerEvent *SMFSysex(const QByteArray &data);
    drumstick::SequencerEvent *SMFText(int typ, const QString &data);
    drumstick::SequencerEvent *SMFTempo(int tempo);
    drumstick::SequencerEvent *SMFTimeSig(int b0, int b1, int b2, int b3);
    drumstick::SequencerEvent *SMFKeySig(int b0, int b1);
123 124
    void SMFError(const QString &errorStr);

125
private Q_SLOTS:
126 127
    // Slots for events generated when playing a MIDI
    void eventReceived(drumstick::SequencerEvent *ev);
128

129
    void outputThreadStopped();
130
    void resetMidiPlayer();
131

132 133
private:
    void appendEvent(drumstick::SequencerEvent *ev);
134

135
private:
136
    int m_outputPortId;
137
    int m_inputPortId;
138
    int m_queueId;
139
    unsigned long m_tick;
140
    Song *m_song;
141
    drumstick::MidiPort *m_outputPort;
142
    drumstick::MidiPort *m_inputPort;
143 144 145 146
    drumstick::QSmf *m_smfReader;
    drumstick::MidiQueue *m_queue;
    drumstick::QueueTempo m_firstTempo;
    drumstick::MidiClient *m_client;
147
    MidiSequencerOutputThread *m_midiSequencerOutputThread;
148
    EventSchedulingMode m_eventSchedulingMode;
149
    QString m_currentSubscribedPort;
150
    QString m_playbackLabel;
151
    State m_state;
152
    PlayMode m_playMode;
153 154
};

Sandro Andrade's avatar
Sandro Andrade committed
155 156
#endif // MIDISEQUENCER_H