Skip to main content
Explorer II
October 5, 2020
Solved

I2S for STM32F429

  • October 5, 2020
  • 8 replies
  • 4432 views

Is there any example walking thru how to convert a .wav file of the audio to play on STM32F429?

    This topic has been closed for replies.
    Best answer by KnarfB

    There is some .wav related code in the F4 firmware repository for similar chips:https://github.com/STMicroelectronics/STM32CubeF4/blob/a86ecaa2fb63029596ba7dabadab2d9c2c139560/Projects/STM32469I-Discovery/Applications/Audio/Audio_playback_and_record/readme.txt

    8 replies

    KnarfBAnswer
    Super User
    October 6, 2020

    There is some .wav related code in the F4 firmware repository for similar chips:https://github.com/STMicroelectronics/STM32CubeF4/blob/a86ecaa2fb63029596ba7dabadab2d9c2c139560/Projects/STM32469I-Discovery/Applications/Audio/Audio_playback_and_record/readme.txt

    Jtron.11Author
    Explorer II
    October 6, 2020

    Thank you KnarfB. I was looking for the way we can play a wav file from binary file embedded in the flash as code. I tried so many different rate but still didn't get the wav file to play correctly. My set up using STM32F429 + MAX98357 amplifier. I set STM32F429 to play back with Phillips I2S with either 16KHz or 44KHz. From the original wav file, I use the software Audacity to export the wave file to export the right rate 16KHz or 44KHz 16bit sign wav then use WaveToC to convert wav file to C header hexadecial binary file. In STM32 i used the HAL_I2S_Transmit_DMA(&hi2s2, data, NUM_ELEMENTS); function to transmit the data. It play the similar sound, but the rate is not matching with the original rate.

    Do you have any idea? Or is there something wrong with my method of conversion?

    Super User
    October 6, 2020

    And your DMA is triggered by a timer?

    Jtron.11Author
    Explorer II
    October 6, 2020

    I called it in the infinite loop in such way

    /* USER CODE BEGIN WHILE */

     while (1)

     {

        HAL_I2S_Transmit_DMA(&hi2s2, data, NUM_ELEMENTS);

        HAL_Delay(5000);

      /* USER CODE END WHILE */

      /* USER CODE BEGIN 3 */

     }

     /* USER CODE END 3 */

    }

    Jtron.11Author
    Explorer II
    October 6, 2020

    This is just to test the sound. The sound clip only 2s timing so I delay 5s to be sure the clip is completed. In the real situation, the sound will only DMA transmit once we wanted.

    Super User
    October 6, 2020

    If you generate a 1kHz square wave in Audacity, after WaveToC conversion you should see blocks of identical values. The block length is half the sample freq. in kHz. If you play that sound, what freq are you hearing? You may use Audacity or an Android app (Spectroid or thelike) for rough measurments.

    Jtron.11Author
    Explorer II
    October 6, 2020

    It play correctly now, but still need an explanation. My source is 22KHz, if I set the STM32 with the same rate playback, the sound is playing back faster. I changed to 11KHz, now I got it plays at the same source. Any idea?

    Super User
    October 6, 2020

    Maybe the output is stereo? That would split the samples into two channels.

    Jtron.11Author
    Explorer II
    October 6, 2020

    Output from STM32? My assumption has been if the source is 22KHz, the playback should be 22KHz to play back correctly.

    Super User
    October 6, 2020

    Yes. Samples may be packed into L and R channels in STM32 (depending on settings). When MAX98357 is set to mono, it plays only half of the samples...