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.

void setup() {<br />

Sonia.start(this);<br />

}<br />

Sound 383<br />

Now, it would be rather irresponsible of us <strong>to</strong> make this connection <strong>to</strong> the sound hardware, but never<br />

bother <strong>to</strong> close it down when we are fi nished. By now, the fl ow of a <strong>Processing</strong> sketch is all <strong>to</strong>o familiar,<br />

start with setup( ) , loop with draw( ) , and s<strong>to</strong>p whenever you quit the sketch. Th e thing is, we want <strong>to</strong><br />

terminate the sound engine during that last step, when the sketch quits. Fortunately, there is an extra<br />

hidden function we can implement entitled s<strong>to</strong>p( ) that does precisely that. When a sketch quits, any<br />

last minute instructions can be executed inside the s<strong>to</strong>p( ) function. Th is is where we will shut down the<br />

Sonia engine.<br />

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

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

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

}<br />

Sonia.s<strong>to</strong>p( ) is the call <strong>to</strong> s<strong>to</strong>p Sonia. Th ere is also, however, the line super.s<strong>to</strong>p( ) . Th e meaning of “ super ”<br />

will be explored further in Chapter 22. For now, we can understand that line of code as saying “ Oh,<br />

whatever else you would normally do in s<strong>to</strong>p( ) , do that stuff , <strong>to</strong>o . ”<br />

All of the above applies <strong>to</strong> Minim as well. Here is the corresponding code:<br />

20.3<br />

import ddf.minim.*;<br />

void setup() {<br />

size(200, 200);<br />

Minim.start(this);<br />

}<br />

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

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

}<br />

Basic Sound Playback<br />

Once this call <strong>to</strong> the library has been made, you have the whole world (of sound) in your hands. We<br />

will start by demonstrating how <strong>to</strong> play sound fi les. Before a sound can be played, it must be loaded in<strong>to</strong><br />

memory, much in the same way we loaded image fi les before displaying them. With Sonia , a Sample<br />

object is used <strong>to</strong> s<strong>to</strong>re a reference <strong>to</strong> a sound sample.<br />

Sample dingdong;<br />

Th e object is initialized by passing the sound fi lename <strong>to</strong> the construc<strong>to</strong>r.<br />

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

Start the Minim sound engine!<br />

With Minim, sounds will be closed<br />

individually here. We will see this<br />

in a moment.<br />

The fi le “dingdong.wav” should be placed in<br />

the data direc<strong>to</strong>ry.

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

Saved successfully!

Ooh no, something went wrong!