Algorithmic Composition with Python and Music21 - Tutorial 01

5
Algorithmic Composition with Python and Music21 Oscar Riveros November 05, 2013 Abstract The most basic, mathematica music, not art at the moment, just take control. Part I Tutorial 01 1 Create a chromatic scale from C3 (60) to C4 (72) In [1]: from music21 import * In [2]: stream = stream.Stream(); In [3]: noteList = [note.Note(midi=n) for n in range(60, 72)]; In [4]: [stream.append(note) for note in noteList]; In [5]: stream.show(’musicxml’) 2 Create a chromatic scale from C3 to C4 with the form of sin(θ) with 24 notes of length In [6]: from music21 import * from pylab import * %pylab --no-import-all inline Populating the interactive namespace from numpy and matplotlib In [7]: def discrete_sin(theta, low_note, high_note): return high_note + int(high_note - low_note) * sin(theta) In [8]: stream = stream.Stream(); In [9]: pitchesList = [discrete_sin(n, 60, 72) for n in range(24)]; In [10]: plot(pitchesList) Out [10]: [<matplotlib.lines.Line2D at 0x105a4d6d0>]

description

The most basic, mathematical music, not art at the moment, just take control.

Transcript of Algorithmic Composition with Python and Music21 - Tutorial 01

Page 1: Algorithmic Composition with Python and Music21 - Tutorial 01

Algorithmic Composition with Pythonand Music21

Oscar Riveros

November 05, 2013

Abstract

The most basic, mathematica music, not art at the moment, just take control.

Part I

Tutorial 011 Create a chromatic scale from C3 (60) to C4 (72)

In [1]:from music21 import *

In [2]:stream = stream.Stream();

In [3]:noteList = [note.Note(midi=n) for n in range(60, 72)];

In [4]:[stream.append(note) for note in noteList];

In [5]:stream.show(’musicxml’)

2 Create a chromatic scale from C3 to C4 with the form ofsin(θ) with 24 notes of length

In [6]:from music21 import *from pylab import *%pylab --no-import-all inline

Populating the interactive namespace from numpy and matplotlib

In [7]:def discrete_sin(theta, low_note, high_note):

return high_note + int(high_note - low_note) * sin(theta)

In [8]:stream = stream.Stream();

In [9]:pitchesList = [discrete_sin(n, 60, 72) for n in range(24)];

In [10]:plot(pitchesList)

Out [10]:[<matplotlib.lines.Line2D at 0x105a4d6d0>]

Page 2: Algorithmic Composition with Python and Music21 - Tutorial 01

In [11]:[stream.append(note.Note(midi=pitch)) for pitch in pitchesList];

In [12]:stream.show(’musicxml’)

3 Create a simple mathematical counterpoint betwen sin(θ +ψsin(θ)) and cos(θ + φcos(θ)) with 256 notes of length in thefirst voice, where φ = eθ and ψ = ln(1 + θ) and first voice is2 times most faster than the second voice and one cotaveuper

In [13]:from music21 import *from pylab import *%pylab --no-import-all inline

Populating the interactive namespace from numpy and matplotlib

In [14]:def psi(theta):

return exp(theta)

def discrete_sin(theta, low_note, high_note):return high_note + int(high_note - low_note) * sin(theta + psi(theta))

In [15]:def phi(theta):

return log(1 + theta)

def discrete_cos(theta, low_note, high_note):return high_note + int(high_note - low_note) * cos(theta + phi(theta))

Page 3: Algorithmic Composition with Python and Music21 - Tutorial 01

In [16]:stream_sin = stream.Stream();stream_cos = stream.Stream();

In [17]:pitchesList_sin = [discrete_sin(n, 60, 72) for n in range(256)];pitchesList_cos = [discrete_cos(n, 48, 60) for n in range(128)];

In [18]:plot(pitchesList_sin)plot(pitchesList_cos)

Out [18]:[<matplotlib.lines.Line2D at 0x105a53ed0>]

In [19]:[stream_sin.append(note.Note(midi=pitch, quarterLength=0.5)) for pitch in pitchesList_sin];[stream_cos.append(note.Note(midi=pitch, quarterLength=1)) for pitch in pitchesList_cos];

In [20]:score = stream.Stream()

In [21]:score.insert(0, stream_sin)score.insert(0, stream_cos)

In [22]:score.show(’musicxml’)

Page 4: Algorithmic Composition with Python and Music21 - Tutorial 01
Page 5: Algorithmic Composition with Python and Music21 - Tutorial 01

4 About Me1. http://twitter.com/maxtuno2. http://soundcloud.com/maxtuno3. http://www.reverbnation.com/maxtuno4. http://mx-clojure.blogspot.com/