Created
March 22, 2022 01:51
-
-
Save jeffehobbs/4241b7f75219e8b996f7c2a6857c9451 to your computer and use it in GitHub Desktop.
loop of fast and slow random audio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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