<< Chapter < Page Chapter >> Page >

Let us analyze the Processing code that implements the Sound Chooser in its salient aspects. The Sonia.start(this) command is necessary to activate the Sonia audio engine. The line Sample mySample1 declares a variable aimed at containing audio samples. Several methods can be applied to suchvariable. Among these, the play methods plays the sound sample back. In the draw() code section the graphic aspect of the applet is defined. Finally, by thefunction mouseReleased() , we detect when the mouse is released after being pressed, and where it has beenreleased. At this point a sequenceo of if conditions finds what instrument/timbre has been selected according to the clicking point. Moreover, within the function mouseReleased() the function filtra(float[] DATAF, float[]DATA, float RO, float WC) is invoked. This function, which is implemented in the lastsegment of the code listing, performs a sound filtering. More precisely, it is a low-pass filter, thus a filter that leavesthe low frequencies unaltered and reduces the intensity of the high frequencies. According to the radial position of themouse click, the filtering effect changes, being more dramatic (that is the sound becomes darker) as the mouse is released closer and closer to thecentre. A lighter realization of the Sound Chooser by means of the library Minim is proposed in problem [link] . The problem [link] explores the recent library Beads.

Trumpet
Oboe
Violin
Flute
Applet: choosing a timbre and controlling its brightness
import pitaru.sonia_v2_9.*;Sample mySample1, mySample2, mySample3, mySample4; Sample mySample1F, mySample2F, mySample3F, mySample4F;float[] data1, data2, data3, data4;float[] data1F, data2F, data3F, data4F;int sr = 11025; // sampling rate void setup(){ size(200, 200);colorMode(HSB, 360, height, height); Sonia.start(this);mySample1 = new Sample("flauto.aif"); mySample2 = new Sample("oboe.wav");mySample3 = new Sample("tromba.wav"); mySample4 = new Sample("violino.wav");mySample1F = new Sample("flauto.aif"); // ... OMISSIS ...data1 = new float[mySample1.getNumFrames()];// creates new arrays the length of the sample // for the original sound// ... OMISSIS ... data1F = new float[mySample1.getNumFrames()]; // creates new arrays the length of the sample// for the filtered sound // ... OMISSIS ...mySample1.read(data1); // ... OMISSIS ...} void draw(){ // ... OMISSIS ...} void mouseReleased(){ float ro;float roLin; float wc;// FLAUTO if ((mouseX>95)&&(mouseX<105)&&(mouseY>50)&&(mouseY<90)) { roLin = (mouseY-49.99)/41;ro = pow(roLin,.33); wc = 298*(TWO_PI/sr);filtra(data1F, data1, wc, ro); mySample1F.write(data1F);mySample1F.play(); }// ... OMISSIS ... }//filtra = new function void filtra(float[]DATAF, float[] DATA, float WC, float RO) {float G; float RO2;RO2 = pow(RO, 2); G = (1-RO)*sqrt(1-2*RO*cos(2*WC)+RO2)*4; // (*4) is for having it louderfor(int i = 3; i<DATA.length; i++){ DATAF[i]= G*DATA[i]+2*RO*cos(WC)*DATAF[i-1]-RO2*DATAF[i-2];//recursive filtering }} // safely stop the Sonia engine upon shutdown.public void stop(){ Sonia.stop();super.stop(); }

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Media processing in processing. OpenStax CNX. Nov 10, 2010 Download for free at http://cnx.org/content/col10268/1.14
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Media processing in processing' conversation and receive update notifications?

Ask