FrameLooper {sourcecode}

21Oct11

Sourcecode for a sketch built with processing (in eclipse). The user can add frames from a video input source to a loop by pressing the mousebutton.

/*
 * 	~~~FrameLooper!~~~
 * 	|||henderson888|||
 *
 *	Loops frames from video input
 *	~ Mousebutton adds a new frame to the loop
 *	~ 'r' key refreshes the loop
 *	~ alter the number of frames in the loop with the 'maxFrames' variable
 *	
 */

import processing.core.*;
import codeanticode.gsvideo.*;

public class multiplication extends PApplet {

	private static final long serialVersionUID = 1L;

	GSCapture video;

	// canvas size
	int w = 320;
	int h = 240;
	// resolution of video input
	int vresx = w;
	int vresy = h;
	int numPixels = 0;

	int maxFrames = 20; // max frames in loop - change this to make loop longer

	int frameIndex = 0; // position within the loop
	int loopLength = 0; // number of frames currently in the loop
	int playIndex = 0; // current frame number in the loop

	// array of color values at frameIndex
	int[][] loop = new int[maxFrames][vresx * vresy];

//#########################################
	// setup
	public void setup() {
		size(w, h);
		frameRate(25);

		video = new GSCapture(this, vresx, vresy);
		video.start();
		// fill array
		for (int i = 0; i < maxFrames; i++) {
			for (int j = 0; j < vresx * vresy; j++) {
				loop[i][j] = 0;
			}
		}
		
		background(0);
		noStroke();

		numPixels = vresx * vresy;
	}
	
//##########################################
	//called every frame
	
	// read video input
	public void captureEvent(GSCapture c) {
		c.read();
	}

	// draw
	public void draw() {
		
		//keep the playIndex within the length of the loop
		if (playIndex < loopLength) {
			playIndex++;
		} else {
			playIndex = 0;
		}
		
		//load pixels and draw each frame in the loop to the screen
		loadPixels();
		for (int i = 0; i < numPixels; i++) {
			pixels[i] = loop[playIndex][i];
		}
		updatePixels();
	}
//##########################################
	//input
	
	// calls the addFrame method: adds new frames to the loop
	public void mousePressed() {
		System.out.println("Adding a frame!");
		addFrame();
	}

	// refresh loop
	public void keyPressed() {
		if (key == 'r') {
			loopLength = 0;
		}
	}

//##########################################
	// method to add new frames to the loop
	public void addFrame() {

		// stop loopLength from exceeding maxFrames
		if (loopLength < maxFrames - 1) {
			loopLength++;
		} else {
			loopLength = maxFrames - 1;
		}

		if (frameIndex < maxFrames - 1) {
			frameIndex++;
		} else {
			frameIndex = 0;
		}
		
		//print the position of the new frame in the loop
		System.out.println("Frame Index: " + frameIndex);
		
		//load and add pixels to the loop
		video.loadPixels();
		// fill array with pixel[] vals
		for (int i = 0; i < numPixels; i++) {
			loop[frameIndex][i] = video.pixels[i];
		}
		video.updatePixels();
	}
}
About these ads


No Responses Yet to “FrameLooper {sourcecode}”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: