はじめてのProcessing

Processingを触っての感想

  • GUIだからシーケンシャルに書いていくのはまだいいけど、オブジェクトを指定しないのがキモイ
  • インタラクションってやっぱ面白い
  • ぽいい感じになると楽しい
  • 今、Processingをやる意味あるのか?ActionScriptのほうがいいんじゃね?
  • EclipseでCtrl+F11を押しまくるのもいいけど、GUI構築の最初期にはやっぱ対話環境が必要
    • JRubyでもJythonでもいい気がするけど、Groovyのほうが安定してるかな

はじめてのProcessing

Built with Processingのサンプルを書いて、ついでに音も鳴らすようにしてみた。soundbankを含んでないので各ローカルで用意しないといけないけど、それはいいや。2008年にJava Appletについて深入りしたくないし。
書いてるのは全てEclipse上で。

package shrkw;

import javax.sound.midi.MidiChannel;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Synthesizer;

import processing.core.PApplet;

public class MyP5 extends PApplet {
	private static final long serialVersionUID = 1L;

	private MidiChannel cha;

	private final int screenWidth = 400;
	private final int bgColor = 255;

	private Synthesizer synthesizer;

	@Override
	public void setup() {
		size(screenWidth, screenWidth);
		colorMode(RGB, 256);
		background(bgColor);

		frameRate(2);

		try {
			synthesizer = MidiSystem.getSynthesizer();
			synthesizer.open();
			MidiChannel[] channels = synthesizer.getChannels();
			cha = channels[9];
			cha.programChange(56);

			// synthesizer.close();

		} catch (MidiUnavailableException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void draw() {
		int color = randomRGBColor();
		stroke(color);
		fill(color);
		rectLines(new Double(random(width)).intValue(), new Double(
				random(height)).intValue(), 30, 30);
	}

	private void rectLines(int x, int y, int w, int h) {
		sound(x + y);
		// line(x, y, x + w, y);
		// line(x, y, x, y + h);
		// line(x + w, y, x + w, y + h);
		// line(x, y + h, x + w, y + h);
		// fill(x, y, x + w, y + h);

		rect(x, y, w, h);

	}

	int randomRGBColor() {
		int c = color(random(256), random(256), random(256), 60);
		return c;
	}

	@Override
	public void mouseClicked() {
		fill(bgColor);
		rect(0, 0, width, height);
	}

	@Override
	public void keyPressed() {
		// synthesizer.close();
	}

	private void sound(int point) {
		MidiChannel channel = null;
		try {
			int s = 1;
			int x = point % 127;
			// if (point % 5 == 0) {
			int program = cha.getProgram();
			cha.programChange(program + 1);
			// }
			cha.noteOff(s);
			x %= 19;
			x += 40;
			cha.noteOn(x, 100);
			s = x;

		} catch (Exception e) {
			e.printStackTrace();
			if (channel != null)
				channel.allNotesOff();
		}

	}
}

Built with Processing [改訂版]

Built with Processing [改訂版]