-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessing_01_Basics.pde
176 lines (155 loc) · 4.47 KB
/
Processing_01_Basics.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* A simple example of how to use
* Processing as a demo platform.
*
* Features:
* - Setup for rendering resolution-independent
* 2D graphics
* - Music playback with Minim
* - Simple play/pause/seek-functionality
*/
// Minim is needed for the music playback.
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
// All these you can (must) change!
// These control how big the opened window is.
// Before you release your demo, set these to
// full HD resolution (1920x1080).
final int CANVAS_WIDTH = 1920;
final int CANVAS_HEIGHT = 1080;
// You can skip backwards/forwards in your demo by using the
// arrow keys; this controls how many milliseconds you skip
// with one keypress.
int SONG_SKIP_MILLISECONDS = 2000;
// Don't change this – needed for resolution independent rendering
float ASPECT_RATIO = (float)CANVAS_WIDTH/CANVAS_HEIGHT;
// Needed for audio
Minim minim;
AudioPlayer song;
/*
* Sets up audio playing: call this last in setup()
* so that the song doesn't start to play too early.
*
* By default, playback doesn't start automatically –
* you must press spacebar to start it.
*/
void setupAudio() {
minim = new Minim(this);
song = minim.loadFile("../common/tekno_127bpm.mp3");
// Uncomment this if you want the demo to start instantly
// song.play();
}
/*
* Function settings must be used when calling size with variable height and width
* New in processing 3
*/
void settings() {
// Set up the drawing area size and renderer (usually P2D or P3D,
// respectively for accelerated 2D/3D graphics).
size(CANVAS_WIDTH, CANVAS_HEIGHT, P2D);
}
/*
* Processing's setup method.
*
* Do all your one-time setup routines in here.
*/
void setup() {
// Drawing options that don't change, modify as you wish
frameRate(60);
noStroke();
fill(255);
setupAudio();
}
/*
* Your drawing code ends up in here!
*
*/
void drawDemo(int time) {
// TODO: implement some example drawing
// and time-based sync done in code
// Draw centered unit circle
ellipse(0., 0., 1.0, 1.0);
// The following lines draw a full-screen quad.
// Params for rect: x, y, width, height
// rect(-ASPECT_RATIO, -1, 2*ASPECT_RATIO, 2);
}
/*
* Draws coordinate axes (for reference).
* You can remove this method if you don't
* need to see the axes.
*/
void drawAxes() {
// Drawing options for axes
stroke(255);
strokeWeight(0.004);
fill(255);
// X-axis
line(-ASPECT_RATIO, 0, ASPECT_RATIO, 0);
pushMatrix();
resetMatrix();
text(String.format("%.3f", -ASPECT_RATIO), 12, CANVAS_HEIGHT/2);
text(String.format("%.3f", ASPECT_RATIO), CANVAS_WIDTH-42, CANVAS_HEIGHT/2);
popMatrix();
// Y-axis
line(0, -1, 0, 1);
pushMatrix();
resetMatrix();
text("1", CANVAS_WIDTH/2+12, 12);
text("-1", CANVAS_WIDTH/2+12, CANVAS_HEIGHT - 12);
popMatrix();
}
/*
* Processing's drawing method – all
* rendering should be done here!
*/
void draw() {
// Reset all transformations.
resetMatrix();
// The following lines map coordinates so that we can
// draw in a resolution independent coordinate system.
//
// After this line coordinate axes are as follows:
// x: -ASPECT_RATIO ... ASPECT_RATION
// y: -1 ... 1
// Negative x is at left, y at bottom, origo at the center of
// the screen.
// This is the coordinate system you're probably used to
// already!
translate(CANVAS_WIDTH/2.0, CANVAS_HEIGHT/2.0);
scale(CANVAS_WIDTH/2.0/ASPECT_RATIO, -CANVAS_HEIGHT/2.0);
// Clear the screen after previous frame.
// If you comment this line, you always draw on top the last frame,
// which can lead to something interesting.
background(0);
// Draw coordinate axes for reference.
drawAxes();
// Draw demo at the current song position.
drawDemo(song.position());
}
void keyPressed() {
if (key == CODED) {
// Left/right arrow keys: seek song
if (keyCode == LEFT) {
song.skip(-SONG_SKIP_MILLISECONDS);
}
else if (keyCode == RIGHT) {
song.skip(SONG_SKIP_MILLISECONDS);
}
}
// Space bar: play/payse
else if (key == ' ') {
if (song.isPlaying())
song.pause();
else
song.play();
}
// Enter: spit out the current position
// (for syncing)
else if (key == ENTER) {
print(song.position() + ", ");
}
}