Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Created December 19, 2023 10:12
Show Gist options
  • Save kylemcdonald/56243e642e8ea41771cfb3083d8e69b5 to your computer and use it in GitHub Desktop.
Save kylemcdonald/56243e642e8ea41771cfb3083d8e69b5 to your computer and use it in GitHub Desktop.
Video delay for openFrameworks.
#pragma once
#include <vector>
#include "ofImage.h"
class FixedVideoDelay {
private:
int delayAmount;
int totalFrames;
int readPosition;
int writePosition;
std::vector<ofPixels> buffer;
public:
FixedVideoDelay() {}
void setup(int width, int height, int delayAmount) {
this->delayAmount = delayAmount;
int bufferSize = delayAmount * 2;
for (int i = 0; i < bufferSize; i++) {
ofPixels pix;
pix.allocate(width, height, OF_IMAGE_COLOR);
buffer.push_back(pix);
}
totalFrames = 0;
readPosition = 0;
writePosition = 0;
}
void writeFrame(ofPixels &pixels) {
buffer[writePosition] = pixels;
writePosition = (writePosition + 1) % buffer.size();
totalFrames++;
}
bool ready() {
return totalFrames >= delayAmount;
}
ofPixels &readFrame() {
readPosition = (readPosition + 1) % buffer.size();
return buffer[readPosition];
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment