#01 Play a Melody

Play a note by midi number or predefined variables or use hz_to_midi( freq ) to convert the frequency to note number.

dittytoy.net/syntax
dittytoy.net/syntax#ticksvsseconds
sonic-pi.mehackit.or…2-play-a-melody.html

#dittytoy #tutorial

Log in to post a comment.

// #01 Play a Melody. DittyToy 2022.
// The MIT License.
//
// https://dittytoy.net/ditty/555180eb60
//
// Play a note by midi number or predefined variables or use hz_to_midi( freq ) to convert 
// the frequency to note number.
// 
// https://dittytoy.net/syntax
// https://sonic-pi.mehackit.org/exercises/en/01-introduction/02-play-a-melody.html
//

ditty.bpm = 120;

for (let i=0; i<4; i++) {
    sine.play(c4);
    sleep( 0.25 ); // sleep in ticks
}

sine.play(d4);
sleep( 0.5 ); // sleep in ticks
sine.play(f4);
sleep( 0.5 ); // sleep in ticks

//
// Ticks vs seconds
//
// Dittytoy uses both ticks and seconds. So pay close attention when you call or implement a particular 
// function, and check the time unit of the variables used.
//
// A tick (or beat or a quarter note) is inversely proportional to the number of beats per minute, which 
// you can set with ditty.bpm. The default bpm of a ditty is 120, in which case a tick will have a duration 
// of 0.5 seconds.
//