PWM

This example uses a comparator to generate a pulse-width modulation style sound from two sawtooth waves.

import zrna
z = zrna.api.Client()
z.connect()
z.clear()

osc1 = z.OscillatorSaw(
    oscillation_frequency=0.090,
    sawtooth_direction=z.RISING
)

osc2 = z.OscillatorSaw(
    oscillation_frequency=0.090,
    sawtooth_direction=z.RISING
)

c = z.Comparator()
c.compare_to = z.SECOND_INPUT

g = z.GainLimiter(gain=0.25, output_voltage_limit=1)

for module in [osc1, osc2, c, g]:
    module.set_clock(z.CLOCK3)
    z.add(module)

audio_out = z.AudioOut()
z.add(audio_out)

osc1.saw.connect(c.noninverting)
osc2.saw.connect(c.inverting)

c.output.connect(g.input)

g.output.connect(audio_out.input)

z.set_divisor(z.CLOCK_SYS1, 8)
z.run()
Privacy

© 2024 Zrna Research