Hello, world!

Info

See the full example source code on GitHub.

Supriya’s “hello, world!” is a little longer than most traditional “hello, world!” recipes, because playing a sound from scratch requires more than one step:

  • We need a server, booted and online

  • We need to find (or define) a SynthDef to act as the template for the synths that actually make sound

  • We need to allocate that SynthDef on the running server, and we need to wait for allocation to complete (because SynthDef allocation is an asynchronous command)

  • Once the SynthDef finishes allocating, we need to create one or more synths

  • We need to give those synths some time to play so we can actually hear them

  • And we should be good and clean up after ourselves: release the synths, wait for them to fade out, then quit the running server

All of this logic is outlined in the example’s main function:

def main() -> None:
    """
    The example entry-point function.
    """
    # Create a server and boot it:
    server = supriya.Server().boot()
    # Define a C-major chord in Hertz
    frequencies = [261.63, 329.63, 392.00]
    # Create an empty list to store synths in:
    synths: list[supriya.Synth] = []
    # Start an OSC bundle to run immediately:
    with server.at():
        # Add the default synthdef to the server and open a "completion"
        # context manager to group further commands for when the synthdef
        # finishes loading:
        with server.add_synthdefs(supriya.default):
            # Loop over the frequencies:
            for frequency in frequencies:
                # Create a synth using the default synthdef and the frequency
                # and add it to the list of synths:
                synths.append(
                    server.add_synth(synthdef=supriya.default, frequency=frequency)
                )
    # Let the notes play for 4 seconds:
    time.sleep(4)
    # Loop over the synths and free them
    for synth in synths:
        synth.free()
    # Wait a second for the notes to fade out:
    time.sleep(1)
    # Quit the server:
    server.quit()

Invocation

You can invoke the script with …

josephine@laptop:~/supriya$ python -m examples.hello_world

You should hear a C-major chord play for a few seconds, then fade out quickly before the script exits.

Hello, world!