# Breath Piece - Tramway 2018 - Rossana Irvine
# Composer/Sound Artist - Tim Sayer
# Version 12
###################################################################################################

# set user to paths change either tim, rosanna or kian
user = "tim"

# change section, 0 - 6 with 6 being a faded end
currentSec = 0

# number of audio channels
channelNum = 2

# duration for each section
# section 0-25m silence, 1-9m section1, 2-12m silence, 3-5m section2, 4-11m section3, 5-3m section4, 6-END
duration = [1500,540,720,300,660,360,60]   # FOR PERFORMANCE
duration = [10,10,10,10,10,10,10]   # FOR TESTING

# number of layers for each sections
layers = [0,530,0,290,660,360,0] # FOR PERFORMANCE
#layers = [10,10,10,10,10,10,10] # FOR TESTING

# how low (slowed down) the sound may go in each section
#low = [0,-1,0,0.8,-0.8,-0.8,0] #causes distortion
low = [0,-1,0,0.8,0.1,0.5,0]

# how high (sped up) the sound may go in each section
high = [0,1.3,0,1.2,1.2,1,0]

# low gain threshold for those sections that use it
lowGain = [0,0.1,0,0.1,0.1,0.1,0]

# high gain threshold for those sections that use it
highGain = [0,5,0,5,8,5,0]

# if true then kick drum punctuation will be sounded between sections
playKick = false

# probability list for playing a new sample, lower the number the lower the probability
prob = [0,5,0,100,100,100,0]

# volume weightings for each section
vol = [0,0.01,0,0.01,0.03,0.01,0]

# folders containing sound files - note the one for smp0 and smp2 isn't used but needs to be there
if (user == "tim")
  path = "/Users/sayetim/Desktop/RISounds/"
end
if (user == "rosanna")
  path = "/Users/rosannairvine/Desktop/BreathPieceV10/"
end
if (user == "kian")
  path = "/Users/kianmcevoy/Desktop/BreathPieceV10/"
end

smp0 = path + "section01/"
smp1 = path + "section01/"
smp2 = path + "section02/"
smp3 = path + "section02/"
smp4 = path + "section03/"
smp5 = path + "section04/"

num = [0,4,0,4,5,3,0] # number of files in each folder, remember the first value is always 0
###################################################################################################

use_bpm 60
smp = [smp0,smp1,smp2,smp3,smp4,smp5]
load_sample smp
#load and kill dummy sample to create a variable
s = sample smp[1]
kill s

soundSection = currentSec
loopCount = 0
counter = 0
t = Time.new
startTime = (t.hour*360)+(t.min*60)+t.sec
use_random_seed t.sec
whichChannel = rand_i(channelNum)+1
l = lowGain[soundSection]
h = highGain[soundSection]
r = rrand(low[soundSection],high[soundSection])
m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
st = rand/2
fn = rand(1-st)+st

in_thread do
  live_loop :l1 do
    l = lowGain[soundSection]
    h = highGain[soundSection]
    r = rrand(low[soundSection],high[soundSection])
    m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
    st = rand
    fn = rand(1-st)+st
    whichChannel = rand_i(channelNum)+1
    
    if (counter >= duration[currentSec])
      counter=0
      currentSec += 1
      loopCount = 0
      if (currentSec > duration.length-1)
        sample_free_all
        stop
      end
      if (playKick == true)
        sample :bd_boom, amp: 10
      end
    end
    counter +=1
    db
    case currentSec
    when 0
      puts "SILENCE - START"
    when 1
      soundSection=1
      sec1
    when 2
      #sample_free_all
      soundSection=2
      puts "SILENCE"
    when 3
      soundSection=3
      sec3
    when 4
      soundSection=4
      sec4
    when 5
      soundSection=5
      sec5
    when 6
      soundSection=6
      puts "SILENCE - END"
    end
    sleep 1
  end
end


define :sec1 do
  loopCount +=1
  with_fx :sound_out, output: whichChannel, amp: 0 do
    with_fx :reverb, room: 1 do
      m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
      if one_in(prob[soundSection])
        puts "NO SAMPLE"
      else
        s = sample smp[soundSection], rand_i(num[soundSection]), rate: rrand(low[soundSection],high[soundSection]), amp: m, attack: 2, release: 2, start: 0, finish: 1
      end
    end
  end
  if (loopCount >= layers[soundSection])
    puts "KILL 1"
    kill s
  end
end

define :sec3 do
  loopCount +=1
  if one_in(prob[soundSection])
    puts "NO SAMPLE"
  else
    with_fx :sound_out, output: whichChannel, amp: 0 do
      with_fx :reverb, room: 1 do
        whichChannel = rand_i(channelNum)+1
        m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
        r = rrand(low[soundSection],high[soundSection])
        s = sample smp[soundSection], rand_i(num[soundSection]), rate: 1, amp: m, attack: 2, release: 2, start: 0, finish: 1
        if (loopCount > duration[soundSection] * 0.4)
          whichChannel = rand_i(channelNum)+1
          m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
          r = rrand(low[soundSection],high[soundSection])
          sa = sample smp[soundSection], rand_i(num[soundSection]), rate: r, amp: m, attack: 2, release: 2, start: 0, finish: 1
        end
        if (loopCount > duration[soundSection] * 0.6)
          whichChannel = rand_i(channelNum)+1
          m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
          r = rrand(low[soundSection],high[soundSection])
          sb = sample smp[soundSection], rand_i(num[soundSection]), rate: -r, amp: m, attack: 2, release: 2, start: 0, finish: 1
        end
        if (loopCount > duration[soundSection] * 0.8)
          whichChannel = rand_i(channelNum)+1
          m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
          r = rrand(low[soundSection],high[soundSection])
          sc = sample smp[soundSection], rand_i(num[soundSection]), rate: -r, amp: m, attack: 2, release: 2, start: 0, finish: 1
        end
      end
    end
  end
  
  if (loopCount >= layers[soundSection])
    #puts "KILL 2"
    #kill s
  end
end

define :sec4 do
  loopCount +=1
  if one_in(prob[soundSection])
    puts "NO SAMPLE"
  else
    with_fx :sound_out, output: whichChannel, amp: 0 do
      with_fx :reverb, room: 1 do
        if (loopCount < duration[soundSection] * 0.6)
          whichChannel = rand_i(channelNum)+1
          st = rand
          fn = rand(1-st)+st
          m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
          r = rrand(low[soundSection],high[soundSection])
          sc = sample smp[soundSection], rand_i(num[soundSection]), rate: -r, amp: m, attack: 2, release: 2, start: st, finish: fn
        end
        if (loopCount < duration[soundSection] * 0.8)
          whichChannel = rand_i(channelNum)+1
          st = rand
          fn = rand(1-st)+st
          m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
          r = rrand(low[soundSection],high[soundSection])
          sb = sample smp[soundSection], rand_i(num[soundSection]), rate: -r, amp: m, attack: 2, release: 2, start: st, finish: fn
        end
        if (loopCount < duration[soundSection] * 0.9)
          whichChannel = rand_i(channelNum)+1
          st = rand
          fn = rand(1-st)+st
          m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
          r = rrand(low[soundSection],high[soundSection])
          sa = sample smp[soundSection], rand_i(num[soundSection]), rate: r, amp: m, attack: 2, release: 2, start: st, finish: fn
        end
        whichChannel = rand_i(channelNum)+1
        st = rand
        fn = rand(1-st)+st
        m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
        r = rrand(low[soundSection],high[soundSection])
        s = sample smp[soundSection], rand_i(num[soundSection]), rate: r, amp: m, attack: 2, release: 2, start: st, finish: fn
      end
      if (loopCount >= layers[soundSection])
        puts "KILL 3"
        kill s
      end
    end
  end
end

define :sec5 do
  loopCount +=1
  if one_in(prob[soundSection])
    puts "NO SAMPLE"
  else
    m = rrand((l * vol[soundSection]),(h * vol[soundSection]))
    with_fx :sound_out, output: whichChannel, amp: 0 do
      with_fx :reverb, room: 1 do
        s = sample smp[soundSection], rand_i(num[soundSection]), rate: r, amp: m, attack: 2, release: 2, start: 0, finish: 1
      end
      if (loopCount >= layers[soundSection])
        puts "KILL 4"
        kill s
      end
    end
  end
end

define :db do
  puts "currentSec: " + currentSec.to_s
  puts "soundSection " + soundSection.to_s
  puts "duration: " + duration[currentSec].to_s
  puts "countdown: " + (duration[currentSec]-counter).to_s
  puts "loopCount: " + loopCount.to_s
  puts "layers: " + layers[currentSec].to_s
  puts "vol: " + vol[currentSec].to_s
  puts "whichChannel " + whichChannel.to_s
end
