Fork problem: what am I doing wrong?

Hi,

I’m new to scamp and excited to get it working. I’m trying to use a function that handles playing melody, volume and duration to create a simple two-part unison melody similar to the tutorial video on forking. Unfortunately, I am getting a lengthy error (“Exception in thread Thread-203” is start of error message) when I run the code below. However, the example 09_multi_part.py file from Github does work (scamp/09_multi_part.py at master · MarcTheSpark/scamp · GitHub). Any help understanding why this doesn’t work and what best practices are for creating multipart melodies with function and lists of pitches would be appreciated.

from scamp import *

s = Session()
s.tempo = 90

piano = s.new_part("piano")
clarinet = s.new_part("clarinet")

mel = [60, 63, 65, 66, 67, 70, 72, 70, 67, 66, 65, 63, 60]
vol = [0.5, 0.5, 0.5, 0.5,1.0, 1.0, 1.0, 1.0, 0.75, 0.75, 0.75, 0.75, 0.25]
dur = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0]

def play_melody(instr, pitch_list: list, vol_list: list, dur_list: list) -> None:
     for pitch, volume, duration in zip(pitch_list, vol_list, dur_list):
         instr.play_note(pitch, volume, duration)

piano_part = play_melody(piano, mel, vol, dur)
clarinet_part = play_melody(clarinet, mel, vol, dur)

fork(piano_part)
fork(clarinet_part)
s.wait_for_children_to_finish()

Actually, just got it working. Just needed to use fork as follows:

fork(play_melody, args=[piano, mel, vol, dur])
fork(play_melody, args=[clarinet, mel, vol, dur])

Still, any insight on why what I originally did won’t work would be appreciated.

Welcome!

The reason it didn’t work originally is that play_melody has 4 required arguments. Just as you can’t call play_melody() with no arguments, you can’t fork it without providing arguments either.

To be explicit: fork(play_melody) is the fork equivalent of play_melody(), while fork(play_melody, args=[piano, mel, vol, dur]) is the fork equivalent of play_melody(piano, mel, vol, dur)

Make sense?

Yes, thanks!