User Tools

Site Tools


prensf2010:normalize
Ess Minim
  // Example by Krister Olsson
  // Modified by spc
 
  import krister.Ess.*;
 
  AudioChannel myChannel;
 
  void setup() {
    size(256,200);
    background(0, 0, 255);
 
    // start up Ess
    Ess.start(this);
 
    // load "cell.aif" into a new AudioChannel
    myChannel=new AudioChannel("bassoon-g4.wav");
    normalize(myChannel);
 
    /* draw every 200th sample
    for (int i=0;i<256;i++) {
      set(i,(int)(100+myChannel.samples[i*200]*100),255);
    }*/
 
    // draw each sample (as many as the window can hold)
    for (int i=0;i<myChannel.size;i++) {
      set(i,(int)(100+myChannel.samples[i]*100),0);
    }
 
    // start the sound looping forever
    myChannel.play();
    myChannel.saveSound("modified.wav");
  }
 
  float maxSample(AudioChannel chan) {
    float largest = 0;
    for (int i = 0; i < chan.size; i++) {
      if (largest < chan.samples[i])
         largest = chan.samples[i];
    }
    return largest;
  }
 
  void normalize(AudioChannel chan) {
    float largest;
 
    // find the value of the largest amplitude
    largest = maxSample(chan);
    println(largest);
 
    // compute a multiplier, assuming max positive == max negative
    float mult = 1.0 / largest;
 
    // apply the multiplier to every sample
    for (int i = 0; i < chan.size; i++) {
      chan.samples[i] *= mult;
    }
  }
 
  void draw() {
  } 
 
  // we are done, clean up Ess
 
  public void stop() {
    Ess.stop();
    super.stop();
  }
// Nels Oscar
// Simple normalization using Minims AudioEffecs interface
 
import ddf.minim.*;
import ddf.minim.signals.*;
 
Minim minim;
 
AudioSample sample;
 
void setup(){
  size(200,100);
  background(0);
  minim=new Minim(this);
  sample=minim.loadSample("c4.wav",4096);
  sample.addEffect(new Nermalize());
}
 
void draw(){
  background(0);
  text(sample.isEffected() ? "Normalized":"Normal",10,height-10);
}
 
void keyPressed(){
  if(sample.isEffected())
    sample.disableEffect(0);
  else
    sample.enableEffect(0);
  sample.trigger();
}
 
 
 
void stop(){
  minim.stop();
  super.stop();
}
 
class Nermalize implements AudioEffect{
   public Nermalize(){}
 
   void process(float[] sig){
     float hiSample=0.;
     for (int j = 0; j < sig.length/2; j++) {
       if(hiSample<abs(sig[j]))
         hiSample=abs(sig[j]);
     }
     float diff=1./hiSample;
     for(int i=0; i<sig.length; i++){
       sig[i]*=diff;
     }
   }
   void process(float[] sigL, float[] sigR){
     process(sigL);
     process(sigR);
   }
 
}
prensf2010/normalize.txt · Last modified: 2010/07/05 10:22 by nels_oscar