Micropython学习交流群 学习QQ群:786510434 提供多种固件下载和学习交流。

Micropython-扇贝物联 QQ群:31324057 扇贝物联是一个让你与智能设备沟通更方便的物联网云平台

Micropython学习交流群 学习QQ群:468985481 学习交流ESP8266、ESP32、ESP8285、wifi模块开发交流、物联网。

Micropython老哥俩的IT农场分享QQ群:929132891 为喜欢科创制作的小白们分享一些自制的计算机软硬件免费公益课程,由两位多年从事IT研发的中年大叔发起。

Micropython ESP频道

micropython ESP32 的 MicroPython I2S


ESP32 的 MicroPython I2S


适用于 ESP32 的 MicroPython I2S 指南和示例

弃用通知:这些示例与现已过时的MicroPython PR相关联。自2021年7月5日起,官方MicroPython版本现在支持I2S。二进制文件可以在MicroPython下载中下载。有一组新的示例可用于 Pyboard 和 ESP32。

本指南概述了为MicroPython项目开发的新MicroPython I2S类的功能。I2S 类适用于 ESP32 处理器,并使用乐鑫的 ESP-IDF API 实现。要将I2S与MicroPython一起使用,您需要创建自定义的MicroPython构建并将拉取请求集成到构建中。或者,使用预构建的固件二进制文件之一下载 ESP32 开发板并对其进行编程。

用法示例 - 从 I2S 麦克风模块读取音频样本

from machine import I2S
from machine import Pin

bck_pin = Pin(14)  # Bit clock output
ws_pin = Pin(13)   # Word clock output
sdin_pin = Pin(12) # Serial data input

audio_in = I2S(I2S.NUM0,                                 # create I2S peripheral to read audio
              bck=bck_pin, ws=ws_pin, sdin=sdin_pin,    # sample data from an INMP441
              standard=I2S.PHILIPS, mode=I2S.MASTER_RX, # microphone module
              dataformat=I2S.B32,                      
              channelformat=I2S.RIGHT_LEFT,
              samplerate=16000,
              dmacount=16,dmalen=256)
             
samples = bytearray(2048)                                # bytearray to receive audio samples

num_bytes_read = audio_in.readinto(samples)              # read audio samples from microphone
                                                        # note:  blocks until sample array is full
                                                        # - see optional timeout argument
                                                        # to configure maximum blocking duration      

使用示例 - 使用 I2S 放大器模块通过扬声器播放音频样本

from machine import I2S
from machine import Pin

bck_pin = Pin(14)   # Bit clock output
ws_pin = Pin(13)    # Word clock output
sdout_pin = Pin(12) # Serial data output

audio_out = I2S(I2S.NUM1,                                  # create I2S peripheral to write audio
               bck=bck_pin, ws=ws_pin, sdout=sdout_pin,   # sample data to an Adafruit I2S Amplifier
               standard=I2S.PHILIPS, mode=I2S.MASTER_TX,  # breakout board,
               dataformat=I2S.B16,                        # based on MAX98357A device
               channelformat=I2S.ONLY_RIGHT,
               samplerate=16000,
               dmacount=16,dmalen=512)
             
samples = bytearray(1024)                                 # bytearray containing audio samples to transmit

num_bytes_written = audio_out.write(samples)              # write audio samples to amplifier
                                                         # note:  blocks until sample array is emptied
                                                         # - see optional timeout argument
                                                         # to configure maximum blocking duration      

I2S 类

构造 函数

class machine.I2S(id,
                 bck, ws, [sdin], [sdout],
                 [standard=I2S.PHILIPS], mode,
                 dataformat, channelformat,
                 samplerate,
                 [dmacount=16], [dmalen=64],
                 [apllrate=0])

构造并返回具有给定参数的新 I2S 对象:

id 指定 I2S 外设实例

位时钟输出的后插针对象

ws 引脚对象用于单词选择输出

用于串行数据输入的 sdin 引脚对象(可选)

用于串行数据输出的 sdout 引脚对象(可选)

I2S 外设使用的标准协议(可选)

模式指定接收或发送

每个样本中的数据格式位数

通道格式指定音频格式,例如立体声、单声道

采样率音频采样率(采样/秒)

dmacount 链接的 DMA 缓冲区数量(可选)

每个 DMA 缓冲液的 dmalen 长度(以样品为单位)(可选)

脉冲音频 PLL 采样率(采样/秒)(可选)

笔记:

必须为模式 = I2S 指定 sdin。MASTER_RX

必须为模式 = I2S 指定 sdout。MASTER_TX

只能指定 sdin 或 sdout 中的一种

apllrate允许精确指定采样时钟速率。大多数音频应用程序不需要。有关用法,请参阅 ESP-IDF 文档

方法


开源地址:https://github.com/miketeachman/micropython-esp32-i2s-examples



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 ESP56.com All Rights Reserved

执行时间: 0.0085330009460449 seconds