Skip to content

Instantly share code, notes, and snippets.

@jeffehobbs
Created March 22, 2022 01:51
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 jeffehobbs/4241b7f75219e8b996f7c2a6857c9451 to your computer and use it in GitHub Desktop.
Save jeffehobbs/4241b7f75219e8b996f7c2a6857c9451 to your computer and use it in GitHub Desktop.
loop of fast and slow random audio
import time
import random
import array
import math
import audiocore
import board
import digitalio
import audiobusio
audio = audiobusio.I2SOut(board.GP10, board.GP11, board.GP9)
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
slow_threshold = 0.2
fast_threshold = 0.001
sleep_num = 0.1
speeding_up = True
while True:
if speeding_up:
sleep_num = round((sleep_num - 0.001), 3)
else:
sleep_num = round((sleep_num + 0.001), 3)
if (sleep_num == fast_threshold):
speeding_up = False
elif (sleep_num == slow_threshold):
speeding_up = True
else:
pass
tone_volume = 0.25
frequency = random.randrange(220, 880, 1)
length = 8000 // frequency
sine_wave = array.array("h", [0] * length)
for i in range(length):
sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * tone_volume * (2 ** 15 - 1))
sine_wave_sample = audiocore.RawSample(sine_wave)
print(sleep_num)
audio.play(sine_wave_sample, loop=True)
led.value = False
time.sleep(sleep_num)
led.value = True
audio.stop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment