File: radio_audio.py

package info (click to toggle)
firmware-microbit-micropython 1.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 25,448 kB
  • sloc: ansic: 83,496; cpp: 27,664; python: 2,475; asm: 274; makefile: 245; javascript: 41; sh: 25
file content (74 lines) | stat: -rw-r--r-- 1,951 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

import audio
import radio
from microbit import button_a, button_b, display, running_time, sleep
import os

def sample_generator(filename):
    buf = audio.AudioFrame()
    with open(filename, "rb") as file:
        ln = -1
        while ln:
            ln = file.readinto(buf)
            yield buf

# 1 second of 128Hz sawtooth wave.
def sawtooth_generator():
    sawtooth = audio.AudioFrame()
    for i in range(32):
        sawtooth[i] = i*8+4
    for i in range(256):
        yield sawtooth


def send():
    display.clear()
    radio.on()
    radio.config(channel=90, power=4)
    if "sample.raw" in os.listdir():
        gen = sample_generator("sample.raw")
    else:
        gen = sawtooth_generator()
    start = running_time()
    sent = 0
    for f in gen:
        # One frame every 4ms = 8kHz
        while sent > ((running_time() - start) >> 2) + 3:
            sleep(1)
        radio.send_bytes(f)
        sent += 1
    print(sent)

def play():
    display.clear()
    radio.on()
    radio.config(channel=90, queue=12)
    count = -1
    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
    audio.play(gen())

while True:
    message = "Press button a to send 'sample.raw' or sawtooth wave. Press button b to play received waveform. Press both buttons to stop."
    display.scroll(message, delay=100, wait=False)
    message_end = running_time() + len(message)*600
    if button_a.is_pressed() and button_b.is_pressed():
        break
    while True:
        sleep(50)
        if button_a.is_pressed():
            send()
            break
        if button_b.is_pressed():
            play()
            break
        if running_time() > message_end:
            break