Lesson goal: Musical Symmetries: Bach's Geometry & Transformations

Previous: Introduction to Coding Music and ABC Notation | Home | Next: Introduction to the 3D Rubik's Cube

Music and mathematics have shared a deep bond since the ancient Pythagoreans discovered the integer ratios behind musical harmony.

Johann Sebastian Bach and later classical composers took this connection to new heights by constructing music around geometric symmetries in 2D coordinate space.

If we plot a melody on a coordinate grid where the horizontal axis represents Time ($x$) and the vertical axis represents Pitch ($y$), musical forms become geometric transformations:
  • Transposition = Translation $(x, y) \to (x, y + k)$: Shifting all pitches up or down by a constant interval $k$.
  • Retrograde = Horizontal Reflection $(x, y) \to (L - x, y)$: Playing the melody backward in time, mirroring across a vertical line.
  • Inversion = Vertical Reflection $(x, y) \to (x, 2y_{\text{pivot}} - y)$: Mirroring pitches across a horizontal pitch axis $y_{\text{pivot}}$. An ascending step of $+2$ semitones becomes a descending step of $-2$ semitones!
  • Retrograde Inversion = 180° Rotation: Combining both reflections so the melody is both backward and upside down!


With CodeByMath's interactive abcjs music engine, you can generate sheet music in standard ABC notation and hear it played immediately with a built-in audio synthesizer:
  • music_play(abc_string) renders clean SVG sheet music and launches the audio player in the viewport!
  • music_render(abc_string) displays sheet music notation only.
music_play(abc)
scale = {"C", "D", "E", "F", "G", "A", "B", "c"}

retro[i] = theme[n - i + 1]

inver[i] = 2 * pivot - theme[i]
Move the mouse over a dotted box for more information.

  • Interactive Playback: Click Play in the music player to hear the theme and all three geometric transformations synthesized directly in your browser!
  • Loop and Warp: Use the player controls to loop the playback or change the tempo speed.
  • Download ABC: Click Download ABC to save your musical composition as a standard .abc notation file.

Now you try. Change the original theme to your own 7-note melody, or change the inversion pivot from 3 (E) to 4 (F) or 5 (G) and listen to how the pitch symmetry changes!

Type your code here:


See your results here: