get all sample values with minim

edited October 2013 in Library Questions

https://dl.dropboxusercontent.com/u/17630770/temp/a.mp3

I like to get all values of a short sound sample in an array. Only what i have now makes no sense comparing to the image i see in audacity. I know audacity draws from 1 to -1 and i map them so all goes up but that's not the problem.

import ddf.minim.analysis.*;
import ddf.minim.*;

Minim       minim;
AudioPlayer out;

float[] mix;

float minValue = MAX_FLOAT;
float maxValue = MIN_FLOAT;


int bufferLenghtInMillis;


void setup()
{
  size(800, 600);

  minim = new Minim(this);

  out = minim.loadFile("a.mp3", 1024);

  println("lenght in ms: "+out.length());

  // sample rate is in samples a second and lenght is in millis
  int numberOfSamples = int(out.sampleRate()*((float)out.length()/1000));
  println("numberOfSamples: "+numberOfSamples);

  // here we will store all samples
  mix = new float[numberOfSamples];

  // let's say sampleRate is 2048 a second
  // and buffer is 1024 then the buffer hold 0.5 seconds
  // 1024 / 2048 = 0.5 seconds
  // our sample rate is 32000
  // 1024 / 32000 = 0.032 
  // 0.032 * 1000 = 32 milliseconds for the buffer

  bufferLenghtInMillis = int((out.bufferSize()*1000) / out.sampleRate());
  println("bufferLenghtInMillis: "+bufferLenghtInMillis);

  // without play all is NaN
  out.play();
}


void draw() {

  background(255);

  //println(out.bufferSize());


  int cuePos = 0;
  int index = 0;

  while(true) {
    out.cue(cuePos);

    for (int i = 0; i < out.bufferSize(); i++) {
      if(index < mix.length) {
        mix[index] = out.mix.get(i);

        minValue = min(minValue, mix[index]);
        maxValue = max(maxValue, mix[index]);
      }
      index++;
    }

    cuePos += bufferLenghtInMillis;

    if(cuePos >= out.length()) {
      break;
    }

  }

  // visualise
  strokeWeight(0.1);
  float x, y;


  for(int i = 0; i < mix.length; i++) {
     x = map(i, 0, mix.length, 0, width);
     y = map(mix[i], minValue, maxValue, height, 0);

     //print(y+" ");
     line(x, height, x, y);
  }


  noLoop();
  println("done");

}
Tagged:

Answers

  • edited October 2013

    Hi! Does this help at all?

    import ddf.minim.analysis.*;
    import ddf.minim.*;
    
    Minim       minim;
    AudioPlayer jingle;
    
    void setup() {
      size(800, 600);
      background(0);
      stroke(255, 10);
      minim = new Minim(this);  
      jingle = minim.loadFile("jingle.mp3", 1024);
      jingle.play();
    }
    
    void draw() {
     float n; 
      for(int i = 0; i < jingle.bufferSize()-1; i++) {
        n = jingle.left.get(i)*height/2;
        line(frameCount, height*.33 - n, frameCount, height*.33 + n );
        n = jingle.right.get(i)*height/2;
        line(frameCount, height*.66 - n, frameCount, height*.66 + n );
      }
    }
    

    Ps. I know it's not reading all the data into an array. I thought it might give ideas about how to display the numbers so they look like in Audacity...

  • Sorry for my late answer. I was not getting an email notification. I will check later when i'm at home.

Sign In or Register to comment.