Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any valid example for M5Stack Core to play wav on Speaker #296

Open
2 tasks done
ExtremeGTX opened this issue Nov 7, 2022 · 3 comments
Open
2 tasks done

Any valid example for M5Stack Core to play wav on Speaker #296

ExtremeGTX opened this issue Nov 7, 2022 · 3 comments
Assignees

Comments

@ExtremeGTX
Copy link

Describe the bug

I tried to use the following code to play some wavfiles but unfortunatly it didn't work.

The MicroPython runtime keep complaining about I2S.MODE_MASTER | I2S.MODE_TX | I2S.MODE_DAC_BUILT_IN that neither of them is part of I2S module. could you please point me to an example which works on m5stack Core basic (The EOL version).

from machine import I2S, SDCard, Pin
from m5stack import *
import os, uos
from wav import wave

#mount the sd card
sd = SDCard(slot = 2, sck = Pin(23), miso = Pin(33), mosi = Pin(19), freq = 10000000)

#initialize the I2S device
i2s = I2S(  mode = I2S.MODE_MASTER | I2S.MODE_TX | I2S.MODE_DAC_BUILT_IN,
            rate = 16000,
            bits = 16,
            channel_format = I2S.CHANNEL_ONLY_LEFT,
            data_format = I2S.FORMAT_I2S_MSB)

#create a function to play the wav
def wav_player(fname):
    wav = wave.open(fname)
    i2s.set_dac_mode(I2S.DAC_RIGHT_EN)
    i2s.sample_rate(wav.getframerate())
    i2s.bits(wav.getsampwidth() * 8)
    i2s.nchannels(wav.getnchannels())
    i2s.volume(20)

    while True:
        data = wav.readframes(1024)
        if len(data) > 0:
            i2s.write(data)
        else:
            wav.close()
            break

# Playing WAV audio file
lcd.clear()
lcd.print('working',0,0,0xffffff)

try:
    uos.mountsd(sd, '/sd')
except:
    #os.mountsd()
    pass

speaker.setVolume(2)
#kill the weird start noise glitch
i2s.stop()

#point the directory to the files you have on your sd card - works best with short low file size wavs
while True:
    if btnA.wasPressed():
        wav_player('/flash/res/yourfile.wav')
        i2s.stop()
    if btnB.wasPressed():
        wav_player('/sd/yourOtherFile.wav')
        i2s.stop()
    if btnC.wasPressed():
        wav_player('/sd/anotherfile.wav')
        i2s.stop()

To reproduce

  1. Use M5Burner-v3-beta-win-x64 to flash UiFlow v1.10.7.1 to the m5stack core basic
  2. Go to https://flow.m5stack.com/
  3. switch to python mode
  4. Copy the code above
  5. Click Run
  6. Check the device screen for the related python error

Expected behavior

The code should run without displaying python errors.

Screenshots

No response

Environment

Additional context

No response

Issue checklist

  • I searched for previous reports in the issue tracker
  • My report contains all necessary details
@ExtremeGTX ExtremeGTX changed the title any valid example for M5Stack Core any valid example for M5Stack Core play wave on Speaker Nov 7, 2022
@ExtremeGTX ExtremeGTX changed the title any valid example for M5Stack Core play wave on Speaker Any valid example for M5Stack Core to play wave on Speaker Nov 7, 2022
@ExtremeGTX ExtremeGTX changed the title Any valid example for M5Stack Core to play wave on Speaker Any valid example for M5Stack Core to play wav on Speaker Nov 7, 2022
@Tinyu-Zhao
Copy link
Collaborator

@imliubo Please help check this.

@Tinyu-Zhao Tinyu-Zhao assigned Tinyu-Zhao and imliubo and unassigned Tinyu-Zhao Nov 9, 2022
@chipguyhere
Copy link
Contributor

I don't think Core has any I2S hardware in the first place. I think it plays analog audio directly from ESP32 pin 25, which supports direct digital-to-analog output. There's a Speaker class in the library that looks like it can play wave audio just by loosely banging it out to the pin.

@floriansenf
Copy link

DId someone find a solution? Facing the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants