Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Json link refresh lagg

$
0
0

@noobCoder wrote:

hey,
im trying to get data from an api key, this works fine. but i need to refresh that link to get the newest data, then my program starts to lagg, anybody know why?

#include "ofApp.h"


//--------------------------------------------------------------
void ofApp::setup(){
    ofSetFrameRate(30);
    //load and play sound
    sound.load("sound.mp3");
    //sound.play();

    //make sphere, set resolution(detail(vectors)), get the mesh and put it in planet mesh.
    sphere.setRadius(100);
    sphere.setResolution(resolution);
    planet = sphere.getMesh();

    //the usual make fftsmooth and set bands
    fftSmooth = new float [8192];
    bands = resolution+12;
    for (int i=0; i<bands; i++) {
        fftSmooth[i] = 0;
    }


    //store original vertices
    originVerts = planet.getVertices();
    for( int i = 0; i < originVerts.size(); i++ ) {
        originVerts[i].normalize();
    }

    //give a nice color
    vector<ofVec3f>& verts = planet.getVertices();
    for(int i = 0; i < verts.size(); i++){
        planet.addColor(ofFloatColor( float(i)/planet.getVertices().size(), 0, 1.0 - (float(i)/planet.getVertices().size()) ));
    }
    volume = 1;

    endtime = 15000;
}

//--------------------------------------------------------------
void ofApp::update(){


    //get the spectrum
    float * spectrum = ofSoundGetSpectrum(bands);
    for (int i=0; i<bands; i++) {
        fftSmooth[i] *= 0.9f;
        if (fftSmooth[i] < spectrum[i]) {
            fftSmooth[i] = spectrum[i];
        }
    }

    //the magic
    float baseRadius = 100.f;                                                   //initial radius of sphere
    vector<ofVec3f>& verts = planet.getVertices();                              //get the current vector points to manipulate
    for(int i = 0; i < verts.size(); i++){                                      // standard forloop that loop through entire sphere
        int fftindex = ofMap( i, 0, verts.size(), 3, bands-10, true );      //map the index, i don't use the first 2 and the last 10,
                                                                                                        because they don't have great values to see
        if( fftindex >= bands) fftindex = bands-1;                          //failsafe, if index gets out of bounds it goes back
        float fftRadius = fftSmooth[fftindex] * 100.f;                      //get the spectrum and store it
         verts[i] = originVerts[i] * (baseRadius + fftRadius);               //change the sphere radius to the radius + fftsmooth
                                                                                                          value
    }



    jsondata.open("https://api.thingspeak.com/channels/350905/fields/1.json?
      api_key=MTK6T8XV424RLQTD&results=1");

}

//--------------------------------------------------------------
void ofApp::draw(){
    ofBackground(180, 180, 180);
    timer = ofGetElapsedTimeMillis() - starttime;
    cam.begin();


    planet.drawWireframe();
    cam.end();


    if (timer >= endtime) {
        starttime = ofGetElapsedTimeMillis();
        //internetData();
    }
};

void ofApp::keyPressed(int key){
    if (key == 'a') {
        sound.play();
    }
    if(key == 's'){
        sound.stop();
    }
}
void ofApp::internetData(){
    resp = ofLoadURL("https://api.thingspeak.com/channels/354585/fields/1.json?
    api_key=9OVAHUOI8L88Z4PX&results=1");

    textData.clear();
    textData = resp.data.getText();
    words.clear();
    words = ofSplitString(textData, "1");

    if( words[words.size()-2] == "2"){
        if(sound.isPlaying() == false){
            sound.play();
        }
    }
    if( words[words.size()-2] == "0"){
        if(sound.isPlaying() == true){
            sound.stop();
        }
     }
     cout << words[words.size()-2] << endl;
}

in this code i make an audio controlled sphere, i tried working with http response, this gives lagg if i refresh it while program is running, the reason i put it on a timer, the same goes for the json, i need to refresh it to get the newest data from the webhook but when i do my program laggs. anybody know how i can remove the lag, or even check if the file is gonna change so i can only refresh it when new data is comming. else its gonna be on a timer to minimize it.

thanks for reading

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles