

note_num_to_name ( current_note_num, true ) - convert note number to name redraw () end end function stop_play () - stops the coroutine playing the notes clock. note_num_to_freq ( current_note_num )) - convert note number to freq and play current_note_name = MusicUtil. snap_note_to_array ( rnd, notes_array ) - snap the random number to the scale array print ( "snapped: ". sync ( 1 / 2 ) rnd = math.random ( 40, 80 ) print ( "random: ".

generate_scale_of_length ( 60, "dorian", 16 ) - builds quantization scale end function play_notes () while true do clock. release ( 2 ) build_scale () - builds initial scale end function build_scale () notes_array = MusicUtil. name = "PolyPerc" playing = false - whether notes are playing function init () engine.

MusicUtil = require ( "musicutil" ) engine. This works especially well with the functions above. These functions enable you to snap (aka quantize) incoming MIDI note numbers to an array of desired notes. text_center ( "press k2 to play" ) end screen. text_center ( current_note_name ) - display the name of the note that is playing else screen. move ( 64, 32 ) if playing = true then screen. run ( play_notes ) - starts the clock coroutine which plays a random note from the scale playing = true elseif playing then stop_play () end end end function redraw () screen. cancel ( play ) playing = false redraw () end function key ( n, z ) if n = 2 and z = 1 then if not playing then play = clock. hz ( notes_freq ) - play note using the corresponding frequency current_note_name = MusicUtil. sync ( 1 / 2 ) local rnd = math.random ( 1, # notes_nums ) - a random integer current_note_num = notes_nums - select a random note from the scale engine. note_nums_to_freqs ( notes_nums ) - converts note numbers to an array of frequencies end function play_notes () while true do clock. generate_scale_of_length ( params : get ( "root_note" ), params : get ( "scale" ), params : get ( "pool_size" )) - builds scale notes_freq = MusicUtil. Returns an array of MIDI note numbers: build_scale () - builds initial scale end function build_scale () notes_nums = MusicUtil. Octaves: (optional) the number of octaves to return, defaults to 1: integer Scale_type: the scale type (eg, “major”, “aeolian” or “neapolitan major”). Root_num: the MIDI note number (0-127) where the scale will begin: integer Musicutil.generate_scale (root_num, scale_type)
