# Melody improvisation

**URL:** <https://in-thread.sonic-pi.net/t/melody-improvisation/988>\
**Category:** Creations & Ideas\
**Created:** [April 19, 2018, 5:03am UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988 "2018-04-19T05:03:07Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![EarthToAbigail](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/earthtoabigail/32/1057_2.png) [@EarthToAbigail](https://in-thread.sonic-pi.net/u/EarthToAbigail)\
**Post date:** [May 7, 2018, 7:22am UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/22 "2018-05-07T07:22:22Z")

</div>

@Eli Thank you for sharing the performance from @NanoMan (really beautiful…) and the MusicRadar resource! I also coincidentally found this post from 2017 on this forum the same day I wrote the comment on middle eastern music:

[Maqam Music with Sonic Pi - Performance](http://in-thread.sonic-pi.net/t/maqam-music-with-sonic-pi-performance/235)

I think I will try and see if I can create my own fork of SonicPi and customize it the same way 😃

@mrbombmusic That function is quite nice and actually gives a result that is not so far off from what I achieved manually! I learned a lot by examining your code, I started to play around with it to see if I can add a bit more functionality like chord changes and timing variations. If I come up with anything interesting I will post it here 😀

Thanks all of you for the amazing inspiration! This forum is quickly becoming one of my favourite online hangout places 😁

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [May 18, 2020, 10:01pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/23 "2020-05-18T22:01:20Z")

</div>

```ruby
#..........A.M.I.
mn=[:r]; for jn in 1..1023
  mn[jn]=hz_to_midi(24*jn).round
end

c=[6,5,8,9]
h=[48,54,60,72,81,96,0]
m=[0.25,0.5,0.75]

live_loop :keys do
  for x in 0..3
    synth :dtri, note: mn[c[x]], detune: 7,
      sustain: 2, release: 0,amp: 0.3
sleep 2; end;end

live_loop :lead do
  n=choose(h)
  t=choose(m)
  synth :pluck, note: mn[n/8]
sleep t; end

```

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [May 19, 2020, 8:36am UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/24 "2020-05-19T08:36:02Z")

</div>

This sounds very nice and works well.

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [May 19, 2020, 1:05pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/25 "2020-05-19T13:05:54Z")

</div>

Thank You ! … 🙂  
A lesson I have learned  
is that ‘music’ is in the ear  
of the beholder. There is a  
threshold where chaos transforms  
into beauty. (Or at least tunefulness)

---

<div class="post-metadata">

**Author:** ![Eli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/eli/32/116_2.png) [@Eli](https://in-thread.sonic-pi.net/u/Eli)\
**Post date:** [May 19, 2020, 4:57pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/26 "2020-05-19T16:57:59Z")

</div>

I agree with Robin, it works well.

There are several people who post code here in the  
same… ‘style’?

To me it seems very mathematical, far more so than  
musical… is it a syntax from a different programme that  
people know about?

Eli…

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [May 19, 2020, 6:36pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/27 "2020-05-19T18:36:22Z")

</div>

Thank You !  
Do You mean it sounds mathematical, or looks mathematical ?  
Here is the essence …

```ruby
 mn=[:r]; for jn in 1..1023# midi-number[just-number]
  mn[jn]=hz_to_midi(24*jn).round; end
#just intonation uses the mathematical relationships
#of the frequencies of pitch as the framework
#I.E.
c=[6,5,8,9]
#6,8,9 are the relationships of (key of C) C,F,G
#tonic, sub-dominant, dominant
#5 is the relative Minor ... A

```

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [May 19, 2020, 6:50pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/28 "2020-05-19T18:50:58Z")

</div>

> [@hitsware](#):
>
> ```ruby
> mn[jn]=hz_to_midi(24*jn).round;
> 
> ```

sorry but where does this come from ? a wizard hat ?

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [May 19, 2020, 8:53pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/29 "2020-05-19T20:53:19Z")

</div>

> [@nlb](#):
>
> sorry but where does this come from ? a wizard hat ?

Derivation of C scale @ A 440 Hz using just intonation …  
Sequence of lowest integers that represent the ratios of frequencies of a major scale …  
24, 27, 30, 32, 36, 40, 45, 48  
In the key of C  
C, D, E, F, G, A, B, C  
We see that ’ A ’ is represented by ’ 40 ’  
If we want to normalize the scale to A - 440 Hz  
440 / n = 40 … therefore n = 11  
If we want to produce the scale numerically using  
midi numbers ( as with Sonic Pi ) …  
Then …  
midi-number [just-number] = hz\_to\_midi ( just-number \* 11 )

---

<div class="post-metadata">

**Author:** ![Eli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/eli/32/116_2.png) [@Eli](https://in-thread.sonic-pi.net/u/Eli)\
**Post date:** [May 19, 2020, 11:20pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/30 "2020-05-19T23:20:27Z")

</div>

I mean it -looks- mathematical. The sound does not -sound- mathematical, nor just ‘random’…  
it sounds natural.

Eli…

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [May 19, 2020, 11:59pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/31 "2020-05-19T23:59:18Z")

</div>

> [@Eli](#):
>
> nor just ‘random’…

Random , but with ’ steerage ’  
The ’ chords ’ are 1 rst & 5 th pairs .  
The notes are a pentatonic scale  
in C … Sort of like a harmonica …  
hard to go wrong . The one possible  
rest in each choice helps . The timing  
is random … but on intervals that imply  
4 / 4 time . It was a lucky try 🙂

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [July 23, 2020, 5:53pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/32 "2020-07-23T17:53:30Z")

</div>

```ruby
mn=[:r]; for jn in 1..1023
mn[jn]=hz_to_midi(jn*1.5); end

o=[1,2,4]
m=[9,9,10,10,11,11,10,10]
c=[9,8,6,9,6,6,6,6,8,8,6,6]

loop do
  for x in 0..11; for y in 0..7
      synth :piano, note: mn[12*choose(o)*c[x]]
      synth :piano, note: mn[m[y]*choose(o)*c[x]]
sleep 0.3; end; end; end

```

---

<div class="post-metadata">

**Author:** ![fredguth](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/fredguth/32/1534_2.png) [@fredguth](https://in-thread.sonic-pi.net/u/fredguth)\
**Post date:** [July 24, 2020, 4:58am UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/33 "2020-07-24T04:58:53Z")

</div>

I know this might seem weird for some. But this kind of code helps me learn more about music because it makes more sense for me. There is no right way just your way. Thanks all for sharing codes and thoughts.

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [July 24, 2020, 7:14am UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/34 "2020-07-24T07:14:39Z")

</div>

hi @hitsware

may i suggest some improvements ? Explain what this code does with comments to help people to understand and learn about your code 🙂

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [July 24, 2020, 9:57am UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/35 "2020-07-24T09:57:40Z")

</div>

There has been some brief discussion on the code further up this thread.

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [July 24, 2020, 4:40pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/36 "2020-07-24T16:40:13Z")

</div>

```ruby
# Since normally a piano melody is played with the right hand
# ( higher notes ) while the left plays the rhythym , our mind
# translates the random high octave notes as an improvision .

midinumber=[:r]
# create array with '0' element as rest

for justnumber in 1..1023
  # array index 1 to 1023 (ratiometric notes)
  # I.E. octave = 2 : 1 .... fifth = 3 : 2
  
  midinumber [justnumber] = hz_to_midi(justnumber * 1.5)
  # set midinumbers to desired frequencies
end

chord=[9,8,6,9,6,6,6,6,8,8,6,6]
# 12 bar blues progression
# key = 6 ... forth = 8 ... fifth = 9

multiplier = [9,9,10,10,11,11,10,10]
# boogie style note pattern

octave=[1,2,4]
# possible note octaves

loop do
  for x in 0..11
    for y in 0..7
      # 12 bars of eighth notes
      
      synth :piano, note: midinumber [12*choose(octave)*chord[x]]
      # finger one plays root chord notes (random octaves)
      
      synth :piano, note: midinumber[multiplier[y]*choose(octave)*chord[x]]
      # finger two plays pattern (random octaves)
      
sleep 0.3; end; end; end

```

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [July 25, 2020, 3:11pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/37 "2020-07-25T15:11:26Z")

</div>

hi @hitsware,

Thanks for these comments.  
I copy paste the code and play and

> [@hitsware](#):
>
> ```ruby
> synth :piano, note: midinumber[multiplier[y]*choose(octave)*chord[x]]
> 
> ```

plays “weird”, 'disonnant" ?  
Is it wanted 🙂

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [July 25, 2020, 6:33pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/38 "2020-07-25T18:33:33Z")

</div>

hopefully correcting

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [July 25, 2020, 7:05pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/39 "2020-07-25T19:05:36Z")

</div>

```ruby
# One man's dissonance is another man's funk : )
# Yes ... I like to use as small of numbers as possible
# and tolerate some instrument abuse ...

mn=[:r]; for jn in 1..1023
  mn[jn]=hz_to_midi(jn*1.5)
  
  # change above to : mn[jn]=hz_to_midi(jn*1.5).round
  # this will make all midi numbers integars insuring
  # equal tempered tuning
end

o=[1,2,4]
m=[9,9,10,10,11,11,10,10]
# OR the above should be [27,27,30,30,32,32,30,30]
c=[9,8,6,9,6,6,6,6,8,8,6,6]

loop do
  for x in 0..11; for y in 0..7
      synth :piano, note: mn[12*choose(o)*c[x]]
      # AND the 12 changed to 9 above ...
      synth :piano, note: mn[m[y]*choose(o)*c[x]]
sleep 0.3; end; end; end

```

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [July 27, 2020, 2:26pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/40 "2020-07-27T14:26:51Z")

</div>

thanks for your changes but, well not a lot of differences for me, sorry about that but i’m not convinced. Or let say your build the very new synth for sonic pi : "old piano from the attic "  
Cheers

---

<div class="post-metadata">

**Author:** ![hitsware](https://avatars.discourse-cdn.com/v4/letter/h/a9adbd/32.png) [@hitsware](https://in-thread.sonic-pi.net/u/hitsware)\
**Post date:** [July 27, 2020, 3:09pm UTC](https://in-thread.sonic-pi.net/t/melody-improvisation/988/41 "2020-07-27T15:09:46Z")

</div>

Hmmmmm …  
That is interesting .  
What type of music do you usually listen to ?  
The boogie type riffs do use intervals which  
could sound off to someone unaccustomed  
( I think ) Does this YouTube sound ?

[![](https://img.youtube.com/vi/NmCv6UOF22A/hqdefault.jpg "Incredible Blues Pianist Luca Sestak's Slow Blues Improvisation.") ](https://www.youtube.com/watch?v=NmCv6UOF22A)

[Previous page](https://in-thread.sonic-pi.net/t/melody-improvisation/988.md?page=1)

[Next page](https://in-thread.sonic-pi.net/t/melody-improvisation/988.md?page=3)
