Processing 2. Креативное программирование
.pdf.keyPressed(). , Minim,stop().
void keyPressed()
{
if ( key == 'f' ) { fade = !fade;
}
}
void stop()
{
player.close();
minim.stop();
super.stop();
}
Particle. ,. Cmd + Shift + N Mac OS X Ctrl + Shift + N Windows Linux .
class Particle
{
PVector loc; PVector vel;
float radius; float h; float s; float b;
Particle( int id )
{
loc = new PVector( map( id, 0, fft.specSize(), 0, width ), height/2 );
vel = new PVector( random( -1, 1 ), random( -1, 1 ) );
h = map( id, 0, fft.specSize(), 0, 360 ); s = 100;
b = 100;
}
void update( float _r, float _b )
{
loc.add( vel );
if ( loc.x < 0 || loc.x > width ) { vel.x *= -1;
150
7
}
if ( loc.y < 0 || loc.y > height ) { vel.y *= -1;
}
radius = _r;
radius = constrain( radius, 2, 100 );
b = map( _b, -1, 1, 0, 100 );
}
void render()
{
stroke( h, s, b, 50 ); fill( h, s, b, 20 );
ellipse( loc.x, loc.y, radius*2, radius*2 );
}
}
, , .. F /.
151
, .Particle , - . , - Particle.
PVector .. PVector
: http:// processing.org/reference/PVector.html.,.
PVector loc;
PVector vel;
float radius; float h; float s; float b;
Particle ..-1 1.
Particle( int id )
{
loc = new PVector( map( id, 0, fft.specSize(), 0, width ), height/2 );
vel = new PVector( random( -1, 1 ), random( -1, 1 ) );
h = map( id, 0, fft.specSize(), 0, 360 ); s = 100;
b = 100;
}
update() Particle ,. draw(). update(). ,. ,, . ,.
void update( float _r, float _b )
{
loc.add( vel );
if ( loc.x < 0 || loc.x > width ) {
152
7
vel.x *= -1;
}
if ( loc.y < 0 || loc.y > height ) { vel.y *= -1;
}
radius = _r;
radius = constrain( radius, 2, 100 );
b = map( _b, -1, 1, 0, 100 );
}
render(). .
void render()
{
stroke( h, s, b, 50 ); fill( h, s, b, 20 );
ellipse( loc.x, loc.y, radius*2, radius*2 );
}
-
, , -. - 16- TR-808 TR-909 - Roland.Vintage Synth Explorer : at http:// www.vintagesynth.com/roland/808.php.
drum_machine.pde.Button.pde.Button, ..
class Button
{
float x; float y; float w; float h;
153
boolean isOn;
Button( float _x, float _y )
{
x = _x; y = _y; w = 20; h = 20;
isOn = false;
}
void render()
{
if ( isOn == true ) { fill( 255, 0, 0 );
} else { fill( 255 );
}
rect( x, y, w, h );
}
void pressButton( int _x, int _y )
{
if ( _x > x && _x < x + w && _y > y && _y < y + h ) { isOn = !isOn;
}
}
}
minim . AudioSample AudioPlayer , .Button. setup().
import ddf.minim.*;
import ddf.minim.signals.*; import ddf.minim.analysis.*; import ddf.minim.effects.*;
Minim minim;
AudioSample samples[];
Button[] bd;
154
7
Button[] sn;
Button[] oh;
Button[] ch;
int playhead;
void setup()
{
size( 640, 200 );
minim = new Minim( this );
samples = new AudioSample[4];
samples[0] = minim.loadSample( "bd.aif" ); samples[1] = minim.loadSample( "sn.aif" ); samples[2] = minim.loadSample( "oh.aif" ); samples[3] = minim.loadSample( "ch.aif" );
frameRate( 8 );
playhead = 0;
bd = new Button[16];
for ( int i = 0; i < bd.length; i++ ) { bd[i] = new Button( 125 + i * 30, 50 ); if ( i % 4 == 0 ) {
bd[i].isOn = true;
}
}
sn = new Button[16];
for ( int i = 0; i < sn.length; i++ ) { sn[i] = new Button( 125 + i * 30, 80 ); if ( i % 8 - 4 == 0 ) {
sn[i].isOn = true;
}
}
oh = new Button[16];
for ( int i = 0; i < oh.length; i++ ) { oh[i] = new Button( 125 + i * 30, 110 ); if ( i % 2 == 1 ) {
oh[i].isOn = true;
}
155
}
ch = new Button[16];
for ( int i = 0; i < ch.length; i++ ) { ch[i] = new Button( 125 + i * 30, 140 ); if ( i % 2 == 0 ) {
ch[i].isOn = true;
}
}
}
draw() , ., .
void draw()
{
background( 255 );
//draw playhead fill( 0 );
rect( 120 + playhead * 30, 45, 30, 120 );
//draw buttons
fill( 0 );
text( "*** PROCESSING DRUM MACHINE v1.0 ***", 125, 30 ); text( "Bassdrum", 20, 65 );
text( "Snare", 20, 95 );
text( "Open Hi-hat", 20, 125 ); text( "Closed Hi-hat", 20, 155 );
for ( int i = 0; i < bd.length; i++ ) { bd[i].render();
}
for ( int i = 0; i < sn.length; i++ ) { sn[i].render();
}
for ( int i = 0; i < oh.length; i++ ) { oh[i].render();
}
for ( int i = 0; i < ch.length; i++ ) { ch[i].render();
156
7
}
// play samples
if ( bd[playhead].isOn ) { samples[0].trigger();
}
if ( sn[playhead].isOn ) { samples[1].trigger();
}
if ( oh[playhead].isOn ) { samples[2].trigger();
}
if ( ch[playhead].isOn ) { samples[3].trigger();
}
// move playhead playhead++;
if ( playhead >= 16 ) { playhead = 0;
}
}
mousePressed() . -pressButton()Button , . ,.
void mousePressed()
{
for ( int i = 0; i < bd.length; i++ ) { bd[i].pressButton( mouseX, mouseY );
}
for ( int i = 0; i < sn.length; i++ ) { sn[i].pressButton( mouseX, mouseY );
}
for ( int i = 0; i < oh.length; i++ ) { oh[i].pressButton( mouseX, mouseY );
}
157
for ( int i = 0; i < ch.length; i++ ) { ch[i].pressButton( mouseX, mouseY );
}
}
Minimstop().
void stop()
{
samples[0].close();
samples[1].close();
samples[2].close();
samples[3].close();
minim.stop();
super.stop();
}
, -..
AudioSample AudioPlayer.- ,. .trigger().
setup() frameRate 8., , - ., , .; .
158
7
0. , -.
if ( bd[playhead].isOn ) { samples[0].trigger();
}
Button pressButton(). , . if , _x _y . _x _y ,, mouseX mouseY, mousePressed().
void pressButton( int _x, int _y )
{
if ( _x > x && _x < x + w && _y > y && _y < y + h ) { isOn = !isOn;
}
}
, - . -., .
, minim Minim AudioOutput. SineWave SawWave . setup() .
import ddf.minim.*;
import ddf.minim.signals.*; import ddf.minim.analysis.*; import ddf.minim.effects.*;
Minim minim;
AudioOutput out;
SineWave sine;
SawWave saw;
void setup()
{
159