Edit: I wrote “You can change the notes array any time to add,
remove or change notes.” Here is how to start or stop a note, i.e. to
add or remove it from the list of active notes:
// Start a note. Returns an index to be used with stop_note(), or -1 if
// we couldn't start the note because we are already outputting the
// maximum number of simultaneous notes.
int start_note(float frequency)
{
int i; // index of the note in the notes[] array
// Find a free slot in the array.
for (i = 0; i < MAX_NOTES; i++)
if (!notes[i].is_active) break; // found
// Couldn't find a free slot?
if (i == MAX_NOTES) return -1;
// Setup the note parameters.
notes[i].frequency = frequency;
notes[i].phase = 0;
notes[i].is_active = true;
return i;
}
// Stop the note with the given index.
void stop_note(int index)
{
if (index >=0 && index < MAX_NOTES)
notes[index].is_active = false;
}