Save and Load a Performance

Example directory (scamp): examples/Playback/Other/save_and_load_performance

Download: save_and_load_performance.zip

Contains two scripts: One that plays some music and records a portion of that music to a JSON file, and a second that reads the JSON file and plays the recorded performance repeatedly while accelerating.

Topics: Playback › Other

save_performance.py: Saves an Ensemble and a recorded Performance to JSON, for the load_and_play_performance example.
from scamp import *
import random
import math


s = Session()

shaku = s.new_part("shakuhachi")
oboe = s.new_part("oboe")


def oboe_part():
    while True:
        oboe.play_note(75 + random.random() * 7 + 15 * math.sin(get_beat() / 10), 0.4, 0.25)


def shaku_part():
    pentatonic = [0, 2, 4, 7, 9]
    while True:
        if random.random() < 0.5:
            shaku.play_note(66 + random.choice(pentatonic) + 12*random.randint(0, 2),
                            Envelope([1.0, 0.2, 1.0, 1.0], [0.15, 0.85, 0.15], [0, 2, 0]),
                            2.5, blocking=True)
            wait(0.5 + random.choice([0, 0.5]))
        else:
            shaku.play_note(66 + random.choice(pentatonic) + 12*random.randint(0, 2), 1.0, 0.2*(1+random.random()*0.3))
            wait(random.choice([1, 2, 3]))


s.fork(oboe_part)
s.fork(shaku_part)

s.set_tempo_target(300, Moment.after_time(30))

s.wait(15)
s.start_transcribing()
print("Starting transcription...")
s.wait(15)
performance = s.stop_transcribing()
print("Stopped transcribing. Saving transcription.")

performance.save_to_json("perfShakoboe.json")

s.wait_forever()
load_and_play_performance.py: Loads the Ensemble and Performance saved by save_performance.py and plays the performance back under a gradually accelerating tempo.
from scamp import *

s = Session()

shaku = s.new_part("shakuhachi")
oboe = s.new_part("oboe")

recorded_performance = Performance.load_from_json("perfShakoboe.json")

s.tempo = 60
s.set_tempo_target(300, Moment.after_time(40))

while True:
    print("(Re)starting performance.")
    # by default, the performance plays on the currently active clock, forking all parts according to
    # the tempo envelope at recording time. So the acceleration inside the recording is compounded with
    # the gradual session-wide acceleration here.
    # also, by default, we use the currently active session for the instruments.
    recorded_performance.play()