-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Open
Labels
Description
notes:
- Using ESP32 and v1.21.0
- As for web.py I am using the great https://github.com/wybiral/micropython-aioweb
- As for queue.py I am using https://github.com/peterhinch/micropython-async/blob/master/v3/primitives/queue.py
from machine import I2S
from machine import Pin
import web
import asyncio
from queue import Queue
bck_pin = Pin(33) # Bit clock output
ws_pin = Pin(32) # Word clock output
sdin_pin = Pin(25) # Serial data input
au_chunk_len = 8192
au_chunks = 8 # must be power of 2
audio_in = I2S(1, # create I2S peripheral to read audio
sck=bck_pin, ws=ws_pin, sd=sdin_pin, # sample data from an INMP441
mode=I2S.RX,
bits=32,
format=I2S.MONO,
rate=8000,
ibuf=au_chunk_len)
sreader = asyncio.StreamReader(audio_in)
queue = Queue(au_chunks)
async def produce():
print("Running produce()")
#ring buffer
dma_buffer = memoryview(bytearray(au_chunk_len*au_chunks))
f=0
while True:
b = dma_buffer[f*au_chunk_len:(f+1)*au_chunk_len]
t = au_chunk_len
while t>0:
t -= await sreader.readinto(b[au_chunk_len-t:])
await queue.put(b)
f= (f+1)&(au_chunks-1)
app = web.App(host='0.0.0.0', port=80)
@app.route('/')
async def scan_network_handler(r, w):
w.write(b'HTTP/1.0 200 OK\r\n')
w.write(b'Content-Type: application/octet-stream\r\n')
w.write(b'\r\n')
# empty queue
for i in range(au_chunks):
_ = await queue.get()
for i in range(30):
print(f"c {i}, {queue.qsize()}")
samples = await queue.get()
w.write(samples)
await w.drain()
# Start event loop and create server task
loop = asyncio.get_event_loop()
loop.create_task(app.serve())
loop.create_task(produce())
loop.run_forever()