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

MessengersExample

Python Code

from mmm_src.MMMAudio import MMMAudio
from mmm_utils.functions import midicps
a = MMMAudio(128, graph_name="MessengersExample", package_name="examples")

a.start_audio()

a.send_bool("bool",True)
a.send_bool("bool",False)

a.send_float("float", 440.0)
a.send_float("float", 880.0)

a.send_floats("floats", [440.0, 550.0, 660.0])
a.send_floats("floats", [880.0, 990.0, 1100.0])

a.send_int("int", 42)
a.send_int("int", 84)

a.send_ints("ints", [1, 22, 3, 4, 5])
a.send_ints("ints", [5, 4, 3, 2, 1])
a.send_ints("ints", [100,200])

a.send_string("string", "Hello, World!")
a.send_string("string", "Goodbye, World!")

a.send_strings("strings", ["hello", "there", "general", "kenobi"])
a.send_strings("strings", ["goodbye", "there", "general", "grievous"])

a.send_trig("trig")

a.send_bool("tone_0.gate",True)
a.send_bool("tone_1.gate",True)
a.send_float("tone_0.freq",440 * 1.059)
a.send_float("tone_1.freq",midicps(74))
a.send_bool("tone_0.gate",False)
a.send_bool("tone_1.gate",False)

a.stop_audio()

Mojo Code

from mmm_src.MMMWorld import MMMWorld
from mmm_utils.Messenger import *
from mmm_utils.functions import *
from mmm_src.MMMTraits import *
from mmm_dsp.Distortion import *
from mmm_dsp.Osc import *
from mmm_utils.Print import Print
from mmm_utils.RisingBoolDetector import RisingBoolDetector

struct Tone(Movable,Copyable):
    var world: UnsafePointer[MMMWorld]
    var osc: Osc
    var freq: Float64
    var m: Messenger
    var gate: Bool

    fn __init__(out self, world: UnsafePointer[MMMWorld], namespace: String):
        self.world = world
        self.osc = Osc(self.world)
        self.freq = 440.0
        self.m = Messenger(self.world,namespace)
        self.gate = False

    fn next(mut self) -> Float64:

        if self.m.notify_update(self.freq,"freq"):
            print("Tone freq updated to ", self.freq)

        if self.m.notify_update(self.gate,"gate"):
            print("Tone gate updated to ", self.gate)

        sig = self.osc.next(self.freq) if self.gate else 0.0

        return sig

struct MessengersExample():
    var world: UnsafePointer[MMMWorld]
    var m: Messenger
    var tones: List[Tone]

    var bool: Bool
    var bools: List[Bool]
    var float: Float64
    var floats: List[Float64]
    var int: Int64
    var ints: List[Int64]
    var string: String
    var strings: List[String]

    fn __init__(out self, world: UnsafePointer[MMMWorld]):
        self.world = world
        self.m = Messenger(world)

        self.tones = List[Tone]()
        for i in range(2):
            self.tones.append(Tone(world, "tone_" + String(i)))

        self.bool = False
        self.bools = List[Bool](False, False)
        self.float = 0.0
        self.floats = List[Float64](0.0, 0.0)
        self.int = 0
        self.ints = List[Int64](0, 0)
        self.string = ""
        self.strings = List[String]("", "")

    fn next(mut self) -> SIMD[DType.float64, 2]:


        if self.m.notify_update(self.bool,"bool"):
            print("Bool value is now: " + String(self.bool))

        if self.m.notify_update(self.float,"float"):
            print("Float value is now: " + String(self.float))

        if self.m.notify_update(self.floats,"floats"):
            print("Updated floats to ")
            for f in self.floats:
                print("  ", f)

        if self.m.notify_update(self.int,"int"):
            print("Updated int to ", self.int)

        if self.m.notify_update(self.ints,"ints"):
            print("Updated ints to:", end="")
            for i in self.ints:
                print("  ", i, end="")
            print("")

        if self.m.notify_update(self.string,"string"):
            print("Updated string to ", self.string)

        if self.m.notify_update(self.strings,"strings"):
            print("Updated strings to ")
            for s in self.strings:
                print("  ", s)

        if self.m.notify_trig("trig"):
            print("Received trig")

        out = SIMD[DType.float64, 2](0.0, 0.0)
        for i in range(2):
            out[i] = self.tones[i].next()

        return out * dbamp(-20)