2021-02-02 20:14:39 -05:00
|
|
|
import audiocore
|
|
|
|
import audiopwmio
|
|
|
|
import board
|
|
|
|
import digitalio
|
|
|
|
import array
|
|
|
|
import time
|
|
|
|
import math
|
|
|
|
|
|
|
|
trigger = digitalio.DigitalInOut(board.D4)
|
|
|
|
trigger.switch_to_output(True)
|
|
|
|
|
|
|
|
# Generate one period of sine wav.
|
|
|
|
length = 8000 // 440
|
|
|
|
|
|
|
|
samples = []
|
2021-03-15 09:57:36 -04:00
|
|
|
sample_names = ["unsigned 8 bit", "signed 8 bit", "unsigned 16 bit", "signed 16 bit"]
|
2021-02-02 20:14:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
# unsigned 8 bit
|
|
|
|
u8 = array.array("B", [0] * length)
|
|
|
|
for i in range(length):
|
2022-01-29 17:14:01 -05:00
|
|
|
u8[i] = int(math.sin(math.pi * 2 * i / length) * (2**7) + 2**7)
|
2021-02-02 20:14:39 -05:00
|
|
|
|
|
|
|
samples.append(audiocore.RawSample(u8, sample_rate=4000))
|
|
|
|
|
|
|
|
# signed 8 bit
|
|
|
|
s8 = array.array("b", [0] * length)
|
|
|
|
for i in range(length):
|
2022-01-29 17:14:01 -05:00
|
|
|
s8[i] = int(math.sin(math.pi * 2 * i / length) * (2**7))
|
2021-02-02 20:14:39 -05:00
|
|
|
|
|
|
|
samples.append(audiocore.RawSample(s8, sample_rate=16000))
|
|
|
|
|
|
|
|
# unsigned 16 bit
|
|
|
|
u16 = array.array("H", [0] * length)
|
|
|
|
for i in range(length):
|
2022-01-29 17:14:01 -05:00
|
|
|
u16[i] = int(math.sin(math.pi * 2 * i / length) * (2**15) + 2**15)
|
2021-02-02 20:14:39 -05:00
|
|
|
|
|
|
|
samples.append(audiocore.RawSample(u16, sample_rate=8000))
|
|
|
|
|
|
|
|
|
|
|
|
# signed 16 bit
|
|
|
|
s16 = array.array("h", [0] * length)
|
|
|
|
for i in range(length):
|
2022-01-29 17:14:01 -05:00
|
|
|
s16[i] = int(math.sin(math.pi * 2 * i / length) * (2**15))
|
2021-02-02 20:14:39 -05:00
|
|
|
|
|
|
|
samples.append(audiocore.RawSample(s16, sample_rate=8000))
|
|
|
|
|
|
|
|
|
|
|
|
dac = audiopwmio.PWMAudioOut(board.D13)
|
|
|
|
for sample, name in zip(samples, sample_names):
|
|
|
|
print(name)
|
|
|
|
trigger.value = False
|
|
|
|
dac.play(sample, loop=True)
|
|
|
|
time.sleep(1)
|
|
|
|
dac.stop()
|
|
|
|
time.sleep(0.1)
|
|
|
|
trigger.value = True
|
|
|
|
print()
|
|
|
|
|
|
|
|
print("done")
|