I want to create a (live) video delay effect with adjustable delay time.
(tweaking delay time with mouseX).
Works ok at start, but then delayed image suddenly disappears.
Two video images: one without delay, and on top a delayed one.
For the latter I use a buffer, playhead continuously running between frame[0] and a middle point (adjustable), and then looping back to frame[0]:
import processing.video.*;
Capture cam;
PImage[] buffer;
int nFrames = 60; // 2" max buffer length (at 30 fps)
int iWrite = 0, iRead = 1;
int adjust_buf;
int bufSize;
void setup() {
size(640, 480);
cam = new Capture(this, 640, 480);
cam.start(); // start reading from camera
buffer = new PImage[nFrames];
}
void draw() {
if (cam.available()) { // check if a new image is available
cam.read(); // reads the current frame of the device
// 1.CURRENT IMAGE (no delay)
tint(255, 255, 255, 255);
image(cam, 0, 0);
// map mouseX between left <>right screen edges:
// longest possible delay (60 frames) <> no delay (1 frame)
adjust_buf = round(map(mouseX, 0, width, 0, nFrames-1));
// how far into buffer playhead can read:
bufSize = nFrames - adjust_buf;
// (print values)
println("bufSize: ", bufSize);
println("iRead: ", iRead); // playhead position
buffer[iWrite] = cam.get();
if (buffer[iRead] != null) {
// 2.DELAYED IMAGE, displayed on top of current image
tint(255, 120);
image(buffer[iRead], 0, 0);
}
// playhead progress
iWrite++;
iRead++;
// loop playhead to beginning of buffer
if (iRead >= bufSize) {
iRead = 0;
}
if (iWrite >= bufSize) {
iWrite = 0;
}
}
}
It seems that sudden delay-time adjustment (fast mouseX movement) causes the delayed image to ‘quit’. Or maybe when delay time becomes too short? Not entirely sure.