Skip to content

Instantly share code, notes, and snippets.

@berndverst
Last active January 11, 2021 02:43
Show Gist options
  • Save berndverst/56d914cd11fef86dbbad7112b80a6959 to your computer and use it in GitHub Desktop.
Save berndverst/56d914cd11fef86dbbad7112b80a6959 to your computer and use it in GitHub Desktop.
Solution for Workshop "Creating Music through Live Coding"

You can find the workshop here: https://gist.github.com/berndverst/c1b47d052716327a3694358557e9c3c6

Solution

The following code can be copied and run (listened to) in Sonic Pi

use_bpm 68
use_synth :piano

load_sample :perc_snap

live_loop :hamilton_drums do
  # I prefer thinking of music in measures, therefore I include 4 repeating beats. Other solutions are possible and even simpler :)
  sleep 1
  sample :perc_snap
  sleep 2
  sample :perc_snap
  sleep 1
end

live_loop :hamilton_piano do
  play :B2
  sleep 4
  play :Fs2
  sleep 4
  play :G2
  sleep 4
  play :D2
  sleep 2
  play :As2
  sleep 2
end

Bonus

Create piano octaves for a fuller sound

The play command allows playing multiple pitches at once by listining these in a list like [:F1, :G1]. Update the bass line to play the same key in two adjacent octaves (an octave is a key 12 steps apart).

Fix the piano pitch / note lengths

In synth music the most important concept is ADSR -- attack, decay, sustain and release that affect the sound. The play command supports specifying these parameters.

On a piano holding a key means sustaining the pitch. Update your existing bass line by adding the sustain for the correct number of beats.

play :C2, sustain: 1

Bonus Solution

Go to the Bonus Solution here

Here is the full example with the bonus prompts

use_bpm 68
use_synth :piano

load_sample :perc_snap

live_loop :hamilton_drums do
  sleep 1
  sample :perc_snap
  sleep 2
  sample :perc_snap
  sleep 1
end

live_loop :hamilton_piano do
  play [:B2, :B1], sustain: 4
  sleep 4
  play [:Fs2, :Fs1], sustain: 4
  sleep 4
  play [:G2, :G1], sustain: 4
  sleep 4
  play [:D2, :D1], sustain: 2
  sleep 2
  play [:As2, :As1], sustain: 2
  sleep 2
end

For a more complete example, also enjoy the intro with some additions to below:

use_bpm 68
use_synth :piano

load_sample :perc_snap

define :hamilton_intro do
  play_pattern_timed chord(:Fs2, '1', num_octaves: 3), [0, 0, 0.5], release: 0.5
  play_pattern_timed chord(:Fs2, '1', num_octaves: 3), [0, 0, 0.5/3], release: 0.3
  play_pattern_timed chord(:Fs2, '1', num_octaves: 3), [0, 0, 0.5/3], release: 0.3
  play_pattern_timed chord(:Fs2, '1', num_octaves: 3), [0, 0, 0.5/3], release: 0.3
  play_pattern_timed chord(:Fs2, '1', num_octaves: 3), [0, 0, 0.5], release: 0.5
  play_pattern_timed chord(:Fs2, '1', num_octaves: 3), [0, 0, 0.5], release: 0.5
  play_pattern_timed chord(:Fs2, '1', num_octaves: 3), [0, 0, 0.5], release: 0.5
  play_pattern_timed chord(:Fs4, '1', num_octaves: 3), [0, 0, 0.25], release: 0.5
  play_pattern_timed chord(:Cs5, '1', num_octaves: 2), [0, 0.25], release: 0.5
  play_pattern_timed chord(:B4, '1', num_octaves: 2), [0, 0.5], release: 0.5
  play_pattern_timed chord(:As4, '1', num_octaves: 2), [0, 0.5], release: 0.5
end

hamilton_intro


live_loop :hamilton_drums do
  sleep 1
  sample :perc_snap
  sleep 2
  sample :perc_snap
  sleep 1
end

live_loop :hamilton_piano do
  play [:B2, :B1], sustain: 4
  sleep 4
  play [:Fs2, :Fs1], sustain: 4
  sleep 4
  play [:G2, :G1], sustain: 4
  sleep 4
  play [:D2, :D1], sustain: 2
  sleep 2
  play [:As2, :As1], sustain: 2
  sleep 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment