Difference between revisions of "MakaPython Audio"

From MakerFabsWiki
Jump to: navigation, search
(Indicator LED Status)
(Introduction)
 
(17 intermediate revisions by one other user not shown)
Line 3: Line 3:
 
The MakePython Audio board uses the uDA1334 digital to analog converter (DAC), which converts I2S (not be confused with I2C) audio to an analog signal to drive speakers. The I2S Audioboard converts the digital audio signals using the I2S standard to an analog signal.<br>
 
The MakePython Audio board uses the uDA1334 digital to analog converter (DAC), which converts I2S (not be confused with I2C) audio to an analog signal to drive speakers. The I2S Audioboard converts the digital audio signals using the I2S standard to an analog signal.<br>
  
'''Model: [https://www.makerfabs.com/makepython-audio.html xxxxxxxx]'''<br>
+
'''Model: [https://www.makerfabs.com/makepython-audio.html MPAUDIO]'''<br>
 
[[File:MakePython_Audio_01.png|500px]]<br>
 
[[File:MakePython_Audio_01.png|500px]]<br>
  
Line 34: Line 34:
 
4. Speaker<br>
 
4. Speaker<br>
 
5. PC<br>
 
5. PC<br>
6. SD card<br>
+
6. microSD card<br>
  
[[File:MakePython_Audio_02.png|500px]]<br>
 
System diagram
 
 
[[File:MakePython_Audio_03.png|500px]]<br>
 
[[File:MakePython_Audio_03.png|500px]]<br>
Application
+
System diagram<br>
 
+
Makepython Audio can’t worl alone, it need work with Makepython ESP32(Wrover).<br>
You can get Makepython ESP32(Wrover) from:
+
[[File:MakePython_Audio_04.png|500px]]<br>
https://www.makerfabs.com/makepython-esp32.html
+
Application<br>
 
 
Note: Makepython ESP32 Wroom’s RAM is not enough for audio application, so you need Makepython ESP32(Wrover)
 
  
 +
You can get Makepython ESP32(Wrover) from:<br>
 +
https://www.makerfabs.com/makepython-esp32.html<br>
  
 +
Note: Makepython ESP32 Wroom’s RAM is not enough for audio application, so you need Makepython ESP32(Wrover)<br>
  
 
==Usage==
 
==Usage==
 
Warning: Don't operate when powered on in case of short circuit.<br>
 
Warning: Don't operate when powered on in case of short circuit.<br>
This guides help you how to use makepython Audio with Makepython ESP32 to fulfill text to speech(TTS). It also can play music from SD card or Flash too.
+
This guides help you how to use makepython Audio with Makepython ESP32 to fulfill text to speech(TTS). It’s very simple and easy to understand. It also can play music from SD card or Flash too.
 
<br>
 
<br>
  
Line 72: Line 71:
  
 
===Project_1: Text to speech===
 
===Project_1: Text to speech===
Attention: To get GPS location, please ensure you are outdoor, and plug the GPS Antenna. Indoor may get the incorrect location or no location information.<br>
 
<br>
 
 
The example uses MicroPython programming, very simple and easy to understand.<br>
 
The example uses MicroPython programming, very simple and easy to understand.<br>
This MakePython A9G can do nothing. It needs a microcontroller like ESP32 to drive it. The following two routines are implemented by A9G with MakePython ESP32, they can be directly combined to achieve various functions.<br>
+
MakePython Audio board cannot work alone. It needs a microcontroller like ESP32(Wrover) to drive it. With Makepython ESP32(Wrover) and Makepython Audio, they can be directly combined to fulfill various functions about audio.<br>
 
[[File:MakePython_A9G_3.JPG|700px]]<br>
 
[[File:MakePython_A9G_3.JPG|700px]]<br>
 
You can get MakePython ESP32 from here:  
 
You can get MakePython ESP32 from here:  
 
https://www.makerfabs.com/makepython-esp32.html<br>
 
https://www.makerfabs.com/makepython-esp32.html<br>
 +
 +
Hardware interface
 +
{| cellspacing="0" cellpadding="5" border="1"
 +
|-
 +
| align="center" width="150" | '''I2S Interface'''
 +
| align="center" width="200" | '''ESP32 PIN'''
 +
| align="center" width="200" | '''Remark'''
 +
 +
|- style="font-size: 90%"
 +
| align="center" | I2S_BCK
 +
| align="center" | IO26
 +
|- style="font-size: 90%"
 +
| align="center" | I2S_LCK
 +
| align="center" | IO25
 +
| align="center" | WS
 +
|- style="font-size: 90%"
 +
| align="center" | I2S_DIN
 +
| align="center" | IO27
 +
|}
 +
 +
ESP32 Booting Mode:
 +
{| cellspacing="0" cellpadding="5" border="1"
 +
|-
 +
| align="center" width="150" | '''PIN'''
 +
| align="center" width="150" | '''Default'''
 +
| align="center" width="150" | '''SPI Boot(Running Mode)'''
 +
| align="center" width="150" | '''Download Boot(Download Mode)'''
 +
|- style="font-size: 90%"
 +
| align="center" | GPIO0
 +
| align="center" | Pull-up
 +
| align="center" | 1
 +
| align="center" | 0
 +
|- style="font-size: 90%"
 +
| align="center" | GPIO2
 +
| align="center" | Pull-down
 +
| align="center" | Don’t care
 +
| align="center" | 0
 +
|- style="font-size: 90%"
 +
| align="center" | GPIO12(MTDI)
 +
| align="center" | Pull-down
 +
| align="center" | 0
 +
| align="center" | 0
 +
|}
 +
Note: when GPIO12 set to LOW, the internal LDO (VDD_SDIO) output 3.3V; when GPIO12 set to HIGH, the internal LDO (VDD_SDIO) output 1.8V, so it may get fails when you download the firmware if you had insert the Makepython Audio.
 +
 
Step:<br>
 
Step:<br>
1. Plug in the GPS antenna<br>
+
1. Connecting MakePython ESP32 to PC via microUSB cable.<br>
2. Plug to MakePython ESP32, connect the USB to the PC.<br>
+
2. Open flash_download_tools_v3.6.8,Select ESP32 DownloadTool<br>
[[File:MakePython_A9G_4.JPG|700px]]<br>
+
[[File:MakePython_Audio_06.png|200px]]<br>
3. Sample code, or you can get it from here: [https://github.com/Makerfabs/MakePython-A9G A9G_GPS.py]<br>
+
3. Select the related COM port, baud rate 921600,erase the ESP32 first.<br>
 +
[[File:MakePython_Audio_07.png|300px]]<br>
 +
Note: In my PC, there is COM272 that may different in your PC.<br>
 +
4. Find the firmware bin file esp32_tts.bin,address 0x1000<br>
 +
[[File:MakePython_Audio_08.png|300px]]<br>
 +
5. Click the START to download the firmware.<br>
 +
[[File:MakePython_Audio_09.png|500px]]<br>
 +
Wait until it finished.<br>
 +
[[File:MakePython_Audio_10.png|500px]]<br>
 +
6. Insert the ESP32 board to Makepython Audio<br>
 +
[[File:MakePython_Audio_11.png|500px]]<br>
 +
7. Open uPyCraft, select com port from '''Tools->Serial'''<br>
 +
[[File:MakePython_Audio_12.png|500px]]<br>
 +
8. Select esp32 from '''Tools->Board'''<br>
 +
[[File:MakePython_Audio_13.png|500px]]<br>
 +
9. Connect the Makepython ESP32 board.<br>
 +
[[File:MakePython_Audio_14.png|300px]]<br>
 +
10. Open main.py from '''File->Open''', you can get the code from here: [https://github.com/Makerfabs/MakerPython-Audio tts-test.py]<br>
 +
[[File:MakePython_Audio_15.png|500px]]<br>
 +
11. Click DownloadAndRun Button to download the main.py file to Makepython ESP32.<br>
 +
[[File:MakePython_Audio_16.png|500px]]<br>
 +
Input say("hello,world")  it will speech hello world.<br>
 +
 
 +
Or also, you can update the firmware via command terminal, such as:<br>
 +
<pre>
 +
esptool.py --chip esp32 --port COM272 erase_flash
 +
esptool.py --chip esp32 --port COM272 write_flash --flash_mode dio -z 0x1000 esp32-tts.bin
 +
</pre>
 +
Code Show:<br>
 
<pre>
 
<pre>
from machine import UART,Pin,I2C
+
from machine import I2S
import machine
+
from machine import Pin
import ssd1306
+
import array
import utime
+
import speech
 +
 
  
uart = UART(2, baudrate=115200, rx=21, tx=22,timeout=10)
+
bck_pin = Pin(26)
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)      #Init i2c
+
ws_pin = Pin(25)
lcd=ssd1306.SSD1306_I2C(128,64,i2c)         
+
sdout_pin = Pin(27)
lcd.text('Hello, Makerfabs',0,0)
 
lcd.text('waiting...',0,8)
 
lcd.show()  
 
  
A9G_RESET_PIN = Pin(33, Pin.OUT)
 
A9G_RESET_PIN.value(0)            #set pin to low
 
  
utime.sleep_ms(2000)
+
audio_out = I2S(I2S.NUM0,
A9G_PWR_KEY = Pin(27, Pin.OUT)
+
                bck=bck_pin,
A9G_PWR_KEY.value(0)
+
                ws=ws_pin,
utime.sleep_ms(2000)
+
                sdout=sdout_pin,
A9G_PWR_KEY.value(1)
+
                standard=I2S.PHILIPS,
utime.sleep_ms(20000)
+
                mode=I2S.MASTER_TX,
 +
                dataformat=I2S.B16,
 +
                channelformat=I2S.ONLY_RIGHT,
 +
                samplerate=16000,
 +
                dmacount=16,
 +
                dmalen=512)
  
#Display line wrap
+
a = bytearray(16000*8)
p=0
+
size = speech.say('Hello, wolrld', a)
def text(string,c=0,r=0):
+
audio_out.write(a[:size])
    global p
 
    if p>80:
 
        p=0
 
        lcd.fill(0)
 
    colum=int(len(string)/15)+2
 
    i=0
 
    for a in range(0,colum):
 
        lcd.text(string[i:i+15],c,a*8)
 
        i=i+15
 
    lcd.show()  
 
  
if True:
+
def say(text):
  lcd.fill(0)
+
    a = bytearray(16000*8)
  uart.write('AT+GPS=1\r\n')#1: turn on GPS  0:Turn off GPS
+
    size = speech.say(text, a)
  utime.sleep_ms(1000)
+
     audio_out.write(a[:size])
  uart.write('AT+GPSRD=10\r\n')
+
   
  utime.sleep_ms(1000)
+
   
  uart.write('AT+LOCATION=2\r\n')
+
def close():
  utime.sleep_ms(1000)
+
     audio_out.deinit()
  while True:     
 
     if uart.any():
 
      uart.write('AT+LOCATION=2\r\n') #Get GPS address
 
      bin_data = uart.readline()
 
      msg = len(bin_data)
 
      print(bin_data)
 
      mystr = str(bin_data[0:msg-2],'utf-8')
 
      lcd.fill(0)
 
      text(mystr,0,0) 
 
      lcd.show()  
 
     utime.sleep_ms(2000)
 
 
</pre>
 
</pre>
4. The screen displays NEMA message.<br>
 
[[File:MakePython_A9G_5.JPG|700px]]<br>
 
<br>
 
5. The screen displays GPS address.<br>
 
[[File:MakePython_A9G_20.JPG|700px]]<br>
 
*Location information may not be received indoors, and GPS NOT FIX NOW will be displayed on the screen if reception fails.<br>
 
<br>
 
6. We can show our location where we are in https://www.gps-coordinates.net/.<br>
 
[[File:MakePython_A9G_21.JPG|900px]]<br>
 
  
===Project_2: Display SMS message on OLED display===
+
Resute:<br>
Please note that each text message may require some fees depending on your local GSM operator, make sure the SIM card is active, and leave enough money for this application.<br>
+
[[File:MakePython_Audio_18.png|500px]]<br>
<br>
+
 
This example also uses MakePython ESP32 for MicroPython programming.<br>
+
===Project_2: Play music from micro SD card===
Step:<br>
+
1. Copy the taunt-16k-16bits-mono-12db.wav file to the microSD card then insert to Makepython Audio.<br>
1. Plug in the GSM antenna<br>
+
 
2. Insert a mini SIM card(GPS does work without a SIM)<br>
+
2. Download the [https://github.com/Makerfabs/MakerPython-Audio play-mono-wav-from-sdcard.py] file like previous.<br>
[[File:MakePython_A9G_6.JPG|700px]]<br>
 
3. Plug to MakePython ESP32, connect the USB to the PC.<br>
 
4. Sample code, or you can get it from here: [https://github.com/Makerfabs/MakePython-A9G A9G_GSM.py]<br>
 
 
<pre>
 
<pre>
from machine import UART,Pin,I2C
+
from machine import I2S
import machine
+
from machine import SDCard
import ssd1306
+
from machine import Pin
import utime
+
 
uart = UART(2, baudrate=115200, rx=21, tx=22,timeout=10)
+
#======= USER CONFIGURATION =======
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)      #Init i2c
+
WAV_FILE = 'taunt-16k-16bits-mono-12db.wav'
lcd=ssd1306.SSD1306_I2C(128,64,i2c)         
+
#WAV_FILE = 'LightMusic.wav'
A9G_RESET_PIN = Pin(33, Pin.OUT)
+
SAMPLE_RATE_IN_HZ = 16000
A9G_RESET_PIN.value(0)            
+
#======= USER CONFIGURATION =======
utime.sleep_ms(2000)
+
 
A9G_PWR_KEY = Pin(27, Pin.OUT)  
+
bck_pin = Pin(26)
A9G_PWR_KEY.value(0)
+
ws_pin = Pin(25)
utime.sleep_ms(2000)
+
sdout_pin = Pin(27)
A9G_PWR_KEY.value(1)
+
 
utime.sleep_ms(20000)
+
# channelformat settings:
p=0
+
#    mono WAV:  channelformat=I2S.ONLY_LEFT
def text(string,c=0,r=0):
+
audio_out = I2S(
     global p
+
    I2S.NUM0,
     if p>80:
+
    bck=bck_pin, ws=ws_pin, sdout=sdout_pin,
        p=0
+
     standard=I2S.PHILIPS,
        lcd.fill(0)
+
     mode=I2S.MASTER_TX,
     colum=int(len(string)/15)+2
+
     dataformat=I2S.B16,
     i=0
+
     channelformat=I2S.ONLY_LEFT,
     for a in range(0,colum):
+
     samplerate=SAMPLE_RATE_IN_HZ,
        lcd.text(string[i:i+15],c,a*8)
+
    dmacount=10, dmalen=512)
        i=i+15
+
 
    lcd.show()  
+
# configure SD card
if True:
+
#  slot=2 configures SD card to use the SPI3 controller (VSPI), DMA channel = 2
    uart.write('AT+GPS=0\r\n')#1: turn on GPS  0:Turn off GPS
+
#  slot=3 configures SD card to use the SPI2 controller (HSPI), DMA channel = 1
    utime.sleep_ms(1000)
+
sd = SDCard(slot=3, sck=Pin(18), mosi=Pin(23), miso=Pin(19), cs=Pin(22))
    uart.write('AT+CCID\r\n')
+
uos.mount(sd, "/sd")
    utime.sleep_ms(1000)
+
wav_file = '/sd/{}'.format(WAV_FILE)
    uart.write('AT+CREG?\r\n')
+
wav = open(wav_file,'rb')
    utime.sleep_ms(1000)
+
 
    uart.write('AT+CGATT=1\r\n')
+
# advance to first byte of Data section in WAV file
    utime.sleep_ms(1000)
+
pos = wav.seek(44)  
    uart.write('AT+CGACT=1,1\r\n')
+
 
    utime.sleep_ms(1000)
+
# allocate sample arrays
    uart.write('AT+CGDCONT=1,\"IP\",\"CMNET\"\r\n')
+
#  memoryview used to reduce heap allocation in while loop
    utime.sleep_ms(1000)
+
wav_samples = bytearray(1024)
    uart.write('AT+CSQ\r\n')
+
wav_samples_mv = memoryview(wav_samples)
    utime.sleep_ms(1000)
+
 
    uart.write('AT+CPMS="SM","SM","SM"\r\n')
+
print('Starting')
    utime.sleep_ms(1000)
+
# continuously read audio samples from the WAV file
    uart.write('AT+CMGF=1\r\n')
+
# and write them to an I2S DAC
     utime.sleep_ms(1000)
+
while True:
    #uart.write('AT+CMGL="ALL"\r\n')
+
     try:
    #utime.sleep_ms(1000)
+
        num_read = wav.readinto(wav_samples_mv)
    while True:    
+
        num_written = 0
      if uart.any():
+
        # end of WAV file?
        lcd.fill(0)
+
        if num_read == 0:
         uart.write('AT+CMGR=1\r\n')
+
            # advance to first byte of Data section
        utime.sleep_ms(1000)
+
            pos = wav.seek(44)  
         bin_data = uart.readline()
+
        else:
        print(bin_data)
+
            # loop until all samples are written to the I2S peripheral
        text(bin_data,0,0)
+
            while num_written < num_read:
        lcd.show()  
+
                num_written += audio_out.write(wav_samples_mv[num_written:num_read], timeout=0)
      utime.sleep_ms(2000)
+
    except (KeyboardInterrupt, Exception) as e:
 +
         print('caught exception {} {}'.format(type(e).__name__, e))
 +
         break
 +
   
 +
wav.close()
 +
uos.umount("/sd")
 +
sd.deinit()
 +
audio_out.deinit()
 +
print('Done')
 
</pre>
 
</pre>
4. The screen displays the phone number, time and information for sending SMS.<br>
+
3. Reset the board, Makepython Audio will make sound.<br>
[[File:MakePython_A9G_7.JPG|500px]]<br>
 
*Mobile phone to send SMS.<br>
 
[[File:MakePython_A9G_8.JPG|700px]]<br>
 
*Display the phone number and time of sending SMS<br>
 
[[File:MakePython_A9G_10.JPG|700px]]<br>
 
*Show received content
 
  
 
== FAQ  ==
 
== FAQ  ==
Line 233: Line 273:
  
 
== Resources ==
 
== Resources ==
*[https://www.makerfabs.com/wiki/images/a/a0/Gprs_series_module_at_instruction_set_v1.0%28A9_A9G%29.pdf AT Command Set]
+
*[https://github.com/Makerfabs/MakerPython-Audio Github Source]
*[https://www.makerfabs.com/wiki/images/2/22/MakePython_A9G_GPS_Tracker_V1.0.pdf V1.0 Schematic]
 

Latest revision as of 03:53, 10 July 2020

Introduction

The MakePython Audio board uses the uDA1334 digital to analog converter (DAC), which converts I2S (not be confused with I2C) audio to an analog signal to drive speakers. The I2S Audioboard converts the digital audio signals using the I2S standard to an analog signal.

Model: MPAUDIO
MakePython Audio 01.png

Features

• NXP Low power Audio DAC: uDA1334
• Integrated digital filter plus DAC
• Supports sample frequencies from 8 to 100 kHz
• Automatic system clock versus sample rate detection
• Low power consumption
• I2S-bus and LSB-justified format compatible
• Digital de-emphasis for 44.1 kHz sampling rate
• Support Micro SD Card
• Working Temperature: -40 – 85℃
• Size: 70*32.6mm

Interface Function

MakePython Audio 02.png
①toggle switch
②toggle switch
③Audio IC:uDA1443
④D3: PWR LED
⑤microSD card
⑥D1:LED controlled by IO21
⑦audio jack(3.5mm)

Required Materials

1. Makepython ESP32
2. MakePython Audio v2.0
3. microUSB Cable
4. Speaker
5. PC
6. microSD card

MakePython Audio 03.png
System diagram
Makepython Audio can’t worl alone, it need work with Makepython ESP32(Wrover).
MakePython Audio 04.png
Application

You can get Makepython ESP32(Wrover) from:
https://www.makerfabs.com/makepython-esp32.html

Note: Makepython ESP32 Wroom’s RAM is not enough for audio application, so you need Makepython ESP32(Wrover)

Usage

Warning: Don't operate when powered on in case of short circuit.
This guides help you how to use makepython Audio with Makepython ESP32 to fulfill text to speech(TTS). It’s very simple and easy to understand. It also can play music from SD card or Flash too.

Makepython ESP32(Wrover) used USB-to-UART(CP2104) to upload firmware, you can install the CP2102 driver from:
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers

Indicator LED Status

Indicator LED Status
D3 Light on when powered on, light off when powered off
D1 Light on when IO21 output low, light off when IO21 output High

Project_1: Text to speech

The example uses MicroPython programming, very simple and easy to understand.
MakePython Audio board cannot work alone. It needs a microcontroller like ESP32(Wrover) to drive it. With Makepython ESP32(Wrover) and Makepython Audio, they can be directly combined to fulfill various functions about audio.
MakePython A9G 3.JPG
You can get MakePython ESP32 from here: https://www.makerfabs.com/makepython-esp32.html

Hardware interface

I2S Interface ESP32 PIN Remark
I2S_BCK IO26
I2S_LCK IO25 WS
I2S_DIN IO27

ESP32 Booting Mode:

PIN Default SPI Boot(Running Mode) Download Boot(Download Mode)
GPIO0 Pull-up 1 0
GPIO2 Pull-down Don’t care 0
GPIO12(MTDI) Pull-down 0 0

Note: when GPIO12 set to LOW, the internal LDO (VDD_SDIO) output 3.3V; when GPIO12 set to HIGH, the internal LDO (VDD_SDIO) output 1.8V, so it may get fails when you download the firmware if you had insert the Makepython Audio.

Step:
1. Connecting MakePython ESP32 to PC via microUSB cable.
2. Open flash_download_tools_v3.6.8,Select ESP32 DownloadTool
MakePython Audio 06.png
3. Select the related COM port, baud rate 921600,erase the ESP32 first.
MakePython Audio 07.png
Note: In my PC, there is COM272 that may different in your PC.
4. Find the firmware bin file esp32_tts.bin,address 0x1000
MakePython Audio 08.png
5. Click the START to download the firmware.
MakePython Audio 09.png
Wait until it finished.
MakePython Audio 10.png
6. Insert the ESP32 board to Makepython Audio
MakePython Audio 11.png
7. Open uPyCraft, select com port from Tools->Serial
MakePython Audio 12.png
8. Select esp32 from Tools->Board
MakePython Audio 13.png
9. Connect the Makepython ESP32 board.
MakePython Audio 14.png
10. Open main.py from File->Open, you can get the code from here: tts-test.py
MakePython Audio 15.png
11. Click DownloadAndRun Button to download the main.py file to Makepython ESP32.
MakePython Audio 16.png
Input say("hello,world") it will speech hello world.

Or also, you can update the firmware via command terminal, such as:

esptool.py --chip esp32 --port COM272 erase_flash
esptool.py --chip esp32 --port COM272 write_flash --flash_mode dio -z 0x1000 esp32-tts.bin

Code Show:

from machine import I2S
from machine import Pin
import array
import speech


bck_pin = Pin(26)
ws_pin = Pin(25)
sdout_pin = Pin(27)


audio_out = I2S(I2S.NUM0,
                bck=bck_pin,
                ws=ws_pin,
                sdout=sdout_pin,
                standard=I2S.PHILIPS,
                mode=I2S.MASTER_TX,
                dataformat=I2S.B16,
                channelformat=I2S.ONLY_RIGHT,
                samplerate=16000,
                dmacount=16,
                dmalen=512)

a = bytearray(16000*8)
size = speech.say('Hello, wolrld', a)
audio_out.write(a[:size])

def say(text):
    a = bytearray(16000*8)
    size = speech.say(text, a)
    audio_out.write(a[:size])
    
    
def close():
    audio_out.deinit()

Resute:
MakePython Audio 18.png

Project_2: Play music from micro SD card

1. Copy the taunt-16k-16bits-mono-12db.wav file to the microSD card then insert to Makepython Audio.

2. Download the play-mono-wav-from-sdcard.py file like previous.

from machine import I2S
from machine import SDCard
from machine import Pin

#======= USER CONFIGURATION =======
WAV_FILE = 'taunt-16k-16bits-mono-12db.wav'
#WAV_FILE = 'LightMusic.wav'
SAMPLE_RATE_IN_HZ = 16000
#======= USER CONFIGURATION =======

bck_pin = Pin(26)
ws_pin = Pin(25)
sdout_pin = Pin(27)

# channelformat settings:
#     mono WAV:  channelformat=I2S.ONLY_LEFT
audio_out = I2S(
    I2S.NUM0, 
    bck=bck_pin, ws=ws_pin, sdout=sdout_pin, 
    standard=I2S.PHILIPS, 
    mode=I2S.MASTER_TX,
    dataformat=I2S.B16, 
    channelformat=I2S.ONLY_LEFT,
    samplerate=SAMPLE_RATE_IN_HZ,
    dmacount=10, dmalen=512)

# configure SD card
#   slot=2 configures SD card to use the SPI3 controller (VSPI), DMA channel = 2
#   slot=3 configures SD card to use the SPI2 controller (HSPI), DMA channel = 1
sd = SDCard(slot=3, sck=Pin(18), mosi=Pin(23), miso=Pin(19), cs=Pin(22))
uos.mount(sd, "/sd")
wav_file = '/sd/{}'.format(WAV_FILE)
wav = open(wav_file,'rb')

# advance to first byte of Data section in WAV file
pos = wav.seek(44) 

# allocate sample arrays
#   memoryview used to reduce heap allocation in while loop
wav_samples = bytearray(1024)
wav_samples_mv = memoryview(wav_samples)

print('Starting')
# continuously read audio samples from the WAV file 
# and write them to an I2S DAC
while True:
    try:
        num_read = wav.readinto(wav_samples_mv)
        num_written = 0
        # end of WAV file?
        if num_read == 0:
            # advance to first byte of Data section
            pos = wav.seek(44) 
        else:
            # loop until all samples are written to the I2S peripheral
            while num_written < num_read:
                num_written += audio_out.write(wav_samples_mv[num_written:num_read], timeout=0)
    except (KeyboardInterrupt, Exception) as e:
        print('caught exception {} {}'.format(type(e).__name__, e))
        break
    
wav.close()
uos.umount("/sd")
sd.deinit()
audio_out.deinit()
print('Done')

3. Reset the board, Makepython Audio will make sound.

FAQ

You can list your questions here or contact with support@makerfabs.com for technology support.

Resources