Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Circuitpython raspi pico , PCM5102A and SD card
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Circuitpython raspi pico , PCM5102A and SD card

by Soltrar on Thu Jun 10, 2021 2:13 pm

Hi all,

It's my first post. My apologies in advance if it's not the right place for this topic.

I'm trying to play audio from the pico using a PCM5102A.

Everything goes fine if I get the audio file from the internal pico memory.

If I try to get the same audio file from the sd card, the wav files sounds very distorted and sometimes the code doesn't detect the end of the audio.

Could someone give me a clue about what could be the issue?

Code: Select all | TOGGLE FULL SIZE
import digitalio
import audiobusio
import audiocore
import board
import time
import busio
import sdcardio
import storage

i2s = None
vfs = None
sd = None
spi = None

MOSI = board.GP11
MISO = board.GP12
clk = board.GP10
cs = board.GP15

BCK_PIN = board.GP26
LCK_PIN = board.GP27
DIN_PIN = board.GP28

def init():
    initI2S()
    # initSDCard()

def initSDCard():
    global spi
    spi = busio.SPI(clk, MOSI=MOSI, MISO=MISO)

    global sd
    sd = sdcardio.SDCard(spi, cs)

    print("getting vfs...")
    global vfs
    # vfs = storage.VfsFat(sd)

    print("mounting sd...")
    # storage.mount(vfs, '/sd')

def initI2S():
    global i2s
    i2s = audiobusio.I2SOut(BCK_PIN, LCK_PIN, DIN_PIN)

def getWavFileFlash():
    print("opennign wav ...")
    f = open("thunder.wav", "rb")

    print("creating wav object...")
    wav = audiocore.WaveFile(f)

    return wav

def getWavFileSD():
    spi = busio.SPI(clk, MOSI=MOSI, MISO=MISO)

    sd = sdcardio.SDCard(spi, cs)

    print("getting vfs...")
    vfs = storage.VfsFat(sd)

    print("mounting sd...")
    storage.mount(vfs, '/sd')

    print("opennign wav ...")
    f = open("/sd/thunder_16bits.wav", "rb")

    print("creating wav object...")
    wav = audiocore.WaveFile(f)

    return wav

def thunderSound():
    wav = getWavFileFlash()

    print("starting playing audio...")
    i2s.play(wav)

    while i2s.playing:
        print("playing audio...")
        time.sleep(0.5)
        pass

    print("sound ends")

def bucle():
    while True:
        thunderSound()
        time.sleep(60)

init()
bucle()

Soltrar
 
Posts: 3
Joined: Thu Jun 10, 2021 2:02 pm

Re: Circuitpython raspi pico , PCM5102A and SD card

by mikeysklar on Thu Jun 10, 2021 7:11 pm

Thanks for the clean example code to work with.

Do you think that reading off the SD card for the audio file could be the bottleneck? We do have some benchmarking scripts you can use to see the performance of your SD card. There are some different SD libraries you can consider using for optimizing performance.

https://learn.adafruit.com/adafruit-mic ... ard-access

mikeysklar
 
Posts: 4559
Joined: Mon Aug 01, 2016 8:10 pm

Re: Circuitpython raspi pico , PCM5102A and SD card

by Soltrar on Sat Jun 12, 2021 2:20 am

Hi Again,

Thanks a lot for your answer. I can provide now more information.

SD card I/O benchmarks

write 512 x 1 in 0.00292969s 174.8Kb/s
read 512 x 1 in 0.000976562s 524.3Kb/s

write 512 x 16 in 0.0302734s 270.6Kb/s
read 512 x 16 in 0.0146484s 559.2Kb/s

write 4096 x 1 in 0.00683594s 599.2Kb/s
read 4096 x 1 in 0.00585938s 699.1Kb/s

write 4096 x 16 in 0.0986328s 664.4Kb/s
read 4096 x 16 in 0.0878906s 745.7Kb/s

logging test
Logged 10000 lines in 48.7148 seconds, 4871us/line
296825 bytes written, 6.1Kb/s

And I think that the most important is that the auido is going to be distorted just only mounting the SD card. Without getting the audio from the sd. I have identified that this line :

vfs = storage.VfsFat(sd)

is the one that is breaking the audio.

UPDATED: I have tried with another SD car reader device (same model, I bought 5 of them) and it's nearly the same behaviour, same figures from the benchmark. The audio is distorted and sometimes it's not able to play it. When it's not able to play the audio I have to remove the power from the raspi to reset everything, otherwise the code is not working anymore with a soft reset (it's stopped at "storage.VfsFat(sd)").

Regards.

Soltrar
 
Posts: 3
Joined: Thu Jun 10, 2021 2:02 pm

Re: Circuitpython raspi pico , PCM5102A and SD card

by Soltrar on Mon Jun 14, 2021 10:09 am

Hi all,

Based on my last information. Do you think that it could be a bug? Could someone else do the same configuration?, so that would help me to discard a hardware problem and confirm that it could be a software related issue.

If someone else is doing the same configuration, could you please provide the hardware information?

Thanks a lot,
:)

Soltrar
 
Posts: 3
Joined: Thu Jun 10, 2021 2:02 pm

Re: Circuitpython raspi pico , PCM5102A and SD card

by mikeysklar on Mon Jun 14, 2021 4:46 pm

These RAW read / write times are pretty horrible. Once you consider other overhead involved it seems like the SD card is running too slow. I don't believe you have a hardware failure or bug as everything works, just poorly.

Let's examine your setup a little closer.

What model/brand/spec SD card are you using? Please be specific and take a photo if necessary to show the relevant speed markings on the SD card.

What is the sample rate of the WAV file you are using? If it is higher than 16-bit can you cut it down for testing purposes? MP3 would also be an option with a 44.1khz/s bit rate and up to 128kbit/s.

mikeysklar
 
Posts: 4559
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.