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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Sound 387<br />

check and see if a sound is playing before you call the play( ) function. Sonia has a nice function called<br />

isPlaying( ) which does exactly this, returning true or false. We want <strong>to</strong> play the sound if it is not already<br />

playing, that is,<br />

if (!dingdong.isPlaying()) {<br />

dingdong.play();<br />

}<br />

Exercise 20-3: Expand the Doorbell class <strong>to</strong> animate (perhaps moving its location and<br />

changing its size randomly) only while its sound is playing. Create an array of fi ve Doorbell<br />

objects. Here is part of a function <strong>to</strong> add <strong>to</strong> the Doorbell class:<br />

void jiggle() {<br />

}<br />

if (_________________)) {<br />

}<br />

x += ____________;<br />

y += ____________;<br />

_________________;<br />

With Minim instead of Sonia, very little changes. Instead of a Sample object, we have an AudioPlayer<br />

object. Th e following example includes the solutions for Exercises 20-2 and 20-3 (but uses the Minim<br />

code, rather than Sonia). It does not implement an array, however.<br />

Example 20-3: Doorbell with Minim<br />

import ddf.minim.*;<br />

Remember, “!” means not!<br />

// A doorbell object (that will trigger the sound)<br />

Doorbell doorbell;<br />

void setup() {<br />

size(200,200);<br />

// start up Minim<br />

Minim.start(this);<br />

// Create a new doorbell<br />

doorbell = new Doorbell(150,100,32, "dingdong.wav");<br />

smooth();<br />

}

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

Saved successfully!

Ooh no, something went wrong!