This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

PWM Audio driver

Hello,

I just wanted to throw in that for a hobby project I created a PWM audio driver, with which one can play back 8-bit audio data very easily. Since it may be useful for others, too, I have uploaded the sources to https://nrf52-pwm-audio.sourceforge.io/, along with SES project files for nRF52832 and nRF52840. Plus, there you will also find a link to a video to see what you can do with the driver.

Cheers,
Tamas

Parents
  • Hello Tamas,

    you inspired me to create my own project which supports additional codecs. I started with a sbc decoder implementation.

    Example:

    PWMPlayer player;
    player.init(AUDIO_PIN);
    
    AudioDecoderSbc decoder(sample_itsworking_sbc, sample_itsworking_sbc_size);
    player->play(&decoder, 2.0);
    while(player->isPlaying()) { __WFE(); }

    The only problem I have, is the initialisation of the audio PWM pin, which creates a spike (click noise) on calling `nrfx_pwm_init`. Does anyone have an idea how to fix this?

    The project can be found on:

    https://github.com/don41382/nrf52-pwm-wav-sbc-decoder-player

    Cheers,

    Felix

  • Dear Felix,

    I am happy you've found the PWM driver and could use it.

    Regarding your question, as far as I remember, I connected the output between Vdd (not Gnd) and the PWM output pin and used the PWM output in an inverted fashion. This gave me better quality and less noise. You could try to change the PWM config and remove inversion (see https://sourceforge.net/p/nrf52-pwm-audio/code/ci/master/tree/Modules/nrf_pwm_audio.c, Line 221, remove NRFX_PWM_PIN_INVERTED flag) and see if the click goes away.

    Other than that I can report that I successfully combined the PWM driver with minimp3 (https://github.com/lieff/minimp3) to play back mono (low bitrate) mp3 files in real-time on nRF52840. You may want to include that too among your codecs.

    Best,
    Tamas

  • Thanks Tamas for the nice trick with the positive Speaker connection. I'll try this later on. Removing the NRFX_PWM_PIN_INVERTED just enhanced the clicking noise. I am getting an Oscilloscope for Christmas - so I guess seeing is better than hearing.

    I just started with including the minimp3 library from lieff, but there is one issue. The `mp3dec_decode_frame` always returns at least 1152 samples (1 frame). Do you know if I can reduce this? Because the PWM buffers need to be filled by 512 / 256 (for 16kHz) or do I have to chunk this by myself?

    Thanks again for your quick support and your nice work on the PWM stuff.

  • Hey Felix,

    I don't think you'll be able to change the 1152 samples frame size of minimp3. Either, you change buffer size of PWM, or you'll need to implement a FIFO between the two modules.

    For the latter, the producer and consumer would be minimp3 and the PWM driver, respectively. Whenever PWM consumes 512 samples and there is less than 512 left in the FIFO, you'd let minimp3 decode the next 1152 samples and add them to the FIFO. If minimp3 decoding happens fast enough, this would work.

    Tamas

Reply
  • Hey Felix,

    I don't think you'll be able to change the 1152 samples frame size of minimp3. Either, you change buffer size of PWM, or you'll need to implement a FIFO between the two modules.

    For the latter, the producer and consumer would be minimp3 and the PWM driver, respectively. Whenever PWM consumes 512 samples and there is less than 512 left in the FIFO, you'd let minimp3 decode the next 1152 samples and add them to the FIFO. If minimp3 decoding happens fast enough, this would work.

    Tamas

Children
  • Hi tamas,

    I read more about MP3 decoding and yes, for MP3 1152 is a fixed frame size for mono. FIFO totally makes sense, but increasing the PWM buffer keeps code simpler.

    Currently I am struggling with "Data Access Violation". I already tried to change CONFIG_MAIN_STACK_SIZE and CONFIG_HEAP_MEM_POOL_SIZE, but that doesn't help. Maybe you know a working memory setting or what needs to be tweaked.

    Cheers,
    Felix

Related