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