Tuesday, August 9, 2016

Learn music for programmers with Sonci Pi - 04 - Jam Session

In second lesson we created random Chinese music generator just by randomly playing tones from pentatonic scale. But pentatonic scale is not the only one of course.

Theory


Diatonic Scale


The basic and most known is of course diatonic scale. It's the one all of you know C-D-E-F-G-A-B(H)-C. I will not go in to further details. First of all we already touch the basic in first lesson and second you can ready more on wikipedia.

Pentatonic Scale


We touch pentatonic scale in second lesson and we said it's the black keys on the piano, so C# D# F# G# A#. However in the music the difference between tones matter more then the frequencies so if you move all of them by half tones you again get pentatonic scale. If you are interested more you can go on and again wikipedia is good source

Jazz Scale


There is also jazz scale. A great deal of modern jazz harmony arises from the modes of the ascending form of the melodic minor scale, also known as the jazz melodic minor scale C D E-flat F G A B
Well there are many jazz scales and again rather then explain all, you can read wikipedia.

There are also other scales, but for today enough of theory.


Sonic PI


So the question is if  we can create the Chinese music by just randomly playing pentatonic scale can we create Jam session by "randomly" playing jazz scale?

Let's start with little repetition

Function

Just use define :name do |prametr1, parametr2| to define function, if you want it to be function  you must return some value, so then before the end use name = value
Below example is factorial function.

define :fact do |n|
  fact = (1..n).reduce(:*)||1
end

So with factorial and combination functions, I've create ghoose function from second lesson, choose "randomly" with Gaussian probability distribution, but this time it's general for any list.

define :gchoose do |list|
  fl = list.length
  choosen_index = (fl/2).to_i
  base = 2**(fl-1)
  index = 0
  (fl). times do
    #puts (combination (fl-1), index)
    choosen_index = index if one_in((base/(combination (fl-1), index)).to_i)
    index = inc index
  end
  ghoose = list[choosen_index]
end


Now we just play random notes from Jazz scale with random duration

loop do
  play (gchoose jazz_scale)
  sleep (gchoose durations)
end

Here is the complete code on github if you will play it you find out that it's not what you would expect. The problem is that it miss one important think, rhythm.

OK, let's add underlying rhythm (jazz use 4/4 time signature so easiest would be 4 quarter notes, F D D D for example).

#Basic rhythm
in_thread do
  loop do
    play (F-12)
    sleep 1
    3. times do
      play (D-12)
      sleep 1
    end
  end
end

We also need to sync the our "improvisation" part to reflect the time signature. We just set Total Duration to 4 and initiate Remaining Duration to the same. Choose Current Duration, if it's higher then remaining we cut it short and use remaining. Once RD is 0 we reset it to TD.

#Total Duration
TD = 4
#Remaining Duration
RD = TD

loop do
  play (gchoose jazz_scale)
  #Choosen Duration
  CD = gchoose durations
  if CD > RD
    CD = CD - RD
  end
  sleep CD
  RD = RD - CD
  if RD <= 0
    RD = TD
  end
end

The complete code is my github. If you will try to play it I think you find that even we add rhythm and it sound little more like jazz, it's actually terrible. If you use default choose function instead of my gchoose it sound better, but still not jam session.

So let's can we create Jam session by "randomly" playing jazz scale?
No, not even if add rhythm to it.

Tuesday, August 2, 2016

Learn music for programmers with Sonci Pi - 03 - Rythm

Theory


Beat

One beat is basic time unit. One beat equals (most often) 1/4 note (because it's most common time measure in music).

Measure or Bar(line)

Measure or sometimes referred as bar is musical time unit which divides the song into larger units

Time signature

Time signature actually is telling us how many notes (the blue number) of and how long (the red number) we shall play. In example below it 3 quarter notes. if it would be 4/4 it would be 4 quarter notes, 2/2 would be 2 half notes, 4/8 would be 4 eights notes, etc.
Now as you can see in example below you don't have to play quarter notes, but if you summarize you shall get time corresponding to the time signature.
Take example of part between green lines(the end part). It's six 1/8 notes hence 6/8 = 3/4.

Rhythm

Rhythm is event repeating over time. Main beat and unstressed off beat or Accented back beat, repeating in pattern. To distinct them we use different drums, high and low, long and short beats.

Except using the traditional notation to understand and follow the rhythm, instead of linear display we can use circle, so one circle equals to one measure. Hence the measure with 3/4 time signature we can divide into 3 parts which each correspond to 1/4 note. (If tempo is 60 bpm then the full circle is 3 seconds).

In below section I show you few examples of rhythms, how they look on the "rhythm circle" and how we can write that in Sonic PI

Sonic PI


Waltz

Waltz use 3/4 time signature. Below we can see the basic simple waltz rhythm the first tone higher(main beat) next two lower(off and secondary beat).



It divides the rhythm circle below in to 3 equal parts each is for one 1/4 note.
In sonic code example below it corresponds to F and two D


Sonic Pi code:
# Welcome to Sonic Pi v2.10
use_bpm 90
use_synth :piano

#Let first define basic tones
D = 63
F = 66

#Basic rhythm
loop do
  play F
  sleep 1
  play D
  sleep 1
  play D
  sleep 1
end

Polka

Polka use 2/4 time signature. Below we can see it on rhythm circle. It's pretty easy one note (let it be E) starts on "12 o'clock" and last 1/4 as we have 2/4 = full circle, then 1/4 is one half of the circle so second note(let it be F) starts on "6 o'clock"


And here is the same in Sonic Pi

loop do
  play E
  sleep 1
  play F
  sleep 1
end

Let's try something little more interesting.


It's not hard we only need to play more things at once so lets do threads. It's easy syntax:
in_thread do
  commands
end

Complete Sonic PI code for polka rhythm above, the main rhythm(2 quarter F notes) are played in thread 1/8ths in "main" part:

# Welcome to Sonic Pi v2.10
use_bpm 90
use_synth :piano

#Let first define basic tones
C = 61
F = 66

#Basic rhythm
in_thread do
  loop do
    2.times do 
      play F
      sleep 1
    end
 end
end

loop do
  sleep 0.5
  play C+12
  sleep 0.5
  sleep 0.5
  play C+12
  sleep 0.5
end

Tango

Tango is in use 4/4 time signature. If we little simplify it's long, short, short, long. If you learn tango in dancing lesson you might remember the step pattern, slow, slow, quick, quick.

So here we go this one is pretty simple two long C to short E. (The dot behind the C means 1/8 longer)

Here it's on the rhythm circle



And here is the Sonic Pi code:
# Welcome to Sonic Pi v2.10
use_bpm 78
use_synth :piano
loop do
  play 61
  sleep 1
  sleep 0.5
  play 65
  sleep 0.5
  play 65
  sleep 1
  play 61
  sleep 1
end


Here is one little more complected as it's more changing the pitches but the timing is important (not the pitch)


You can try to write Sonic Pi code your self and then compare with my on github