<< Chapter < Page Chapter >> Page >

The content of a PImage object is accessible through its pixels[] field. The pixels, corresponding to a row-by-row reading of the image, arecontained in this array of size width*height . Modify the code in [link] to use the field pixels[] instead of the method get() . The final outcome should remain the same.

The invocation b.set() should be replaced by b.set(i,j,b.pixels[j*b.width+i]+ color(0,0,0, 255 - (int)((1-ramp)*255)) );

Got questions? Get instant answers now!

Complete the code reported in [link] to obtain the complete Sound Chooser applet.

Got questions? Get instant answers now!

Add some color to the radii of the Sound Chooser, by replacing the line instructions with rect instructions and coloring the bars with a brightness that increasesgoint from the centre to the periphery.

Got questions? Get instant answers now!

Produce a new version of the Sound Chooser of problem [link] employing the library Minim. Note the gained compact form and simplicity of the code.

import ddf.minim.*; import ddf.minim.effects.*;Minim minim; AudioPlayer mySample1, mySample2, mySample3, mySample4;LowPassSP lpf1, lpf2, lpf3, lpf4; float cutoff1, cutoff2, cutoff3, cutoff4;void setup() {size(200, 200); colorMode(HSB, 360, height, height);minim = new Minim(this); mySample1 = minim.loadFile("flauto.aif");mySample2 = minim.loadFile("oboe.wav"); mySample3 = minim.loadFile("tromba.wav");mySample4 = minim.loadFile("violino.wav");lpf1 = new LowPassSP(4000, mySample1.sampleRate()); lpf2 = new LowPassSP(4000, mySample2.sampleRate());lpf3 = new LowPassSP(4000, mySample3.sampleRate()); lpf4 = new LowPassSP(4000, mySample4.sampleRate());mySample1.addEffect(lpf1); mySample2.addEffect(lpf2);mySample3.addEffect(lpf3); mySample4.addEffect(lpf4);}void draw() {stroke(255); strokeWeight(1);fill(0, 88, 88); ellipseMode(CORNER);ellipse(50,50,100,100); beginShape(LINES);vertex(50, 100); vertex(90, 100);vertex(110, 100); vertex(150, 100);vertex(100, 50); vertex(100, 90);vertex(100, 110); vertex(100, 150);endShape(); }void mouseReleased() {// FLUTE if ((mouseX>95)&&(mouseX<105)&&(mouseY>50)&&(mouseY<90)) { cutoff1 = map(mouseY, 50, 90, 1000, 30);lpf1.setFreq(cutoff1); println(mouseY + " + " +cutoff1);mySample1.rewind(); mySample1.play();} // OBOEif ((mouseX>110)&&(mouseX<149)&&(mouseY>95)&&(mouseY<105)) { cutoff2 = map(mouseX, 110, 149, 30, 1000);lpf2.setFreq(cutoff2); println(mouseX + " + " +cutoff2);mySample2.rewind(); mySample2.play();} // TRUMPETif ((mouseX>95)&&(mouseX<105)&&(mouseY>110)&&(mouseY<149)) { cutoff3 = map(mouseY, 110, 149, 30, 1000);lpf3.setFreq(cutoff3); println(mouseY + " + " +cutoff3);mySample3.rewind(); mySample3.play();} // VIOLINif ((mouseX>50)&&(mouseX<90)&&(mouseY>95)&&(mouseY<105)) { cutoff4 = map(mouseX, 50, 90, 1000, 30);lpf4.setFreq(cutoff4); println(mouseX + " + " +cutoff4);mySample4.rewind(); mySample4.play();} }// safely stop the Minim engine upon shutdown. public void stop(){mySample1.close(); mySample2.close();mySample3.close(); mySample4.close();minim.stop(); super.stop();}

Got questions? Get instant answers now!

Produce a new version of the Sound Chooser of problem [link] using the Beads library. The signal-processing flow is particularly readable from the resulting code.

import beads.*; AudioContext ac;String sourceFile; //path to audio file SamplePlayer mySample1, mySample2, mySample3, mySample4;Gain g; Glide cutoff1, cutoff2, cutoff3, cutoff4;OnePoleFilter lpf1, lpf2, lpf3, lpf4; void setup() {size(200, 200); colorMode(HSB, 360, height, height);ac = new AudioContext();sourceFile = sketchPath("") + "data/flauto.aif"; try {mySample1 = new SamplePlayer(ac, new Sample(sourceFile)); }catch (Exception e) { println("Exception while attempting to load sample.");e.printStackTrace(); // description of error exit();} mySample1.setKillOnEnd(false);sourceFile = sketchPath("") + "data/oboe.wav";try { mySample2 = new SamplePlayer(ac, new Sample(sourceFile));} catch (Exception e) {println("Exception while attempting to load sample."); e.printStackTrace(); // description of errorexit(); }mySample2.setKillOnEnd(false); sourceFile = sketchPath("") + "data/flauto.aif"; sourceFile = sketchPath("") + "data/tromba.wav";try { mySample3 = new SamplePlayer(ac, new Sample(sourceFile));} catch (Exception e) {println("Exception while attempting to load sample."); e.printStackTrace(); // description of errorexit(); }mySample3.setKillOnEnd(false); sourceFile = sketchPath("") + "data/flauto.aif"; sourceFile = sketchPath("") + "data/violino.wav";try { mySample4 = new SamplePlayer(ac, new Sample(sourceFile));} catch (Exception e) {println("Exception while attempting to load sample."); e.printStackTrace(); // description of errorexit(); }mySample4.setKillOnEnd(false);cutoff1 = new Glide(ac, 1000, 20); lpf1 = new OnePoleFilter(ac, cutoff1);lpf1.addInput(mySample1); cutoff2 = new Glide(ac, 1000, 20);lpf2 = new OnePoleFilter(ac, cutoff2); lpf2.addInput(mySample2);cutoff3 = new Glide(ac, 1000, 20); lpf3 = new OnePoleFilter(ac, cutoff3);lpf3.addInput(mySample3); cutoff4 = new Glide(ac, 1000, 20);lpf4 = new OnePoleFilter(ac, cutoff4); lpf4.addInput(mySample4);g = new Gain(ac, 1, 1);g.addInput(lpf1); g.addInput(lpf2);g.addInput(lpf3); g.addInput(lpf4);ac.out.addInput(g); ac.start();background(0); }void draw() {stroke(255); strokeWeight(1);fill(0, 88, 88); ellipseMode(CORNER);ellipse(50,50,100,100); beginShape(LINES);vertex(50, 100); vertex(90, 100);vertex(110, 100); vertex(150, 100);vertex(100, 50); vertex(100, 90);vertex(100, 110); vertex(100, 150);endShape(); }void mouseReleased(){ // FLAUTOif ((mouseX>95)&&(mouseX<105)&&(mouseY>50)&&(mouseY<90)) { cutoff1.setValue(map(mouseY, 50, 90, 1000, 30));mySample1.setToLoopStart(); mySample1.start();} // OBOEif ((mouseX>110)&&(mouseX<149)&&(mouseY>95)&&(mouseY<105)) { cutoff2.setValue(map(mouseX, 110, 149, 30, 1000));mySample2.setToLoopStart(); mySample2.start();} // TROMBAif ((mouseX>95)&&(mouseX<105)&&(mouseY>110)&&(mouseY<149)) { cutoff3.setValue(map(mouseY, 110, 149, 30, 1000));mySample3.setToLoopStart(); mySample3.start();} // VIOLINOif ((mouseX>50)&&(mouseX<90)&&(mouseY>95)&&(mouseY<105)) { cutoff4.setValue(map(mouseX, 50, 90, 1000, 30));mySample4.setToLoopStart(); mySample4.start();} }

Got questions? Get instant answers now!

Processing programmers are encouraged to use bitmap fonts, encoded in a file with extension .vlw . This makes Processing independent from the fonts that areactually installed on a specific machine. However, it is possible to use vectorial fonts (e.g., TrueType ) by inserting their files (e.g., with extension .ttf ) in the Data folder. Try experimenting with vectorial fonts by using the createFont() function. If we give up the invariance of behavior on different machines, we can passthis function the name of a font that is installed on a specific computer and not found in the Data folder. Finally, under JAVA2D rendering mode, it is possible to use logical fonts, by passing Serif , SansSerif , Monospaced , Dialog , or DialogInput as a string that specifies the font as an argument of createFont() . Without the need of loading any font files in the Data folder, the correspondence between logical and physical fonts will be system dependent. Tryexperimenting with logical fonts on your computer.

This is an example of solution. Please make sure that the fonts used are present in your computer or inthe Data folder. size(200,200, JAVA2D); PFont fonte;fonte = loadFont("HoeflerText-Black-48.vlw"); // previously created and inserted in Data textFont(fonte, 12);fill(10, 20, 250, 80); textAlign(RIGHT);text("pippo pippo non lo sa", 10, 14, 35, 70); textFont(fonte, 94);textAlign(LEFT); fill(200, 0, 0, 100);text("ppnls", 25, 5, 150, 190); fonte = createFont("Serif", 10, false); // Java logical fonttextFont(fonte, 80); fill(0, 200, 0, 170);rotate(PI/6); text("LO SO", 20, 20, 280, 280);fonte = createFont("cmsy10", 10, true); // font installed in the system textFont(fonte, 80);fill(0, 20, 150, 170); rotate(PI/12);text("ECCO", 20, 20, 280, 280); fonte = createFont("grunge.ttf", 10, true); // vectorial font in the Data foldertextFont(fonte, 80); fill(100, 100, 0, 170);rotate(-PI/6); text("qui", 20, 20, 280, 280);

Got questions? Get instant answers now!

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