Saturday, July 16, 2016

Learn music for programmers with Sonic Pi - 01 - Brother John

I my self don't have any musical education and they try to teach me in school it was completely nonsense for me. The reason is that it's basically substitution cipher for basic physics. So let's start with few basic substitutions.

Tones/Notes

There is 12 half-tones which musicians call octave(Latin name for 8)
It's actually 7 tones (C,D,E,F,G,A,B) + 5 Sharp tones(C#, D#, F#, G#, A#). - To be more complicated German notation use H instead of B.
Reference tone is A = 440Hz, A octave higher have frequency 2*A (880Hz) and octave lower is 1/2*A (220Hz)

Now if we check the notation we can find where it's C, D, E etc. based on the position, as you can see on the we start with C on the line below "standard lines". D is under first line, E on the first line F between 1st and 2nd, etc. If you would like to ask where are the # (Sharps), there are not there if the note is sharp would see symbol # in front  of the note. (If you try to find logic in it I'm not able to help sorry, it's a cipher, really it is Bb read as B flat = A# read as A Sharp or Ais, 4 names for one frequency, see not only simple substitution it's almost like enigma)


Duration

Tones have duration, whole, half, quarter, 1/8, 1/16 etc.



This is what every musician explains, but still you don't know anything. Half of what? Second, minute, year.... ?
The answer is it depends on tempo. Tempo is number of whole notes which you can play in the minute, BPM, beats per minute.
Hence if tempo is 60 bpm it 60 wholes per minute, hence whole note plays for 1 second,  or half note 0.5 second, etc.

Tones/Notes in Sonic Pi


One more substitution we need to learn this time easy for Sonic Pi. Sonic Pi don't use names of tones it use simple integers.
1 is lowest C
2 is C#
3 is D
4 is D#
etc.
This is however way too low, to get something which we can hear we will start at 61 so

CC#DD#EFF#GG#AA#B
616263646566676869707172

Let's play in Sonic Pi


It's easy we will use command play to play a note and sleep to say how long.
Hence

play 61
sleep 0.25

plays quarter C

Now we just need to define what we are going to play and how fast (tempo). We will use piano and tempo 60 BPM

use_bpm 60
use_synth :piano

And last for today we need to learn to repeat certain parts for that we will use X. times do cyckle it have easy syntax:

X. times do
  commands
end

Will do X times commands between do and end. In below song we use 2 times do.

Brother John

An finally let's play very easy song, which have mutation in various languages, Brother Jacob.

Sonic Pi code:

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

#Let first define basic tones
C = 61
CSharp = 62
D = 63
DSharp = 64
E = 65
F = 66
FSharp = 67
G = 68
GSharp = 69
A = 70
ASharp = 71
H = 72
B = 72

#Now lets play the song Brother Jacob

#1st Part 2 times
#Are you slee - pin'
2. times do
  play C
  sleep 0.50
  play D
  sleep 0.50
  play E
  sleep 0.50
  play C
  sleep 0.50
end

#Second part 2 times
#Bro-ther John
2. times do
  play E
  sleep 0.5
  play F
  sleep 0.5
  play G
  sleep 1
end

#3rd part 2 times
#Mor- nin bells are ring-ing
2. times do
  play G
  sleep 0.25
  play A
  sleep 0.25
  play G
  sleep 0.25
  play F
  sleep 0.25
  play E
  sleep 0.5
  play C
  sleep 0.5
end

#4th and last part 2 times
# Bim bam boom
2. times do
  play C
  sleep 0.5
  play G
  sleep 0.5
  play C
  sleep 1
end

No comments: