Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active May 24, 2023 00:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todbot/41414a94139d84651e29987343198134 to your computer and use it in GitHub Desktop.
Save todbot/41414a94139d84651e29987343198134 to your computer and use it in GitHub Desktop.
rude noises using synthio in CircuitPython
# synthio_rude_noises0.py -- rude noises using synthio
# 23 May 2023 - @todot / Tod Kurt
import board
import audiobusio, audiomixer, synthio
import ulab.numpy as np
SAMPLE_RATE = 28000 # clicks @ 36kHz & 48kHz on rp2040
SAMPLE_SIZE = 512
SAMPLE_VOLUME = 32000 # 0-32767 I think
# qtpy rp2040 pins (board.* names just match silkscreen, do not indicate functionality)
lck_pin, bck_pin, dat_pin = board.MISO, board.MOSI, board.SCK
audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin)
mixer = audiomixer.Mixer(voice_count=1, sample_rate=SAMPLE_RATE, channel_count=1,
bits_per_sample=16, samples_signed=True, buffer_size=2048 )
audio.play(mixer)
synth = synthio.Synthesizer(sample_rate=SAMPLE_RATE)
mixer.voice[0].level = 0.25 # turn down the volume a bit since this can get loud
mixer.voice[0].play(synth)
wave_sine = np.array(np.sin(np.linspace(0, 2*np.pi, SAMPLE_SIZE, endpoint=False)) * SAMPLE_VOLUME, dtype=np.int16)
wave_saw = np.linspace(SAMPLE_VOLUME, -SAMPLE_VOLUME, num=SAMPLE_SIZE, dtype=np.int16)
lfo1 = synthio.LFO(rate=(1/4), waveform=wave_saw)
lfo2 = synthio.LFO(rate=(1/8), waveform=wave_sine)
mathl12 = synthio.Math( synthio.MathOperation.LERP, lfo1, lfo2, 0.5)
note1 = synthio.Note( frequency=220, waveform=wave_saw,
ring_frequency=220, ring_bend=mathl12, ring_waveform=wave_saw)
synth.press( (note1,) )
while True:
pass
@todbot
Copy link
Author

todbot commented May 24, 2023

# synthio_rude_noises1.py
#
import board, time, random
import audiobusio, audiomixer, synthio
import ulab.numpy as np

SAMPLE_RATE = 28000
SAMPLE_SIZE = 512
SAMPLE_VOLUME = 32000

lck_pin, bck_pin, dat_pin = board.MISO, board.MOSI, board.SCK
audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin)
mixer = audiomixer.Mixer(voice_count=1, sample_rate=SAMPLE_RATE, channel_count=1,
                         bits_per_sample=16, samples_signed=True, buffer_size=2048 )
audio.play(mixer)
synth = synthio.Synthesizer(sample_rate=SAMPLE_RATE)
mixer.voice[0].level = 0.25 # turn down the volume a bit since this can get loud
mixer.voice[0].play(synth)

wave_sine = np.array(np.sin(np.linspace(0, 2*np.pi, SAMPLE_SIZE, endpoint=False)) * SAMPLE_VOLUME, dtype=np.int16)
wave_saw = np.linspace(SAMPLE_VOLUME, -SAMPLE_VOLUME, num=SAMPLE_SIZE, dtype=np.int16)

lfo1 = synthio.LFO(rate=35, waveform=wave_sine)
lfo2 = synthio.LFO(rate=1, waveform=wave_sine)

n = 45

note1 = synthio.Note( frequency=synthio.midi_to_hz(n), waveform=wave_sine,
                      ring_frequency=synthio.midi_to_hz(n)*1.05, ring_bend=0, #lfo1,
                      ring_waveform=wave_sine)
note1.bend = lfo1

synth.press( note1 )

print("hi")
while True:
    print("no ring_bend", n)
    note1.ring_bend = 0
    time.sleep(1)
    #print("lfo bend")
    #note1.bend = lfo2
    print("ring_bend lfo1")
    note1.ring_bend = lfo1
    time.sleep(1)
    n = n+4
    if n > 100: n = 45
    note1.frequency = synthio.midi_to_hz(n)
    note1.ring_frequency = synthio.midi_to_hz(n) * 1.05
    #f = f*1.5
    #fd = f*1.047
    #note1.frequency = f
    #note1.ring_frequency = fd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment