Music Programming using Python
https://jythonmusic.me/

Music Programming using Python

JythonMusic is an environment for music making and creative programming. It is meant for musicians and programmers alike, of all levels and backgrounds.

JythonMusic provides composers and software developers with libraries for music making, image manipulation, building graphical user interfaces, and connecting to external devices, such as digital pianos, smartphones, and tablets.

JythonMusic is based on Python programming. It is easy to learn for beginners, and powerful enough for experts.

I have been using Jython with Komodo Edit.

Following are the steps to setup the environment for getting started with running Jython scripts in Komodo Edit.

Running Jython with Komodo Edit

  • Start Komodo Edit and within Komodo, open the sample file “furElise.py”, which is located in the jythonMusic folder.
  • Run the file “furElise.py”, as follows: select from Tools > Run Command
  • In the Run Command window that appears enter “jython.bat -i %F” (without the quotes) in the text box for Run, and enter “%D” (without the quotes) in the text box for Start in.
No alt text provided for this image
  • If you hear the music, you’re all set.
  • To run the next command, you must manually finish the current command. To do so, click the white square highlighted below
No alt text provided for this image

How to code music?

For beginners (who do not have background in Music theory) in simple terms Music is all about the 7 fundamental notes and duration.

The 7 fundamental notes repeat over and over in a musical instrument and each repetition represents the pitch. The pitch essentially determines how high/low a note will sound. For example in a piano the C4 will have higher pitch than C3 but lower than C5.

In Jython the pitches are represented by the letters C, D, E, F, G, A and B followed by an integer representing the pitch. For example C4 represents the 4th pitch of note C. This is similar to a stave note representation on sheet music.

No alt text provided for this image

Duration represents how long a given note is held or played. In Jython duration is represented by floating point number. Typically the common duration used in music is whole, half, quarter, eighth and sixteenth note.

No alt text provided for this image

Composing Simple Melody in JythonMusic

Lets take a simple melody to begin with. Lets explore the ideas of composing extremely common nursery rhyme "twinkle twinkle little star"

There are plenty of online resources available to figure out the stave note representation of your favorite music. I picked one from the web that looked like below:

No alt text provided for this image

The above representation is for kids and beginners and that's probably why you would see the note duration to be pretty large than compared how you normally would recite the rhyme. I doubled the speed in my program to make it sound how I would normally recite.

# TwinkleTwinkle.py
# Generates the theme for KG Rhyme Twinkle Twinkle Little Star.
 
from music import *
 
# theme has some repetition, so break it up to maximize economy
# (also notice how we line up corresponding pitches and durations)
pitches1   = [C5, C5, G5, G5, A5, A5, G5]
durations1 = [EN, EN,  EN, EN,  EN, EN, QN]

pitches2   = [F5, F5, E5, E5, D5, D5, C5]
durations2 = [EN, EN,  EN, EN,  EN, EN, QN]

pitches3   = [G5, G5, F5, F5, E5, E5, D5]
durations3 = [EN, EN,  EN, EN,  EN, EN, QN]
durations4 = [EN, EN,  EN, EN,  EN, EN, QN]

# create an empty phrase, and construct theme from the above motifs
theme = Phrase()   
theme.addNoteList(pitches1, durations1)
theme.addNoteList(pitches2, durations2)
theme.addNoteList(pitches3, durations3)
theme.addNoteList(pitches3, durations4)
theme.addNoteList(pitches1, durations1)
theme.addNoteList(pitches2, durations2)

# play it
Play.midi(theme)

As can be seen I have chosen the notes with pitch above the middle C. EN represents the eighth note and QN quarter note.

To view or add a comment, sign in

More articles by Vallabha Hampiholi

Others also viewed

Explore content categories