Computers and music are a wonderful combination! Using code, you can compose melodies, experiment with musical timing, and explore the mathematical patterns of harmony.
To write music with code, CodeByMath uses ABC notation: a simple, readable text format for representing sheet music.
How ABC Notation Works
In ABC notation, musical notes are written using ordinary letter names:
Middle Octave: Capital letters C, D, E, F, G, A, B represent the standard middle notes.
Higher Octave: Lowercase letters c, d, e, f, g, a, b represent notes one octave higher!
Lower Octave: Add a comma , to go down an octave (e.g. C, is low C).
Sharps & Flats: Put ^ in front for a sharp (^C = C♯), or _ for a flat (_B = B♭).
Note Lengths and Measures
Quarter Note (1 beat): A plain letter like C lasts 1 beat.
Half Note (2 beats): Add a 2 like C2 to hold the note twice as long.
Whole Note (4 beats): Add a 4 like C4 for a full 4-beat note.
Eighth Note (half beat): Add a slash like C/2 or C/ to play twice as fast!
Measures: Use vertical bar lines | to separate measures, and |] to end the piece.
ABC Song Headers
Every ABC song starts with a few standard header lines:
X:1 — Tune reference number
T:My Song — Title of the song
M:4/4 — Time signature / meter (4 beats per measure)
L:1/4 — Default note length (quarter note)
K:C — Key signature (C Major)
For a complete guide to all ABC notation symbols and features, check out the abcjs ABC notation guide.
With CodeByMath's music_play(abc_string) function, passing any ABC text will automatically draw the sheet music in the viewport and launch an interactive audio synthesizer so you can hear your song played out loud!
abc = "X:1\nT:My First Scale\nM:4/4\nL:1/4\nK:C\n" abc = abc .. "C D E F | G A B c | " abc = abc .. "????" music_play(abc)
Move the mouse over a dotted box for more information.
Listen to Your Music: Click Play on the synthesizer bar below the staff to hear your code perform!
Control Playback: Adjust the tempo slider or click the loop button to hear the melody continuously.
Download ABC: Click Download ABC to save the song file to your computer.
Now you try. Change the melody to play the opening of "Twinkle, Twinkle, Little Star": replace the note lines with "C C G G | A A G2 | F F E E | D D C2 |]"!
Type your code here:
See your results here:
The code has ???? for the descending half of the scale. Replace ???? with "c B A G | F E D C |]" and click Run to generate the sheet music and audio synthesizer!
1. In the viewport, you will see a clean 4-measure musical score with a treble clef and time signature.
2. Click the Play button on the player controls below the staff to hear the synthesizer play the ascending and descending scale.
3. Notice how capital letters C D E F G A B are in the middle octave, while lowercase c is an octave higher.
4. To learn more ABC notation tricks (like chords, triplets, and key signatures), visit the abcjs ABC notation guide.
Share your code
Show a friend, family member, or teacher what you've done!