/
ofApp.cpp
executable file
·140 lines (114 loc) · 3.9 KB
/
ofApp.cpp
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (c) 2013 Dan Wilcox <danomatika@gmail.com>
*
* BSD Simplified License.
* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
*
* See https://github.com/danomatika/ofxMidi for documentation
*
*/
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofSetVerticalSync(true);
ofBackground(255, 255, 255);
ofSetLogLevel(OF_LOG_VERBOSE);
// print input ports to console
midiIn.listInPorts();
// open port by number (you may need to change this)
midiIn.openPort(1);
//midiIn.openPort("IAC Pure Data In"); // by name
//midiIn.openVirtualPort("ofxMidiIn Input"); // open a virtual port
// don't ignore sysex, timing, & active sense messages,
// these are ignored by default
midiIn.ignoreTypes(false, false, false);
// add ofApp as a listener
midiIn.addListener(this);
// print received messages to the console
midiIn.setVerbose(true);
}
//--------------------------------------------------------------
void ofApp::update() {
}
//--------------------------------------------------------------
void ofApp::draw() {
for(unsigned int i = 0; i < midiMessages.size(); ++i) {
ofxMidiMessage &message = midiMessages[i];
int x = 10;
int y = i*40 + 40;
// draw the last recieved message contents to the screen,
// this doesn't print all the data from every status type
// but you should get the general idea
stringstream text;
text << ofxMidiMessage::getStatusString(message.status);
while(text.str().length() < 16) { // pad status width
text << " ";
}
ofSetColor(127);
if(message.status < MIDI_SYSEX) {
text << "chan: " << message.channel;
if(message.status == MIDI_CONTROL_CHANGE) {
text << "\tctl: " << message.control;
ofDrawRectangle(x + ofGetWidth()*0.2, y + 12,
ofMap(message.control, 0, 127, 0, ofGetWidth()*0.2), 10);
}
else if(message.status == MIDI_PITCH_BEND) {
text << "\tval: " << message.value;
ofDrawRectangle(x + ofGetWidth()*0.2, y + 12,
ofMap(message.value, 0, MIDI_MAX_BEND, 0, ofGetWidth()*0.2), 10);
}
else {
text << "\tpitch: " << message.pitch;
ofDrawRectangle(x + ofGetWidth()*0.2, y + 12,
ofMap(message.pitch, 0, 127, 0, ofGetWidth()*0.2), 10);
text << "\tvel: " << message.velocity;
ofDrawRectangle(x + (ofGetWidth()*0.2 * 2), y + 12,
ofMap(message.velocity, 0, 127, 0, ofGetWidth()*0.2), 10);
}
text << " "; // pad for delta print
}
text << "delta: " << message.deltatime;
ofSetColor(0);
ofDrawBitmapString(text.str(), x, y);
text.str(""); // clear
}
}
//--------------------------------------------------------------
void ofApp::exit() {
// clean up
midiIn.closePort();
midiIn.removeListener(this);
}
//--------------------------------------------------------------
void ofApp::newMidiMessage(ofxMidiMessage& msg) {
// add the latest message to the message queue
midiMessages.push_back(msg);
// remove any old messages if we have too many
while(midiMessages.size() > maxMessages) {
midiMessages.erase(midiMessages.begin());
}
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
switch(key) {
case '?':
midiIn.listInPorts();
break;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key) {
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y) {
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button) {
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {
}
//--------------------------------------------------------------
void ofApp::mouseReleased() {
}