# Polyphony Machine

**URL:** <https://in-thread.sonic-pi.net/t/polyphony-machine/2142>\
**Category:** Educators\
**Created:** [March 20, 2019, 5:26pm UTC](https://in-thread.sonic-pi.net/t/polyphony-machine/2142 "2019-03-20T17:26:45Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Gisbert](https://avatars.discourse-cdn.com/v4/letter/g/d6d6ee/32.png) [@Gisbert](https://in-thread.sonic-pi.net/u/Gisbert)\
**Post date:** [April 6, 2019, 6:13pm UTC](https://in-thread.sonic-pi.net/t/polyphony-machine/2142/9 "2019-04-06T18:13:09Z")

</div>

I noticed a mistake in the Markov Polyphony Machine, the “Komplementärrhythmus” live\_loop did not work correct, I fixed that by using set and get.  
By the way, @Robin Newman, this does the same thing I looked for in this thread:

> [@Imaginary Gamelan](http://in-thread.sonic-pi.net/t/imaginary-gamelan/1809):
>
> Using the concept of state machines (thanks to @nabisco) on polyphonic lines, this resembles some stuff I heard Gamelan ensembles play. Due to the changing random seed, this will sound different each time you execute the code. #MarkovChains Freequencies t = Time.now.to\_i use\_random\_seed t puts rand live\_loop :temp do set :temp, 52 sleep 10 end #Setting the range of possible pitches, in this case one octave set :min\_pitch, 66 set :max\_pitch, 78 #Setting the pitches to be used; not quan…

I never answered your suggestion, sorry, how rude of me!

Here is the fixed Markov Polyphony Machine:

```
#Markov Polyphony Machine 1
#by Gisbert Schürig w thanks to Nabisco
#Komplementärrhythmus fixed

use_random_seed 1 #change for different results!

Bpm = 30

#initial state
set :current_state, 0
set :current_state_harmonik, 0

Tonleiter = scale(:e4, :harmonic_minor, num_octaves: 2)
#try other scales, ":mayor" gives traditional results but I love ":messiaen1" or ":pelog"

Melodie = (ring 0, 4, 2, 5, 1) #melody pattern for the four voices
Rhythmus_mel = (ring 0.5, 0.75, 0.25, 0.25, 0.25) #rhythm pattern

#State machine rules for the four voices; each state corresponds to one note and one duration
#from the "Melodie" und "Rhythmus_mel" rings
RULES = {
  0 => [1, 2, 4], #prime intervall ("0" in "Melodie" ring) may lead to second, third or fifth intervall
  1 => [2],
  2 => [3],
  3 => [1, 4], #sixth may lead to third or fifth
  4 => [0, 1, 2] #second may lead to prime, fifth or second
}

Akkordfolge = (ring 0, 3, 6, 2, 5, 1, 4) #chord pattern

#State machine rules for the chord pattern/transpositions
#used to emulate "traditional" harmonic progressions
RULES_harmonik = {
  0 => [0, 1, 3, 4, 5, 6], #tonic may lead to any other chord
  1 => [0, 2, 6], #subdominant may lead to tonic or dominant functions
  2 => [3],
  3 => [4],
  4 => [5, 1], #tonic parallel minor may lead to dominant or subdominant
  5 => [6],
  6 => [0, 4] #dominant leads to tonic or the tonics parallel minor chord
}

live_loop :Akkorddauer do #determining the tempo of the harmonic pattern/transpositions
  use_bpm Bpm
  Dauer = (ring 1, 2, 4, 8).choose
  sleep 8
end

live_loop :Transposition do #applying the state machine rules to the "Transposition" variable
  use_bpm Bpm
  s1 = get[:current_state_harmonik]
  Transposition = Akkordfolge[s1]
  sleep Dauer
  set :current_state_harmonik, RULES_harmonik[s1].choose
end

live_loop :Komplementärrhythmus do #varying the rhythm by multiplying it
  use_bpm Bpm
  set :T1, (ring 0.5, 1, 2, 4).shuffle
  set :T2, T1.drop(0).shuffle #dropping the element chosen by T1
  set :T3, T2.drop(0).shuffle #dropping the element chosen by T2, resulting in a complementary rhythm
  sleep (ring 0.5, 1, 2, 4, 8).choose
end

with_fx :reverb do
  
  live_loop :Spieler_Melodie do
    use_synth :sine
    use_bpm Bpm
    s = get[:current_state]
    play Tonleiter[1 * Melodie[s] + Transposition], amp: rrand(0.4, 0.9), decay: 0.3, sustain: 0, release: 0.3, pan: 0.1
    sleep Rhythmus_mel[s] * get[:T1][0] #applying the rhythm state machine rules and multiplication for
    #complementary rhythm
    set :current_state, RULES[s].choose
  end
  
  live_loop :Melodische_Transposition do
    Mel_Transp = (ring 0, 2, 4, 7).choose
    Mel_Transp1 = (ring 0, 2, 4, 7).choose
    sleep rrand_i(1, 8)
  end
  
  
  live_loop :Spieler_Sequenz do
    use_synth :sine
    use_bpm Bpm
    s = get[:current_state]
    play Tonleiter[1 * Melodie.reflect[s] + Transposition + Mel_Transp], amp: rrand(0.15, 0.6), decay: 0.2, sustain: 0.2, release: 0.2, pan: 0.35
    sleep Rhythmus_mel.mirror[s] * get[:T2][0]
    set :current_state, RULES[s].choose
  end
  
  live_loop :Spieler_Sequenz1 do
    use_synth :sine
    use_bpm Bpm
    s = get[:current_state]
    play Tonleiter[1 * Melodie[s] + Transposition + Mel_Transp1] - 12, amp: rrand(0.15, 0.6), decay: 0.2, sustain: 0.2, release: 0.2, pan: -0.35
    sleep Rhythmus_mel[s] * get[:T3][0]
    set :current_state, RULES[s].choose
  end
  
  
  live_loop :Spieler_Bass do
    use_synth :sine
    use_bpm Bpm
    s = get[:current_state]
    play Tonleiter[1 * Melodie[s] + Transposition] - 24, amp: rrand(0.15, 0.4), decay: 0.2, sustain: 0.2, release: 0.2
    sleep Rhythmus_mel[s] * 2
    set :current_state, RULES[s].choose
  end
  
end#fx reverb

```

---

_[View the full topic](https://in-thread.sonic-pi.net/t/polyphony-machine/2142)._
