Repository Public
1// ORIX Sonata2// File: StandardLibrary/std/music.sonata3// Description: Standard music helpers: note values, intervals, dynamics, melodic and rhythmic patterns.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7sonata 0.189// Note values. They are durations, so they can also set tempo-synced times: delay(time: eighth).10const whole = note_value(1/1)11const half = note_value(1/2)12const quarter = note_value(1/4)13const eighth = note_value(1/8)14const sixteenth = note_value(1/16)15const thirty_second = note_value(1/32)1617// Intervals.18const unison = 0st19const minor_second = 1st20const major_second = 2st21const minor_third = 3st22const major_third = 4st23const perfect_fourth = 5st24const tritone = 6st25const perfect_fifth = 7st26const minor_sixth = 8st27const major_sixth = 9st28const minor_seventh = 10st29const major_seventh = 11st30const octave_up = 12st3132// Dynamics, as linear velocities.33const ppp = 0.1534const pp = 0.2535const p = 0.436const mp = 0.5537const mf = 0.738const f = 0.8539const ff = 0.9540const fff = 1.04142// Linear amplitude ratio of a number of cents.43fn cents_ratio(c: Number) -> Number {44 return pow(2, c / 1200)45}4647// A scale spanning several octaves, from the root upwards.48fn scale_run(root: Pitch, mode: String, octaves: Number) -> List<Pitch> {49 let acc = []50 for o in 0..octaves {51 acc = acc + (scale(root, mode) + 12st * o)52 }53 return acc + [root + 12st * octaves]54}5556// The notes of a list, up then back down without repeating the ends.57fn up_down(notes: List<Pitch>) -> List<Pitch> {58 if len(notes) < 3 {59 return notes60 }61 return notes + reverse(take(drop(notes, 1), len(notes) - 2))62}6364// Plays the notes of a list one after the other.65pattern arpeggio(notes: List<Pitch>, step: Duration = 1/8, vel: Velocity = 0.8) {66 for n in notes {67 play n duration step velocity vel68 }69}7071// Plays the same chord `count` times.72pattern strum(notes: List<Pitch>, count: Number = 4, step: Duration = 1/4, vel: Velocity = 0.8) {73 repeat count {74 chord notes duration step velocity vel75 }76}7778// Plays a pitch on every onset of a rhythm (a list of Bool, for example euclid(3, 8)).79pattern rhythm(pitch: Pitch, hits: List<Bool>, step: Duration = 1/16, vel: Velocity = 0.8) {80 for hit in hits {81 if hit {82 play pitch duration step velocity vel83 } else {84 rest step85 }86 }87}88