on 2022-06-22 6:46 AM
Assuming you have done the previous steps in the part 1 , the files are now created and you will need to manually copy the adpcm.c and adpcm.h from the STSW-STM32022 pack into your STM32CubeIDE project: Core/Src and Core/Inc folders. The result should look like this:
As you might have noticed, in the image above there is also a source folder called “AudioFile”, do not worry, as the next steps will allow us to create it, but a small process for converting a given audio file (WAV format) into a C vector that can be used by our STM32 is needed. So, speaking of the process, this is how ST presented the approach it on the AN4453 that we are using as a reference:
As mentioned in the prerequisites, we need to install a tool called SOX (click here to download it) this tool can convert WAV into IMA using a given output sampling frequency. As you may recall, our target for audio output sampling frequency is 15625Hz.
Once installed, the SOX tool needs to be called using the CMD.exe, so this is a quick example of how to call it–you can refer to the application note if you need more details:
Change the directory to be in the installation folder of the SOX tool, in this case it was in C:\Program files (x86)\sox-14-4-2. The call is quite simple. Call the sox.exe and pass the arguments: *.wav input file, resample frequency (-r 15625), output file name *.ima, gain.
In this example, the audio file used is called “pure_insperation.wav” and it is in this path” C:\Users\MYUSERNAME\Desktop\AudioADPCM\AudioFile”, thus the CLI command looks like this:
/* USER CODE BEGIN Includes */
#include "adpcm.h"
/* USER CODE END Includes */
/* USER CODE BEGIN PD */
#define DEFAULT_STARTUP_VAL (0x80)
/* USER CODE END PD */
/* USER CODE BEGIN PV */
void LoadAudioFiles(void);
/* USER CODE END PV */
/* USER CODE BEGIN 0 */
AudioElement AudioFile;
uint8_t AudioFileToPlay = 0;
/* USER CODE END 0 */
  /* USER CODE BEGIN 2 */
  LoadAudioFiles();
  /* USER CODE END 2 */
/* USER CODE BEGIN WHILE */
#ifdef USE_DAC
	HAL_DAC_Start(&hdac1, DAC_CHANNEL_1);
	HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, 0x7FF);
#endif
	// capture/compare registers (CC1 PWM duty 50%)
	TIM3->CCR1 = DEFAULT_STARTUP_VAL;
	TIM3->CCR2 = DEFAULT_STARTUP_VAL;
	LL_TIM_EnableIT_UPDATE(TIM3);
	TIM3->CCER |= TIM_CCER_CC2E | TIM_CCER_CC1E;
	LL_TIM_EnableCounter(TIM3);
	while (1)
	{
   	 /* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
		if(!HAL_GPIO_ReadPin(BT1_GPIO_Port,BT1_Pin))
		{
			while(!HAL_GPIO_ReadPin(BT1_GPIO_Port,BT1_Pin));
			if(AudioFileToPlay>=1)
			{
				AudioFileToPlay = 0;
			}else
			{
				AudioFileToPlay++;
			}
			/* Disable the TIM3 Interrupt */
			NVIC_EnableIRQ(TIM3_IRQn);
			// stop the timer
			LL_TIM_EnableCounter(TIM3);
		}
		HAL_Delay(100);
	}
  /* USER CODE END 3 */
/* USER CODE BEGIN 4 */
void LoadAudioFiles(void)
{
	AudioFile.AudioFiles[0] = (uint32_t)&Pure_Inspiration;
	AudioFile.AudioSize[0] = NELEMS(Pure_Inspiration);
}
/* USER CODE END 4 */
@B.Montanari wrote:As mentioned in the prerequisites, we need to install a tool called SOX (click here to download it) this tool can convert WAV into IMA using a given output sampling frequency. As you may recall, our target for audio output sampling frequency is 15625Hz.
I'm wondering where did you mention the prerequisites as I cannot find it. I'd like to know how you got the frequency 15625 Hz.
Lastly,
@B.Montanari wrote:
Once opened, add the few lines below, this will create a section called “myAudioFiles” in the FLASH memory, located 20KB (0x5000) after the initial code:
I'd like to know why you used 20kB padding to start the audio file at 0x08005000.