# Chord Progression and Permutated Melody

#### Chord Progression and Permutated Melody

The idea came from Michael New’ s YouTube Series on Music Theory.
I used the permutation function from the Ruby array class to create the melody from a list of permissable notes.

How to write a melody

Listen to minor key version
A code listing follows:

``````#Melody.rb
# 15 Oct 2017
# Idea from watching one of Michael New's Music Theory Videos
##############
#helper function
def midi2note(n)
nn=note(n)
if nn==nil
return nn
else
nn= note_info(nn)
nnn=nn.to_s.split(":")
mmm= nnn[3].chop
return mmm
end
end # midi2note
def listnotes(n)
i=0
while i<n.length
puts midi2note(n[i])
i+=1
end
end
#define intervals
octave=12
#Define tempo and note lengths
#####
tempo=0.750  ### try changing tempo
#define note timings
whole=1.0
half=whole/2.0
dothalf=half*1.5
quart=half/2.0
dotquart=quart*1.5
eighth=quart/2.0
doteighth=eighth*1.5
sixteenth=eighth/2
#########
### try different keys
key= note(:c4)
puts midi2note(key)
puts " "
### try minor
mode=:major
# setup chord progression in a and b
a=chord_invert(chord_degree :i, key, mode,3),0
listnotes(a)

b=chord_invert(chord_degree :ii, key, mode,3),0
listnotes(b)

# create non chord tones  avoiding the 4th
nct=[]
nct[0]=b[0]
c=chord_invert(chord_degree :vi, key, mode,3),0
nct=nct.push(c[0])
c=chord_invert(chord_degree :vii, key, mode,3),0
nct=nct.push(c[0])
listnotes(nct)
aa=a.permutation.to_a
puts "AA",a,aa
bb=b.permutation.to_a
puts "BB",b,bb
cc=nct.permutation.to_a
puts "NCT",nct,cc

live_loop :LL1 do
use_synth :fm
with_fx :level, amp: 0.1 do
j=0
while j<2*a.length
puts midi2note(a[j])
play a[j]
sleep quart*tempo
j+=1
end
j=0
while j<2*b.length
puts midi2note(b[j])
play b[j]
sleep quart*tempo
j+=1
end
end
end #LL1
live_loop :LL2 do
sync :LL1
use_synth :fm
with_fx :level, amp: 0.2 do
i=0
while i<aa.length
j=0
while j<2*aa[i].length
puts midi2note(aa[i][j]+octave)
play aa[i][j]+octave
sleep quart*tempo
j+=1
end
j=0
while j<2*bb[i].length
puts midi2note(bb[i][j]+octave)
play bb[i][j]+octave
sleep quart*tempo
j+=1
end
puts midi2note(nct[j]+octave)
play nct[i]+octave
sleep quart*tempo
i+=1
end #next i
end
end #LL2
``````

Interesting stuff. Thanks for bringing Michael New’s videos to our attention. They look very useful. especially when you do the hard work to convert some if his examples into SP code!.

@JoeMac - thanks so much for this - both for the link to the theory videos and the code - I’m looking forward to playing with it.

One minor point though - defining functions with `def` isn’t supported with Sonic Pi. Please prefer `define`