Henry Leacock

August 2, 2013

Simple Markov Chains Using the Python Opcodes in Csound

Filed under: Uncategorized — Henry @ 2:11 pm

Source: http://en.flossmanuals.net/csound/using-python-inside-csound/

Python opcodes can simplify the creation of complex data structures for algorithmic composition. Below you’ll find a simple example of using the Python opcodes to generate Markov chains for a pentatonic scale. Markov chains require in practice building matrices, which start becoming unwieldy in Csound, especially for more than two dimensions. In Python multi-dimensional matrices can be handled as nested lists very easily. Another advange is that the size of matrices (or lists) need not be known in advance, since it is not necessary in python to declare the sizes of lists.

EXAMPLE 12B08_markov.csd

-odac -dm0

sr = 44100
ksmps = 32
nchnls = 2
0dbfs = 1


; Python script to define probabilities for each note as lists within a list
; Definition of the get_new_note function which randomly generates a new
; note based on the probabilities of each note occuring.
; Each note list must total 1, or there will be problems!

pyruni {{
c = [0.1, 0.2, 0.05, 0.4, 0.25]
d = [0.4, 0.1, 0.1, 0.2, 0.2]
e = [0.2, 0.35, 0.05, 0.4, 0]
g = [0.7, 0.1, 0.2, 0, 0]
a = [0.1, 0.2, 0.05, 0.4, 0.25]

markov = [c, d, e, g, a]

from random import random, seed


def get_new_note(previous_note):
    number = random()
    accum = 0
    i = 0
    while accum < number:
        accum = accum + markov[int(previous_note)] [int(i)]
        i = i + 1
    return i - 1.0

giSine ftgen 0, 0, 2048, 10, 1 ;sine wave
giPenta ftgen 0, 0, -6, -2, 0, 2, 4, 7, 9  ;Pitch classes for pentatonic scale

instr 1  ;Markov chain reader and note spawner
;p4 = frequency of note generation
;p5 = octave
ioct init p5
klastnote init 0 ;Used to remember last note played (start at first note of scale)
ktrig metro p4 ;generate a trigger with frequency p4
knewnote pycall1t ktrig, "get_new_note", klastnote ;get new note from chain
schedkwhen ktrig, 0, 10, 2, 0, 0.2, knewnote, ioct ;launch note on instrument 2
klastnote = knewnote ;New note is now the old note

instr 2 ;A simple sine wave instrument
;p4 = note to be played
;p5 = octave
ioct init p5
ipclass table p4, giPenta
ipclass = ioct + (ipclass / 100) ; Pitch class of the note
ifreq = cpspch(ipclass) ;Note frequency in Hertz
aenv linen .2, 0.05, p3, 0.1 ;Amplitude envelope
aout poscil  aenv, ifreq , giSine ;Simple oscillator
outs aout, aout

;        frequency of       Octave of
;        note generation    melody
i 1 0 30      3               7
i 1 5 25      6               9
i 1 10 20     7.5             10
i 1 15 15     1               8
;Example by Andrés Cabrera
  1. Open a Terminal and type “python”. If your python version is not 2.7, download and install the proper version from http://www.python.org.^
  2. This printing does not work in CsoundQt. You should run all the examples here in the Terminal.^


You should have csound installed to run this. http://csounds.com/

Copy the above code example into 12B08_markov.csd then from a terminal run csound 12B08_markov.csd.

August 19, 2012

First Chord Progression

Filed under: Uncategorized — Henry @ 7:57 pm

April 22, 2012

More Refinery

Filed under: Uncategorized — Tags: — Henry @ 9:40 pm

So, yes. We are going to talk about improvisation here in this blog. And also some other things. But mainly, if you are looking to improve your improvisational skills, whether you’re an instrumentalist, composer or academic then you will find insight here.

I’ll start with a bit of background. I’m a computer programmer by day and practicing composer by night. I aspire to create music for television, film and video-games using software to compose scores and synthesize sounds.

I have been studying music for almost 15 years, including my higher education at Webster University, culminating in a bachelor of arts in music. Additionally, I have racked up a number of credit hours in computer science.

Computers have provided a direction for my work. Not only my livelihood during the day, but it is the way of the future when it comes to music. However, this blog will only partially focus on synthesizing sounds with a computer while a great deal of focus will be on the more universal topic of functional harmony, melody and rhythm.

Apart from other topics we might touch on why there are so many programming musicians,  the important role that music plays in this digital age, and why hipsters have evolved to what they are (I am a self proclaiming and proud hipster).

So, some tools. There are lots of tools out there. Books are good, internet resources, blogs, social networks and the list goes on. My personal favorite are the Real Books with it’s various editions. Pretty much everything you need to know about improvisation is encoded in those books. Then I like using lilypond to input scores into the computer and I currently play them back with Mac’s Logic Pro 9, though I hope to move in the direction of using csound as my default synthesizer (just as soon as my understanding and facility of it mature to the point where it allows me to give up Logic).

My writing style will err on the side of technical seeing as this is a technical blog. Though, there is the possibility (and I encourage it) that discourse will emerge from resulting topics and trends. This blog should be a platform for social education and hopefully we can cultivate an environment that encourages creativity. I look forward to it.

March 17, 2012

Game Plan

Filed under: Uncategorized — Tags: , , , — Henry @ 10:44 am

Here we go! The majority of this site will be devoted to discussions about the ongoing process of developing musical improvisational skills. What that means: I’ll be submitting examples of sheet music, commentary, and approaches and methods that I feel aid the cultivation of a musical language.

October 22, 2011

New Topics

Filed under: Blogging — Henry @ 11:42 pm

Hey Folks, I’ve got some new topics in the mix.  I’ll give you a list to run down.


Audio Engineering

Jazz Improvisation


Computer Programming


Html css javascript ruby rails python php 


Music and having a day job

Going to college

Living at home



Being happy


Being an artist

Just let me know what you want to hear more of so I can figure out who is reading this blog.  This is a pretty exhaustive list of things that I’ve got insight on, so leave a comment describing why you want to hear more about any of these topics.  I will be working through the list in a timely manner so stop back or subscribe via RSS or send me an email.

Older Posts »

The WordPress Classic Theme. Blog at WordPress.com.


Get every new post delivered to your Inbox.

Join 103 other followers

%d bloggers like this: