Skip to content

Note selectors

David Anderson edited this page Apr 5, 2025 · 10 revisions

Many of the nuance functions let you select a set of notes using a "selector" function: a boolean-valued function taking a Note argument. These functions can refer to any of the Note attributes, including:

  • n.time, n.dur: the note's start time and duration in score units.
  • n.pitch: the note's pitch
  • n.tags: the list of tags associated with the note. Two tags are automatically assigned: "top" means the note has the highest pitch of the notes sounding at its start time, and "bottom" means it has the lowest pitch.
  • n.measure_offset: the time (in score units) from the last measure boundary to the note's start time.
  • n.nchord: the number of notes with the same start time as this one.
  • n.chord_pos: of these notes, this one's pitch order (0 = lowest).

For example, the function

def top_eighths(note):
    return note.dur == 1/8 and 'top' in note.tags

selects eighth notes that are topmost (i.e. the highest sounding note at their start time). Given a Score ns, you could change the performed duration of these notes to .3 seconds by calling

ns.perf_dur_abs(.3, top_eighths)

For brevity, you could define the selector as a lambda function:

ns.perf_dur_abs(.3, lambda n: n.dur == 1/8 and 'top' in n.tags)

Clone this wiki locally