Data table to Midi question

Hi there. So I’m trying to create a project where I have some biometric data that I’ve recorded and convert it into MIDI to create a soundscape. I’ve managed to get the Midi library list part to work on its own and also managed to print the list of the data on it’s own.

Now i’m trying to combine the two and get it to send a MIDI note out if it reads the number 50 from the list, for example.

I feel like this is a really simple solution but I’m quite new to all of this so sorry for seeming like a noob! From research and speaking to a friend, I believe I need to use some kind of string search function but it’s slowly blowing my brain apart so any help would be most useful!

Many thanks,

Si.

import themidibus.*; //Import the library  


MidiBus midi; 


void setup() {
{
  MidiBus.list();
  midi = new MidiBus(this, -1, "Bus 1");

}


void sendNote(int ch, int n) {

  midi.sendNoteOn(ch, n, 100);
  

  
String[] lines = loadStrings("SimonData.Txt");
println("there are " + lines.length + " lines");
for (int i = 0 ; i < lines.length; i++) {
  println(lines[i]);


if (lines[i] == 50){
 sendNote(0, 70);
}

}

 }
}


    }
  

UPDATE!

This is the new code and now it runs but it no longer reads the .txt file, doesn’t print the data in the console before and doesn’t send a midi note. But there’s no errors!

import themidibus.*; //Import the library  


MidiBus midi; 


void setup() {
  {
    MidiBus.list();
    midi = new MidiBus(this, -1, "Bus 1");
  }
}


void sendNote(int ch, int n) {

  midi.sendNoteOn(ch, n, 100);



  String[] lines = loadStrings("SimonData.Txt");
  println("there are " + lines.length + " lines");
  for (int i = 0; i < lines.length; i++) {
    println(lines[i]);


    if (Integer.parseInt(lines[i]) < 50) {
      sendNote(0, 70);
    }
  }
}




Hi! Your program looks a bit odd: it seems like the only place where sendNote() is called is inside sendNote(), which makes it a recursive function, and I guess that is not intentional. Also loading a text file every time sendNote() is called doesn’t look ideal.

I believe the content of sendNote(), except the first line that sends the midi message, should be inside setup(), so it does that when the program starts.

Okay, I’ve tried to move

void sendNote(int ch, int n) {

  midi.sendNoteOn(ch, n, 100);

into the set up but am greeted with unexpected token:void again :frowning:

Hi, I meant the content of the function, not the function itself.

It would look like this:

import themidibus.*; //Import the library  

MidiBus midi; 

// when the program starts...
void setup() {
  // it shows the midi ports
  MidiBus.list();
  // connects to one of the buses
  midi = new MidiBus(this, -1, "Bus 1");

  // loads the file and prints how many lines there are
  String[] lines = loadStrings("SimonData.Txt");
  println("there are " + lines.length + " lines");

  // and for the first 50 lines
  for (int i = 0; i < lines.length; i++) {
    println(lines[i]);
    // if it contains a value below 50
    if (Integer.parseInt(lines[i]) < 50) {
      // it sends a note 70 on channel 0 
      // note: sends potentially many notes simultaneously, if there's many values below 50
      sendNote(0, 70);
    }
  }

}

void sendNote(int ch, int n) {
  midi.sendNoteOn(ch, n, 100);
}

Ah, amazing, I understand now what you’ve done! The only problem I’m having now is that the numbers contained in my txt file are a combination of whole numbers and decimal pointed numbers. Would I have to replace for (int i = 0; i < lines.length; i++) { and with a Float function? (I tried replacing for the word Float but it says I cannot convert from float to int)

Thank you so much for your help on this so far!

// Convert String to int
Integer.parseInt("42");

// Convert String to float
Float.parseFloat("0.39");