VS1053b audio drivers: play music, speech or sound FX

Discuss development of drivers for external hardware and components, such as LCD screens, sensors, motor drivers, etc.
Target audience: Users and developers of drivers.
User avatar
pythoncoder
Posts: 4351
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

VS1053b audio drivers: play music, speech or sound FX

Post by pythoncoder » Sat May 16, 2020 3:09 pm

Synchronous and asynchronous drivers may be found in this repo. The have been tested on this Adafruit breakout on Pyboards, ESP32 and ESP8266.

The capabilities of the synchronous driver are as follows.

On all platforms 128Kbps and 256Kbbs MP3 files may be played back. Files may be located on the onboard SD card or elsewhere. On the Pyboard CD quality FLAC files may also be played; audio may also be recorded, but not to audiophile standards. It produces excellent quality speech recordings.

The asynchronous driver is effectively Pyboard only - MP3 playback (up to 256Kbps) is the limit. The problem here is the limited buffering on the chip coupled with the overhead of task scheduling. Application design needs some care. When uasyncio acquires priority I/O scheduling the constraints will be reduced.
Peter Hinch

zaord
Posts: 32
Joined: Fri Jan 31, 2020 3:56 pm

Re: VS1053b audio drivers: play music, speech or sound FX

Post by zaord » Wed Jul 15, 2020 12:20 pm

Hi Peter !

Do you have any idea to play wave sample files in wave format or mp3 in the same time to make a polyphonic sampler ?
Would it be more simple to use a Pyboard or esp32 ?
I would need to play polyphonic sample up to 8 files simultaneously. Best and thanks

User avatar
pythoncoder
Posts: 4351
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: VS1053b audio drivers: play music, speech or sound FX

Post by pythoncoder » Wed Jul 15, 2020 5:04 pm

That sounds very ambitious. The data rates would be high. As you'll see in my docs, I hit data rate problems on some platforms. The Pyboard is the best platform for sustained high data rates because there is no underlying OS grabbing time slices, but I'm not sure what you want to do is feasible on either platform.
Peter Hinch

zaord
Posts: 32
Joined: Fri Jan 31, 2020 3:56 pm

Re: VS1053b audio drivers: play music, speech or sound FX

Post by zaord » Thu Jul 16, 2020 9:53 am

Hi,
I know aldready 2 project working through stm32 making similar things :
https://hackaday.io/project/8320-the-be ... 32-sampler
http://www.midibox.org/dokuwiki/doku.ph ... ple_player

What do you think could make the projet a miss in micropython ? Because of the language ?

Best and kind regards

SpotlightKid
Posts: 417
Joined: Wed Apr 08, 2015 5:19 am

Re: VS1053b audio drivers: play music, speech or sound FX

Post by SpotlightKid » Thu Jul 16, 2020 1:46 pm

You would need to mix the different samples when they are played into a continuous (stereo) audio stream and send that to the audio card. So you'd need <time polyphony> waveform oscillators and at least <times polyphony> amplitude envelope generators (plus optionally as many filters, LFOs etc.). Basically you need to build a synthesizer, just that the oscillators play a sample instead of generating a waveform. The principles of building a software synthesizer are similar, regardless of how the audio output is handled. There a good tutorials on that topic out there, though most assume you are using some audio plugin framework in a PC setting. Here's one I liked.

A garbage-collected run-time environment like MicoPython is not ideal for audio / DSP programming, since you don't want any interruptions in the audio stream, just because the GC decides it's time to stop everything for dozens to hundred of milliseconds.

I would look into putting the audio DSP part in a C extension module, which runs its own audio thread, and only control that from MicroPython. But a RTOS foundation would be better.

User avatar
pythoncoder
Posts: 4351
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: VS1053b audio drivers: play music, speech or sound FX

Post by pythoncoder » Fri Jul 17, 2020 6:05 am

SpotlightKid wrote:
Thu Jul 16, 2020 1:46 pm
...
A garbage-collected run-time environment like MicoPython is not ideal for audio / DSP programming, since you don't want any interruptions in the audio stream, just because the GC decides it's time to stop everything for dozens to hundred of milliseconds...
On a Pyboard, if you regularly issue gc.collect(), blocking can be kept down to 1-2ms in my experience. But even that may be too long for audio work. I think figures in the 100ms region apply to ESP32 with SPIRAM.
I would look into putting the audio DSP part in a C extension module
Good point. My negative comments assumed Python code. I still think it's an ambitious project ;)
Peter Hinch

zaord
Posts: 32
Joined: Fri Jan 31, 2020 3:56 pm

Re: VS1053b audio drivers: play music, speech or sound FX

Post by zaord » Fri Jul 17, 2020 1:11 pm

Thanks Guys for your answears !
The other projects i quote just before is made with freeRTOS, seems effectively a good way. I was questioning myself because the board used in the hardware is quite old, but seems to works with 8 simultaneous tracks, so that's a good point to start.

I don't wants to spends so much time on a such project right know so I will see how I coiuld use the Midibox project right know and when I will have more skill about understand more the topic I will have make a try.

I know there is some possibilities to do it with Teensy 4.1 audio module also.
Cheers !

IHOXOHI
Posts: 77
Joined: Sat Apr 25, 2020 7:31 am

Re: VS1053b audio drivers: play music, speech or sound FX

Post by IHOXOHI » Thu Jul 23, 2020 7:10 am

Hi Peter,
Thanks for your library.

I tried it...

When I play sound I have the return : " File "/flash/lib/vs1053_syn.py", line 370, in patch
OSError: [Errno 2] ENOENT"

When I record, it looks working, but in final I have an audio file without sound...

Have I to use an amplificator with the adafruit Max4466 electret that I use for record?

Otherwise, do you know how much time it could be possible to record on a pyb-d?

Thanks.

IHOXOHI
Posts: 77
Joined: Sat Apr 25, 2020 7:31 am

Re: VS1053b audio drivers: play music, speech or sound FX

Post by IHOXOHI » Thu Jul 23, 2020 8:24 am

Yop,

I have tested differents connections...

The play doesn't work yet.

But the record works, not correctly. I have a sound very low and with a lots of "parasits sound". And the playback works.
The noise happen during record on the headphone, and stay on the record file.
Without headphone, noise stay on the record.

The connection of my micro "electret" is vcc on vcc, gnd on gnd, and out(of the electret) on MIC+.

Have you an idea to stop noise sound?

User avatar
pythoncoder
Posts: 4351
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: VS1053b audio drivers: play music, speech or sound FX

Post by pythoncoder » Thu Jul 23, 2020 5:23 pm

IHOXOHI wrote:
Thu Jul 23, 2020 7:10 am
...
When I play sound I have the return : " File "/flash/lib/vs1053_syn.py", line 370, in patch
OSError: [Errno 2] ENOENT"
It's hard to diagnose this without seeing your calling code - but It looks like you've issued .patch(). You only really need to do this if you want to play FLAC files. The error is arising because its attempt to access the directory is failing. Please check the docs. If you issued .patch() with no args it will look on the SD card. Perhaps the SD card is not being recognised?

Re recording time I haven't tested long recordings but in principle duration is set by MP3 bit rate and SD card capacity.

@IHOXOHI Re playback: playback of MP3 and FLAC files definitely works here as per the docs. I suggest anyone experiencing problems tries my test scripts: if they fail please report any issues.

Re microphone recording noise. This is an electronic problem which can arise with any recording system. Microphones produce tiny voltages so connections to them are prone to pickup. I'm sure there is plenty of guidance on the web on how to minimise this. In my testing I used a professional microphone with a separate pre-amp connected to the VS1053b. Screened cable was used throughout. I've just listened again to the recordings I made. There is some background noise (I don't have a soundproof studio) but I can't hear any electrical artefacts.
Peter Hinch

Post Reply