Processingを触っての感想
はじめての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(); } } }
- 作者: 前川峻志,田中孝太郎
- 出版社/メーカー: ビー・エヌ・エヌ新社
- 発売日: 2008/03/31
- メディア: 単行本
- 購入: 2人 クリック: 68回
- この商品を含むブログ (23件) を見る