summaryrefslogtreecommitdiffstats
path: root/src/controllers/midi/midiutils.h
blob: 56e912929c3a31558f57b5743f4550b42673b509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef MIDIUTILS_H
#define MIDIUTILS_H

#include "controllers/midi/midimessage.h"
#include "util/duration.h"

class MidiUtils {
  public:
    static inline unsigned char channelFromStatus(unsigned char status) {
        return status & 0x0F;
    }

    static inline MidiOpCode opCodeFromStatus(unsigned char status) {
        unsigned char opCode = status & 0xF0;
        // MIDI_SYSEX and higher don't have a channel and occupy the entire byte.
        if (opCode == 0xF0) {
            opCode = status;
        }
        return static_cast<MidiOpCode>(opCode);
    }

    static inline bool isMessageTwoBytes(unsigned char opCode) {
        switch (opCode) {
            case MIDI_SONG:
            case MIDI_NOTE_OFF:
            case MIDI_NOTE_ON:
            case MIDI_AFTERTOUCH:
            case MIDI_CC:
                return true;
            default:
                return false;
        }
    }

    static inline bool isClockSignal(const MidiKey& mappingKey) {
        return (mappingKey.key & MIDI_TIMING_CLK) == MIDI_TIMING_CLK;
    }

    static QString opCodeToTranslatedString(MidiOpCode code);
    static QString formatByteAsHex(unsigned char value);
    static QString midiOptionToTranslatedString(MidiOption option);
    static QString formatMidiMessage(const QString& controllerName,
                              unsigned char status, unsigned char control,
                              unsigned char value, unsigned char channel,
                              unsigned char opCode,
                              mixxx::Duration timestamp = mixxx::Duration::fromMillis(0));
    static QString formatSysexMessage(const QString& controllerName,
                              const QByteArray& data,
                              mixxx::Duration timestamp = mixxx::Duration::fromMillis(0));
};


#endif /* MIDIUTILS_H */