sharing work and request for help to bring it to the OpenProcessing platform

edited May 2018 in JavaScript Mode

this code takes in input a wav(for now just wav file!!) file, but unlike minim works in deferred time, so you can save every frame without having the problem of a non-constant frameRate, so we will not have problems synchronizing with the audio later.

the code generates points approximating the waveform of the buffer read in the for loop. the points are connected by lines, and the rangeMax parameter is used to define the maximum distance for which a line joins two points. the other modifiable parameters are: start - this parameter determines from which sample to start reading the audio buffer. end - this parameter determines which sample to stop reading the audio buffer. buffer - the size of the buffer read at each cycle

Note fps are determined by the buffer size because a second of audio is equivalent to 44100 samples, so the calculation to know the fps is: 44100 / buffer. so if the buffer is 1024 the corresponding fps will be: 44100/1024 = 43.06640625 to a larger buffer will correspond more points read every cycle, therefore less fps. to synchronize everything so you will need to import the photos into a program like Adobe Premiere or similar and re-map the photo display time according to the size of end less start. so if we have saved 30 seconds of audio we will have to remap the total number of frames generated in this time. or if you prefere you can generate 24 fps settings buffer=44100/24

on my pc it work perfectly, but if i try it here: https://www.openprocessing.org/sketch/556158 it doesn't work and give me this error: $this_1.str is not a function

can someone help me please? thank you very much!

here the code:

Wav wav;
ArrayList<PVector> pos;
int start=0, end=200;
int rangeMax=300, sampleStart=44100*start, sampleFinal=44100*end, buffer=1024;
float max, min, sample_Max;
color back=0, line=50, ball=color(0, 0, 255);
float[] r;
void setup() {
  size(1920, 1080, P2D);
  wav = new Wav();
  wav.read("ikeda_01.wav");
  r=wav.getSampleArray();
  float max=max(r);
  float min=min(r);
  sample_Max=max(abs(min), abs(max));
  println(sample_Max);
  pos=new ArrayList<PVector>();
  frameRate(1000);
  println(buffer);
}


void draw() {
  //println(frameCount);
  while (pos.size()>0) { 
    for (int i=0; i<pos.size(); i+=1) {
      pos.remove(i);
    }
  }

  if (abs(r[sampleStart])<sample_Max/3) {
    back=color(0);
    line=color(255, 50);
    ball=color(10, 100, 255);
    //println(frameCount, "ciao");
  } else {
    back=color(255);
    line=color(0);
    ball=color(255, 0, 0);
  }
  background(back);
  stroke(ball);
  strokeWeight(12);
  for (int i=sampleStart; i<(sampleStart+buffer); i+=3) {
    int si=(int)map(i, sampleStart, (sampleStart+buffer), 0, width);
    float val=height/2+(r[i]/sample_Max)*(height/2);
    point(si, val);
    PVector momPos=new PVector(si, val);
    pos.add(momPos);
  }

  sampleStart+=buffer;
  stroke(line);
  strokeWeight(1);
  for (int i=0; i<pos.size(); i+=1) {
    for (int y=1; y<pos.size(); y+=1) {
      if (pos.get(i).dist(pos.get(y))<rangeMax) {
        line(pos.get(i).x, pos.get(i).y, pos.get(y).x, pos.get(y).y);
      }
    }
  }
  if (sampleStart>=sampleFinal) {
    println(sampleStart);
    exit();
  }

  if (frameCount<10) saveFrame("E:/MediaWork/ProcesampleStarting/Sketch/pointline_audio_offline/frame/" + "asterion000"+str(frameCount)+".png");
  if (frameCount>=10 && frameCount<100) saveFrame("E:/MediaWork/ProcesampleStarting/Sketch/pointline_audio_offline/frame/" + "asterion00"+str(frameCount)+".png");
  if (frameCount>=100 && frameCount<1000) saveFrame("E:/MediaWork/ProcesampleStarting/Sketch/pointline_audio_offline/frame/" + "asterion0"+str(frameCount)+".png");
  if (frameCount>=1000 && frameCount<10000) saveFrame("E:/MediaWork/ProcesampleStarting/Sketch/pointline_audio_offline/frame/" + "asterion"+str(frameCount)+".png");
  println("save image n:", frameCount);
  //if (frameCount==120) exit();
}

/*
Method of Class:

 void read(String fileName)
 void write(float[] r_, String fileName) 
 void write(float[] r_, float[] l_, String fileName)
 void write(float[] r_, int numbit_, int samplingrate_, String fileName)
 void write(float[] r_, float[] l_, int numbit_, int samplingrate_, String fileName) 
 int getSampleLength()
 float getSampleValueLeft(int n) 
 float getSampleValueRight(int n) 
 float getSampleValue(int n)
 float getSampleArray()
 float getSampleArrayLeft()
 float getSampleArrayRight()
 int getBit()
 int getSamplingRate()

 */

class Wav {
  int sampleLength, stereomono, numbit;
  int b1, b2, b3, b4, x=0;
  int samplingrate;
  int Dim_file_dati;
  float [] r, l;
  int c1, c2, j;
  byte[] b;
  String str=str(hour())+"_"+str(minute())+"_"+str(second());

  Wav() {
  }

  void read(String fileName) {
    b=loadBytes(fileName);
    stereomono=b[22];

    b1=(b[24]+ 256) % 256;
    b2=(b[25]+ 256) % 256;
    b3=(b[26]+ 256) % 256;
    b4=(b[27]+ 256) % 256;
    samplingrate = b4*16777216+b3*65536+b2*256+b1;

    b1=(b[34] + 256) % 256;
    b2=(b[35] + 256) % 256;
    numbit=b2*256+b1;

    b1=(b[40]+ 256) % 256;
    b2=(b[41]+ 256) % 256;
    b3=(b[42]+ 256) % 256;
    b4=(b[43]+ 256) % 256;
    Dim_file_dati=b4*16777216+b3*65536+b2*256+b1;

    sampleLength=Dim_file_dati/(stereomono*(numbit/8));

    r = new float [sampleLength];
    l = new float [sampleLength];

    btf();
  }

  void btf() { 
    if (stereomono==1 && numbit==16) {
      j=44;
      for (int i=0; i<sampleLength; i++)
      {
        c1=(b[j]+256) % 256;
        j++;
        c2=(b[j]+256) % 256;
        j++;

        r[i]= int(c2*256+c1);
        if (r[i] > 32768) r[i]=r[i]-65536;
      }
    }
    if (stereomono==2 && numbit==16) {
      j=44;
      for (int i=0; i<sampleLength; i++) {
        c1=(b[j]+256) % 256;
        j++;
        c2=(b[j]+256) % 256;
        j++;
        r[i]= int(c2*256+c1);
        if (r[i] > 32768) r[i]=r[i]-65536;

        c1=(b[j]+256) % 256;
        j++;
        c2=(b[j]+256) % 256;
        j++;
        l[i]= int(c2*256+c1);
        if (l[i] > 32768) l[i]=l[i]-65536;
      }
    }
  }

  int getBit() {
    return numbit;
  }
  int getSamplingRate() {
    return samplingrate;
  }
  int getSampleLength() {
    return sampleLength;
  }

  float getSampleValue(int n) {
    return r[n];
  }
  float[] getSampleArray() {
    return r;
  }

  float getSampleValueLeft(int n) {
    return l[n];
  }
  float getSampleValueRight(int n) {
    return r[n];
  }
  float[] getSampleArrayLeft() {
    return l;
  }
  float[] getSampleArrayRight() {
    return r;
  }
  void write(float[] r) {
    write(r, str);
  }

  void write(float[] r, float[] l) {
    write(r, l, str);
  }

  void write(float[] r, String fileName) {
    write(r, 16, 44100, fileName);
  }

  void write(float[] r, float[] l, String fileName) {
    write(r, l, 16, 44100, fileName);
  }

  void write(float[] r, int numbit, int samplingrate, String fileName) {
    str=fileName;
    sampleLength=r.length;
    this.numbit=numbit;
    stereomono=1;
    this.samplingrate=samplingrate;
    this.r =r;
    normaLize(100);
    header();
  }

  void write(float[] r, float[] l, int numbit, int samplingrate, String fileName) {
    str=fileName;
    sampleLength=r.length;
    this.numbit=numbit;
    stereomono=2;
    this.samplingrate=samplingrate;
    this.r =r;
    this.l=l;
    normaLize(100);
    header();
  }

  void normaLize(float gain) {
    float maxSampleValueFinal=0;
    float maxSample=max(r);
    float minSample=min(r);
    float maxSampleValue=max(abs(minSample), abs(maxSample));
    if (stereomono==2) {
      maxSample=max(r);
      minSample=min(r);
      maxSampleValueFinal=max(abs(minSample), abs(maxSample));
    }
    gain=gain*32767/100;
    for (int i=0; i<sampleLength; i++)
    {
      r[i]=gain*r[i]/maxSampleValue;
      if (stereomono==2) l[i]=gain*l[i]/maxSampleValueFinal;
    }
  }

  void header() {
    int aux=sampleLength;
    int DimFile, dimFileData, sr = samplingrate;
    byte Stereomono = byte(stereomono), Numbit = byte(numbit);
    dimFileData = aux * Stereomono * (Numbit / 8);
    DimFile = dimFileData + 44;
    byte[] f=new byte[DimFile];
    f[0]='R';
    f[1]='I';
    f[2]='F';
    f[3]='F';
    byte f1, f2, f3, f4;
    f1=byte((DimFile-8)/16777216);
    f2=byte(((DimFile-8)- f1 * 16777216) / 65536);
    f3= byte(((DimFile-8) - f1 * 16777216 - f2 * 65536) / 256);
    f4 = byte((DimFile-8) % 256);
    f[4]=f4;
    f[5]=f3;
    f[6]=f2;
    f[7]=f1;
    f[8]='W';
    f[9]='A';
    f[10]='V';
    f[11]='E';
    f[12]='f';
    f[13]='m';
    f[14]='t';
    f[15]=' ';
    f[16]=16;
    f[17]=0;
    f[18]=0;
    f[19]=0;
    f[20]=1;
    f[21]=0;
    f[22]=Stereomono;
    f[23]=0;
    f1=byte(sr/16777216);
    f2=byte((sr - f1 * 16777216) / 65536);
    f3= byte((sr - f1 * 16777216 - f2 * 65536) / 256);
    f4 = byte(sr % 256);
    f[24]=byte(68);
    f[25]=byte(172);
    f[26]=byte(0);
    f[27]=byte(0);
    int byte_per_secondo= sr * Stereomono * Numbit / 8;
    f1=byte(byte_per_secondo/16777216);
    f2=byte((byte_per_secondo- f1 * 16777216) / 65536);
    f3= byte((byte_per_secondo - f1 * 16777216 - f2 * 65536) / 256);
    f4 = byte(byte_per_secondo % 256);
    f[28]=f4; 
    f[29]=f3; 
    f[30]=f2; 
    f[31]=f1; 
    int byte_da_leggere_in_ogni_istante = Stereomono * Numbit / 8;
    f[32]=byte(byte_da_leggere_in_ogni_istante);
    f[33]=0;
    f[34]=Numbit;
    f[35]=0;
    f[36]='d';
    f[37]='a';
    f[38]='t';
    f[39]='a';
    f1=byte(dimFileData/16777216);
    f2=byte((dimFileData- f1 * 16777216) / 65536);
    f3= byte((dimFileData - f1 * 16777216 - f2 * 65536) / 256);
    f4 = byte(dimFileData % 256);
    f[40]=f4;
    f[41]=f3;
    f[42]=f2;
    f[43]=f1;
    byte[] out=new byte[stereomono*2*sampleLength+44];
    int d1, d2;
    for (int j=0; j<44; j++) {
      out[j]=f[j];
    }

    for (int i=0; i<sampleLength; i++) {

      r[i]=r[i]+65536; // Questa somma si fa per evitare gli errori causati dai numeri negativi.
      d1=byte(r[i]/256);
      d2= byte(int(r[i]) % 256);
      out[j]=byte(d2);
      j++;
      out[j]=byte(d1);
      j++;

      if (stereomono==2) {
        l[i]=l[i]+65536; // Questa somma si fa per evitare gli errori causati dai numeri negativi.
        d1=byte(l[i]/256);
        d2= byte(int(l[i]) % 256);
        out[j]=byte(d2);
        j++;
        out[j]=byte(d1);
        j++;
      }
    }
    saveBytes(str+".wav", out);
    println(str);
  }
}

Answers

Sign In or Register to comment.