Created
April 5, 2022 19:16
-
-
Save brockbrownwork/89b8ac4d2d7c4428ed2fd15cf89f3c0e to your computer and use it in GitHub Desktop.
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 numpy as np | |
import sounddevice | |
import time | |
import random | |
samples_per_second = 44100 | |
def sawtooth(frequency, duration): | |
each_sample_number = np.range(duration * samples_per_second) | |
def sin_wave(frequency, duration, attenuation = 0.1): | |
duration = random.random() | |
attenuation = 0.1 | |
each_sample_number = np.arange(int(duration * samples_per_second)) | |
waveform = np.sin(2 * np.pi * each_sample_number * frequency / samples_per_second) | |
waveform += np.random.rand(int(duration * samples_per_second)) * 0.1 | |
# soften the waveform | |
waveform = waveform * attenuation | |
return waveform | |
def blip_blorp(): | |
waveform = np.array([]) | |
for i in range(100): | |
waveform = np.append(waveform, sin_wave(random.random() * 440, random.random() * 3)) | |
return waveform | |
# Play the waveform out the speakers | |
waveform = blip_blorp() | |
duration = len(waveform) / samples_per_second | |
print("duration", duration) | |
sounddevice.play(waveform, samples_per_second) | |
time.sleep(duration) | |
sounddevice.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment