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

ParallelGraphs

MMMAudio can run multiple graphs in parallel, each in its own process. This example shows how to create multiple MMMAudio instances, each with its own graph, and send messages to them independently.

Python Code

from mmm_python import *
m_s = []
for i in range(4):
    m_s.append(MMMAudio(128, graph_name=
    "ParallelGraphs", package_name="examples"))
    m_s[-1].start_audio()
    m_s[-1].send_float("pan", linlin(i, 0, 3, -1, 1)) # pan each graph to a different position in the stereo field


picker = Pxrand([0,1,2,3])
def set_random_freq():
    m_s[picker.next()].send_float("freq", rrand(100, 600)) # set the frequency to a random value

set_random_freq()

Mojo Code

from mmm_audio import *

struct ParallelGraphs(Representable, Movable, Copyable):
    var world: World  
    var osc: Osc[1,Interp.sinc,1]
    var filt: SVF[1]
    var messenger: Messenger
    var freq: Float64
    var pan: Float64

    fn __init__(out self, world: World):
        self.world = world
        self.osc = Osc[1,Interp.sinc,1](self.world)
        self.filt = SVF[1](self.world)
        self.messenger = Messenger(self.world)
        self.freq = 440.0
        self.pan = -1.0

    fn __repr__(self) -> String:
        return String("Default")

    fn next(mut self) -> MFloat[2]:
        self.messenger.update(self.freq,"freq")
        self.messenger.update(self.pan,"pan")

        osc = self.osc.next(self.freq, osc_type=OscType.saw) 
        osc = self.filt.next[filter_type=SVFModes.lowpass](osc, 2000.0, 1.0)
        osc2 = pan2(osc, self.pan)

        return osc2 * 0.3