Tuesday, January 24, 2017

JFugue Jython Example 7. Twelve-Bar Blues in Two Lines of Code

This is Example 7 from JFugue website (JFugue Examples) in Jython.



# JFugue 7. Twelve-Bar Blues in Two Lines of Code


'''
Twelve-bar blues uses a I-IV-V chord progression.
But really, it's the Major 7ths that you'd like
to hear... and if you want to play each chord in
arpeggio, you need a 6th in there as well. But
creating a I7%6-IV7%6-V7%6 chord progression is
messy. So, this code creates a I-IV-V progression,
then distributes a 7%6 across each chord, then
creates the twelve bars, and then each chord is
played as an arpeggio with note dynamics (note on
velocity - how hard you hit the note). Finally,
the pattern is played with an Acoustic_Bass
instrument at 110 BPM. With all of the method
chaining, that is kinda done in one line of code.
'''

import sys
sys.path.append("C:/jfugue-5.0.7.jar")

from org.jfugue.player import Player
from org.jfugue.theory import ChordProgression

p1 = "$0 $0 $0 $0 $1 $1 $0 $0 $2 $1 $0 $0"
p2 = """
$0ia100 $1ia80 $2ia80 $3ia80
$4ia100 $3ia80 $2ia80 $1ia80"""
pattern = ChordProgression("I IV V")
pattern = pattern.distribute("7%6")
pattern = pattern.allChordsAs(p1)
pattern = pattern.eachChordAs(p2)
pattern = pattern.getPattern()
pattern = pattern.setInstrument("Acoustic_Bass")
pattern = pattern.setTempo(100)
Player().play(pattern)

In Jython and Python, it is important to write code that can fit in one line. It is possible to write a very long line in a editor, but that is hardly clean code, which is the advantage of Python. Usually, you will use functions or classes, which will define a global variable like pattern, and inside the function use p or something simple. In Python it is very important to use simple names whenever possible so you do not accidentally create new variables.


No comments:

Post a Comment