You are on page 1of 3

Algorithmic Composition with Python and Music21

Oscar Riveros
November 05, 2013
Abstract Basic musicalization of the Lorenz attractor.

Part I

Tutorial 02
1 The Sound of Lorenz Attractor
from music21 import * In [738]: from qutip import * from pylab import * from mpl_toolkits.mplot3d import Axes3D %pylab --no-import-all inline
Populating the interactive namespace from numpy and matplotlib

def lorenz(x, y, z, s=10, r=28, b=2.667) : In [739]: x_dot = s*(y - x) y_dot = r*x - y - x*z z_dot = x*y - b*z return x_dot, y_dot, z_dot dt = 0.01 stepCnt = 1024 # Need one more for the initial values xs = np.empty((stepCnt + 1,)) ys = np.empty((stepCnt + 1,)) zs = np.empty((stepCnt + 1,)) # Setting initial values xs[0], ys[0], zs[0] = (0., 1., 1.05) # Stepping through "time". for i in range(stepCnt) : # Derivatives of the X, Y, Z state x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i]) xs[i + 1] = xs[i] + (x_dot * dt) ys[i + 1] = ys[i] + (y_dot * dt) zs[i + 1] = zs[i] + (z_dot * dt) fig = figure() ax = fig.gca(projection=3d) ax.plot(xs, ys, zs)

ax.set_xlabel("X Axis") ax.set_ylabel("Y Axis") ax.set_zlabel("Z Axis") ax.set_title("Lorenz Attractor") show()

stream_x = stream.Stream(); stream_y = stream.Stream(); In [740]: stream_z = stream.Stream(); def discrete_sin(theta, low_note, high_note): In [741]: return high_note + int(high_note - low_note) pitchesList_xaxis = map(lambda x: discrete_sin(x pitchesList_yaxis = map(lambda x: discrete_sin(x In [742]: pitchesList_zaxis = map(lambda x: discrete_sin(x * , , , sin(theta) 72, 84), xs) 60, 72), ys) 48, 60), zs)

[stream_x.append(note.Note(midi=pitch, quarterLength=0.25)) for pitch in flatten(pi In [743]: [stream_y.append(note.Note(midi=pitch, quarterLength=0.5)) for pitch in flatten(pit In [744]: [stream_z.append(note.Note(midi=pitch, quarterLength=1.)) for pitch in flatten(pitc In [745]: score = stream.Stream() score.insert(0, stream_x) In [746]: score.insert(0, stream_y) score.insert(0, stream_z) score.write(midi) /var/folders/4t/54tv_bvd6kz28x8zwhy3mcp80000gn/T/music21/tmpMG4HIY.mi In [747]: Out [747]: d

In []:

2 Final Result
http://youtu.be/yJe8RoWNyeY

3 About Me
1. 2. 3. 4. http://twitter.com/maxtuno http://soundcloud.com/maxtuno http://www.reverbnation.com/maxtuno http://mx-clojure.blogspot.com

You might also like