How could I reduce latency in 3D visual with sound

#1

Hi, I’m making audio-visual project that is a track which is already composed, and 3D is written in Xcode.

Problem: To be clear, there is no any audio latency in openFrameworks, however, when 3D interacts with music, 3D showing late.

How could I reduce delay of visual? I just want them to release without delay.

Best wishes

#2

Hi, can you be more specific about what you are doing. Paste code, or explain how the music interacts with your 3d stuff, otherwise there are just too many possible answers.
best

#3

void ofApp::setup(){

ofEnableDepthTest();

ofEnableLighting();

ofEnableNormalizedTexCoords();
light.setPointLight();
light.setDiffuseColor(ofFloatColor(0.7, 0.7, 0.7));
light.setAmbientColor(ofFloatColor(0.9, 0.9, 0.9));


light.setPosition(ofGetWidth()*.5, ofGetHeight()*.7, 300);
boxMaterial.setDiffuseColor(ofFloatColor(1.0, 0.0, 0.0));


boxRotateX = 0;

boxX = ofGetWidth()/2;
boxY = ofGetHeight()/2;
boxZ = 0;


dBox.setTexture(ofImage("s1.jpg"));
dBox2.setTexture(ofImage("s2.jpg"));
dBox3.setTexture(ofImage("s4.jpg"));


dBox.setWidth(25);
dBox.setHeight(500);
dBox.setDepth(500);

dBox.moveTo(0, ofGetHeight()/2, 0);

dBox2.setWidth(25);
dBox2.setHeight(500);
dBox2.setDepth(500);
dBox2.moveTo(ofGetWidth(), ofGetHeight()/2, 0);

dBox3.setWidth(ofGetWidth());
dBox3.setHeight(500);
dBox3.setDepth(50); //(25)
dBox3.moveTo(ofGetWidth()/2, ofGetHeight()/2, -540);



//sound
space.loadSound("Space.wav");
space.setVolume(1.0);
space.setLoop(true);


//tosen
anime = false;

fftSmooth = new float [8192];
for (int i = 0; i < 8192; i++){
    fftSmooth [i] = 0;
}

bands = 64;

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

boxRotateX += 0.25;
dBox.rotateXDeg(1.0);
dBox2.rotateXDeg(1.0);
dBox3.rotateXDeg(1.0);

ofSoundUpdate();
float * value = ofSoundGetSpectrum(bands);
for (int i = 0; i < bands; i++){
    fftSmooth[i] *= 0.5f;
    if(fftSmooth[i] < value[i]){
        fftSmooth[i] = value[i];
    }
}

}

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

ofSetColor(255,255,255,220);

light.enable();
ofSetColor(220,220,220,226);
ofPushMatrix();
ofTranslate(ofGetWidth()/2.0, ofGetHeight()/2.0,-742.0);
ofRotate(180);
textureImg.bind();
boxback.draw();
boxback.setWidth(ofGetWidth()*2.0);
boxback.setHeight(ofGetHeight()*2.0);

textureImg.unbind();

ofPopMatrix();



dBox.draw();
dBox2.draw();
dBox3.draw();

if(anime){
    for(double i = 0.0; i < 3.0; i++){
        ofRotateXDeg(boxRotateX * -i);
        
    }
    

    ofSetColor(220,220,220,226);
    for(int i = 0; i < bands; i++){
        ofTranslate(ofGetWidth()/2.0, ofGetHeight()/2.0,-(fftSmooth[i]*150));
        ofRotate(180);
        tosenImg.bind();
        tosen.draw();
        dBox.draw();
        dBox2.draw();
        tosen.setWidth(ofGetWidth()/2.0);
        tosen.setHeight(ofGetHeight()/20.0);
        tosen.setDepth(-500.0);
        tosenImg.unbind();
        
        
    }

    
    
}



light.disable();

}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
switch (key) {
case ‘1’ :
space.play();
anime = true;
dBox3.draw();
break;
case ‘2’ :
space.stop();
anime = false;
break;

}

}

#4

This is the code, sorry this is quite long.
I’m planning is that image making 3D, interacting with volume and frequencies.

3D works well with music, changing size.

But 3D visual responds slowly when the 3D size changed by sound.

Should I change other codes or remove some codes?

Best wishes

#5

Hi,
first, when posting code please select it all and then clic the </> button so it will get all preformated nicely which makes it easier to reaad.
Regarding your problem, you are smoothing the fft so that will induce a latency, if you use the raw fft values you can get a more direct reaction.
if you just replace the following lines

float * value = ofSoundGetSpectrum(bands);
for (int i = 0; i < bands; i++){
    fftSmooth[i] *= 0.5f;
    if(fftSmooth[i] < value[i]){
        fftSmooth[i] = value[i];
    }
}

by

fftSmooth = ofSoundGetSpectrum(bands);

you should get a reaction without smoothing.