summaryrefslogtreecommitdiff
path: root/liveengine/src/testApp.cpp
blob: c23ec86d3a07f084a30ff28e6dbcb4044df2065f (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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
    int midiPort=0;
    midiChannel=0;
    if( !XML.loadFile("settings.xml") ){
		printf("unable to load settings.xml check data/ folder\n");
	}else{
		printf("settings loaded!\n");
		midiPort=ofToInt(XML.getAttribute("liveEngine", "port", "0")); //default to 0/all
        midiChannel=ofToInt(XML.getAttribute("liveEngine", "channel", "0"));
        if (midiChannel) printf("listening on port %d, midi channel %d\n",midiPort,midiChannel);
        else printf("listening on port %d, all midi channels\n",midiPort);
    }

    midiIn.listPorts();
    midiIn.openPort(midiPort);
    midiIn.addListener(this);

    // to register only to one controller pass the id as first argument
    // midiIn.addListener(84,this);

    // to debug
    // midiIn.setVerbose(true);

    controllers=new unsigned char[NUM_CONTROLLERS];
    memset(controllers,NUM_CONTROLLERS,0);
	note=0;

    makeColours();

    controlColours=false;

    grab.allocate(ofGetWidth(), ofGetHeight(),GL_RGB);
	//grab.setUseTexture(true);

   showFPS=false;
   ofBackground(0,0,0);

   ofSetBackgroundAuto(false);

   xshift=-1;
   yshift=-1;



   mode=BLOCKS;

   lastnoteTime=ofGetElapsedTimef();
   decayTime=2.0f;

   //ofSetVerticalSync(true); deosn't seem effective
   //glXSwapIntervalSGI(1);
    /*
   printf("hue of red is %f\n",ofColor(255,0,0).getHue());
   printf("hue of green is %f\n",ofColor(0,255,0).getHue());
   printf("hue of blue is %f\n",ofColor(0,0,255).getHue());
   hue is float from 0.0-255.0
   */
}

void testApp::makeColours() {
	controller_colours=new ofColor[NUM_CONTROLLERS];
	for (int i=0;i<NUM_CONTROLLERS;i++) controller_colours[i]=ofColor::fromHsb(ofRandom(255), 255, 255);
}
//--------------------------------------------------------------
void testApp::update(){
	//for (int i=0;i<numLayers;i++) layers[i]->update();
}

//--------------------------------------------------------------
void testApp::draw(){
	ofSetColor(255,255,255);
	grab.loadScreenData( 0, 0, ofGetWidth(), ofGetHeight() );

	/* can this work?
    grab.setAnchorPoint(xshift,yshift);
    grab.setTextureWrap( GL_WRAP_BORDER,  GL_WRAP_BORDER);
	grab.draw(0,0); // xshift,yshift);
	*/
	int startx=(xshift>0?xshift-ofGetWidth():xshift);
	int starty=(yshift>0?yshift-ofGetHeight():yshift);

	float notewidth=ofGetWidth()/NUM_NOTES;
	float noteheight=ofGetHeight()/NUM_CONTROLLERS;

	float lamda=max(0.0f,1.0f-((ofGetElapsedTimef()-lastnoteTime)/decayTime));

	if (note>0) {
		switch(mode) {
			case BLOCKS:
				for (int i=0;i<NUM_CONTROLLERS;i++){
				ofSetColor(ofColor((controller_colours[i].r*controllers[i])>>7,(controller_colours[i].g*controllers[i])>>7,(controller_colours[i].b*controllers[i])>>7));
				ofRect((note-START_NOTE)*notewidth,i*noteheight,notewidth,noteheight);
				}
				break;
			case LIST:
                if (list.lock()) { //if playlist is loaded
                    ofPushMatrix();
                    //ofScale(sin(ofGetElapsedTimef())+1.1,sin(ofGetElapsedTimef())+1.1);
                    if (list.layers.find(note)!=list.layers.end()) {
                        if (controlColours) list.layers[note]->draw(lamda,controllers);
                        else list.layers[note]->draw(lamda);
                    }
                    ofPopMatrix();
                    list.unlock();
                }
				break;
		}
	}

	//for (int i=0;i<numLayers;i++) layers[i]->draw();
	ofSetColor(255,255,255);
	if (showFPS) ofDrawBitmapString(ofToString(ofGetFrameRate(), 2),20,20);

}

//--------------------------------------------------------------
void testApp::keyPressed  (int key){
        if(key == 'q'){
			list.load("insects.xml");
		}
		if(key == 'w'){
			list.load("barcelona.xml");
		}
		if(key == 'e'){
			list.load("organs.xml");
		}
		if(key == 'r'){
			list.load("tai_chi.xml");
		}
		if(key == 's'){
			XML.saveFile("settings.xml");
			printf("settings saved!\n");
		}
		if(key == 'f'){
			toggleFPS();
		}
		if(key >='0' && key <= '9'){
			mode=key-'0';
		}
		if(key == OF_KEY_LEFT){
			xshift++;

		}
		if(key == OF_KEY_RIGHT){
			xshift--;
		}
		if(key == OF_KEY_DOWN){
			yshift--;
		}
		if(key == OF_KEY_UP){
			yshift++;
		}
        if(key == '='){
			makeColours();
		}
		if(key == '-'){
			controlColours=!controlColours;
		}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){


}
void testApp::mousePressed(int x, int y, int button) {
}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}

void testApp::toggleFPS(){
	showFPS=!showFPS;
}

void testApp::newMidiMessage(ofxMidiEventArgs& eventArgs){

    //newMessage(eventArgs.port, eventArgs.channel, eventArgs.byteTwo, eventArgs.timestamp);

//byteOne : message type

    /*
    int 	port;
	int		channel;
	int 	status;
	int 	byteOne;
	int 	byteTwo;
	double 	timestamp;
	*/

    //printf("%d %d %d %d %d\n",eventArgs.port,eventArgs.channel,eventArgs.status,eventArgs.byteOne,eventArgs.byteTwo);

    bool noteOn; //this old thing!

    if ((midiChannel==0)||(eventArgs.channel==midiChannel)) {
        switch(eventArgs.status) {
            case 144: //noteon-off channel 0
                noteOn=(eventArgs.byteTwo==0?false:true);
                //for (int i=0;i<numLayers;i++){
                //    if (layers[i]->note==eventArgs.byteOne) layers[i]->setActive(noteOn);
                //}
                printf("note: %i %i\n",eventArgs.byteOne,eventArgs.byteTwo);
                note=eventArgs.byteOne;
                lastnoteTime=ofGetElapsedTimef();
                break;
            case 176: //control change channel 0
                //for (int i=0;i<numLayers;i++){
                //    if (layers[i]->mix==eventArgs.byteOne) layers[i]->setMixAmt(((float)eventArgs.byteTwo)/127.0f);
                //}
                printf("cc: %i %i\n",eventArgs.byteOne,eventArgs.byteTwo);
                controllers[eventArgs.byteOne-START_CONTROLLER]=eventArgs.byteTwo;
        }
    }
}