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 chroma chromatic tic scale from from C3 (60) to C4 (72) from music21 import * from In [1] [1]: :
stream = stream. stream.Stream(); In [2]: noteList = [note. [note.Note(midi= Note(midi=n) for n in range( range(60 60, , 72 72)]; )]; In [3]: [stream. [stream.append(note) for note in noteList]; In [4]: stream. stream.show(’musicxml’ show(’musicxml’) ) In [5]:
2 Create a chromatic scale from C3 to C4 with the form of sin(θ) with 24 notes of length In [6] [6]: :
from music21 import * from from pylab import * from % pylab ---no no-importimport-all inline Popu opu at ng t e
nter ntera act ve ve nam namespa espac ce
rom rom nump numpy y an
mat matp ot
def discrete_sin(theta discrete_sin(theta, , low_note, low_note, high_note) high_note): : return high_note + int(high_note int (high_note low_note) In [7]: * sin(theta) stream = stream. stream.Stream(); In [8]: pitchesList = [discrete_sin(n, 60 60, , 72) 72) for n in range( range(24 24)]; )]; In [9]: plot(pitchesList) In [10]: Out [10]:
[stream. [stream.append(note. append(note.Note(midi= Note(midi=pitch)) for pitch in pitchesList]; In [11]: stream. stream.show(’musicxml’ show(’musicxml’) ) In [12]:
3 Create a simple mathematical mathematical counterpoint counterpoint betwen sin(θ + ψsin(θ)) and cos(θ + φcos (θ)) with 256 notes of length in the first voice, where φ = eθ and ψ = ln(1 + θ ) and first voice is 2 times most faster than the second voice and one cotave uper from music21 import * from from pylab import * In [13]: from % pylab ---no no-importimport-all inline Popu opu at ng t e
nter ntera act ve ve nam namespa espac ce
rom rom nump numpy y an
mat matp ot
def psi psi(theta): (theta): return exp(theta) In [14]: def discrete_sin(theta discrete_sin(theta, , low_note, low_note, high_note) high_note): : return high_note + int int(high_note (high_note - low_note) * sin(theta + ps def phi phi(theta): (theta): return log(1 log(1 + theta) In [15]: def discrete_cos(theta discrete_cos(theta, , low_note, low_note, high_note) high_note): : return high_note + int int(high_note (high_note - low_note) * cos(theta + ph
stream_sin = stream. stream.Stream(); stream_cos = stream. stream.Stream(); In [16]: pitchesList_sin = [discrete_sin(n, 60 60, , 72 72) ) for n in range( range(256 256)]; )]; pitchesList_cos = [discrete_cos(n, 48, 48 , 60) 60 ) for n in range range( ( 128)]; 128 )]; In [17]: plot(pitchesList_sin) plot(pitchesList_cos) In [18]:
.
nes.L ne D at
x1
a
e
>
Out [18]:
[stream_sin. [stream_sin.append(note. append(note.Note(midi= Note(midi=pitch, pitch, quarterLen quarterLength gth=0.5 =0.5)) )) for [stream_cos. [stream_cos.append(note. append(note.Note(midi= Note(midi=pitch, pitch, quarterLen quarterLength gth=1 =1)) )) for pi In [19]: score = stream. stream.Stream() In [20]: score. score.insert(0 insert(0, stream_sin) stream_sin) score. score.insert(0 insert(0, stream_cos) stream_cos) In [21]: score. score.show(’musicxml’ show(’musicxml’) ) In [22]:
4 Ab Abou outt Me Me 1. 2. 3. 4.
http://twitter.com/maxtuno http://soundclo http://soundcloud.com ud.com/maxtun /maxtuno o http:/ /www.reverbnation.com/maxtuno /www.reverbnation.com/maxtuno http://mx-clojure.blogspot.com/