Converting a CSV data into a sound track?

edited March 2017 in Library Questions

Hello!

I'm trying convert a long CSV data into a sound track and I wrote this code, but I'm not able to generate a sound. It's just a beep.

It's CSV on meteor spotting and I got the values for longitude and latitude and I tried to map it down for tri values.

Please help me as I'm pretty new to Processing :)

Screen Shot 2017-03-01 at 11.39.42 PM

Tagged:

Answers

  • Please don't post code as an image, nobody's going to retype it in order to try it.

    Where is tri defined? csv? Mydata?

    Don't use delay.

    For loops in draw probably don't do what you want.

    You are missing a }

  • edited March 2017

    Hi

    Sorry for posting the image. I understand. Here's the code. Please let me know if you can figure this out I'm getting the graphLong and graphLat values properly. (I don't know how to attach the CSV here in this comment)

    Similarly I also want to create a soundtrack using brightness values in an image. I know how to get the brightness of the pixel but I'm not sure where to put tri or sine function to get the sound.

    import processing.sound.*;
    import ddf.minim.*;
    import ddf.minim.ugens.*;
    import processing.sound.*;
    
    Minim minim;
    TriOsc tri;
    
    String csv[];
    String myData[][];
    
    void setup() {
      size(800, 500);
      noLoop();
    
      tri = new TriOsc(this);
      tri.play();
    
      csv = loadStrings("MeteorStrikes.csv");
      myData = new String[csv.length][6];
      for(int i=0; i<csv.length; i++) {
        myData[i] = csv[i].split(",");
      }
    }
    
    
    void draw() {
    
    
      for(int i=0; i<myData.length; i++){
    
        float graphLong = map(float(myData[i][3]), -180, 180, 0, width);
        float graphLat = map(float(myData[i][4]), 90, -90, 0, height);
    
        tri.amp(map(graphLong, 0,width, 1.0, 0.0));
        tri.freq(map(graphLat, 0,height, 80.0, 1000.0));
        //delay(2000);
    
      }
    
    }
    

    [mod edit: formatted]

  • edited March 2017

    Put '~~~~' before and after your code for a code block

  • edited March 2017


    import processing.sound.*; Minim minim; TriOsc tri; String csv[]; String myData[][]; void setup() { size(800, 500); noLoop(); tri = new TriOsc(this); tri.play(); csv = loadStrings("MeteorStrikes.csv"); myData = new String[csv.length][6]; for(int i=0; i<csv.length; i++) { myData[i] = csv[i].split(","); } } void draw() { for(int i=0; i<myData.length; i++){ float graphLong = map(float(myData[i][3]), -180, 180, 0, width); float graphLat = map(float(myData[i][4]), 90, -90, 0, height); tri.amp(map(graphLong, 0,width, 1.0, 0.0)); tri.freq(map(graphLat, 0,height, 80.0, 1000.0)); //delay(2000); } }
  • May I have your csv data? Just open it with any text editor,copy and paste it here

  • https://we.tl/qYtpryhMMf Please use this link for csv. It's huge to be pasted here, that's why :) Thanks

  • edited March 2017

    This is what I got. But kinda messed up. Is it okay though?

    import processing.sound.*;
    import ddf.minim.*;
    
    Minim minim; TriOsc tri;
    
    String csv[];String myData[][];
    
    void setup(){
      size(800,500);
      noLoop();
      tri = new TriOsc(this);tri.play();
      csv = loadStrings("MeteorStrikes.csv");
      myData = new String[csv.length-1][6];
      for(int i=0;i<csv.length-1;i++){
        myData[i] = csv[i+1].split(",");
      }
    }
    
    void draw(){
      beginShape();
      for(int i=0;i<myData.length;i++){
        float lon = Float.valueOf(myData[i][2]);
        float lat = Float.valueOf(myData[i][3]);
        float graphLong = map(lon,-180,180,0,width);
        float graphLat = map(lat,90,-90,0,height);
    
        println(graphLong + "\t" + graphLat);
        vertex(graphLong,graphLat);
    
        tri.amp(map(graphLong,0,width,1.0,0.0));
        tri.freq(map(graphLat,0,height,80.0,1000.0));
        //delay(1000);
      }
      endShape();
    }
    
  • Hi Thanks for these, it's much better. But won't be able to slow down and if we try to slow down with delay() the images doesn't show up.

  • by putting that loop in draw you are drawing 34000 lines every frame.

    you are also changing the amplitude and the frequency 34000 times every frame.

    For loops in draw probably don't do what you want.

  • You can try using counter to slow down the speed like..

    import processing.sound.*;
    import ddf.minim.*;
    
    Minim minim; 
    TriOsc tri;
    int counter=0;
    
    String csv[];
    String myData[][];
    
    void setup() {
      size(800, 500);
      frameRate(60);
      //print(frameRate);
      //noLoop();
      tri = new TriOsc(this);
      tri.play();
      csv = loadStrings("MeteorStrikes.csv");
      myData = new String[csv.length-1][6];
      for (int i=0; i<myData.length; i++) {
        myData[i] = csv[i+1].split(";");
      }
    }
    
    void draw() {
      if (counter==0)beginShape();
      println("Current i = " + counter);
    
      float lon = Float.valueOf(myData[counter][2]);
      float lat = Float.valueOf(myData[counter][3]);
      float graphLong = map(lon, -180, 180, 0, width);
      float graphLat = map(lat, 90, -90, 0, height);
    
      //println(graphLong + "\t" + graphLat);
      vertex(graphLong, graphLat);
    
      tri.amp(map(graphLong, 0, width, 1.0, 0.0));
      tri.freq(map(graphLat, 0, height, 300.0, 1000.0));
    
      if (counter < myData.length - 1)counter++;
    
      if (counter == myData.length - 1)endShape();
    }
    
  • out of bounds error on line 29

    myData[i] = csv[i+1].split(";");
    

    it's comma separated, why split on ';' ?

  • yeah will try that, thanks a lot for the support!

  • and why

    float lon = Float.valueOf(myData[counter][2]);
    float lat = Float.valueOf(myData[counter][3]);
    

    when the data looks like this

    Santa Catharina,1875,7000000,-48.6,-26.21667,Found
    

    so [2] is 7000000

    you want [3] and [4]

  • and this doesn't make sense

    if (counter==0)beginShape();
    ...
    if (counter == myData.length - 1)endShape();
    

    it'll only draw to the screen when it's done all 34000 samples

  • Hi koog, Even [2] is fine. But I was just randomly trying.

  • edited March 2017

    it's comma separated, why split on ';' ?

    I replace all the commas with semicolon cause I want to edit using excel

    it'll only draw to the screen when it's done all 34000 samples

    Yeah, but that graph is just an optional feature

    you want [3] and [4]

    Ohhh, my bad.

  • edited March 2017

    If you like insanity then you can try this:-

    import processing.sound.*;
    import ddf.minim.*;
    
    static TriOsc tri;
    int counter=0;
    
    String csv[];
    String myData[][];
    
    void setup() {
      size(800, 500);
      frameRate(100);
      tri = new TriOsc(this);
      tri.play();
      csv = loadStrings("MeteorStrikes2.csv");
      myData = new String[csv.length-1][6];
      for (int i=0; i<myData.length; i++) {
        myData[i] = csv[i+1].split(";");
      }
    }
    
    void draw() {
      println("Current i = " + counter);
    
      beginShape();
    
      float lon1 = Float.valueOf(myData[counter][3]);
      float lat1 = Float.valueOf(myData[counter][4]);
    
      tri.amp(map(lon1, 0, width, 1.0, 0.0));
      tri.freq(map(lat1, 0, height, 300.0, 1000.0));
    
      vertex(map(lon1, -180, 180, 0, width), map(lat1, 90, -90, 0, height));
    
      if (counter < myData.length - 1) {
        counter ++;
    
        float lon2 = Float.valueOf(myData[counter][3]);
        float lat2 = Float.valueOf(myData[counter][4]);
    
        //Dont need to play the next end since it will be play later in the first one
        //tri.amp(map(lon2, 0, width, 1.0, 0.0));
        //tri.freq(map(lat2, 0, height, 300.0, 1000.0));
    
        vertex(map(lon2, -180, 180, 0, width), map(lat2, 90, -90, height/2, height));
        endShape();
      }
    }
    

    Be careful though. LEL

  • Haha Thanks. Small error, cannot find class type 'Chord'

  • Ohhh, my bad. Just delete that Chord thingy. I was just experimenting with that class.

  • This could be more exciting:-

    import processing.sound.*;
    import ddf.minim.*;
    
    static TriOsc tri;
    int counter=0;
    
    String csv[];
    String myData[][];
    
    void setup() {
      size(800, 500);
      frameRate(100);
      tri = new TriOsc(this);
      tri.play();
      csv = loadStrings("MeteorStrikes2.csv");
      myData = new String[csv.length-1][6];
      for (int i=0; i<myData.length; i++) {
        myData[i] = csv[i+1].split(";");
      }
    }
    
    void draw() {
      background(255);
      println("Current i = " + counter);
    
      float lon1 = Float.valueOf(myData[counter][3]);
      float lat1 = Float.valueOf(myData[counter][4]);
    
      tri.amp(map(lon1, 0, width, 1.0, 0.0));
      tri.freq(map(lat1, 0, height, 300.0, 1000.0));
    
      if (counter < myData.length - 1) {
        counter ++;
    
        float lon2 = Float.valueOf(myData[counter][3]);
        float lat2 = Float.valueOf(myData[counter][4]);
    
        //Dont need to play the next end since it will be play later in the first one
        //tri.amp(map(lon2, 0, width, 1.0, 0.0));
        //tri.freq(map(lat2, 0, height, 300.0, 1000.0));
    
        line(map(lon1, -180, 180, 0, width), map(lat1, 90, -90, 0, height),map(lon2, -180, 180, 0, width), map(lat2, 90, -90, height/2, height));
      }
    
      delay((int)random(70,100));
    }
    
  • This is crazy :D Thank you! Now let me figure out this code.

  • You can get better beats if you randomize counter in draw() and you sort your .csv file by its longitude or latitude. By randomizing the beats, it will be endless. :)>-

Sign In or Register to comment.