06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

394 <strong>Learning</strong> <strong>Processing</strong><br />

void draw() {<br />

// Get the overall volume (between 0 and 1.0)<br />

float vol = LiveInput.getLevel();<br />

// If the volume is greater than 0.5, draw a rectangle<br />

if (vol > 0.5) {<br />

stroke(0);<br />

fill(0,100);<br />

rect(random(width),random(height),vol*20,vol*20);<br />

}<br />

// Graph the overall volume<br />

// First draw a background strip<br />

fill(175);<br />

rect(0,0,20,height);<br />

// Then draw a rectangle size according <strong>to</strong> volume<br />

fill(0);<br />

rect(0,height-vol*height/2,20,vol*height/2);<br />

}<br />

// Close the sound engine<br />

public void s<strong>to</strong>p() {<br />

Sonia.s<strong>to</strong>p();<br />

super.s<strong>to</strong>p();<br />

}<br />

Th is application works fairly well, but does not truly emulate the clapper. Notice how each clap results in<br />

several rectangles drawn <strong>to</strong> the window. Th is is because the sound, although seemingly instantaneous <strong>to</strong><br />

our human ears, occurs over a period of time. It may be a very short period of time, but it is enough <strong>to</strong><br />

sustain a volume level over 0.5 for several cycles through draw( ) .<br />

In order <strong>to</strong> have a clap trigger an event one and only one time, we need <strong>to</strong> rethink the logic of our<br />

program. In plain English, this is what we are trying <strong>to</strong> achieve:<br />

• If the sound level is above 0.5, then you are clapping and trigger the event. However, do not trigger the<br />

event if you just did a moment ago!<br />

Th e key here is how we defi ne “ a moment ago. ” One solution would be <strong>to</strong> implement a timer, that is, only<br />

trigger the event once and then wait one second before you are allowed <strong>to</strong> trigger the event again. Th is is<br />

a perfectly OK solution. Nonetheless, with sound, a timer is <strong>to</strong>tally unnecessary since the sound itself will<br />

tell us when we are fi nished clapping!<br />

• If the sound level is less than 0.25, then it is quiet and we have finished clapping.<br />

OK, with these two pieces of logic, we are ready <strong>to</strong> program this “ double-thresholded ” algorithm. Th ere<br />

are two thresholds, one <strong>to</strong> determine if we have started clapping, and one <strong>to</strong> determine if we have<br />

fi nished. We will need a boolean variable <strong>to</strong> tell us whether we are currently clapping or not.<br />

Assume clapping � false <strong>to</strong> start.<br />

If the volume is greater than 0.5<br />

a rectangle is drawn at a random<br />

location in the window. The louder<br />

the volume, the larger the rectangle.<br />

• If the sound level is above 0.5 and we are not already clapping, trigger the event and set clapping � t r u e.<br />

• If we are clapping and the sound level is less than 0.25, then it is quiet and set clapping � false.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!