HELP (stupid and mysterious?) format error - For structure within a MIDI responsive sketch

Good evening,

I am a total newbie.
I followed this tutorial: https://www.youtube.com/watch?v=L7AU2KAM0UE&t=274s
as the basis for a sketch that process MIDI input.
Now I wanted to assign specific colors to specific pitches, but when I try to simply do a “for” statement to assign a key-number (pitch) to a color, the debugger tells me that a ;, (, or a } is missing.

void display() {
  for (this.pitch = 30) {
   fill(255, 0, 0);
   ellipse(this.x, this.y, this.size, this.size); 
 
  }
}

I feel that it should be a very stupid problem… but I have tried different things and I cannot get pass this apparently simple step. Could you help me?
Here I post all the code contained in this particular tab of the sketch

class Note {
  
  int channel, velocity, pitch;   // store the channel, velocity and pitch
  int lifespan;                   // lifespan of note, in frames
  boolean isReleased;             // whether or not the note has been released yet
  float x, y;
  float size;
 
  
  // constructor for new Note object
  Note(int channel_, int pitch_, int velocity_) {
    this.channel = channel_;
    this.pitch = pitch_;
    this.velocity = velocity_;
    this.lifespan = 30;
    this.isReleased = false;
    
    this.x = map(pitch, 48, 109, 0, width);
    this.y = height/2;
    
    this.size = map(velocity, 0, 127, 5, 100);
 

}

// HERE IS THE PROBLEMATIC CODE
void display() {
  for (this.pitch = 30) {
   fill(255, 0, 0);
   ellipse(this.x, this.y, this.size, this.size); 
 
  }
}

  
  // update note properties
  void update() {

  }

I assume you want if (this.pitch == 30) {

(please note the if AND a double == here)

  • for is repeating some lines of code a few times. A for-loop is not an if-clause.

  • An if-clause is executing the code inside { .... } ONLY when the condition is true.

Remark

The correct term for a for-loop would be for(int i=0; i<10; i++) { ... } which would increment i 10 times and repeat everything inside { … }

2 Likes

Chrisir,

Thank you very much for your reply! I guess I need to review in depth the nature of the different types of statements.
But actually, I have already tried the code you suggest (with the double ==) , but if I declare it as an if statement, the debugger tells me: ‘extraneous input’ expecting {‘color’, 'abstract, . etc…

Anyhow,

this is not needed here.

It is only needed in setup() when the variable names are double (which they aren’t in your code)

can you show me your current code?

1 Like

Main Sketch

import themidibus.*;
import java.util.*;

MidiBus bus;
NoteManager nm;

void setup() {
  size(700, 700);
  background(0);
  MidiBus.list();
  
  bus = new MidiBus(this, 1, 2);
  nm = new NoteManager();
}

void draw() {
  background(0);
  nm.track();
}

void noteOn(int channel, int pitch, int velocity) {
  nm.addNote(new Note(channel, pitch, velocity));
  println("Note On: "+channel, +pitch, +velocity);
}

void noteOff(int channel, int pitch, int velocity) {
    nm.releaseNote(new Note(channel, pitch, velocity));

    println("Note Off: "  +channel, +pitch, +velocity);

}

 

Class Definition Note


// Note class handles attributes that all played notes share
class Note {
  
  int channel, velocity, pitch;   // store the channel, velocity and pitch
  int lifespan;                   // lifespan of note, in frames
  boolean isReleased;             // whether or not the note has been released yet
  float x, y;
  float size;
 
    // constructor for new Note object
  Note(int channel_, int pitch_, int velocity_) {
    this.channel = channel_;
    this.pitch = pitch_;
    this.velocity = velocity_;
    this.lifespan = 30;
    this.isReleased = false;
    
    this.x = map(pitch, 48, 109, 0, width);
    this.y = height/2;
    
    this.size = map(velocity, 0, 127, 5, 100);

  }

void display() {
  if (this.pitch == 30) {
   fill(50);
   ellipse(this.x, this.y, this.size, this.size); 
 
  }
}

  
  // update note properties
  void update() {

  }
  
  // display note on canvas
 

// NoteManager class manages the tracking of MIDI notes, as played live
class NoteManager {

public HashSet notes = new HashSet(); // all note objects currently being tracked
private HashSet notesToAdd = new HashSet(); // notes to add to the list of tracked notes
private HashSet release = new HashSet(); // notes that will be released on this iteration of the draw() loop
private HashSet notesToRelease = new HashSet(); // notes waiting for the next iteration of draw() to be released

// construct a new NoteManager object
public NoteManager() {

}

// add a new note to tracked notes
void addNote(Note n) {
// add note to list of notes that will be tracked in the next iteration of draw()
notesToAdd.add(n);
}

// remove a note from tracked notes
void releaseNote(Note n) {
// add note to list of notes that will be released in the next iteration of draw()
notesToRelease.add(n);
}

// add new notes to tracked notens, remove old notes from tracked notes
void track() {
this.release.addAll(this.notesToRelease); // add every note waiting to be released to list of notes about to be released
this.notesToRelease.clear(); // remove everything from list of notes waiting to be released

// for each note that needs to be released
for (Note n : this.release) {
  // find its counterpart in the tracked notes array
  for (Note m : this.notes) {
    if (n.channel == m.channel && n.pitch == m.pitch) {
      m.isReleased = true;  // record that this note is now released
    }
  }
}

this.release.clear();  // remove everything from the list of notes to remove

this.notes.addAll(this.notesToAdd);  // add every note waiting to be kept track of
this.notesToAdd.clear();  // remove everything from list of notes waiting to be tracked

// iterate through all notes currently being tracked
Iterator<Note> iter = this.notes.iterator();
while (iter.hasNext()) {
  Note n = iter.next();
  
  // if note has been released, decrement lifespan
  if (n.isReleased) {
    n.lifespan--;
  }
  
  // update and display each note
  n.update();
  n.display();
  
  // if a note is finished, remove from tracked notes
  if (n.lifespan <= 0) {
    iter.remove();
  }
}

}

}

The basis of the program is by Thomas Castleman.

`

This post has been resolved thanks to the help of Chrisir. The problem was the } missing at the end of the “Note class” tab!

1 Like