Algorithmic Composition with Python and Music21 - Tutorial 02 - The Sound of Lorenz Attractor

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 In [738]: from music21 import * 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 In [739]: def lorenz(x, y, z, s=10,r=28,b=2.667): 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)

description

Basic musicalization of the Lorenz attractor.

Transcript of Algorithmic Composition with Python and Music21 - Tutorial 02 - The Sound of Lorenz Attractor

Page 1: Algorithmic Composition with Python  and Music21 - Tutorial 02 - The Sound of Lorenz Attractor

Algorithmic Composition with Pythonand Music21

Oscar Riveros

November 05, 2013

Abstract

Basic musicalization of the Lorenz attractor.

Part I

Tutorial 021 The Sound of Lorenz Attractor

In [738]:from music21 import *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

In [739]:def lorenz(x, y, z, s=10, r=28, b=2.667) :

x_dot = s*(y - x)y_dot = r*x - y - x*zz_dot = x*y - b*zreturn x_dot, y_dot, z_dot

dt = 0.01stepCnt = 1024

# Need one more for the initial valuesxs = np.empty((stepCnt + 1,))ys = np.empty((stepCnt + 1,))zs = np.empty((stepCnt + 1,))

# Setting initial valuesxs[0], ys[0], zs[0] = (0., 1., 1.05)

# Stepping through "time".for i in range(stepCnt) :

# Derivatives of the X, Y, Z statex_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)

Page 2: Algorithmic Composition with Python  and Music21 - Tutorial 02 - The Sound of Lorenz Attractor

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

show()

In [740]:stream_x = stream.Stream();stream_y = stream.Stream();stream_z = stream.Stream();

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

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

In [742]:pitchesList_xaxis = map(lambda x: discrete_sin(x , 72, 84), xs)pitchesList_yaxis = map(lambda x: discrete_sin(x , 60, 72), ys)pitchesList_zaxis = map(lambda x: discrete_sin(x , 48, 60), zs)

In [743]:[stream_x.append(note.Note(midi=pitch, quarterLength=0.25)) for pitch in flatten(pitchesList_xaxis + pitchesList_yaxis + pitchesList_zaxis)];

In [744]:[stream_y.append(note.Note(midi=pitch, quarterLength=0.5)) for pitch in flatten(pitchesList_yaxis + pitchesList_zaxis)];

In [745]:[stream_z.append(note.Note(midi=pitch, quarterLength=1.)) for pitch in flatten(pitchesList_zaxis)];

In [746]:score = stream.Stream()score.insert(0, stream_x)score.insert(0, stream_y)score.insert(0, stream_z)

In [747]:score.write(’midi’)

Out [747]:’/var/folders/4t/54tv_bvd6kz28x8zwhy3mcp80000gn/T/music21/tmpMG4HIY.mid’

Page 3: Algorithmic Composition with Python  and Music21 - Tutorial 02 - The Sound of Lorenz Attractor

In []:

2 Final Resulthttp://youtu.be/yJe8RoWNyeY

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