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

//--------------------------------------------------------------
void testApp::setup() {

    ofSetVerticalSync(true);
    ofSetCircleResolution(80);
    ofBackground(54, 54, 54);   
    
    // 0 output channels, 
    // 2 input channels
    // 44100 samples per second
    // 256 samples per buffer
    // 4 num buffers (latency)
    
    soundStream.listDevices();
    
    //if you want to set a different device id 
    //soundStream.setDeviceID(0); //bear in mind the device id corresponds to all audio devices, including  input-only and output-only devices.
    
    int rate =44100;
    int channels=2;
    int outputNo=0;

    string soname="qm-vamp-plugins";
    string id="qm-tempotracker";

    int bufferSize = vamphost.init(soname,id,channels,rate,outputNo);
    
    
    left.assign(bufferSize, 0.0);
    right.assign(bufferSize, 0.0);
    volHistory.assign(400, 0.0);
    
    bufferCounter   = 0;
    drawCounter     = 0;
    smoothedVol     = 0.0;
    scaledVol       = 0.0;
    
    //if you want to set a different device id 
    //soundStream.setDeviceID(0); //bear in mind the device id corresponds to all audio devices, including  input-only and output-only devices.
    
    soundStream.setup(this, 0, channels, rate, bufferSize, 4);

    
        
}


//--------------------------------------------------------------
void testApp::update(){
     //lets scale the vol up to a 0-1 range 
    scaledVol = ofMap(smoothedVol, 0.0, 0.17, 0.0, 1.0, true);

    //lets record the volume into an array
    volHistory.push_back( scaledVol );
    
    //if we are bigger the the size we want to record - lets drop the oldest value
    if( volHistory.size() >= 400 ){
        volHistory.erase(volHistory.begin(), volHistory.begin()+1);
    }
}

//--------------------------------------------------------------
void testApp::draw(){
ofSetColor(225);
    ofDrawBitmapString("AUDIO INPUT EXAMPLE", 32, 32);
    ofDrawBitmapString("press 's' to unpause the audio\n'e' to pause the audio", 31, 92);
    
    ofNoFill();
    
    // draw the left channel:
    ofPushStyle();
        ofPushMatrix();
        ofTranslate(32, 170, 0);
            
        ofSetColor(225);
        ofDrawBitmapString("Left Channel", 4, 18);
        
        ofSetLineWidth(1);  
        ofRect(0, 0, 512, 200);

        ofSetColor(245, 58, 135);
        ofSetLineWidth(3);
                    
            ofBeginShape();
            for (int i = 0; i < left.size(); i++){
                ofVertex(i*2, 100 -left[i]*180.0f);
            }
            ofEndShape(false);
            
        ofPopMatrix();
    ofPopStyle();

    // draw the right channel:
    ofPushStyle();
        ofPushMatrix();
        ofTranslate(32, 370, 0);
            
        ofSetColor(225);
        ofDrawBitmapString("Right Channel", 4, 18);
        
        ofSetLineWidth(1);  
        ofRect(0, 0, 512, 200);

        ofSetColor(245, 58, 135);
        ofSetLineWidth(3);
                    
            ofBeginShape();
            for (int i = 0; i < right.size(); i++){
                ofVertex(i*2, 100 -right[i]*180.0f);
            }
            ofEndShape(false);
            
        ofPopMatrix();
    ofPopStyle();
    
    // draw the average volume:
    ofPushStyle();
        ofPushMatrix();
        ofTranslate(565, 170, 0);
            
        ofSetColor(225);
        ofDrawBitmapString("Scaled average vol (0-100): " + ofToString(scaledVol * 100.0, 0), 4, 18);
        ofRect(0, 0, 400, 400);
        
        ofSetColor(245, 58, 135);
        ofFill();       
        ofCircle(200, 200, scaledVol * 190.0f);
        
        //lets draw the volume history as a graph
        ofBeginShape();
        for (int i = 0; i < volHistory.size(); i++){
            if( i == 0 ) ofVertex(i, 400);

            ofVertex(i, 400 - volHistory[i] * 70);
            
            if( i == volHistory.size() -1 ) ofVertex(i, 400);
        }
        ofEndShape(false);      
            
        ofPopMatrix();
    ofPopStyle();
    
    drawCounter++;
    
    ofSetColor(225);
    string reportString = "buffers received: "+ofToString(bufferCounter)+"\ndraw routines called: "+ofToString(drawCounter)+"\nticks: " + ofToString(soundStream.getTickCount());
    ofDrawBitmapString(reportString, 32, 589);
    
}

void testApp::audioIn(float * input, int bufferSize, int nChannels){    

    float curVol = 0.0;
    
    // samples are "interleaved"
    int numCounted = 0; 

    //lets go through each sample and calculate the root mean square which is a rough way to calculate volume   
    for (int i = 0; i < bufferSize; i++){
        left[i]     = input[i*2]*0.5;
        right[i]    = input[i*2+1]*0.5;

        curVol += left[i] * left[i];
        curVol += right[i] * right[i];
        numCounted+=2;
    }
    
    //this is how we get the mean of rms :) 
    curVol /= (float)numCounted;
    
    // this is how we get the root of rms :) 
    curVol = sqrt( curVol );
    
    smoothedVol *= 0.93;
    smoothedVol += 0.07 * curVol;
    
    bufferCounter++;

    vamphost.process_frame(input,bufferSize);

    
}

//--------------------------------------------------------------
void testApp::exit(){
   vamphost.cleanup();
}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
    if( key == 's' ){
        soundStream.start();
    }
    
    if( key == 'e' ){
        soundStream.stop();
    }
}

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

}

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

}

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

}

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

}

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

}

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

}