.fork question?

Hi is this code i’m trying to create a song in which all the instruments play random notes at the same time, what is wrong? Because they are sounding one febore the other.

from scamp import *
import random

s = Session(100)

piano = s.new_part("piano")
saxo = s.new_part("saxo")
organ = s.new_part("organ")
shakuhachi = s.new_part("shakuhachi")
timpani = s.new_part("timpani")
trumpet = s.new_part("trumpet")
trombone = s.new_part("trombone")
violin = s.new_part("violin")
while True:
        
    pitch = int(random.uniform(62, 100))
    volume = random.uniform(0.4, 1)
    length = random.uniform(1/7, 8)
    violin.play_note(pitch, volume, length)
    
    pitch = int(random.uniform(23, 118))
    volume = random.uniform(0.2, 1)
    length = random.uniform(1/7, 4)
    piano.play_note(pitch, volume, length, "staccato")
         
    pitch = int(random.uniform(50, 77))
    volume = random.uniform(0.2, 1)
    length = random.uniform(1/8, 7)
    saxo.play_note(pitch, volume, length)
           
    pitch = int(random.uniform(50, 77))
    volume = random.uniform(0.3, 1)
    length = random.uniform(1/8, 10)
    organ.play_note(pitch, volume, length)
        
    pitch = int(random.uniform(50, 77))
    volume = random.uniform(0.4, 1)
    length = random.uniform(1/8, 5)
    shakuhachi.play_note(pitch, volume, length)
        
    pitch = int(random.uniform(50, 77))
    volume = random.uniform(0.2, 1)
    length = random.uniform(1/8, 3)
    timpani.play_note(pitch, volume, length)
        
    pitch = int(random.uniform(50, 77))
    volume = random.uniform(0.2, 1)
    length = random.uniform(1/8, 8)
    trumpet.play_note(pitch, volume, length)
           
    pitch = int(random.uniform(50, 77))
    volume = random.uniform(0.2, 1)
    length = random.uniform(1/8, 6)
    trombone.play_note(pitch, volume, length)
    
s.fork(piano_part)
s.fork(saxo_part)
s.fork(organ_part)
s.fork(shakuhachi_part)
s.fork(timpani_part)
s.fork(trumpet_part)
s.fork(trombone_part)
s.fork(violin_part)```

I mean if You play the code it sounds like each instruments are sounding one before the other inside the loop but randomly, but I need that the .fork code helps to that all the instruments sound a the same time, I mean all in the same time. It is because of the while True that they can not play at the same time together?. Marc if you are there I appreciate you communication.

hi - I think you need to wrap the various play instructions in a single function, that takes pitch, volume, length and instrument as arguments)
then you can fork the function multiple times
(and put a wait_for_children_to_finish() at the end of the fork instructions, to stop all the functions playing at the same time (though it does sound pretty crazy when they do all play at once!)
try this:

from scamp import *
import random

s = Session(100)

piano = s.new_part("piano")
saxo = s.new_part("saxo")
organ = s.new_part("organ")
shakuhachi = s.new_part("shakuhachi")
timpani = s.new_part("timpani")
trumpet = s.new_part("trumpet")
trombone = s.new_part("trombone")
violin = s.new_part("violin")

def play_part(pitch_min, pitch_max, vol_min, vol_max, length_min, length_max, instrument):
    pitch = int(random.uniform(pitch_min, pitch_max))
    volume = random.uniform(vol_min, vol_max)
    length = random.uniform(length_min, length_max)
    instrument.play_note(pitch, volume, length)
    
while True:
    fork(play_part,(62, 100, 0.4, 1, 1/7, 8, violin))
    fork(play_part,(23, 118, 0.2, 1, 1/7, 4, piano))
    fork(play_part,(50, 77, 0.2, 1, 1/8, 7, saxo))
    fork(play_part,(50, 77, 0.3, 1, 1/8, 10, organ))
    fork(play_part,(50, 77, 0.4, 1, 1/8, 5, shakuhachi))
    fork(play_part,(50, 77, 0.2, 1, 1/8, 3, timpani))
    fork(play_part,(50, 77, 0.2, 1, 1/8, 8, trumpet))
    fork(play_part,(50, 77, 0.2, 1, 1/8, 6, trombone))
    wait_for_children_to_finish()

sounds good ! :slight_smile:

1 Like