Book Image

Raspberry Pi Projects for Kids (Second Edition)

By : Daniel Leonard Bates
Book Image

Raspberry Pi Projects for Kids (Second Edition)

By: Daniel Leonard Bates

Overview of this book

Table of Contents (14 chapters)

Code listing


This section gives a complete listing of the code used in this chapter. You can refer to it if your code is not working to see what changes need to be made. Some of the code blocks have been moved around within the program to keep similar parts of the program together. This can make things easier to read, but it is completely optional. The following is the complete code used in the chapter:

pace = 0.8

define :play_notes do |notes, durations|
  together = notes.zip(durations)
  together.each do |note, duration|
    if one_in(2)
      speaker = -1
    else
      speaker = 1
    end
    play note, release: pace * duration, pan: speaker
    sleep pace * duration
  end
end

define :drum do
  sample :drum_tom_lo_hard, attack: 0, sustain: 0, release: 0.2
end

define :play_beat do |beat|
  beat.each do |pause|
    drum
    sleep pace * pause
  end
end

rhythm = [1, 1, 1, 0.75, 0.25, 1, 0.75, 0.25, 2]
line1 = [:G3, :G3, :G3, :Eb3, :Bb3, :G3, :Eb3, :Bb3, :G3]
line2 = [:D4, :D4, :D4, ...