Ejemplo n.º 1
0
 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
Ejemplo n.º 2
0
def sample_generator(filename):
    buf = audio.AudioFrame()
    with open(filename, "rb") as file:
        ln = -1
        while ln:
            ln = file.readinto(buf)
            yield buf
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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()
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
def sawtooth_generator():
    sawtooth = audio.AudioFrame()
    for i in range(32):
        sawtooth[i] = i * 8 + 4
    for i in range(256):
        yield sawtooth
Ejemplo n.º 10
0
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")
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
 def __init__(self):
     self.frame = audio.AudioFrame()
Ejemplo n.º 13
0
        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)