@@ -79,13 +79,15 @@ <h3>Notes</h3>
7979
8080 < h3 > Code</ h3 >
8181
82- < p class =" txt " > Example 1 </ p >
82+ < hr / >
8383
8484 < pre >
8585< span style ="color: #666666; "> /**</ span >
86- < span style ="color: #666666; "> * In this example, five sine waves are layered to construct a cluster </ span >
87- < span style ="color: #666666; "> * of frequencies. This method is called additive synthesis. Use the </ span >
88- < span style ="color: #666666; "> * mouse position inside the display window to detune the cluster.</ span >
86+ < span style ="color: #666666; "> * Processing Sound Library, Example 1</ span >
87+ < span style ="color: #666666; "> * </ span >
88+ < span style ="color: #666666; "> * Five sine waves are layered to construct a cluster of frequencies. </ span >
89+ < span style ="color: #666666; "> * This method is called additive synthesis. Use the mouse position </ span >
90+ < span style ="color: #666666; "> * inside the display window to detune the cluster.</ span >
8991< span style ="color: #666666; "> */</ span >
9092
9193< span style ="color: #33997E; "> import</ span > processing.sound.*;
@@ -114,7 +116,6 @@ <h3>Code</h3>
114116}
115117
116118< span style ="color: #33997E; "> void</ span > < span style ="color: #006699; "> < b > draw</ b > </ span > () {
117-
118119 < span style ="color: #666666; "> //Map mouseY from 0 to 1</ span >
119120 < span style ="color: #E2661A; "> float</ span > yoffset = < span style ="color: #006699; "> map</ span > (< span style ="color: #D94A7A; "> mouseY</ span > , 0, < span style ="color: #D94A7A; "> height</ span > , 0, 1);
120121 < span style ="color: #666666; "> //Map mouseY logarithmically to 150 - 1150 to create a base frequency range</ span >
@@ -132,7 +133,193 @@ <h3>Code</h3>
132133
133134 < hr />
134135
135- < p class ="txt "> Example 2</ p >
136+ < pre >
137+ < span style ="color: #666666; "> /**</ span >
138+ < span style ="color: #666666; "> * Processing Sound Library, Example 2</ span >
139+ < span style ="color: #666666; "> * </ span >
140+ < span style ="color: #666666; "> * This sketch shows how to use envelopes and oscillators. </ span >
141+ < span style ="color: #666666; "> * Envelopes describe to course of amplitude over time. </ span >
142+ < span style ="color: #666666; "> * The Sound library provides an ASR envelope which stands for </ span >
143+ < span style ="color: #666666; "> * attack, sustain, release. </ span >
144+ < span style ="color: #666666; "> * </ span >
145+ < span style ="color: #666666; "> * .________</ span >
146+ < span style ="color: #666666; "> * . ---</ span >
147+ < span style ="color: #666666; "> * . --- </ span >
148+ < span style ="color: #666666; "> * . ---</ span >
149+ < span style ="color: #666666; "> * A S R </ span >
150+ < span style ="color: #666666; "> */</ span >
151+
152+ < span style ="color: #33997E; "> import</ span > processing.sound.*;
153+
154+ < span style ="color: #666666; "> // Oscillator and envelope </ span >
155+ TriOsc triOsc;
156+ Env env;
157+
158+ < span style ="color: #666666; "> // Times and levels for the ASR envelope</ span >
159+ < span style ="color: #E2661A; "> float</ span > attackTime = 0.001;
160+ < span style ="color: #E2661A; "> float</ span > sustainTime = 0.004;
161+ < span style ="color: #E2661A; "> float</ span > sustainLevel = 0.2;
162+ < span style ="color: #E2661A; "> float</ span > releaseTime = 0.2;
163+
164+ < span style ="color: #666666; "> // This is an octave in MIDI notes.</ span >
165+ < span style ="color: #E2661A; "> int</ span > [] midiSequence = {
166+ 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72
167+ };
168+
169+ < span style ="color: #666666; "> // Set the duration between the notes</ span >
170+ < span style ="color: #E2661A; "> int</ span > duration = 200;
171+ < span style ="color: #666666; "> // Set the note trigger</ span >
172+ < span style ="color: #E2661A; "> int</ span > trigger = 0;
173+
174+ < span style ="color: #666666; "> // An index to count up the notes</ span >
175+ < span style ="color: #E2661A; "> int</ span > note = 0;
176+
177+ < span style ="color: #33997E; "> void</ span > < span style ="color: #006699; "> < b > setup</ b > </ span > () {
178+ < span style ="color: #006699; "> size</ span > (640, 360);
179+ < span style ="color: #006699; "> background</ span > (255);
180+
181+ < span style ="color: #666666; "> // Create triangle wave and envelope </ span >
182+ triOsc = < span style ="color: #33997E; "> new</ span > TriOsc(< span style ="color: #33997E; "> this</ span > );
183+ env = < span style ="color: #33997E; "> new</ span > Env(< span style ="color: #33997E; "> this</ span > );
184+ }
185+
186+ < span style ="color: #33997E; "> void</ span > < span style ="color: #006699; "> < b > draw</ b > </ span > () {
187+
188+ < span style ="color: #666666; "> // If value of trigger is equal to the computer clock and if not all </ span >
189+ < span style ="color: #666666; "> // notes have been played yet, the next note gets triggered.</ span >
190+ < span style ="color: #669900; "> if</ span > ((< span style ="color: #006699; "> millis</ span > () > trigger) && (note<midiSequence.< span style ="color: #33997E; "> length</ span > )) {
191+
192+ < span style ="color: #666666; "> // midiToFreq transforms the MIDI value into a frequency in Hz which we use </ span >
193+ < span style ="color: #666666; "> //to control the triangle oscillator with an amplitute of 0.8</ span >
194+ triOsc.play(midiToFreq(midiSequence[note]), 0.8);
195+
196+ < span style ="color: #666666; "> // The envelope gets triggered with the oscillator as input and the times and </ span >
197+ < span style ="color: #666666; "> // levels we defined earlier</ span >
198+ env.play(triOsc, attackTime, sustainTime, sustainLevel, releaseTime);
199+
200+ < span style ="color: #666666; "> // Create the new trigger according to predefined durations and speed</ span >
201+ trigger = < span style ="color: #006699; "> millis</ span > () + duration;
202+
203+ < span style ="color: #666666; "> // Advance by one note in the midiSequence;</ span >
204+ note++;
205+
206+ < span style ="color: #666666; "> // Loop the sequence</ span >
207+ < span style ="color: #669900; "> if</ span > (note == 12) {
208+ note = 0;
209+ }
210+ }
211+ }
212+
213+ < span style ="color: #666666; "> // This function calculates the respective frequency of a MIDI note</ span >
214+ < span style ="color: #E2661A; "> float</ span > midiToFreq(< span style ="color: #E2661A; "> int</ span > note) {
215+ < span style ="color: #33997E; "> return</ span > (< span style ="color: #006699; "> pow</ span > (2, ((note-69)/12.0)))*440;
216+ }
217+
218+ </ pre >
219+
220+ < hr />
221+
222+ < pre >
223+ < span style ="color: #666666; "> /**</ span >
224+ < span style ="color: #666666; "> * Processing Sound Library, Example 3</ span >
225+ < span style ="color: #666666; "> * </ span >
226+ < span style ="color: #666666; "> * This example shows how to make a simple sampler and sequencer </ span >
227+ < span style ="color: #666666; "> * with the Sound library. In this sketch, five different samples are </ span >
228+ < span style ="color: #666666; "> * loaded and played back at different pitches, in this case five </ span >
229+ < span style ="color: #666666; "> * different octaves. The sequencer triggers an event every 200-1000 </ span >
230+ < span style ="color: #666666; "> * milliseconds randomly. Each time a sound is played a colored </ span >
231+ < span style ="color: #666666; "> * rect with a random color is displayed.</ span >
232+ < span style ="color: #666666; "> */</ span >
233+
234+ < span style ="color: #33997E; "> import</ span > processing.sound.*;
235+
236+ SoundFile[] files;
237+
238+ < span style ="color: #666666; "> // Create an array of values which represent the octaves. </ span >
239+ < span style ="color: #666666; "> // 1.0 is playback at normal speed, 0.5 is half and therefore </ span >
240+ < span style ="color: #666666; "> // one octave down. 2.0 is double so one octave up.</ span >
241+ < span style ="color: #E2661A; "> float</ span > [] octave = {
242+ 0.25, 0.5, 1.0, 2.0, 4.0
243+ };
244+
245+ < span style ="color: #666666; "> // The playSound array is defining how many samples will be </ span >
246+ < span style ="color: #666666; "> // played at each trigger event</ span >
247+ < span style ="color: #E2661A; "> int</ span > [] playSound = {
248+ 1, 1, 1, 1, 1
249+ };
250+
251+ < span style ="color: #666666; "> // The trigger is an integer number in milliseconds so we </ span >
252+ < span style ="color: #666666; "> // can schedule new events in the draw loop</ span >
253+ < span style ="color: #E2661A; "> int</ span > trigger=0;
254+
255+ < span style ="color: #666666; "> // This array holds the pixel positions of the rectangles </ span >
256+ < span style ="color: #666666; "> // that are drawn each event</ span >
257+ < span style ="color: #E2661A; "> int</ span > [] posx = {
258+ 0, 128, 256, 384, 512
259+ };
260+
261+
262+ < span style ="color: #33997E; "> void</ span > < span style ="color: #006699; "> < b > setup</ b > </ span > () {
263+ < span style ="color: #006699; "> size</ span > (640, 360);
264+ < span style ="color: #006699; "> background</ span > (255);
265+
266+ < span style ="color: #666666; "> // Create an array of 5 empty soundfiles</ span >
267+ files = < span style ="color: #33997E; "> new</ span > SoundFile[5];
268+
269+ < span style ="color: #666666; "> // Load 5 soundfiles from a folder in a for loop. By naming </ span >
270+ < span style ="color: #666666; "> // the files 1., 2., 3., [...], n.aif it is easy to iterate </ span >
271+ < span style ="color: #666666; "> // through the folder and load all files in one line of code.</ span >
272+ < span style ="color: #669900; "> for</ span > (< span style ="color: #E2661A; "> int</ span > i = 0; i < files.< span style ="color: #33997E; "> length</ span > ; i++) {
273+ files[i] = < span style ="color: #33997E; "> new</ span > SoundFile(< span style ="color: #33997E; "> this</ span > , (i+1) + < span style ="color: #7D4793; "> ".aif"</ span > );
274+ }
275+ }
276+
277+ < span style ="color: #33997E; "> void</ span > < span style ="color: #006699; "> < b > draw</ b > </ span > () {
278+
279+ < span style ="color: #666666; "> // If the determined trigger moment in time matches up with </ span >
280+ < span style ="color: #666666; "> // the computer clock events get triggered.</ span >
281+ < span style ="color: #669900; "> if</ span > (< span style ="color: #006699; "> millis</ span > () > trigger) {
282+
283+ < span style ="color: #666666; "> // Redraw the background every time to erase old rects</ span >
284+ < span style ="color: #006699; "> background</ span > (255);
285+
286+ < span style ="color: #666666; "> // By iterating through the playSound array we check for </ span >
287+ < span style ="color: #666666; "> // 1 or 0, 1 plays a sound and draws a rect, for 0 nothing happens</ span >
288+
289+ < span style ="color: #669900; "> for</ span > (< span style ="color: #E2661A; "> int</ span > i = 0; i < files.< span style ="color: #33997E; "> length</ span > ; i++) {
290+ < span style ="color: #666666; "> // Check which indexes are 1 and 0.</ span >
291+ < span style ="color: #669900; "> if</ span > (playSound[i] == 1) {
292+ < span style ="color: #E2661A; "> float</ span > rate;
293+ < span style ="color: #666666; "> // Choose a random color and get set to noStroke()</ span >
294+ < span style ="color: #006699; "> fill</ span > (< span style ="color: #006699; "> int</ span > (< span style ="color: #006699; "> random</ span > (255)), < span style ="color: #006699; "> int</ span > (< span style ="color: #006699; "> random</ span > (255)), < span style ="color: #006699; "> int</ span > (< span style ="color: #006699; "> random</ span > (255)));
295+ < span style ="color: #006699; "> noStroke</ span > ();
296+ < span style ="color: #666666; "> // Draw the rect in the positions we defined earlier in posx</ span >
297+ < span style ="color: #006699; "> rect</ span > (posx[i], 50, 128, 260);
298+ < span style ="color: #666666; "> // Choose a random index of the octave array</ span >
299+ rate = octave[< span style ="color: #006699; "> int</ span > (< span style ="color: #006699; "> random</ span > (0, 5))];
300+ < span style ="color: #666666; "> // Play the soundfile from the array with the respective </ span >
301+ < span style ="color: #666666; "> // rate and loop set to false</ span >
302+ files[i].play(rate, 1.0);
303+ }
304+
305+ < span style ="color: #666666; "> // Renew the indexes of playSound so that at the next event </ span >
306+ < span style ="color: #666666; "> // the order is different and randomized.</ span >
307+ playSound[i] = < span style ="color: #006699; "> int</ span > (< span style ="color: #006699; "> random</ span > (0, 2));
308+ }
309+
310+ < span style ="color: #666666; "> // Create a new triggertime in the future, with a random offset </ span >
311+ < span style ="color: #666666; "> // between 200 and 1000 milliseconds</ span >
312+ trigger = < span style ="color: #006699; "> millis</ span > () + < span style ="color: #006699; "> int</ span > (< span style ="color: #006699; "> random</ span > (200, 1000));
313+ }
314+ }
315+
316+ </ pre >
317+
318+ < hr />
319+
320+
321+
322+ < hr />
136323
137324
138325
0 commit comments