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.

382 <strong>Learning</strong> <strong>Processing</strong><br />

used <strong>to</strong> play a WA V or AIFF fi le. WA V is an audio fi le format that stands for “ Waveform audio format ”<br />

and is the standard format for PCs. AIFF stands for “ Audio Interchange File Format ” and is commonly<br />

used on Macin<strong>to</strong>sh computers.<br />

All of the functions we looked at in Chapter 16 are available for a sound fi le. Th e main diff erence, of<br />

course, is that we cannot use image( ) <strong>to</strong> display the Movie object since there is no image.<br />

20.2<br />

Example 20-1: Simple sound with video library<br />

import processing.video.*;<br />

Movie movie;<br />

void setup() {<br />

size(200, 200);<br />

movie = new Movie (this, " dingdong.wav " );<br />

}<br />

void draw() {<br />

background(0);<br />

noLoop();<br />

}<br />

void mousePressed() {<br />

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

movie.play();<br />

}<br />

Exercise 20-1: Using the bouncing ball sketch from Example 5–6, play a sound eff ect every<br />

time the ball bounces off a window’s edge.<br />

Getting Started with Sonia and Minim<br />

Th e Movie class will do for playing simple sound eff ects, but for more sophisticated playback<br />

functionality, we will need <strong>to</strong> look at libraries designed for use with sound. We will start with Sonia<br />

( http://sonia.pitaru.com/ ). First things fi rst, as with any contributed library, you fi rst need the import<br />

statement at the <strong>to</strong>p of your code.<br />

import pitaru.sonia_v2_9.*;<br />

Construct a Movie object with a WAV<br />

(or AIFF) fi le.<br />

The sound is played whenever the mouse is<br />

pressed. Including the function s<strong>to</strong>p() before<br />

play() ensures that the sound fi le will play<br />

from the beginning.<br />

Sonia is an interface <strong>to</strong> a Java sound library, entitled Jsyn by Phil Burk. In order for it <strong>to</strong> work, Sonia has<br />

<strong>to</strong> communicate with the audio playback and input hardware on your computer. Th is communication is<br />

achieved via Jsyn. Before any sounds can be played, therefore, that link needs <strong>to</strong> be established, that is, the<br />

sound engine needs <strong>to</strong> be started. Th is is done with the following line of code, which you would place in<br />

setup( ) .

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

Saved successfully!

Ooh no, something went wrong!