Schedule notes in the future

Hi there from Tenerife!

I am really impressed with tempos Scamp feature, and the fact that you can fork a process is impressive.
I did some algorithmic music about 25 years ago, using C++ and a library called MidiShare. This library (deprecated long time ago) allowed the user to schedule events in the future, by providing a timestamp.
I wonder if Scamp would allow this, since it seems to have a very sophisticated handling of timing.
Thanks for such a wonderful package, Marc!

I am away from my computer but there is an additional optional argument with the fork function tthat delays the execution. Can’t remember te exact synax but I used it before.

1 Like

See the schedule_at argument:

http://scamp.marcevanstein.com/clockblocks/clockblocks.clock.Clock.html#clockblocks.clock.Clock.fork

Of course, you could also just fork a function and add a wait call at the beginning!

1 Like

This is not exactly what I meant (I referred to individual note events), but this feature is superb. It will allow sending a pattern or motive to the future without touching the notes themselves.
Thanks for the help!

Ah, I see what you mean. You could probably even do:

fork(inst.play_note, args=(60, 0.7, 2.0), schedule_at=5)

…although if you scheduled like 100 notes this way it might get a little weird and inefficient, since that would be like 100 separate threads running. Could work though!

Okay, I couldn’t resist. Here’s another approach, which is probably the best!

from scamp import *


def play_scheduled_notes(inst, schedule):
    t = 0
    for start_time, pitch, volume, dur in schedule:
        wait(start_time - t)
        t += start_time - t
        inst.play_note(pitch, volume, dur, blocking=False)
    wait_for_children_to_finish()
    

scheduled_notes = [
    # (start time, pitch, volume, duration)
    (0.25, 75.0, 0.7, 0.25),
    (0.5, 58.0, 1.0, 0.25),
    (1.25, 49.0, 1.0, 1.0),
    (3.5, 64.0, 0.7, 0.25),
    (3.5, 66.0, 0.7, 2.0),
    (6.0, 72.0, 0.4, 0.25),
    (7.25, 58.0, 1.0, 2.0),
    (7.5, 44.0, 1.0, 2.0),
    (7.5, 54.0, 0.7, 0.25),
    (8.75, 73.0, 0.7, 0.5),
    (8.75, 75.0, 1.0, 0.5),
    (10.0, 63.0, 0.4, 0.5),
    (11.25, 49.0, 1.0, 1.0),
    (12.0, 72.0, 0.7, 2.0),
    (14.25, 61.0, 0.7, 0.5),
    (14.5, 60.0, 1.0, 2.0),
    (15.75, 48.0, 0.4, 0.25),
    (16.5, 61.0, 0.7, 1.0),
    (18.75, 78.0, 0.7, 0.25),
    (19.25, 68.0, 0.7, 2.0)
]

s = Session()
marimba = s.new_part("marimba")
play_scheduled_notes(marimba, scheduled_notes)