Instruments as Global Variables

Wonderng how to load instruments once and keep them as “global variables” and access them through “strings”
For example my notation file is as follows:
#Instrument=Veena
C C# D D#
#InstrumenFlute
C C# D D#
#Instrument=Veena
C C# D D#
#InstrumenFlute
C C# D D#

My parser reads and assigns a note array something like
[ [“C”,“Veena”,72,1.0],[]…[“C”,“Flute”,72,1.0],…[“C”,“Flute”,72,1.0]]
My player should parse this and play this.
If I call new-part inside the for loop
Music gets slowed over time and I get some clock error

Yes, it sounds like you’re creating a whole new part for every note that you’re playing. I would start the player with something like this:

# this looks through all of the notes in note_array, and since 
# the second item (index 1) is the instrument name
# constructs a non-repeating set of those
instruments_used = set(note[1] for note in note_array)
# this creates a dictionary mapping all of those instrument names to 
# a new part that we create within the session.
instruments = { 
    instrument_name: s.new_part(instrument_name) 
    for instrument_name in instruments_used
}

So now we have a dictionary instruments which we can use to look up the instrument object that we will use to play the note. Inside the for loop, you can say something like:

instruments["Flute"].play_note(...)

There are probably other ways of doing this, but the important part is to create all the instruments you are going to use before the for loop and access them during the for loop.

1 Like

Wow… So simple. I did think of dictionary and tried variety of things using “eval” of strings…
Thank you