// minim effect demonstration: a simple bit cruncher // Adam Parrish import ddf.minim.*; import ddf.minim.effects.*; AudioPlayer song; BitCrunchEffect bc; // the BitCrunchEffect class is defined below LowPassFS lp; PFont font; int currentBits = 8; void setup() { size(600, 128); smooth(); Minim.start(this); song = Minim.loadFile("snippet.mp3"); song.loop(); bc = new BitCrunchEffect(); // initialize effect bc.setBits(currentBits); // setBits() is a custom method for this effect! song.addEffect(bc); // add this effect to the audioplayer object font = loadFont("NewsGothicMT-16.vlw"); } void draw() { // draw waveform background(0); stroke(255); float[] mix = song.mix.toArray(); for (int i = 0; i < mix.length; i++) { float esize = mix[i] * 64; line(i, 64 + esize, i, 64 - esize); } // how many bits? textFont(font); textSize(16); textAlign(CENTER); text(currentBits, width - 16, height - 8); } void keyPressed() { if (keyCode == UP) { currentBits++; } if (keyCode == DOWN) { currentBits--; } currentBits = constrain(currentBits, 2, 16); bc.setBits(currentBits); } void stop() { song.close(); super.stop(); } // BitCrunchEffect: a very simple audio effect. class BitCrunchEffect implements AudioEffect { int bits = 12; // process() receives an array of floats; you need to change the values in this array // to make your effect work. void process(float[] samp) { for (int i = 0; i < samp.length; i++) { samp[i] = int(samp[i] * bits * bits) / (float)(bits * bits); } } void process(float[] left, float[] right) { process(left); process(right); } // this method allows us to set the number of bits to crunch to void setBits(int bits_) { bits = bits_; } }