audiopwmio
–通过数字 PWM 音频输出¶
该 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 信号驱动。- 参数
简单的 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")
-
paused
:bool¶ 播放暂停时为真。(只读)
-
__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 位样本组成。具有较低输出分辨率的微控制器将使用最高位进行输出。