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

**URL:** <https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266>\
**Category:** Libraries\
**Created:** [December 22, 2022, 7:30pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266 "2022-12-22T19:30:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiu](https://avatars.discourse-cdn.com/v4/letter/x/3d9bf3/32.png) [@xiu](https://discourse.processing.org/u/xiu)\
**Post date:** [December 22, 2022, 7:30pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266/1 "2022-12-22T19:30:06Z")

</div>

Good evening,

I am a total newbie.  
I followed this tutorial: [https://www.youtube.com/watch?v=L7AU2KAM0UE&t=274s](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.

```auto
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

```auto
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() {

  }

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 22, 2022, 7:38pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266/2 "2022-12-22T19:38:36Z")

</div>

> [@xiu](#):
>
> ` for (this.pitch = 30) {`

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 { … }

---

<div class="post-metadata">

**Author:** ![xiu](https://avatars.discourse-cdn.com/v4/letter/x/3d9bf3/32.png) [@xiu](https://discourse.processing.org/u/xiu)\
**Post date:** [December 22, 2022, 7:44pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266/3 "2022-12-22T19:44:49Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 22, 2022, 7:45pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266/4 "2022-12-22T19:45:26Z")

</div>

> [@xiu](#):
>
> ```auto
> for (this.pitch = 30) {
> fill(255, 0, 0);
> ellipse(this.x, this.y, this.size, this.size); 
> 
> ```

this is not needed here.

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 22, 2022, 7:47pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266/5 "2022-12-22T19:47:49Z")

</div>

> [@xiu](#):
>
> the debugger tells me: ‘extraneous input’ expecting {‘color’, 'abstract, . etc…

can you show me your current code?

---

<div class="post-metadata">

**Author:** ![xiu](https://avatars.discourse-cdn.com/v4/letter/x/3d9bf3/32.png) [@xiu](https://discourse.processing.org/u/xiu)\
**Post date:** [December 22, 2022, 7:53pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266/6 "2022-12-22T19:53:46Z")

</div>

Main Sketch

```auto
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

```auto

// 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();
  }
}

```

}

}

```auto
The basis of the program is by Thomas Castleman.

`
```

---

<div class="post-metadata">

**Author:** ![xiu](https://avatars.discourse-cdn.com/v4/letter/x/3d9bf3/32.png) [@xiu](https://discourse.processing.org/u/xiu)\
**Post date:** [December 23, 2022, 2:18pm UTC](https://discourse.processing.org/t/help-stupid-and-mysterious-format-error-for-structure-within-a-midi-responsive-sketch/40266/7 "2022-12-23T14:18:12Z")

</div>

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