User Tools

Site Tools


prensf2010:normalize

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
prensf2010:normalize [2010/07/05 10:18]
nels_oscar
prensf2010:normalize [2010/07/05 10:22] (current)
nels_oscar
Line 1: Line 1:
-<code java>+^Ess ^Minim ^ 
 +|<code java> ​ // 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();​ 
 +  }</​code>​|<code java>
 // Nels Oscar // Nels Oscar
 // Simple normalization using Minims AudioEffecs interface // Simple normalization using Minims AudioEffecs interface
Line 14: Line 81:
   background(0);​   background(0);​
   minim=new Minim(this);​   minim=new Minim(this);​
-  sample=minim.loadSample("​c4.wav",​1024);+  sample=minim.loadSample("​c4.wav",​4096);
   sample.addEffect(new Nermalize());​   sample.addEffect(new Nermalize());​
 } }
Line 47: Line 114:
          ​hiSample=abs(sig[j]);​          ​hiSample=abs(sig[j]);​
      }      }
-     float diff=1.+hiSample;+     float diff=1./hiSample;
      ​for(int i=0; i<​sig.length;​ i++){      ​for(int i=0; i<​sig.length;​ i++){
        ​sig[i]*=diff;​        ​sig[i]*=diff;​
Line 58: Line 125:
     ​     ​
 } }
-</​code>​+</​code>​|
  
prensf2010/normalize.1278343110.txt.gz · Last modified: 2010/07/05 10:18 by nels_oscar