def gen(): recvd = audio.AudioFrame() empty = audio.AudioFrame() while True: if radio.receive_bytes_into(recvd) == 32: yield recvd else: yield empty if button_a.is_pressed() and button_b.is_pressed(): return
def sample_generator(filename): buf = audio.AudioFrame() with open(filename, "rb") as file: ln = -1 while ln: ln = file.readinto(buf) yield buf
def play_snd(fname): frame = audio.AudioFrame() with open(fname, 'rb') as sndfile: audio.play(frames_from_file(sndfile, frame), wait=False) sleep(1000) audio.stop() del frame
def frames_from_file(sndfile, frame): buff = audio.AudioFrame() while (sndfile.readinto(buff, 32) > 0): for i in range(16): frame[i * 2] = buff[i] frame[i * 2 + 1] = (buff[i + 1] + buff[i]) // 2 yield frame
def play_file(name, delay=80, reflect=0.5): #Do allocation here, as we can't do it in an interrupt. frame = audio.AudioFrame() with open(name) as file: gen = from_file(file, frame) r = reverb(gen, delay, reflect) audio.play(r)
def play_snd(fname, num_times): frame = audio.AudioFrame() for i in range(num_times): with open(fname, 'rb') as sndfile: audio.play(frames_from_file(sndfile, frame), wait=True) del frame audio.stop()
def reverb(src, delay, reflect): #Do all allocation up front, so we don't need to do any in the generator. bucket_count = delay>>2 buckets = [ None ] * bucket_count for i in range(bucket_count): buckets[i] = audio.AudioFrame() vol = 1.0 fadeout = 0 while vol > 0.05: fadeout += bucket_count vol *= reflect return reverb_gen(src, buckets, reflect, fadeout)
def make_frame_iterator(frequency): frame = audio.AudioFrame() frame_length = len(frame) position_in_period = 0.0 position_in_period_delta = frequency / SAMPLE_RATE while True: for i in range(frame_length): # frame[i] = int(math.sin(position_in_period * TWO_PI) * MAX_AMPLITUDE + 124) frame[i] = int(math.sin(math.pi * i / 16) * MAX_AMPLITUDE + 128.5) yield frame position_in_period = position_in_period + position_in_period_delta if position_in_period >= 1.0: position_in_period = position_in_period - 1.0
def sawtooth_generator(): sawtooth = audio.AudioFrame() for i in range(32): sawtooth[i] = i * 8 + 4 for i in range(256): yield sawtooth
import audio def read_frame(f_list, frame): for file in f_list: ln = file.readinto(frame) while ln: yield frame ln = file.readinto(frame) def play_file(f): with open(f + "-01.raw", "rb") as file1, \ open(f + "-02.raw", "rb") as file2, \ open(f + "-03.raw", "rb") as file3: f_list = [file1, file2, file3] audio.play(read_frame(f_list, frame), wait=True) # Allocate memory outside the interrupt frame = audio.AudioFrame() ln = -1 file = 1 # play the files play_file("halmsg")
def play_file(name, pin=None, return_pin=None): #Do allocation here, as we can't do it in an interrupt. frame = audio.AudioFrame() with open(name) as file: audio.play(audio_generator(file, frame), pin=pin, return_pin=return_pin)
def __init__(self): self.frame = audio.AudioFrame()
yield frame # Press button A to skip to next wave. def show_wave(name, frame, duration=1500): display.scroll(name + " wave", wait=False, delay=100) audio.play(repeated_frame(frame, duration), wait=False) for i in range(75): sleep(100) if button_a.is_pressed(): display.clear() audio.stop() break frame = audio.AudioFrame() for i in range(len(frame)): frame[i] = int(math.sin(math.pi * i / 16) * 124 + 128.5) show_wave("Sine", frame) triangle = audio.AudioFrame() QUARTER = len(triangle) // 4 for i in range(QUARTER): triangle[i] = i * 15 triangle[i + QUARTER] = 248 - i * 15 triangle[i + QUARTER * 2] = 128 - i * 15 triangle[i + QUARTER * 3] = i * 15 + 8 show_wave("Triangle", triangle)