Created
October 13, 2023 16:14
-
-
Save benevpi/bb27268f23c9b4a8df0e63eb6be1d1de to your computer and use it in GitHub Desktop.
ambient noise generator
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
# SPDX-FileCopyrightText: 2023 John Park | |
# | |
# SPDX-License-Identifier: MIT | |
# Ambient Machine inspired by Yuri Suzuki https://www.yurisuzuki.com/projects/the-ambient-machine | |
import os | |
import gc | |
import board | |
import audiocore | |
import audiobusio | |
import audiomixer | |
from digitalio import Pull | |
import audiopwmio | |
import time | |
from random import randint | |
wav_files = [] | |
num_samples = 0 | |
for filename in os.listdir('/samples/'): # on board flash | |
if filename.lower().endswith('.wav') and not filename.startswith('.'): | |
wav_files.append("/samples/"+filename) | |
print(filename) | |
num_samples += 1 | |
wav_files.sort() # put in alphabetical/numberical order | |
gc.collect() | |
#audio = audiobusio.I2SOut(bit_clock=bck_pin, word_select=lck_pin, data=dat_pin) | |
audio = audiopwmio.PWMAudioOut(board.A1) | |
mixer = audiomixer.Mixer(voice_count=len(wav_files), sample_rate=22050, channel_count=1, | |
bits_per_sample=16, samples_signed=True, buffer_size=2048) | |
audio.play(mixer) | |
for i in range(num_samples): # start playing all wavs on loop w levels down | |
wave = audiocore.WaveFile(wav_files[i], bytearray(1024)) | |
mixer.voice[i].play(wave, loop=True) | |
mixer.voice[i].level = 0.0 | |
LOW_VOL = 0.2 | |
HIGH_VOL = 0.5 | |
mixer.voice[2].level = 0.2 | |
MIXER_MAX_LEVEL=0.1 | |
size_lfo_1 = 100 | |
size_lfo_2 = 250 | |
size_lfo_3 = 450 | |
wave_table_1 = [] | |
wave_table_2 = [] | |
wave_table_3 = [] | |
def fill_wave_table(wave_table, size): | |
wave_table[:] = [] | |
for i in range(size/2): | |
wave_table.append(1/(size/2)*i) | |
for i in range(size/2): | |
wave_table.append(1-(1/(size/2)*i)) | |
fill_wave_table(wave_table_1, size_lfo_1) | |
fill_wave_table(wave_table_2, size_lfo_2) | |
fill_wave_table(wave_table_3, size_lfo_3) | |
print(wave_table_1) | |
counter_vol_1 = 0 | |
counter_vol_2 = 0 | |
counter_vol_3 = 0 | |
lfo_1_voice = 1 | |
lfo_2_voice = 2 | |
lfo_3_voice = 3 | |
while True: | |
time.sleep(0.1) | |
mixer.voice[lfo_1_voice].level = wave_table_1[counter_vol_1]* MIXER_MAX_LEVEL | |
counter_vol_1 += 1 | |
if counter_vol_1 == size_lfo_1: | |
counter_vol_1 = 0 | |
lfo_1_voice = randint(0,num_samples-1) | |
print("new voice: ", lfo_1_voice) | |
mixer.voice[lfo_2_voice].level = wave_table_2[counter_vol_2]* MIXER_MAX_LEVEL | |
counter_vol_2 += 1 | |
if counter_vol_2 == size_lfo_2: | |
counter_vol_2 = 0 | |
lfo_2_voice = randint(0,num_samples-1) | |
mixer.voice[lfo_3_voice].level = wave_table_3[counter_vol_3]* MIXER_MAX_LEVEL | |
counter_vol_3 += 1 | |
if counter_vol_3 == size_lfo_3: | |
counter_vol_3 = 0 | |
lfo_3_voice = randint(0,num_samples-1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment