该 audiopwmio模块包含提供对音频 IO 的访问的类。
如果程序在使用后继续,所有类都会更改硬件状态,并且在不再需要它们时应取消初始化。为此,请调用deinit()或使用上下文管理器。有关更多信息,请参阅
Lifetime 和 ContextManagers。
由于CircuitPython 5Mixer, RawSample 并WaveFile 移动到audiocore。
在这些板上可用
-  ARAMCON Badge 2019
-  ARAMCON2 Badge
-  Adafruit CLUE nRF52840 Express
-  Adafruit Circuit Playground Bluefruit
-  Adafruit Feather Bluefruit Sense
-  Adafruit Feather RP2040
-  Adafruit Feather STM32F405 Express
-  Adafruit Feather nRF52840 Express
-  Adafruit ItsyBitsy RP2040
-  Adafruit ItsyBitsy nRF52840 Express
-  Adafruit LED Glasses Driver nRF52840
-  Adafruit Macropad RP2040
-  Adafruit Metro nRF52840 Express
-  Adafruit QT Py RP2040
-  Adafruit QT2040 Trinkey
-  Arduino Nano 33 BLE
-  Arduino Nano RP2040 Connect
-  AtelierDuMaker nRF52840 Breakout
-  BLE-SS dev board Multi Sensor
-  BastBLE
-  BlueMicro840
-  Challenger RP2040 WiFi
-  Cytron Maker Pi RP2040
-  Electronut Labs Blip
-  Electronut Labs Papyr
-  EncoderPad RP2040
-  HiiBot BlueFi
-  IkigaiSense Vita nRF52840
-  MDBT50Q-DB-40
-  MDBT50Q-RX Dongle
-  MEOWBIT
-  MakerDiary nRF52840 MDK
-  MakerDiary nRF52840 MDK USB Dongle
-  Makerdiary M60 Keyboard
-  Makerdiary Pitaya Go
-  Makerdiary nRF52840 M.2 Developer Kit
-  Melopero Shake RP2040
-  Oak Dev Tech BREAD2040
-  Open Hardware Summit 2020 Badge
-  PCA10056 nRF52840-DK
-  PCA10059 nRF52840 Dongle
-  PCA10100 nRF52833 Dongle
-  Particle Argon
-  Particle Boron
-  Particle Xenon
-  Pimoroni Interstate 75
-  Pimoroni Keybow 2040
-  Pimoroni PGA2040
-  Pimoroni Pico LiPo (16MB)
-  Pimoroni Pico LiPo (4MB)
-  Pimoroni PicoSystem
-  Pimoroni Plasma 2040
-  Pimoroni Tiny 2040
-  PyKey60
-  PyboardV1_1
-  Raspberry Pi Pico
-  STM32F412G_DISCO
-  STM32F4_DISCO
-  Simmel
-  SparkFun MicroMod RP2040 Processor
-  SparkFun MicroMod nRF52840 Processor
-  SparkFun Pro Micro RP2040
-  SparkFun Pro nRF52840 Mini
-  SparkFun STM32 MicroMod Processor
-  SparkFun Thing Plus - RP2040
-  TG-Watch
-  THUNDERPACK_v11
-  THUNDERPACK_v12
-  Teknikio Bluebird
-  TinkeringTech ScoutMakes Azul
-  WarmBit BluePixel nRF52840
-  micro:bit v2
-  nice!nano
-  stm32f411ce-blackpill-with-flash
 
- 
class audiopwmio.PWMAudioOut(left_channel: microcontroller.Pin, *, right_channel: Optional[microcontroller.Pin] = None, quiescent_value: int = 32768)
- 通过改变 PWM 占空比输出模拟音频信号。 - 创建与给定引脚关联的 PWMAudioOut 对象。这允许您在给定的引脚上播放音频信号。与 mod: 相比- audioio,指定的引脚是数字引脚,并由依赖于设备的 PWM 信号驱动。
 - 
- 参数
- 
- left_channel (Pin) – 输出左声道的引脚 
- right_channel (Pin) – 输出右声道的引脚 
- quiescent_value (int) – 不存在信号时的输出值。样本应以此值开始和结束,以防止出现爆音。 
 
 - 简单的 8ksps 440 Hz 正弦波: - import audiocore
import audiopwmio
import board
import array
import time
import math
# Generate one period of sine wav.
length = 8000 // 440
sine_wave = array.array("H", [0] * length)
for i in range(length):
    sine_wave[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15) + 2 ** 15)
dac = audiopwmio.PWMAudioOut(board.SPEAKER)
sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)
dac.play(sine_wave, loop=True)
time.sleep(1)
dac.stop()
- 从闪存播放波形文件: - import board
import audiocore
import audiopwmio
import digitalio
# Required for CircuitPlayground Express
speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
speaker_enable.switch_to_output(value=True)
data = open("cplay-5.1-16bit-16khz.wav", "rb")
wav = audiocore.WaveFile(data)
a = audiopwmio.PWMAudioOut(board.SPEAKER)
print("playing")
a.play(wav)
while a.playing:
  pass
print("stopped")
- 
- 
playing:bool
- 输出音频样本时为真,即使- paused(只读)
 
 - 
- 
paused:bool
- 播放暂停时为真。(只读) 
 - 
- 
deinit(self) → None
- 取消初始化 PWMAudioOut 并释放任何硬件资源以供重用。 
 - 
- 
__enter__(self) → PWMAudioOut
- 上下文管理器使用的无操作。 
 - 
- 
__exit__(self) → None
- 退出上下文时自动取消初始化硬件。有关更多信息,请参阅
Lifetime 和 ContextManagers 。 
 - 
- 
play(self, sample: _typing.AudioSample, *, loop: bool = False) → None
- 当 loop=False 时播放一次样本,当 loop=True 时连续播放。不阻塞。使用 - playing以块。
 - 样品必须是 - audiocore.WaveFile,- audiocore.RawSample,- audiomixer.Mixer或- audiomp3.MP3Decoder。
 - 样本本身应由 16 位样本组成。具有较低输出分辨率的微控制器将使用最高位进行输出。 
 - 
- 
stop(self) → None
- 停止播放并重置到样本的开头。 
 - 
- 
pause(self) → None
- 在记住位置的同时暂时停止播放。使用- resume恢复播放。
 
 - 
- 
resume(self) → None
- 后恢复样本播放- pause().