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.

384 <strong>Learning</strong> <strong>Processing</strong><br />

Just as with images, loading the sound fi le from the hard drive is a slow process so the previous line of<br />

code should be placed in setup( ) so as <strong>to</strong> not hamper the speed of draw( ) .<br />

Th e type of sound fi le compatible with Sonia is somewhat limited. Only sounds that are formatted as WAV<br />

or AIFF fi les (16-bit mono or stereo) are allowed. If you want <strong>to</strong> use a sound fi le that is not s<strong>to</strong>red in a<br />

compatible format, you could download a free audio edi<strong>to</strong>r, such as Audacity ( http://audacity.sourceforge.net/ )<br />

and convert the fi le. Most audio fi les can be saved raw or with compression. Sonia only works with raw fi les.<br />

However, in the next section, we will demonstrate how compressed (MP3) fi les can be played with the<br />

ESS library.<br />

Once the sound is loaded, playing is easy.<br />

dingdong.play();<br />

Th e following example plays a doorbell sound whenever the mouse is clicked on the graphical doorbell.<br />

Th is Doorbell class implements simple but<strong>to</strong>n functionality (rollover and click) and just happens <strong>to</strong><br />

be a solution for Exercise 9–8 . Th e code for the new concepts (related <strong>to</strong> sound playback) is shown in<br />

bold type .<br />

Example 20-2: Doorbell with Sonia<br />

// Import the Sonia library<br />

import pitaru.sonia_v2_9.*;<br />

// A sample object (for a sound)<br />

Sample dingdong;<br />

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

Doorbell doorbell;<br />

void setup() {<br />

size(200,200);<br />

Sonia.start(this); // Start Sonia engine.<br />

// Create a new sample object.<br />

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

// Create a new doorbell<br />

doorbell = new Doorbell(150,100,32);<br />

smooth();<br />

}<br />

void draw() {<br />

background(255);<br />

// Show the doorbell<br />

doorbell.display(mouseX,mouseY);<br />

}<br />

The play() function plays the sound sample once.<br />

fi g. 20.1

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

Saved successfully!

Ooh no, something went wrong!