summaryrefslogtreecommitdiff
path: root/gistanalysis/src/ofApp.h
blob: 59bbfe6baf461c2ca827a7d7a7999c6981a3501c (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
#pragma once

#include "ofMain.h"
#include "ofxHelios.h"
#include "lineTransformer.h"

//This is included only as a way of getting buffer out of loaded sound.
//There are many other ways you can do that.
//This player includes a version of kissFFT. You can remove the one included in Gist.
//https://github.com/borg/ofxOpenALSoundPlayer
#include "ofxOpenALSoundPlayer.h"
#include "ofxMidi.h"
#include "ofxGui.h"

//Slightly modified to add a dynamic getVariable method to be able to plot based on
//gist feature list
//https://github.com/local-projects/ofxHistoryPlot
#include "ofxHistoryPlot.h"
#include "ofxGist.h"


class Audioplotter{
    //store and draw a numbr of audio samples
    //how best to handle transforms - maybe pass in a transform to be added to 2nd and subsequent
    //how best to handle length of history data - fixed number that can be set, or line budget?
public:
    Audioplotter(int _size=1,bool _joined=true){
        set_size(_size);
        set_joined(_joined);
        colour=ofColor(255,255,255);
    }
    void set_size(int _size){
        history_size=_size;
    }
    void set_joined(bool _joined){
        joined=_joined;
    }
    vector <colourPolyline> output(const ofMatrix4x4 xform=ofMatrix4x4(1.0f,0.0f,0.0f,0.0f,
        0.0f,1.0f,0.0f,0.0f,
        0.0f,0.0f,1.0f,0.0f,
        0.0f,0.0f,0.0f,1.0f),
        float scale=1.0f,
        float decay=-1.0f){
        //destructive or non?
        float fadefactor=decay<0.0f?1.0f-(1.0f/history_size):decay;

        vector <colourPolyline> outdata;

        for (int i=0;i<data.size();i++){
                vector <colourPolyline> newdata;
                for (int j=0;j<data[i].size();j++){
                        colourPolyline line=lineTransformer::polyLineTransform(xform,data[i][j],fadefactor);
                        newdata.push_back(line);
                        outdata.push_back(line);
                }
                data[i]=newdata;
        }
        return outdata;
    }
    void addpoints(vector <float> &audio,int number){
        int num=min(number,(int)audio.size());
        int step=audio.size()/(num+1);
        vector <colourPolyline> newdata;
        if (joined){
                colourPolyline line;
                for (int i=0;i<num;i++){
                        line.addVertex(((step*(i+1))*ofGetWidth())/audio.size(),audio[step*(i+1)]*ofGetHeight(),colour);
                }
                newdata.push_back(line);
        }
        else{
                for (int i=0;i<num;i++){
                        colourPolyline line;
                        line.addVertex(((step*(i+1))*ofGetWidth())/audio.size(),audio[step*(i+1)]*ofGetHeight(),colour);
                        line.addVertex(((step*(i+1))*ofGetWidth())/audio.size()+2,audio[step*(i+1)]*ofGetHeight(),colour);
                        newdata.push_back(line);
                }
        }
        data.insert(data.begin(),newdata);
        while (data.size()>history_size) {
            data.pop_back();
        }
    }
    int numpoints(){
        int num=0;
        for (auto d=data.begin();d!=data.end();d++){
                num+=d->size();
        }
        return num;
    }
private:
    vector < vector<colourPolyline>> data;
    bool joined;
    int history_size;
    ofColor colour;
};


class ofApp : public ofBaseApp, public ofxMidiListener{

	public:
		void setup();
		void update();
		void draw();
                void exit();

		void keyPressed(int key);
		void keyReleased(int key);
		void mouseMoved(int x, int y );
		void mouseDragged(int x, int y, int button);
		void mousePressed(int x, int y, int button);
		void mouseReleased(int x, int y, int button);
		void windowResized(int w, int h);
		void dragEvent(ofDragInfo dragInfo);
		void gotMessage(ofMessage msg);

                void newMidiMessage(ofxMidiMessage& eventArgs);

                void updateOutput(ofEventArgs & args);
                void drawOutput(ofEventArgs & args);
                void outputKeyPressed(ofKeyEventArgs & args);
                void outputKeyReleased(ofKeyEventArgs & args);
                void outputMouseDragged(ofMouseEventArgs & args);
                void outputMousePressed(ofMouseEventArgs & args);
                void outputMouseReleased(ofMouseEventArgs & args);
                void outputWindowResized(ofResizeEventArgs &resizeargs);        
		
        ofSoundStream soundStream;
        ofxOpenALSoundPlayer player;
        void processAudio(float * input, int bufferSize, int nChannels);
        
        void audioIn(float * input, int bufferSize, int nChannels);
        
        vector<float>fftSmoothed;
        vector <float> left;
        vector <float> right;
        vector <float> centre;
        vector <float> volHistory;   
        vector<float>mfccSmoothed;
        float mfccMax;
    
        
        int bufferSize;
        int sampleRate;
        bool useMic;
        
        bool isPaused;
        
        void clear();
        void loadSong(string str);

        
        vector<ofxHistoryPlot *>plots;
        map<string,ofxHistoryPlot *>plotMap;
        
        ofxHistoryPlot* addGraph(string varName,float max,ofColor color);

        
        ofxGist gist;
        void onNoteOn(GistEvent &e);
        void onNoteOff(GistEvent &e);
    
        int noteOnRadius;
    
    
        bool showMFCC;
        vector<ofxHistoryPlot *>mfccPlots;

        ofxHelios laser;

        Audioplotter plotter;


        ofxPanel gui;
        ofParameter<bool> joined;
        ofParameter<int> numPlots;
        ofParameter<float> scalePlot;
        ofParameter<float> decayPlot;
        ofParameter<ofVec2f> xform;
    
};