21 #include "midiparser.h" 22 #include "rtmidioutput.h" 27 class MIDIParser::MIDIParserPrivate {
29 MIDIParserPrivate(): m_in(0), m_out(0), m_running_status(0) { }
32 unsigned char m_running_status;
35 void processNoteOff(
const int chan,
const int note,
const int vel)
38 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
39 m_out->sendNoteOff(chan, note, vel);
42 m_in->emit midiNoteOff(chan, note, vel);
46 void processNoteOn(
const int chan,
const int note,
const int vel)
49 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
50 m_out->sendNoteOn(chan, note, vel);
53 m_in->emit midiNoteOn(chan, note, vel);
57 void processKeyPressure(
const int chan,
const int note,
const int value)
60 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
61 m_out->sendKeyPressure(chan, note, value);
64 m_in->emit midiKeyPressure(chan, note, value);
68 void processController(
const int chan,
const int control,
const int value)
71 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
72 m_out->sendController(chan, control, value);
75 m_in->emit midiController(chan, control, value);
79 void processProgram(
const int chan,
const int program)
82 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
83 m_out->sendProgram(chan, program);
86 m_in->emit midiProgram(chan, program);
90 void processChannelPressure(
const int chan,
const int value)
93 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
94 m_out->sendChannelPressure(chan, value);
97 m_in->emit midiChannelPressure(chan, value);
101 void processPitchBend(
const int chan,
const int value)
104 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
105 m_out->sendPitchBend(chan, value);
108 m_in->emit midiPitchBend(chan, value);
112 void processSysex(
const QByteArray &data)
115 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
116 m_out->sendSysex(data);
119 m_in->emit midiSysex(data);
123 void processSystemCommon(
const int status)
126 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
127 m_out->sendSystemMsg(status);
130 m_in->emit midiSystemCommon(status);
135 void processSystemRealtime(
unsigned char byte)
138 if (m_in != 0 && m_in->isEnabledMIDIThru() && m_out != 0) {
139 m_out->sendSystemMsg(byte);
142 m_in->emit midiSystemRealtime(byte);
148 MIDIParser::MIDIParser(MIDIInput *in,
QObject *parent) :
150 d(new MIDIParser::MIDIParserPrivate)
156 MIDIParser::~MIDIParser()
161 void MIDIParser::setMIDIThruDevice(MIDIOutput *device)
166 void MIDIParser::parse(
unsigned char byte)
168 unsigned char status;
171 if (byte >= MIDI_STATUS_REALTIME) {
172 d->processSystemRealtime(byte);
175 d->m_buffer.append(byte);
177 while(d->m_buffer.length() > 0) {
178 status =
static_cast<unsigned>(d->m_buffer.at(0));
179 if (status == MIDI_STATUS_SYSEX) {
180 if (byte == MIDI_STATUS_ENDSYSEX) {
181 d->processSysex(d->m_buffer);
186 if (status > MIDI_STATUS_SYSEX &&
187 status < MIDI_STATUS_ENDSYSEX) {
188 d->processSystemCommon(status);
191 if (status < MIDI_STATUS_SYSEX &&
192 status >= MIDI_STATUS_NOTEOFF) {
193 d->m_running_status = status;
194 chan = status & MIDI_CHANNEL_MASK;
195 status = status & MIDI_STATUS_MASK;
197 case MIDI_STATUS_NOTEOFF:
198 if (d->m_buffer.length() < 3)
200 m1 =
static_cast<unsigned>(d->m_buffer.at(1));
201 m2 =
static_cast<unsigned>(d->m_buffer.at(2));
202 d->processNoteOff(chan, m1, m2);
204 case MIDI_STATUS_NOTEON:
205 if (d->m_buffer.length() < 3)
207 m1 =
static_cast<unsigned>(d->m_buffer.at(1));
208 m2 =
static_cast<unsigned>(d->m_buffer.at(2));
209 d->processNoteOn(chan, m1, m2);
211 case MIDI_STATUS_KEYPRESURE:
212 if (d->m_buffer.length() < 3)
214 m1 =
static_cast<unsigned>(d->m_buffer.at(1));
215 m2 =
static_cast<unsigned>(d->m_buffer.at(2));
216 d->processKeyPressure(chan, m1, m2);
218 case MIDI_STATUS_CONTROLCHANGE:
219 if (d->m_buffer.length() < 3)
221 m1 =
static_cast<unsigned>(d->m_buffer.at(1));
222 m2 =
static_cast<unsigned>(d->m_buffer.at(2));
223 d->processController(chan, m1, m2);
225 case MIDI_STATUS_PROGRAMCHANGE:
226 if (d->m_buffer.length() < 2)
228 m1 =
static_cast<unsigned>(d->m_buffer.at(1));
229 d->processProgram(chan, m1);
231 case MIDI_STATUS_CHANNELPRESSURE:
232 if (d->m_buffer.length() < 2)
234 m1 =
static_cast<unsigned>(d->m_buffer.at(1));
235 d->processChannelPressure(chan, m1);
237 case MIDI_STATUS_PITCHBEND:
238 if (d->m_buffer.length() < 3)
240 m1 =
static_cast<unsigned>(d->m_buffer.at(1));
241 m2 =
static_cast<unsigned>(d->m_buffer.at(2));
242 v = m1 + m2 * 0x80 - 0x2000;
243 d->processPitchBend(chan, v);
248 d->m_buffer.insert(0, d->m_running_status);
253 void MIDIParser::parse(QByteArray bytes)
255 foreach(
unsigned char byte, bytes) {
The QObject class is the base class of all Qt objects.