Skip to content

For more information about the examples, such as how the Python and Mojo files interact with each other, see the Examples Overview

PAF_example

Python Code

from mmm_python import *
from random import getstate, setstate, randint
mmm_audio = MMMAudio(128, graph_name="PAF_example", package_name="examples", in_device=None, audio_init_timeout=60)
mmm_audio.start_audio()

mmm_audio.send_float("center_freq", 200)
mmm_audio.send_float("bandwidth", 800)

mmm_audio.send_float("center_freq", rrand(10, 2000))
mmm_audio.send_float("bandwidth", rrand(10, 2000))


# repetetive random sequence
async def loop():

    # get initial state
    state1 = getstate()
    while True:
        # get new second state (different each time)
        state2 = getstate()
        # loop through same first state x times with constant subdivision
        for _ in range(randint(2, 12)):
            setstate(state1)
            for _ in range(randint(3, 15)):
                mmm_audio.send_float("fundamental", midicps(randint(40, 80)))
                mmm_audio.send_trig("trig")
                await asyncio.sleep(0.1)
        # loop through second state const num of times with changing subdivision
        for _ in range(randint(2, 12)):
            setstate(state2)

            for _ in range(randint(3, 15)):
                mmm_audio.send_float("fundamental", midicps(randint(40, 80)))
                mmm_audio.send_trig("trig")
                await asyncio.sleep(0.1)

s = Scheduler()

fut = s.sched(loop())
fut.cancel()
mmm_audio.stop_audio()

Mojo Code

from mmm_audio import *

struct PAF_example(Copyable, Movable):
    var world: World

    var paf: PAF[2, Interp.linear, TimesOversampling.x2, wrap_gaussian=True] # try changing wrap_gaussian to True

    var fund: MFloat[]
    var center: MFloat[]
    var band: MFloat[]

    var env: Env[]
    var trig: Bool

    var m: Messenger

    def __init__(out self, world: World):
        self.world = world

        self.paf = PAF[2, Interp.linear, TimesOversampling.x2, wrap_gaussian=True](world)# try changing wrap_gaussian to True

        self.fund = 73.0
        self.center = 440.0
        self.band = 100

        self.env = Env(world)
        self.env.params = EnvParams([0, 1, 0], [0.01, 0.07])
        self.trig = False

        self.m = Messenger(world)

    def next(mut self) -> MFloat[2]:
        self.m.update("fundamental", self.fund)
        self.m.update("center_freq", self.center)
        self.m.update("bandwidth", self.band)
        self.trig = self.m.notify_trig("trig")

        env = self.env.next[interp=Interp.linear](self.trig)

        osc = self.paf.next(self.fund, self.center, self.band)
        out = osc * env
        # print("osc: " + String(osc))

        return out