A Markov chain is a mathematical model used to represent the tendency of one event to follow another event or sequence of events. Markov chains were first employed in music by Hiller and Isaacson in 1957 and they have become a popular technique among computer music composers ever since. Scripthica includes a function that performs Markov analysis and then generates a new sequence of notes based on the probabilities of the input sequence.
In Scripthica, we can use the markov function to generate new musical patterns from a given sequence.
; define a pattern.
(define p '(c5 d4 e5 cb5 eb4 cb5 eb4 a3 d4 c5 d4 e3 cb5))
; use the markov function to define the pitches from the pattern
(define pitches (pitches->numbers (markov p 2)))
; define the durations
(define lengths (constant-value (length pitches) 250))
; define the velocities
(define vels (constant-value (length pitches) 96))
; define events and save midi
(define evs (create-events 0 pitches lengths vels 1))
(save-midi evs)
In this example we will recombine two different pieces of music to generate new musical material. We will use the first sections of the courante and double from Bach's violin partita bwv 1002 to generate new musical material using Markov chains.
Lets define the sections as lists.
;; markov bach violin partita bwv 1002
(define courante-a '(
(0 b4)
(1 b4 b3 d4 f#4 b4 d5)
(2 c#5 f#4 a#3 e4 d4 c#4)
(3 b3 d4 f#4 b4 d5 f#5)
(4 e5 a4 c#4 g4 f#4 e4)
(5 d4 f#4 a4 d5 f#5 a5)
(6 g5 b4 e4 d5 c#5 b4)
(7 a#4 g5 e5 e4 d#4 a4)
(8 g#4 f#5 e5 d4 c#4 e4)
(9 a#4 e5 e5 d5 c#5 d5)
(10 c#5 f#4 a#3 g4 f#4 e4)
(11 d4 f#4 b4 d#5 f#5 b5)
(12 g5 b4 e4 g4 b4 d5)
(13 c#5 e4 a3 c#4 e4 g4)
(14 b4 g4 b4 d5 g5 b5)
(15 c#6 e5 g4 e5 a5 c#6)
(16 d6 a5 e5 d5 a4 c#5)
(17 d4 d6 c#6 b5 a#5 b5)
(18 c#6 e5 a#4 g5 f#5 e5)
(19 d5 f#5 b5 g#5 a5 f#5)
(20 e#5 g#5 b5 d6 c#6 b4)
(21 a4 c#5 f#5 a5 c#6 e5)
(22 d#5 f#5 a5 c6 b5 a4)
(23 g#4 b4 e#5 g#5 b5 f#4)
(24 e#4 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f#5 e5 d5 c#5)
(20 d5 b4 d5 f#5 b5 c#6 d6 c#6 b5 a5 g#5 f#5)
(21 e#5 c#6 g#5 e#5 c#5 g#5 e#5 c#6 b4 d5 c#5 b4)
(22 a4 c#5 f#5 g#5 a5 b5 c#6 b5 a5 g#5 f#5 e5)
(23 d#5 b5 f#5 d#5 b4 f#5 d#5 b4 a4 c#5 b4 a4)
(24 g#4 f#5 d5 b5 g#4 d5 b4 g#4 f#4 a4 g#4 f#4)
(25 e#4 g#4 a4 b4 c5 d#5 e#5 f#5 g#5 a5 b5 g#5)
(26 a5 f#5 e5 d5 c#5 b4 a4 g#4 f#4 e4 d4 c#4)
(27 b3 f#4 g4 a4 b4 c#5 d5 e5 f#5 g5 a5 f#5)
(28 g5 b5 g5 d5 b4 g5 d5 b4 g4 d5 b4 g4)
(29 e#4 b4 g#4 e#4 c#4 d#4 e#4 f#4 g#4 a4 b4 g#4)
(30 a4 f#4 a4 c#4 f#5 a4 g#4 f#4 c4 g#4 f#4 e#4)
(31 f#5 f#4 a#4 c#5 e5 f#4 g#4 c#5 d5 f#4 b4 d5)
(32 c#5 b4 a#4 g#4 f#4 a#4 c#5 e#5 f#5)
))
Now lets define a method that will give us the notes from the sections we defined.
(define (make-form l)
(if (null? l) '()
(append (cdar l) (make-form (cdr l)))))
Now with the delta function we can extract the intervals. After extracting the intervals we can append them so it performs the analysis and recombines from both sections.
(define intervals-c (delta (pitches->numbers (make-form courante-a))))
(define intervals-d (delta (pitches->numbers (make-form double-a))))
(define intervals (append intervals-c intervals-d))
In terminal, lets type intervals and see the result.
intervals
=> (0 -12 3 4 5 3 -1 -7 -8 6 -2 -1 -2 3 4 5 3 4 -2 -7 -8 6 -1 -2 -2 4 3 5 4 3
-2 -8 -7 10 -1 -2 -1 9 -3 -12 -1 6 -1 10 -2 -14 -1 3 6 6 0 -2 -1 1 -1 -7 -8 9
-1 -2 -2 4 5 4 3 5 -4 -8 -7 3 4 3 -1 -9 -7 4 3 3 4 -4 4 3 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 -2 -2 -1 -2 -2 -2 -1 -2 7 1 2 2 2 1 2 2 1 2 -3 1 4 -4 -5 -3 8 -5 -3 -4 7 -
3 -4 -2 6 -3 -3 -4 2 2 1 2 1 2 -3 1 -3 3 -8 17 -9 -1 -2 -6 8 -2 -1 13 -12 4 3
3 -10 2 5 1 -8 5 3 -1 -2 -1 -2 -2 4 3 4 1)
We can see that intervals is a list of the intervals of the two sections we defined earlier. Now we can define a function that will call the markov function with the intervals. This performs a markov analysis on the intervals of the sections.
(define (nl) (markov intervals 16))
Each time we call (nl), we will get a different result depending on the size of the markov chain.
(nl)
=> (0 -12 3 4 5 3 -1 -7 -8 6 -2 -1 -2 3 4 5 3 4 -2 -7 -8 6 -1 -2 -2 4 3 5 -2 1
-7 -6 -9 9 6 3 -1 -4 -1 -2 -5 4 1 4 3 3 -2 -3 2 -3 -4 0 -2 -2 -1 -2 -2 -1 8 -
5 -3 -4 7 -3 -4 -1 -2 -1 9 -1 -2 -1 3 2 1 2 -10 -2 -2 -1 13 -1 -2 -1 9 -1 -2 -
2 4 2 2 1 -9 -1 -2 -5 7 1 2 2 2 2 1 2 1 2 -3 1 -3 3 -8 17 -9 -1 -2 -6 8 -2 -1
13 -12 4 3 3 -10 2 5 1 -8 5 3 -1 -2 -1 -2 -2 4 3 4 1)
(nl)
=> (0 -12 3 4 5 3 -1 -7 -8 6 -2 -1 -2 3 4 5 3 4 -2 -7 -8 6 -1 -2 -2 4 3 5 -2 1
-7 -6 -9 9 6 3 -1 -4 -1 -2 -5 4 1 4 3 3 -2 -3 2 -3 -4 0 -2 -2 -1 -2 -2 -1 -2
-2 -1 -2 -2 -2 -1 -2 -2 -1 -2 3 -1 -2 2 3 2 1 2 2 2 1 2 2 1 -1 0 3 2 2 1 -8 -2
-2 -5 7 10 -1 1 -5 -3 8 -5 -3 -4 7 -3 -4 -2 4 -2 -2 -1 10 -4 9 -15 6 -3 -3 -2
3 -1 -2 -1 3 1 2 1 3 2 1 2 1 2 -3 1 -3 -2 -2 -1 -2 -2 -1 -2 3 -1 -2 -1 3 1 2
1 3 2 1 2 1 2 -3 1 -3 -2 -2 -1 -2 -2 4 -2 -2 2 3 2 2 1 2 2 2 1 -3 2 1 -9 3 1 2
2 -11 -1 -2 -1 9 -1 -2 8 -1 -1 -2 -2 -2 -2 -1 1 -3 3 4 5 2 1 -1 -2 -2 -1 -2 3
-1 -2 2 3 2 1 2 2 2 1 2 2 1 -3 1 -3 -1 -2 -2 -2 -1 -2 -2 -1 -2 -2 -1 -2 3 -1
-2 -1 3 1 2 2 2 2 1 2 1 2 -3 1 -3 3 -8 17 -9 -1 -2 -6 8 -2 -1 13 -12 4 3 3 -10
2 5 1 -8 5 3 -1 -2 -1 -2 -2 4 3 4 1)
We can convert these intervals to pitches by using idelta and save our file as MIDI.
(define p (idelta (nl) 59 45 85))
(define d (constant-value (length p) 250))
(define v (constant-value (length p) 96))
(save-midi (create-events 0 p d v 1))
Notice how the idelta function converts the intervals back to pitches.
p
=> (59 59 47 50 54 59 62 61 54 46 52 50 49 47 50 54 59 62 66 64 57 49 55 54 52 50
54 57 62 66 69 67 59 52 62 61 59 58 67 64 52 51 57 56 66 64 50 49 52 58 64 64 62
61 62 61 54 46 55 54 52 50 54 59 56 57 54 53 56 59 62 61 47 46 49 55 58 61 56 55
58 62 67 70 73 71 63 56 69 68 66 64 56 49 62 61 59 57 61 64 69 73 76 74 66 59 69
68 66 65 74 71 59 58 64 63 73 71 57 56 59 65 71 71 69 68 69 68 61 53 62 61 59 57
61 66 63 64 61 60 63 66 69 68 54 52 56 61 64 68 59 58 61 64 67 66 52 51 54 60 63
66 49 48 51 55 60 63 66 64 56 49 62 61 59 57 61 64 69 73 76 74 66 59 69 68 66 65
74 71 59 58 64 63 73 71 57 56 59 65 71 71 69 68 69 68 61 53 62 61 59 57 61 66 63
64 61 60 63 66 69 68 54 52 56 61 64 68 59 58 61 64 67 66 52 51 54 60 63 66 49 48
51 55 60 63 66 64 56 49 62 61 59 57 49 54 67 66 64 62 66 69 74 78 81 79 71 64 74
73 71 70 79 76 64 63 69 68 78 76 62 61 64 70 76 76 74 73 74 73 66 58 67 66 64 62
66 71 75 78 83 79 71 64 67 71 74 73 64 57 61 64 67 71 67 71 74 79 83 85 76 67 76
81 85 74 69 64 62 57 61 50 74 73 71 70 71 73 64 58 67 66 64 62 66 71 75 78 83 79
71 64 67 71 74 73 64 57 61 64 67 71 67 71 74 79 83 85 76 67 76 81 85 74 69 64 62
57 61 50 74 73 71 70 71 73 64 58 67 66 64 62 66 71 75 78 83 79 71 64 67 71 74 73
64 57 61 64 67 71 67 71 74 79 83 85 76 67 76 81 85 74 69 64 62 57 61 50 74 73 71
70 71 73 64 58 67 66 64 62 66 71 68 69 66 65 68 71 74 73 59 58 61 67 70 73 56 55
58 62 67 70 73 71 63 56 69 68 66 64 56 49 62 61 59 57 61 64 69 73 76 74 66 59 69
68 66 65 74 71 59 58 64 63 73 71 57 56 59 65 71 71 69 68 69 68 61 53 62 61 59 57
61 66 70 73 78 74 66 59 62 66 69 68 59 52 56 59 62 66 62 66 69 74 78 80 71 62 71
76 80 81 76 71 69 64 68 57 81 80 78 77 78 80 71 65 74 73 71 69 73 78 82 85 78 74
66 59 62 66 69 68 59 52 56 59 62 66 62 66 69 74 78 80 71 62 71 76 80 81 76 71 69
64 68 57 81 80 78 77 78 80 71 65 74 73 71 69 73 78 82 85 78 74 66 59 62 66 69 68
59 52 56 59 62 66 62 66 69 74 78 80 71 62 71 76 80 81 76 71 69 64 68 57 81 80 78
77 78 80 71 65 74 73 71 69 73 78 82 85 78 74 66 59 62 66 69 68 59 52 56 59 62 66
62 66 69 74 78 80 71 62 71 76 80 81 76 71 69 64 68 57 81 80 78 77 78 80 71 65 74
73 71 69 73 78 82 85 78 74 66 59 62 66 69 68 59 52 56 59 62 66 62 66 69 74 78 80
71 62 71 76 80 81 76 71 69 64 68 57 81 80 78 77 78 80 71 65 74 73 71 69 73 78 75
76 73 72 75 78 81 80 66 64 68 73 76 80 71 70 73 76 79 78 64 62 66 71 74 78 69 68
71 74 77 76 62 60 64 69 72 76 67 66 69 72 75 74 60 58 62 67 70 74 65 64 67 70 73
72 58 56 60 65 68 72 63 62 65 68 71 70 56 54 58 63 66 70 61 60 63 66 69 68 54 53
56 62 65 68 51 50 53 57 62 65 68 66 58 51 64 63 61 59 63 66 71 69 70 63 57 48 57
63 66 65 61 60 58 53 57 58 62 65 68 66 63 65 62 58 58 56 54 53 51 49 48 46 56 54
53 51 49 48 46 56 55 53 56 55 53 55 58 60 61 63 65 67 68 70 72 73 70 72 68 67 65
63 61 60 58 56 55 53 56 55 53 52 55 56 58 59 62 64 65 67 68 70 67 68 65 68 60 77
68 67 65 59 67 65 64 77 65 69 72 75 65 67 72 73 65 70 73 72 70 69 67 65 69 72 76
)
The complete code:
;; markov bach violin partita bwv 1002
(define courante-a '(
(0 b4)
(1 b4 b3 d4 f#4 b4 d5)
(2 c#5 f#4 a#3 e4 d4 c#4)
(3 b3 d4 f#4 b4 d5 f#5)
(4 e5 a4 c#4 g4 f#4 e4)
(5 d4 f#4 a4 d5 f#5 a5)
(6 g5 b4 e4 d5 c#5 b4)
(7 a#4 g5 e5 e4 d#4 a4)
(8 g#4 f#5 e5 d4 c#4 e4)
(9 a#4 e5 e5 d5 c#5 d5)
(10 c#5 f#4 a#3 g4 f#4 e4)
(11 d4 f#4 b4 d#5 f#5 b5)
(12 g5 b4 e4 g4 b4 d5)
(13 c#5 e4 a3 c#4 e4 g4)
(14 b4 g4 b4 d5 g5 b5)
(15 c#6 e5 g4 e5 a5 c#6)
(16 d6 a5 e5 d5 a4 c#5)
(17 d4 d6 c#6 b5 a#5 b5)
(18 c#6 e5 a#4 g5 f#5 e5)
(19 d5 f#5 b5 g#5 a5 f#5)
(20 e#5 g#5 b5 d6 c#6 b4)
(21 a4 c#5 f#5 a5 c#6 e5)
(22 d#5 f#5 a5 c6 b5 a4)
(23 g#4 b4 e#5 g#5 b5 f#4)
(24 e#4 g#4 c5 e#5 g#5 b5)
(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 b4 d5 f#5 b5 c#6 d6 c#6 b5 a5 g#5 f#5)
(21 e#5 c#6 g#5 e#5 c#5 g#5 e#5 c#6 b4 d5 c#5 b4)
(22 a4 c#5 f#5 g#5 a5 b5 c#6 b5 a5 g#5 f#5 e5)
(23 d#5 b5 f#5 d#5 b4 f#5 d#5 b4 a4 c#5 b4 a4)
(24 g#4 f#5 d5 b5 g#4 d5 b4 g#4 f#4 a4 g#4 f#4)
(25 e#4 g#4 a4 b4 c5 d#5 e#5 f#5 g#5 a5 b5 g#5)
(26 a5 f#5 e5 d5 c#5 b4 a4 g#4 f#4 e4 d4 c#4)
(27 b3 f#4 g4 a4 b4 c#5 d5 e5 f#5 g5 a5 f#5)
(28 g5 b5 g5 d5 b4 g5 d5 b4 g4 d5 b4 g4)
(29 e#4 b4 g#4 e#4 c#4 d#4 e#4 f#4 g#4 a4 b4 g#4)
(30 a4 f#4 a4 c#4 f#5 a4 g#4 f#4 c4 g#4 f#4 e#4)
(31 f#5 f#4 a#4 c#5 e5 f#4 g#4 c#5 d5 f#4 b4 d5)
(32 c#5 b4 a#4 g#4 f#4 a#4 c#5 e#5 f#5)
))
(define (make-form l)
(if (null? l) '()
(append (cdar l) (make-form (cdr l)))))
(define intervals-c (delta (pitches->numbers (make-form courante-a))))
(define intervals-d (delta (pitches->numbers (make-form double-a))))
(define intervals (append intervals-c intervals-d))
(define (nl) (markov intervals 16))
(define p (idelta (nl) 59 45 85))
(define d (constant-value (length p) 250))
(define v (constant-value (length p) 96))
(save-midi (create-events 0 p d v 1))