#Fanfare for St Cecilia's Hall by Andrew Baird, first performed 29th November 2017
#at the official opening of St Cecilia's Hall  by HRH The Princess Royal, following a major refurbishment

#coded by Robin Newman, December 2017, with the composer's permission
use_bpm 85
use_synth :saw
with_fx :reverb, room: 0.8,mix: 0.8 do
  define :plarray do |notes,durations,pan=0,amp =0.5|
    notes.zip(durations).each do |n,d|
      play n,sustain: d*0.9,release: d*0.1,amp: amp,pan: pan
      sleep d
    end
  end
  b=4;md=3;m=2;cd=1.5;c=1;qd=0.75;q=0.5;sqd=0.375;sq=0.25;dsq=0.125
  
  n1=[:c4,:g4,:c5,:f5,:bb5,:f5,:c6,:g5,:g5,:bb5,:f5,:e5,:d5,:e5,:r]
  d1=[c,c,c,c,m,m,m,cd,q,m,md,q,q,m+md,c+c]
  n1b=[:c5,:g4,:c5,:c5,:c5,:c5,:g4,:c5,:c5,:d5,:e5,:d5,:e5,:d5,:c5,:g5,\
       :d5,:g4,:g4,:g4,:e5,:d5,:f5,:e5,:d5,:c5,:d5,:c5,:d5,:e5,:r,:c5,:g4,:c5,:c5,:c5,:c5,:g4,:c5]
  d1b=[q,q,qd,dsq,dsq,q,q,c,sq,sq,c,dsq/2,dsq/2,dsq,sq,m,\
       qd,dsq,dsq,q,q,cd,q,sq,sq,q,qd,dsq,dsq,m,q,q,q,qd,dsq,dsq,q,q,c]
  n1c=[:c5,:d5,:e5,:d5,:e5,:d5,:c5,:g5,:f5,:e5,:d5,:e5,:f5,:e5,:d5,:e5,:f5,:g5,:g5,:c6,:e5,:f5,:g5,:g5,:c5,:r,:g4]
  d1c=[sq,sq,c,dsq/2,dsq/2,dsq,sq,m,cd,sq,sq,dsq/2,dsq/2,dsq,sq,sq,sq,q,q,q,sq,sq,q,q,c,q,q]
  n1d=[:c5,:c5,:c5,:d5,:e5,:d5,:e5,:d5,:g4,:g4,:g4,:d5,:d5,:d5,:e5,:f5,:e5,:f5,:e5,:d5,:c5,:c5,\
       :af5,:f5,:f5,:g5,:af5,:g5,:e5,:g5,:c6,:g5,:r,:g4]
  d1d=[qd,sq,q,sq,sq,dsq/2,dsq/2,dsq+q,sq,q,q,qd,sq,q,sq,sq,dsq/2,dsq/2,dsq+q,sq,q,q,qd,sq,q,sq,sq,c+sq,sq,sq,sq,m+cd,sq,q]
  n1e=[:c5,:g4,:e4,:g4,:c5,:d5,:e5,:d5,:e5,:d5,:g4,:g4,:g4,:g4,:d5,:c5,:d5,:e5,:d5,:e5,:f5,\
       :e5,:f5,:e5,:d5,:e5,:d5,:c5,:c5,:c5,:g4,:c5,:e5,:d5,:e5,:d5,:c5,:d5,:e5,:d5,:e5,:g5]
  d1e=[sqd,dsq,sqd,dsq,q,sq,sq,dsq/2,dsq/2,dsq+sq,sqd,dsq,q,q,    sqd,dsq,sqd,dsq,q,sq,sq,dsq/2,dsq/2,dsq,sq,sq,sq,q,q,q,q,q,sq,sq,dsq,dsq,sq,sq,sq,q,sq,sq]
  n1f=[:c6,:e5,:f5,:g5,:g5,:c5,:r,:c4,:g4,:c5,:f5,:bb5,:ab5,:g5,:ab5,:g5,:f5,:c6,:g5,:g5,\
       :bb5,:f5,:e5,:f5,:e5,:d5,:e5,:r]
  d1f=[q,sq,sq,q,q,m,sq,c,c,c,c,cd,q,dsq/2,dsq/2,dsq+sq+q,c,m,cd,q,m,md,dsq/2,dsq/2,dsq+sq,q,b+q,q+c]
  
  n2=[:c4,:g4,:c5,:d5,:bb4,:c5,:g4,:g4,:bb4,:g4,:c5,:c4,:r]
  d2=[m,c,m,m,c,m,cd,q,md,c,b,md,c+c]
  n2b=[:r,:c4,:g4,:c4,:c4,:c4,:c4,:g4,:c5,:c5,:d5,:e5,:e5,:f5,:e5,:d5,:c5,:g4,:g4,:g4,:g4,:g4,:d5,:c5,:g4,:g4,:g4,:c4,\
       :r,:r,:c5,:g4,:c4,:c4,:c4]
  d2b=[c,cd,q,qd,dsq,dsq,q,q,c,sq,sq,q,dsq/2,dsq/2,dsq+sq,sq,sq,m,qd,dsq,dsq,q,q,q,q,qd,sq,m,q,    c,cd,q,qd,dsq,dsq]
  n2c=[:c4,:g4,:c5,:c5,:d5,:e5,:e5,:f5,:e5,:d5,:c5,:d5,:g4,:g4,:c5,:g4,:c5,:d5,:e5,:g4,:g4,:g3,:g3,:c4,:r,:g4]
  d2c=[q,q,c,sq,sq,q,dsq/2,dsq/2,dsq+sq,sq,sq,qd,sq,c,sq,sq,sq,sq,q,q,c,qd,sq,c,q,q]
  n2d=[:c4,:c4,:g3,:g3,:g3,:g3,:c4,:c4,:c4,:c4,:e4,:g4,:c5,:g4,:g4,:g4,:g4,:r,:g4]
  d2d=[cd,q,cd,q,cd,q,cd,q,cd,q,c,q,q,cd,sqd,dsq,cd,sq,q]
  n2e=[:c4,:e4,:c4,:g3,:g3,:g3,:g4,:e4,:c4,:c4,:c4,:g3,:c4,:e4,:g4,:c5,:g4,:g4,:g4]
  d2e=[c,qd,sq,cd,q,c,qd,sq,cd,q,q,q,q,q,qd,sq,q,sq,sq]
  n2f=[:c5,:e4,:g4,:g4,:c4,:r,:c4,:g4,:c5,:d5,:bb4,:c5,:g4,:g4,:bb4,:g4,:c5,:c4,:c4,:c4,:c4,:r]
  d2f=[q,q,q,q,m,sq,m,c,m,m,c,m,cd,q,md,c,md,qd,dsq,dsq,m+q,q+c]
  
  
  use_transpose -2
  in_thread do
    plarray(n1,d1,-0.5)
  end
  plarray(n2,d2,0.5)
  in_thread do
    plarray(n1b,d1b,-0.5)
  end
  plarray(n2b,d2b,0.5)
  in_thread do
    plarray(n1c,d1c,-0.5)
  end
  plarray(n2c,d2c,0.5)
  in_thread do
    plarray(n1d,d1d,-0.5)
  end
  plarray(n2d,d2d,0.5)
  in_thread do
    plarray(n1e,d1e,-0.5)
  end
  plarray(n2e,d2e,0.5)
  in_thread do
    plarray(n1f,d1f,-0.5)
  end
  plarray(n2f,d2f,0.5)
  
end #reverb

permalink

################# Sonic Jam ####################
# ( artficial musical intelligence unleashed )
# ( music is in the ear of the beholder )
mn=[:r];for jn in 1..1023
  mn[jn]=(hz_to_midi((440/81.0)*jn))
end; n1=[]; n2=[]; n3=[]

m=[48,54,64,72,81,96,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

synth :dsaw, note: mn[27],sustain: 1000, detune: 0.1, amp: 0.05
synth :dsaw, note: mn[27]+7,sustain: 1000, detune: 0.1, amp: 0.05

use_random_seed 3
live_loop :i1 do
  for x in 0..7; nr=mn[choose(m)]
    if rand_i(9)==2 then n1[x]=nr; end
    synth :pluck, note: n1[x]
sleep 0.25; end; end

use_random_seed 5
live_loop :i2 do
  for x in 0..7; nr=mn[choose(m)]
    if rand_i(9)==5 then n2[x]=nr; end
    synth :pretty_bell, note: n2[x], release: 0.1
sleep 0.25; end; end

use_random_seed 7
live_loop :i3 do
  for x in 0..7; nr=mn[choose(m)]
    if rand_i(9)==6 then n3[x]=nr; end
    synth :fm, note: n3[x]-12
sleep 0.25; end; end

permalink

play 72
sleep 1
play 48
sleep 1

permalink

live_loop :flibble do
  sample :ambi_choir, rate: 0.3
  sample :bd_haus, rate: 1
  use_synth :saw
  sleep 0.25
end

permalink

with_fx :reverb, mix: 0.2 do
  loop do
    play scale(:Eb2, :major_pentatonic, num_octaves: 3).choose, release: 0.1, amp: rand
    sleep 0.1
  end
end

permalink

# https://gist.github.com/darinwilson/a3e5909db339838a67fe

#########################################
## Sonic Pi Drum Machine
## coded by Darin Wilson
##

use_bpm 128
use_debug false

def context

  in_thread {

    live_loop :hat do
      sleep 1 if one_in(2)
      sample :drum_bass_hard
      sleep 1
    end

    live_loop :kick do
      sample :bd_fat
      sleep 4
    end

    live_loop :snare do
      sleep 2
      sample :elec_mid_snare
      sleep 2
    end

    live_loop :snare2 do
      sleep 1
      sample :elec_hollow_kick
    end

    live_loop :drums do
      with_fx :distortion, amp: 1 do
        with_fx :wobble, phase: 16 do
          sample :loop_breakbeat, beat_stretch: 32
        end
      end
      sleep 32
    end
    with_fx :pitch_shift, pitch: -3 do
      live_loop :yo2 do
        with_fx :echo, decay: 1, mix: 0.025 do
          with_fx :ring_mod, freq: rand(8..32) do
            sample :elec_beep, rate: rrand(1,5), amp: 0.5#, pan: -1
            sleep [0.5, 0.25].choose * 2
          end
        end
      end
      live_loop :yo3 do
        with_fx :echo, decay: 1, mix: 0.025 do
          with_fx :ring_mod, freq: rand(8..32) do
            use_random_seed rrand_i(0, 10000)
            sample :elec_twip, rate: rrand(1, 3), amp: 0.5#, pan: 1
            sleep [0.5, 0.25].choose * 2
          end
        end
      end
    end
  }

end

context

# src: https://gist.github.com/xavriley/cdacb93dc127eafd1f16

# Playing ASCII drum tabs with Sonic Pi

# Ruby is an awesome language for String manipulation.
# Lets use that fact to play some drums!

# Tab for the Amen break taken from Wikipedia
# http://en.wikipedia.org/wiki/Amen_break

# Note that %Q{ ... } is just another way 
# to define a string in Ruby
amen_tab = %Q{
C |----------------|----------------|----------------|----------x-----|
R |x-x-x-x-x-x-x-x-|x-x-x-x-x-x-x-x-|x-x-x-x-x-x-x-x-|x-x-x-x-x---x-x-|
S |----o--o-o--o--o|----o--o-o--o--o|----o--o-o----o-|-o--o--o-o----o-|
B |o-o-------oo----|o-o-------oo----|o-o-------o-----|--oo------o-----|
}

# This is a random tab for the drum intro to "Cold Sweat" by James Brown
cold_sweat_tab = %Q{
C |----------o-----|----------o-----|----------o-----|----------o-----|
hh|x---x---x---x---|x---x---x---x---|x---x---x---x---|x---x---x---x---|
S |----o--g------o-|-o--o--g----o---|----o--g------o-|-o--o--g----o---|
B |o---------o-----|--oo----o-o-----|o---------o-----|--oo----o-o-----|
  |1e+a2e+a3e+a4e+a|1e+a2e+a3e+a4e+a|1e+a2e+a3e+a4e+a|1e+a2e+a3e+a4e+a|
}

# use_bpm 220 # 192.0 # tempo of the sampled break

# use_sample_bpm :loop_amen_full
# use_bpm_mul 1*16

def main_59857418(tab, repeat, delay=0)

  sleep delay

  # reduce to just essential characters
  # in this case 'x', 'o', 'g', - (hyphen) and line break
  drum_lines = tab.strip.gsub!(/[^\-xog\n]/, '')

  tab = drum_lines.split(/\n+/).map {|line|
    line.chars.map do |c|
      1 if (c == 'x' || c == 'o' || c == 'g')
    end
  }

  # We've turned our text into an array of arrays
  tab.each {|row| puts row }

  define :beat do |crash, ride, snare, bass|
    sample :drum_splash_hard   if crash
    sample :drum_cymbal_closed if ride
    sample :elec_snare         if snare
    sample :drum_heavy_kick    if bass
  end

  # We get the number of beats
  tab_length = tab.first.length

  in_thread do

    #sleep 4

    (1..repeat).each { |x|
      tab_length.times.with_index do |i|

        # the '*' here is called a splat!
        # it means we can call beat(0, 1, 0, 1)
        # instead of beat([0, 1, 0, 1])
        with_fx :distortion, distort: 0.5, pre_amp: 2, amp: 0.75 do
          beat *tab.map {|row| row[i] }
        end

        sleep 0.25 * 2
      
        # Make it sound like a bad drummer
        # sleep rrand(0.15, 0.35)
      end
    }
  end

  # Uncomment this to see how we did
  in_thread do
    #sleep 4
    (1..repeat).each { |x|
      with_fx :distortion, distort: 0.5, pre_amp: 2, amp: 0.5 do
        with_fx :pitch_shift, pitch: 0 do
          # sample :loop_amen_full, beat_stretch: 32
        end
      end
      sleep 32
    }
  end

end

in_thread do
  main_59857418(amen_tab, 4, 16)
  main_59857418(cold_sweat_tab, 4, 16*4)
end

arr = {'HH' => [], 'BD' => [], 'SD' => [], 'CC' => []}

arr['HH'][0] = [5, 0, 5, 0,  5, 0, 5, 0,  5, 0, 5, 0,  5, 0, 5, 0]
arr['BD'][0] = [9, 0, 9, 0,  0, 0, 0, 0,  9, 0, 0, 3,  0, 0, 0, 0]
arr['SD'][0] = [0, 0, 0, 0,  9, 0, 0, 2,  0, 1, 0, 0,  9, 0, 0, 1]
arr['CC'][0] = [2, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0]

i = 0

pat = (0..7).to_a * 2

# in_thread(name: :drum_machine) do
# live_loop :drum_machine do

#   # choose your kit here:
#   i = i + 1
#   case i
#     when -> (n) { (0..16).to_a.include?(n)  } then use_kit :toy
#     when -> (n) { (17..32).to_a.include?(n) } then use_kit i % 2 == 0 ? :electro : :toy 
#     else use_kit [:acoustic, :electro, :toy].choose
#   end

#   # program your pattern here - each item in the list represents 1/4 of a beat
#   # for each item, enter a number between 0 and 9 (0=silent,9=loudest)
#   with_fx :distortion, distort: 0.9, pre_amp: 2, amp: 1 do
#     hat          arr['HH'][0] * 4  + pat.sort # + arr['HH'][0] + arr['HH'][0] + arr['HH'][0].shuffle + (0..7).to_a
#   end
#   with_fx :bitcrusher, bits: 8, sample_rate: 128*rand(0..16).round do
#     kick         arr['BD'][0] + arr['BD'][0].shuffle + arr['BD'][0] + arr['BD'][0].shuffle + (0..7).to_a * 2
#   end
#   snare        arr['SD'][0] + arr['SD'][0].shuffle + arr['SD'][0] + arr['SD'][0].shuffle + pat.sort
#   crash_cymbal arr['CC'][0] + [0]*(16*3) + pat.sort

#   sleep 1

# end

##################################################################
##
## The gory details - you don't need to change anything down here,
## unless you're curious :)
##

drum_kits = {
  acoustic: {
    hat:   :drum_cymbal_closed,
    kick:  :drum_bass_hard,
    snare: :drum_snare_hard,
    crash_cymbal: :elec_cymbal
  },
  acoustic_soft: {
    hat:   :drum_cymbal_closed,
    kick:  :drum_bass_soft,
    snare: :drum_snare_soft,
    crash_cymbal: :elec_cymbal
  },
  electro: {
    hat:   :elec_wood,
    kick:  :elec_soft_kick,
    snare: :elec_hi_snare,
    crash_cymbal: :elec_cymbal
  },
  toy: {
    hat:   :elec_blup,
    kick:  :elec_hollow_kick,
    snare: :elec_pop,
    crash_cymbal: :elec_cymbal
  }
}

current_drum_kit = drum_kits[:acoustic]

define :use_kit do |kit_name|
  current_drum_kit = drum_kits[kit_name]
end

live_loop :pulse do
  sleep 4
end

define :run_pattern do |name, pattern|
  in_thread(name: name) {
    sync :pulse
    pattern.each do |p|
      sample current_drum_kit[name], amp: p/9.0
      sleep 0.25
    end
  }
end

define :hat do |pattern|
  run_pattern :hat, pattern
end

define :kick do |pattern|
  run_pattern :kick, pattern
end

define :snare do |pattern|
  with_fx :pitch_shift, pitch: rand(-12..12) do
    run_pattern :snare, pattern
  end
end

define :crash_cymbal do |pattern|
  run_pattern :crash_cymbal, pattern
end

permalink

define :drums do |level, b_level=1, rand_cf=false|
  synth :fm, note: :e2, release: 0.1, amp: b_level * 3, cutoff: 130
  co = rand_cf ? rrand(110, 130) : 130
  a  = rand_cf ? rrand(0.3, 0.5) : 0.6
  n  = rand_cf ? :bnoise         : :noise
  synth :noise, release: 0.05, cutoff: co, res: 0.95, amp: a if level > 0
  sample :elec_blip, amp: 2, rate: 2, pan: rrand(-0.8, 0.8) if level > 1
  sleep 1
end

define :synths do |s_name, co, n=:e2|
  use_synth s_name
  use_transpose 0
  use_synth_defaults detune: [12,24].choose, amp: 1, cutoff: 100, pulse_width: 0.12, attack: rrand(0.2, 0.5), release: 0.5 ,  mod_phase: 0.25, mod_invert_wave: 1
  
  play :e1, mod_range: [7, 12].choose, pan: rrand(-1, 1)
  sleep 0.125
  play :e3, mod_range: [7, 12].choose, pan: rrand(-1, 1)
  sleep [0.25, 0.5].choose
  play n, mod_range: 12, pan: rrand(-1, 1)
  sleep 0.5
  play chord(:e2, :minor).choose, mod_range: 12, pan: rrand(-1, 1)
  sleep 0.25
end

in_thread name: :synths do
  use_random_seed 0
  level = -1
  with_fx :echo do |e|
    50.times do |i|
      puts i
      drums level, 0.8
      sleep 1
      sync :foo
    end
  end
end

in_thread name: :synths1 do
  with_fx :reverb do |r|
    with_fx :echo, phase: 0.25 do |e|
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :a2)
    end
  end
end

permalink

loop do
    s = synth [:bnoise, :cnoise, :gnoise].choose, amp: rrand(0.5, 1.5), attack: rrand(0, 4), sustain: rrand(0, 2), release: rrand(1, 3), cutoff_slide: rrand(0, 3), cutoff: rrand(60, 80), pan: rrand(-1, 1), pan_slide: 1, amp: rrand(0.5, 1)
    control s, pan: rrand(-1, 1), cutoff: rrand(60, 115)
    sleep rrand(2, 3)
  end

permalink

use_bpm 75

live_loop :pad do
  sync :kick
  with_fx :lpf, cutoff: 40 do
    use_synth :zawa
    play chord(:A3, :major7), attack: 2, sustain: 4, release: 2
    sleep 8
    play chord(:B3, :minor7), attack: 2, sustain: 4, release: 2
    sleep 8
  end
end

permalink

# Welcome to Sonic Pi
define :synths do |s_name, co, n=:e2|
  use_synth s_name
  use_transpose 0
  use_synth_defaults detune: [12,24].choose, amp: 1, cutoff: 100, pulse_width: 0.12, attack: rrand(0.2, 0.5), release: 0.5 ,  mod_phase: 0.25, mod_invert_wave: 1
  
  play :e1, mod_range: [7, 12].choose, pan: rrand(-1, 1)
  sleep 0.125
  play :e3, mod_range: [7, 12].choose, pan: rrand(-1, 1)
  sleep [0.25, 0.5].choose
  play n, mod_range: 12, pan: rrand(-1, 1)
  sleep 0.5
  play chord(:e2, :minor).choose, mod_range: 12, pan: rrand(-1, 1)
  sleep 0.25
end

synths(:mod_saw, 100, :e3) # Modify
synths(:mod_dsaw, 100, :e5) # Add
synths(:mod_dsaw, 100, chord(:e5, :minor)) # Delete
synths(:mod_saw, 120, :e1) # Rename
synths(:mod_saw, 100, :a2) # Move

permalink

plqy 66

permalink

play 50

permalink

# rand-seed-ver 32
#
# Coded by Sam Aaron
#
# Video: https://vimeo.com/110416910

use_debug false
load_samples [:bd_haus, :elec_blip, :ambi_lunar_land]

define :ocean do |num, amp_mul=1|
  num.times do
    s = synth [:bnoise, :cnoise, :gnoise].choose, amp: rrand(0.5, 1.5) * amp_mul, attack: rrand(0, 1), sustain: rrand(0, 2), release: rrand(0, 5) + 0.5, cutoff_slide: rrand(0, 5), cutoff: rrand(60, 100), pan: rrand(-1, 1), pan_slide: 1
    control s, pan: rrand(-1, 1), cutoff: rrand(60, 110)
    sleep rrand(0.5, 4)
  end
end

define :echoes do |num, tonics, co=100, res=0.9, amp=1|
  num.times do
    play chord(tonics.choose, :minor).choose, res: res, cutoff: rrand(co - 20, co + 20), amp: 0.5 * amp, attack: 0, release: rrand(0.5, 1.5), pan: rrand(-0.7, 0.7)
    sleep [0.25, 0.5, 0.5, 0.5, 1, 1].choose
  end
end

define :bd do
  cue :in_relentless_cycles
  16.times do
    sample :bd_haus, amp: 4, cutoff: 120
    sleep 0.5
  end
  cue :winding_everywhichway
  2.times do
    2.times do
      sample :bd_haus, amp: 4, cutoff: 120
      sleep 0.25
    end
    sample :ambi_lunar_land
    sleep 0.25
  end
end

define :drums do |level, b_level=1, rand_cf=false|
  synth :fm, note: :e2, release: 0.1, amp: b_level * 3, cutoff: 130
  co = rand_cf ? rrand(110, 130) : 130
  a  = rand_cf ? rrand(0.3, 0.5) : 0.6
  n  = rand_cf ? :bnoise         : :noise
  synth :noise, release: 0.05, cutoff: co, res: 0.95, amp: a if level > 0
  sample :elec_blip, amp: 2, rate: 2, pan: rrand(-0.8, 0.8) if level > 1
  sleep 1
end

define :synths do |s_name, co, n=:e2|
  use_synth s_name
  use_transpose 0
  use_synth_defaults detune: [12,24].choose, amp: 1, pan: lambda{rrand(-1, 1)}, cutoff: co, pulse_width: 0.12, attack: rrand(0.2, 0.5), release: 0.5 ,  mod_phase: 0.25, mod_invert_wave: 1

  play :e1, mod_range: [7, 12].choose
  sleep 0.125

  play :e3, mod_range: [7, 12].choose
  sleep [0.25, 0.5].choose

  play n, mod_range: 12
  sleep 0.5

  play chord(:e2, :minor).choose, mod_range: 12
  sleep 0.25
end

define :play_synths do
  with_fx :reverb do |r|
    with_fx :echo, phase: 0.25 do |e|
      synths = [:mod_pulse, :mod_saw, :mod_dsaw, :mod_dsaw, :mod_dsaw, :mod_dsaw]
      cutoffs = [108, 78, 88, 98]
      synth = synths.rotate!.first
      4.times do |t|
        puts shuffle("0" * (30 - t) + ("1" * t)) unless t == 0
        co = cutoffs.rotate!.first + (t * 2)
        7.times do
          n = chord([:e2, :e3, :e4, :e5][t], :minor).choose
          synths(synth, co, n)
        end
        sleep 2
      end
      sleep 1
      cue :within
    end
  end
end

define :binary_celebration do |n=1, st=1|
  in_thread do
    n.times do
      puts (0..30).map{|_| ["0", "1"].choose}.join
      sleep st
    end
  end
end

puts 'Introduction'
puts 'The Curved Ebb of Carpentry'
sleep 2

cue :oceans
at [7, 12], [:crash, :within_oceans] do |m|
  cue m
end

uncomment do
  use_random_seed 1000
  with_bpm 45 do
    with_fx :reverb do
      with_fx(:echo, delay: 0.5, decay: 4) do
        in_thread do
          use_random_seed 2
          ocean 5
          ocean 1, 0.5
          ocean 1, 0.25
        end
        sleep 10
        use_random_seed 1200
        echoes(5, [:b1, :b2, :e1, :e2, :b3, :e3])
        cue :a_distant_object
        echoes(5, [:b1, :e1, :e2, :e3])
        cue :breathes_time
        in_thread do
          echoes(5, [:e1, :e2, :e3])
        end
        use_synth :tb303
        echoes(1, [:e1, :e2, :e3], 60, 0.9, 0.5)
        echoes(1, [:e1, :e2, :e3], 62)
        echoes(1, [:e1, :e2, :e3], 64, 0.97)
        echoes(1, [:e1, :e2, :e3], 66)
        echoes(1, [:e1, :e2, :e3], 68)
        cue :liminality_holds_fast
        echoes(4, [:b1, :e1, :e2, :b3, :e3], 80)
        echoes(1, [:b1, :b2, :e1, :e2, :b3, :e3], 85,  0.98)
        cue :within_reach
        echoes(5, [:e1, :b2], 90)
        cue :as_it_unfolds
        in_thread do
          echoes(5, [:e1], 90)
        end
      end
    end
  end
end


in_thread(name: :bassdrums) do
  use_random_seed 0
  sleep 22
  3.times do
    bd
  end
  sleep 28
  live_loop :bd do
    bd
  end
end

in_thread(name: :drums) do
  use_random_seed 0
  level = -1
  with_fx :echo do |e|
    sleep 2
    drums -1, 0.1
    drums -1, 0.2
    drums -1, 0.4
    drums -1, 0.7
    puts "Part 2"
    puts "Inside the Machine"
    3.times do
      8.times do
        drums level, 0.8
      end
      6.times do
        drums(level)
      end

      sleep 1
      level += 1
    end
    sleep 4
    cue :dreams
    8.times do
      drums 1, 1, true
    end

    10.times do
      m = choose [shuffle(:within_dreams), :within_dreams, :dreams_within]
      cue m
      drums 2, 1, true
    end

    6.times do
      m = choose [shuffle("within") + "_dreams", :within_dreams.shuffle, "dreams_" + shuffle("within")]
      cue m
      drums 2
    end

    live_loop :drums do
      8.times do |i|
        drums 1
      end

      16.times do |i|
        cue " " * rand_i(32)
        at 1 do
          cue "  " * i
        end
        drums 2
      end
    end
  end
end

in_thread name: :synths do
  use_random_seed 0
  sleep 12
  cue :the_flow_of_logic
  play_synths
end

in_thread do
  use_random_seed 0
  sync :within
  puts "Part 3"
  puts "Reality A"
  sleep 12
  use_synth_defaults phase: 0.5, res: 0.5, cutoff: 80, release: 3.3, wave: 1

  2.times do
    [80, 90, 100, 110].each do |cf|
      use_merged_synth_defaults cutoff: cf
      puts "1" * 30
      synth :zawa, note: :e2, phase: 0.25
      synth :zawa, note: :a1
      sleep 3
    end
    4.times do |t|
      binary_celebration(6, 0.5)
      synth :zawa, note: :e2, phase: 0.25, res: rrand(0.8, 0.9), cutoff: [100, 105, 110, 115][t]
      sleep 3
    end
  end

  puts 'Part n'
  puts 'The Observer becomes the Observed'
  # Your turn...
end

permalink

use_bpm 192

live_loop :flibble do
  sample :ambi_choir, rate: 0.3
  sample :bd_haus, rate: 1
  use_synth :saw
  sleep 0.25
end

t = [1, 2, 3, 4, 4, 3, 2, 2]
x = 0
live_loop:a do
  x = 0 if x == 8
  sample :bass_voxy_hit_c, amp:0.25 , rate:t[x], beat_stretch:t[x+(x-1)]
  x+=1
  sleep 0.25
end

sleep 4

8.times do
  sample :bd_klub, amp:1
  sleep 0.5
  sample :bd_klub, amp:1
  sleep 0.25
  sample :bd_klub, amp:1
  sleep 0.75
end

sleep 2

elec = [1,5,3,2,2,6,1,4,3]
live_loop:c do
  sample :elec_blip, rate:elec[x]
  sleep 0.25
end

sleep 4
live_loop:d do
  sample :elec_soft_kick, amp:2
  sleep 0.5
  sample :elec_soft_kick, amp:2, rate:5
  sleep 0.5
  sample :elec_soft_kick, amp:2
  sleep 0.25
  sample :elec_soft_kick, amp:2
  sleep 0.25
  sample :elec_soft_kick, amp:2, rate:5
  sleep 0.5
end

8.times do
  
  live_loop :breakbeat do
    sample :loop_breakbeat, beat_stretch: 4, amp: 4, start: 0, finish: 0.25; sleep 1;
    sample :loop_breakbeat, beat_stretch: 4, amp: 4, start: 0.25, finish: 0.5; sleep 1;
    sample :loop_breakbeat, beat_stretch: 4, amp: 4, start: 0.5, finish: 0.75; sleep 1;
    sample :loop_breakbeat, beat_stretch: 4, amp: 4, start: 0.75, finish: 1; sleep 1;
  end
  
  live_loop :breakbeat2 do
    with_fx :slicer, phase: 1.75, wave: 0, mix: 1.0 do
      sample :loop_garzul, beat_stretch: 16, amp: 1
      sleep 16
    end
  end
  
  live_loop :test do
    with_fx :distortion, amp: 0.5, distort: 0.9 do
      with_fx :bitcrusher, bits: 8, sample_rate: 12800 do;
        sample :loop_amen, amp: 4, beat_stretch: 4, start: 0, finish: 0.375;     sleep 1.5;
        sample :loop_amen, amp: 4, beat_stretch: 4, start: 0, finish: 0.375;     sleep 1.5;
        sample :loop_amen, amp: 4, beat_stretch: 4, start: 0.375, finish: 0.625; sleep 1;
        sample :loop_amen, amp: 4, beat_stretch: 4, start: 0, finish: 1;         sleep 4;
      end
    end
  end
  
  live_loop :kick do
    sample :bd_haus
    sleep 1.5;
    sample :bd_haus
    sleep 2.5;
  end
  
  live_loop :pulputus do
    with_fx :reverb, amp: 5, mix: 0.25 do
      use_synth :tb303
      nuotti = (chord :C2, :minor).choose
      play nuotti, cutoff: rrand(10, 130), release: 0.1, amp: 0.7, release: 0.1
      sleep 0.25
    end
  end
  
  live_loop :test do
    use_synth :pulse
    play 68,  release: 0.2; sleep 0.33;
    play 78,  release: 0.2; sleep 0.33;
    play 75, attack: 0.2, sustain: 0.1,  release: 0.2; sleep 0.34;
  end
  
end

permalink

define :drums do |level, b_level=1, rand_cf=false|
  synth :fm, note: :e2, release: 0.1, amp: b_level * 3, cutoff: 130
  co = rand_cf ? rrand(110, 130) : 130
  a  = rand_cf ? rrand(0.3, 0.5) : 0.6
  n  = rand_cf ? :bnoise         : :noise
  synth :noise, release: 0.05, cutoff: co, res: 0.95, amp: a if level > 0
  sample :elec_blip, amp: 2, rate: 2, pan: rrand(-0.8, 0.8) if level > 1
  sleep 1
end

define :synths do |s_name, co, n=:e2|
  use_synth s_name
  use_transpose 0
  use_synth_defaults detune: [12,24].choose, amp: 1, cutoff: 100, pulse_width: 0.12, attack: rrand(0.2, 0.5), release: 0.5 ,  mod_phase: 0.25, mod_invert_wave: 1
  
  play :e1, mod_range: [7, 12].choose, pan: rrand(-1, 1)
  sleep 0.125
  play :e3, mod_range: [7, 12].choose, pan: rrand(-1, 1)
  sleep [0.25, 0.5].choose
  play n, mod_range: 12, pan: rrand(-1, 1)
  sleep 0.5
  play chord(:e2, :minor).choose, mod_range: 12, pan: rrand(-1, 1)
  sleep 0.25
end

in_thread name: :synths do
  use_random_seed 0
  level = -1
  with_fx :echo do |e|
    50.times do |i|
      puts i
      drums level, 0.8
      sleep 1
      sync :foo
    end
  end
end

in_thread name: :synths1 do
  with_fx :reverb do |r|
    with_fx :echo, phase: 0.25 do |e|
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :a2)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :a2)
      synths(:mod_dsaw, 60, chord(:e5, :minor))
      synths(:mod_dsaw, 60, :e5)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, chord(:e5, :minor))
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, chord(:e5, :minor))
      synths(:mod_dsaw, 60, :e5)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, chord(:e5, :minor))
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, :e5)
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, chord(:e5, :minor))
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_dsaw, 60, chord(:e5, :minor))
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
      cue :foo
      synths(:mod_saw, 60, :e3)
    end
  end
end

permalink

live_loop :drum_chop do
  with_fx :reverb do |fx|
    number_of_slices = 8
    random_slice = rand_i(number_of_slices)
    length_of_slice = 1.0/number_of_slices
    
    if random_slice.even? then
      control fx, mix: 0.55
    else
      control fx, mix: 0.3
    end
    
    s = length_of_slice * random_slice
    f = s + length_of_slice
    
    sample :loop_amen, start: s, finish: f
    sleep  (sample_duration :loop_amen) * length_of_slice
  end
end

live_loop :andy do
  sync :drum_chop
  use_synth :fm
  use_synth_defaults depth: rrand(0,2), pan: rrand(-1,1)
  notes = (chord :d2, :m9, num_octaves: 1).shuffle
  play notes.tick, release: 0.28
end

permalink

define :pl do |s,p=0|
  sample s,pan: p
  sleep 1
end
define :chos do
  return [:bd_haus,:bd_sone,:bd_klub,:drum_snare_soft,:drum_cymbal_closed].choose
end
live_loop :stutter do
  use_bpm 120
  s=chos
  pl(chos,-1)
  density ([2,3,4].choose) do
    sample chos,pan: -1
    sleep 1
  end  
  density ([2,3,4].choose) do
    sample chos,pan: -1
    sleep 1
  end  
  pl(chos,-1)
end

live_loop :stutter2 do
  use_bpm 120
  s=chos
  pl(s,1)
  pl(s,1)
  density ([2,3,4].choose) do
    sample chos,pan: 1
    sleep 1
  end
  density ([2,3,4].choose) do
    sample chos,pan: 1
    sleep 1
  end

permalink

play 50
sleep 1
play 60 
sleep 2
play 70

permalink

live_loop :flibble do
  sample :ambi_choir, rate: 0.3
  sample :bd_haus, rate: 1
  sleep 1
end

permalink