from scamp import *
from random import *
from math import *

listRhythm= [0.17, 0.5, 0.17, 0.17, 0.5, 0.17, 1.5, 0.17, 0.33, 1.67, 1.67, 0.5, 0.5, 2.17, 3.17, 5.33, 2.0, 2.83, 1.0, 6.67, 2.17]
listMelody=[65,   64,  65,   64,   62,  64,   65,  64,   65,   64,   65,   60,  62,  57,   58,   60,   62,  64,   65,  67,   69]
staccato,staccatissimo,marcato,tenuto,accent ="staccato", "staccatissimo", "marcato" , "tenuto", "accent"

s = Session()  # or canleave blank and add later ...
cello =s.new_part("Cello")
brass = s.new_part("brass")
organ = s.new_part("Organ 1")

#-------------setting differnt tempos--sound intersting
def playSustain(inst, note, dur, levels):
    newLevels = [val*0.35 for val in levels]
    curves = [uniform(-1,2) for i in range(len(levels)-1)]
    durList =[dur/(len(levels)-1) for i in curves]
    vol =Envelope.from_levels_and_durations(newLevels, durList, curves)
    if random() < 0.4:    listChord =[note, note+3, note+6, note+9, note+13]
    elif random() < 0.8 : listChord =[note, note+7, note+11, note+14, note+17]
    else:                 listChord =[note, note+5, note+7, note+14]
    inst.play_chord(listChord , vol,dur*3)
    wait(0.1)

def playInstrument(inst, boolFp,interval, whenNotToPlay, listMelody,listRhythm ):
    articulations = [staccato,staccatissimo,marcato,accent] 
    list_fp_cres, list_articulation = [],[] 
    levels = [round((uniform(0.05, 0.9)),2) for i in range(0,randint(2,3))]
    curves =[float(randint(-1,2)) for i in range (len(levels)-1)]
    counter =0
    for pitch, rhythm in zip(listMelody,listRhythm ):
        if counter%whenNotToPlay==0 and counter >0:wait(rhythm)
        else:
            if boolFp :
                avTime = rhythm/(len(levels)-1) #(len(levels)-1)
                dur = [avTime]*(len(levels)-1)
                fp_cres = Envelope.from_levels_and_durations(levels, dur, curves)
                #if inst ==cello:
                    #if random() >0.65: fork(playSustain, args =[organ, pitch, rhythm, fp_cres.levels]) #playSustain(organ, pitch, rhythm, fp_cres.levels) #fork(playSustain, args =[organ, pitch, rhythm, fp_cres.levels])
                if random() <=0.35:  inst.play_note(pitch+interval , fp_cres,rhythm, staccatissimo)   #,blocking =False
                else:                inst.play_note(pitch+interval , fp_cres,rhythm)
            else:
                fp_cres=choice([0.25,0.5,0.75])
                if random() <=0.35:  inst.play_note(pitch+interval , fp_cres,rhythm, staccatissimo)   #,blocking =False
                else:                inst.play_note(pitch+interval , fp_cres,rhythm)
        counter +=1
    wait(0.1)
    
cello_whenNotToPlay,organ_whenNotToPlay, brass_whenNotToPlay = randint(2,3) ,randint(4,10) ,randint(3,8)        
cello_interval,brass_interval = 0,-12

playInstrument(cello, True,  cello_interval, cello_whenNotToPlay, listMelody,listRhythm )
wait(0.3)
s.set_tempo_targets(tempo_targets=[40,180,100] , durations=[10,10,10], curve_shapes = [-1,-1,-1], )  
fork(playInstrument,                     args = [cello, True,  cello_interval, cello_whenNotToPlay, listMelody,listRhythm ])
fork(playInstrument,                     args = [organ, True,  cello_interval, organ_whenNotToPlay, listMelody,listRhythm ])
fork(playInstrument, initial_tempo = 40, args = [brass, True, brass_interval, brass_whenNotToPlay, listMelody,listRhythm ])
s.wait_for_children_to_finish()
wait(0.5)
s.tempo =60
playInstrument(cello, True,  cello_interval, cello_whenNotToPlay, listMelody,listRhythm )
wait(0.3)
finalNote = listMelody[0]
fp_cres = Envelope.from_levels_and_durations([0.001,1], [3], ["exp** 2/5"])
cello.play_note(finalNote,    fp_cres, [6], blocking =False)
brass.play_note(finalNote-12, fp_cres, [6], blocking =False)
organ.play_chord([finalNote,finalNote + 3, finalNote + 6, finalNote + 9,finalNote + 13], fp_cres, [6]) 
print("*************DONE*****************************")