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.

Pressing the mouse s<strong>to</strong>ps and starts the sound<br />

void mousePressed() {<br />

if (<strong>to</strong>ne.isPlaying()) {<br />

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

} else {<br />

<strong>to</strong>ne.repeat();<br />

}<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 />

Sound 391<br />

Exercise 20-4: In Example 20-4, fl ip the Y-axis so that the lower sound plays when the<br />

mouse is down rather than up.<br />

An AudioPlayer object can also be manipulated with Minim using the functions: setVolume( ) ,<br />

setPan( ) , and addEff ect( ) , among others documented on the Minim site ( http://code.compartmental.<br />

net/<strong>to</strong>ols/minim/ ).<br />

20.5<br />

Live input<br />

In Chapter 16, we looked at how serial communication allows a <strong>Processing</strong> sketch <strong>to</strong> respond <strong>to</strong> input<br />

from an external hardware device connected <strong>to</strong> a sensor. Reading input from a microphone is a similar<br />

pursuit. In essence, the microphone acts as a sensor. Not only can a microphone record sound, but it can<br />

determine if the sound is loud, quiet, high-pitched, low-pitched, and so on. For example, a <strong>Processing</strong><br />

sketch could determine if it is living in a crowded room based on sound levels, or whether it is listening<br />

<strong>to</strong> a soprano or bass singer based on pitch levels. Th is section will cover how <strong>to</strong> retrieve and use sound<br />

volume data using the Sonia library. For analyzing sound pitch levels from a microphone, visit the Sonia<br />

web site ( http://sonia.pitaru.com ) for further examples.<br />

Th e previous sections used a Sample object <strong>to</strong> play a sound. Sound input from a microphone is retrieved<br />

with a LiveInput object. Th ere is a somewhat odd distinction here in the way we will use these two<br />

classes that we have yet <strong>to</strong> encounter over the course of this book.<br />

Consider a scenario where we have three sound fi les. We would create three Sample objects.<br />

Sample sample1 = new Sample( "file1.wav");<br />

Sample sample2 = new Sample( "file2.wav");<br />

Sample sample3 = new Sample( "file3.wav");<br />

Technically speaking, we have made three instances of Sample objects, born via the Sample class. If we<br />

want <strong>to</strong> play a sound, we have <strong>to</strong> refer <strong>to</strong> a specifi c Sample object.<br />

sample1.play();<br />

The sound can be s<strong>to</strong>pped<br />

with the function s<strong>to</strong>p().

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

Saved successfully!

Ooh no, something went wrong!