~connorbell/ShaderChain

6cfafd75ab3c9d704ea5607fe3390916b9d3f2ef — Eris Fairbanks 4 years ago 450a952
Changing time and time delta precision from float to double because accumulation error was throwing off audio sync. Adding a parameter to modify shader start time, which is useful for rendering different spots in time and audio dependent shaders.
M src/FFTManager.cpp => src/FFTManager.cpp +2 -2
@@ 149,11 149,11 @@ void FFTManager::setPaused(bool val) {
    isPaused = val;
}

void FFTManager::setTime(float time) {
void FFTManager::setTime(double time) {
    //soundPlayer.setPaused(true);

    if (currentState != InputStateNone) {
        float t = fmod(time,soundFileDuration) * 1000.;
        double t = fmod(time,soundFileDuration) * 1000.;
        soundPlayer.setPositionMS(t);
    }
}

M src/FFTManager.h => src/FFTManager.h +1 -1
@@ 36,7 36,7 @@ public:
    void loadSoundFile(string filePath);
    void UpdateShader(ofShader *shader, int textureIndex);
    void setPaused(bool val);
    void setTime(float time);
    void setTime(double time);
    void resetSongIfPlaying();
    void addToGui(ofxGuiContainer *container);


M src/PNGRenderer.cpp => src/PNGRenderer.cpp +2 -0
@@ 6,6 6,7 @@

PNGRenderer::PNGRenderer(float animduration, int fps, glm::vec2 resolution) {
    this->animduration = animduration;
    this->starttime = 0.0;
    this->FPS = fps;
    this->presetFilePath = "";
    this->presetDisplayName = "";


@@ 57,6 58,7 @@ void PNGRenderer::AddToGui(ofxGuiContainer *panel, ofxGuiContainer *statusLabelP
    renderingMenu->add<ofxGuiFloatInputField>(resolutionX.set("Res x", resolutionX, 1, 4096));
    renderingMenu->add<ofxGuiFloatInputField>(resolutionY.set("Res y", resolutionY, 1, 4096));
    renderingMenu->add<ofxGuiFloatInputField>(animduration.set("Duration          ", animduration, 0, 10000000));
    renderingMenu->add<ofxGuiFloatInputField>(starttime.set("Start Time           ", starttime, 0, 10000000));
    renderingMenu->add<ofxGuiIntInputField>(FPS.set("FPS", FPS, 1, 1000));
    renderingMenu->add(numBlendFrames.set("Blend Frames", numBlendFrames, 1, 128));
    renderingMenu->add(frameskip.set("Frameskip", frameskip, 1, 10));

M src/PNGRenderer.h => src/PNGRenderer.h +1 -0
@@ 34,6 34,7 @@ public:
    ofParameter<int> numLoops;
    ofParameter<int> numBlendFrames;
    ofParameter<float> animduration;
    ofParameter<float> starttime;
    ofParameter<int> FPS;
    ofParameter<void> encodeMp4Button;
    ofParameter<void> encodeGifButton;

M src/RenderStruct.h => src/RenderStruct.h +1 -1
@@ 4,7 4,7 @@ class FFTManager;
class ShaderPass;

struct RenderStruct {
    float time;
    double time;
    int frame;
    int numBlendFrames;
    FFTManager *fft;

M src/ShaderChain.cpp => src/ShaderChain.cpp +5 -5
@@ 41,7 41,7 @@ void ShaderChain::Setup(glm::vec2 res) {
    this->isMouseDown = false;
    this->isShowingFileDialogue = false;
    this->frame = 0;
	this->time = 0.0;
	  this->time = 0.0;
    this->parameterPanel = gui.addContainer();
    this->cumulativeShader.load("shaders/internal/vertex.vert","shaders/internal/cumulativeAdd.frag");
    this->renderStruct.passes = &this->passes;


@@ 146,7 146,7 @@ void ShaderChain::BeginSaveFrames() {
    }

    if (fft.currentState == InputStateSoundFile) {
        this->time = 0.0;
        this->time = this->pngRenderer->starttime;
        ofFloatColor *black = new ofFloatColor(0.0, 0.0, 0.0, 0.0);
        for (unsigned int i = 0; i < this->passes.size(); i++) {
            if (passes[i]->lastBuffer.isAllocated()) {


@@ 207,7 207,7 @@ void ShaderChain::draw() {
        if (capturingThisFrame && this->isRunning) {
            pngRenderer->Tick();
        }
        float deltaTime = 1. / (pngRenderer->FPS * pngRenderer->numBlendFrames);
        double deltaTime = 1. / (pngRenderer->FPS * pngRenderer->numBlendFrames);

        if (this->isRunning) {
            this->cumulativeBuffer.begin();


@@ 229,7 229,7 @@ void ShaderChain::draw() {
                }
                else {
                    if (!scrubber.isScrubbing) {
                         this->time = pngRenderer->preview ? fmod(this->time + deltaTime, pngRenderer->animduration) : this->time + deltaTime;
                         this->time = pngRenderer->preview ? fmod(this->time + deltaTime, (double)pngRenderer->animduration) : this->time + deltaTime;
                    }
                    fft.Update();
                }


@@ 582,7 582,7 @@ void ShaderChain::saveVideo(string outputFilename) {
    if (fft.currentState == InputStateSoundFile) {
        string outputMp4AudioFilename = outputFilename + "_audio.mp4";
        outputMp4AudioFilename = createUniqueFilePath(outputMp4AudioFilename);
        string addSoundCommand = this->ffmpegCommand + " -i \"" + outputMp4Filename + "\" -i \"" + fft.soundFilePath + "\" -vcodec copy -acodec aac -shortest " + outputMp4AudioFilename;
        string addSoundCommand = this->ffmpegCommand + " -i \"" + outputMp4Filename + "\" -itsoffset " + std::to_string(-this->pngRenderer->starttime) + " -i \"" + fft.soundFilePath + "\" -vcodec copy -acodec aac -shortest " + outputMp4AudioFilename;
        system(addSoundCommand.c_str());
        outputMp4Filename = outputMp4AudioFilename;
    }

M src/ShaderPass.cpp => src/ShaderPass.cpp +1 -1
@@ 162,7 162,7 @@ void ShaderPass::SetInputTexture(ofFbo *buffer) {
  this->shader.setUniformTexture("_MainTexture", buffer->getTexture(), 1);
}

void ShaderPass::UpdateTime(float time) {
void ShaderPass::UpdateTime(double time) {
    this->shader.setUniform1f("_Time", time);
}


M src/ShaderPass.h => src/ShaderPass.h +1 -1
@@ 65,7 65,7 @@ class ShaderPass {
    void addCameraParameter(glm::vec3 pos, glm::quat rot, float sens);
    void Render(ofFbo *previousBuffer, RenderStruct *renderStruct);
    void SetInputTexture(ofFbo *buffer);
    void UpdateTime(float time);
    void UpdateTime(double time);
    void UpdateResolution(int x, int y);
    void LoadJsonParametersFromLoadedShader();
    void AddToGui(ofxGuiContainer *gui, TextureInputSelectionView *selectionView);