Reading .CSV file and displaying visuals in accordance with the data

edited October 2017 in Library Questions

Hello everyone,

I have a spreadsheet with the first 1 million numbers in PI. The goal is to have different visuals displayed along with music tones in accordance with the numbers in the chart (0-9). The sound works, however I cannot get the program to show any visuals at the same time. Eg. I want the program to draw an ellipse whenever "0" is read from the .CSV file.

Here is my code:

Table data;
int[] row; 

import ddf.minim.*;

Minim minim;

AudioPlayer DO;
AudioPlayer RE;
AudioPlayer MI;
AudioPlayer FA;
AudioPlayer SOL;
AudioPlayer LA;
AudioPlayer TI;
AudioPlayer DOO;

void setup() {
  size(500, 500);
  background(50);
  minim = new Minim(this);
  data = loadTable("pi.csv", "header");

  DO = minim.loadFile("do.wav");
  RE = minim.loadFile("re.wav");
  MI = minim.loadFile("mi.wav");
  FA = minim.loadFile("fa.wav");
  SOL = minim.loadFile("sol.wav");
  LA = minim.loadFile("la.wav");
  TI = minim.loadFile("ti-WAV.wav");
  DOO = minim.loadFile("do-octave.wav");
}

void draw() {
  fill(250);
  stroke(250);


  row = new int[ data.getRowCount()]; //create an array for row


  for (TableRow row : data.rows()) {
    for (int i=0; i < 10; i++) { // for loop with an increasing "i" allows program go get info through the rows
      int A = row.getInt(i);  //intA is equal to the number in the row

      delay(200);
      ellipse(mouseX, mouseY, 100, 50);

      if (A==0) {
        delay(500);
        ellipse(100, 100, 100, 50);
      }

      if (A==1) {
        RE.rewind();
        RE.play();
        ellipse(200, 200, 50, 50);
      }

      if (A==2) {
        MI.rewind();
        MI.play();
      }

      if (A==3) {
        FA.rewind();
        FA.play();
      }

      if (A==4) {
        SOL.rewind();
        SOL.play();
      }

      if (A==5) {
        LA.rewind();
        LA.play();
      }
      if (A==6) {
        TI.rewind();
        TI.play();
      }
      if (A==7) {
        DOO.rewind();
        DOO.play();
      }
      if (A==8) {
        delay(300);
      }

      if (A==9) {
        DO.rewind();
        DO.play();
      }
    }
  }
}

What can I do? Thank you!!

Answers

  • Your problem is that you have a loop within draw when draw itself is a loop.

    Read the thread in Common Questions about setup and draw.

  • And also the one about many variables to an array because your code is full of unnecessary repetition.

  • @koogs

    Thank you for your response! How would it be possible to read data from a .csv file without using a for loop?

  • read all the data in setup() (for loop). use a global array. access that array in draw (using a global index counter that increments every frame).

  • @koogs

    Thank you for your response once again, I tried reading all of the data in setup using a for loop and a global array, however, I get the error NullPointerException from the line "datafromtable[d] = row.getInt(i);"

    Table data;
    int[] row;
    int[] datafromtable;
    
    void setup() {
      size(500, 500);
      background(50);
      data = loadTable("pi.csv", "header");
    
      row = new int[data.getRowCount()]; 
    
      for (TableRow row : data.rows()) {
        for (int i=0; i < 9; i++) { 
          for (int d=0; d<999999; d++) {
          datafromtable[d] = row.getInt(i);
    
          }
        }
      }
    }
    
    
    void draw() {
    
    }
    

    What should I do :-( ???

  • You need to give datafromtable a size.

    That said, I think you might have everything you need in line 8, you can read the data from the table one value at a time in your draw loop using your data variable without the need for an array (and certainly not two).

    Is your csv just one digit per line?

  • something like

    Table table;
    int index = 0;
    int count;
    
    void setup() {
      frameRate(2); // just slow things down
      table = loadTable("pi.csv", "header");
      count = table.getRowCount();
      println("Count: " + count);
    }
    
    void draw() {
      // get next row
      TableRow row = table.getRow(index);
      // get and print value
      println("Value[" + index + "]: " + row.getInt(0));
      // next row
      index++;
      if (index >= count) {
        println("Done");
        noLoop(); // stop
      }
    }
    

    the csv file:

    pi
    3
    1
    4
    1
    5
    9
    2
    6
    5
    4
    
Sign In or Register to comment.