circuitpython/tests/circuitpython-manual/audiobusio/i2s_sample_loop.py
Scott Shawcroft 360475e266
Implement audiobusio and enhance PIO for it
This adds I2SOut and PDMIn support via PIO.

StateMachines can now:
 * read and read while writing
 * transfer in 1, 2 or 4 byte increments
 * init pins based on expected defaults automatically
 * be stopped and restarted
 * rxfifo can be cleared and rxstalls detected (good for tracking when
   the reading code isn't keeping up)

Fixes #4162
2021-02-23 15:50:00 -08:00

37 lines
702 B
Python

import audiocore
import audiobusio
import board
import digitalio
import array
import time
import math
import rp2pio
import adafruit_pioasm
time.sleep(10)
trigger = digitalio.DigitalInOut(board.D4)
trigger.switch_to_output(True)
# Generate one period of sine wav.
length = 8000 // 440
# signed 16 bit
s16 = array.array("h", [0] * length)
for i in range(length):
s16[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15))
print(s16[i])
sample = audiocore.RawSample(s16, sample_rate=8000)
dac = audiobusio.I2SOut(bit_clock=board.D10,
word_select=board.D11, data=board.D12)
trigger.value = False
dac.play(sample, loop=True)
time.sleep(1)
dac.stop()
trigger.value = True
print("done")