Dear everyone,
I’m working on a project where I have several video files I want to play back in a sequence, and have the sequence itself repeat indefinitely. Every other video clip, Processing writes to the serial port which is received by my Arduino UNO board. Additionally, the output is mapped to a surface using the Keystone plugin (I adapted the code to from the example to fit my needs).
At first, everything worked as it should EXCEPT after a few repetitions of the sequence (ca. 3.5 times through for a sequence of 5 videos between ca. 10 and 40 seconds) it seems to get stuck, simply freezing on a frame and not incrementing the clips. I came back to the sketch later and it seemed to run fine indefinitely. Then, more recently, I ran the sketch and it exhibited the freezing behavior almost immediately.
I’ve searched forums and consulted some friends and I’m still not sure exactly what I’m doing wrong here. Thanks for any advice!
import processing.video.*;
import deadpixel.keystone.*;
import processing.serial.*;
Serial myPort;
Keystone ks; // the keystone object
CornerPinSurface surface; // this is the surface
int numClips =5;
Movie[] clips = new Movie[numClips];
boolean isPlaying = false;
boolean pause = true;
//boolean reset = false;
//int step =0;
//int prev = 0;
//int count=0;
int next=0;
int last = 0;
int index = 0;
// the offsceen buffer for keystone
PGraphics offscreen;
void setup() {
size(1200, 800, P3D);
//fullScreen(P3D);
//write to serial port
//get a bug if write to arduino and its not plugged in
printArray(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
//keystone object for projection mapping
ks = new Keystone(this);
surface = ks.createCornerPinSurface(width, height, 20);
offscreen = createGraphics(width, height, P3D);
for (int i = 0; i < clips.length; i ++ ) {
clips[i] = new Movie(this, "SuH_" +(i+1)+ ".mov");
}
println("this many clips:"+clips.length);
}
void draw() {
// start writing into the offscreen buffer
offscreen.beginDraw();
offscreen.background(255);
if (isPlaying) {
playClip();
};
if (!isPlaying) {
nextClip();
}
serialWrite();
pauseClip();
//finished writing to offscreen buffer
offscreen.endDraw();
background(0);
// add everything to the surface
surface.render(offscreen);
}
void playClip() {
//when clip time goes above clip duration increment variable 'next'
if (clips[index].time() < clips[index].duration()) {
offscreen.image(clips[index], 0, 0, width, height);
} else if (clips[index].time() >= clips[index].duration() ) {
println("next");
next++;
isPlaying = false;
}
}
void nextClip() {
//when change in variable 'next' detected, play next clip
if ( next != last && index < (clips.length-1)) {
next = last;
index++;
println("index is "+ index);
offscreen.image(clips[index], 0, 0, width, height);
clips[index].play();
clips[index].jump(0);
isPlaying = true;
//when index reaches length of clip array, reset routine and play first clip
} else if (next!=last && index >= (clips.length-1)) {
// next = last;
index = 0;
next = 0;
last = 0;
isPlaying = true;
pause = false;
offscreen.image(clips[index], 0, 0, width, height);
clips[index].play();
clips[index].jump(0);
}
}
void pauseClip() {
if (!isPlaying && pause) {
offscreen.image(clips[index], 0, 0, width, height);
}
}
//write to serial port every other clip
void serialWrite() {
if (isPlaying && clips[index].time() < clips[index].duration() && index%2 == 1) {
myPort.write('1');
} else if (isPlaying && clips[index].time() < clips[index].duration() && index%2 == 0) {
myPort.write('0');
}
}
void keyPressed() {
switch(key) {
case 'c':
// enter/leave calibration mode, where surfaces can be warped
// and moved
ks.toggleCalibration();
break;
case 'l':
// loads the saved layout
ks.load();
break;
case 's':
// saves the layout
ks.save();
break;
case 'p':
// play/pause the movie on keypress
if (!isPlaying && pause) {
index = 0;
clips[index].play();
println("index is "+ index);
println("playing");
isPlaying = true;
pause = false;
} else if (isPlaying && !pause) {
clips[index].pause();
println("paused");
isPlaying = false;
pause = true;
}
break;
}
}
void movieEvent(Movie m) {
m.read();
}