summaryrefslogtreecommitdiff
path: root/liveengine/src/testApp.cpp
blob: 5c337b0077ab87b438b50bc1cea43b65a91ac582 (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
#include "testApp.h"


//--------------------------------------------------------------
void testApp::setup(){
    int midiPort=0;
    midiChannel=0;
    //numLayers=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);
         if(XML.pushTag("liveEngine")) {
             /*
             numLayers=XML.getNumTags("layer");
              if(numLayers) {
                    layers=new globeLayer*[numLayers];
                     for (int i=0;i<numLayers;i++){
                         XML.pushTag("layer",i);
			    string layerType=XML.getAttribute("content", "type", "");
			    XML.pushTag("content");
				    float x=ofToFloat(XML.getAttribute("pos", "x", ""));
				    float y=ofToFloat(XML.getAttribute("pos", "y", ""));
				    float w=ofToFloat(XML.getAttribute("pos", "w", ""));
				    float h=ofToFloat(XML.getAttribute("pos", "h", ""));
				    int midiNote=ofToInt(XML.getAttribute("midi", "note", "0"));
				    int midiMix=ofToInt(XML.getAttribute("midi", "mix", "0"));
				    if (layerType=="player") {
					string fileName=XML.getAttribute("media", "name", "0");
					layers[i]=new globePlayer(x,y,w,h,midiNote,midiMix,fileName);
				    }
				    if (layerType=="grabber") {
                    bool deInt=ofToInt(XML.getAttribute("grab", "deInt", "0"));
					layers[i]=new globeGrabber(x,y,w,h,midiNote,midiMix,deInt);
				    }
			    XML.popTag();
                         XML.popTag();
                    }
              }
              */
         }
         //if (numLayers==0) printf("no layers loaded!\n");
	}

    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;

	controller_colours=new ofColor[NUM_CONTROLLERS];
	for (int i=0;i<NUM_CONTROLLERS;i++) controller_colours[i]=ofColor::fromHsb(ofRandom(255), 255, 255);

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

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

//--------------------------------------------------------------
void testApp::update(){
	//for (int i=0;i<numLayers;i++) layers[i]->update();
}

//--------------------------------------------------------------
void testApp::draw(){
    ofSetColor(255,255,255);
    grab.grabScreen( 0, 0, ofGetWidth(), ofGetHeight() );
    grab.update();
    grab.reloadTexture();
    ofBackground(0,0,0);
    grab.draw( 1,1);

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

    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*notewidth,i*noteheight,notewidth,noteheight);
    }

    //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 == 's'){
			XML.saveFile("settings.xml");
			printf("settings saved!\n");
		}
		if(key == 'f'){
			toggleFPS();
		}
}

//--------------------------------------------------------------
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-START_NOTE;
                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;
        }
    }
}