cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F746 cannot read level of audio input signal

aciuf
Associate II

I would like to read the level of audio input signal to use like a vu-meter instead of use analog input.

 

This is my code but I read always 0dB on both channels.

 

 

void AUDIO_PLAYER_Init2()
{

/* Allocat mem for Smr */

pSmrPersistentMem = malloc(smr_persistent_mem_size); /* smr_persistent_mem_size 0x188 */

pSmrScratchMem = malloc(smr_scratch_mem_size); /* smr_scratch_mem_size 0xF04 */

 

 

if (BSP_AUDIO_IN_Init(INPUT_DEVICE_INPUT_LINE_1, 80, AUDIO_FREQUENCY_48K) != 0)

LCD_LOG_SetHeader((uint8_t *)"Audio SMR Application KO");

else

LCD_LOG_SetHeader((uint8_t *)"Audio SMR Application OK");

}

 

void Audio_Process2()
{

    int32_t error = SMR_ERROR_NONE;

    __HAL_RCC_CRC_CLK_ENABLE();

    CRC->CR = CRC_CR_RESET;

   

    error = smr_reset(pSmrPersistentMem, pSmrScratchMem);

    if (error != SMR_ERROR_NONE)

    {

        BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--err0--", LEFT_MODE);

        return (AUDIO_ERROR_SMR);

    }

 

    smr_static_param.sampling_rate = 48000; /* only sampling rate supported */

    error = smr_setParam(&smr_static_param, pSmrPersistentMem);

    if (error != SMR_ERROR_NONE)

    {

       BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--err1--", LEFT_MODE);

       return (AUDIO_ERROR_SMR);

    }

    smr_dynamic_param.enable = 1; /* SMR module enabler */

    smr_dynamic_param.averaging_time = AUDIO_CFG_SMR_AVGTIME_DEFAULT;

    smr_dynamic_param.filter_type = SMR_PREFILTER_AWEIGHTING;

 

    error = smr_setConfig(&smr_dynamic_param, pSmrPersistentMem);

    if (error != SMR_ERROR_NONE)

    {

        BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--err2--", LEFT_MODE);

       return (AUDIO_ERROR_SMR);

    }

    error = smr_getConfig(&smr_dynamic_param, pSmrPersistentMem);

    if (error != SMR_ERROR_NONE)

    {

       BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--err4--", LEFT_MODE);

       return AUDIO_ERROR_SMR;

   }

 

   BufferHandler.nb_bytes_per_Sample = 2; /* 16-bit */

   BufferHandler.nb_channels = 2; /* stereo */

   BufferHandler.data_ptr = PCM_Data_SMR;

   BufferHandler.buffer_size = 32/2; /* just half buffer is process (size per channel) */

   BufferHandler.mode = INTERLEAVED;

   BufferHandler.data_ptr = &BufferCtl.buff[AUDIO_OUT_BUFFER_SIZE /2];

 

   error = smr_process(pBufferHandler, pBufferHandler, pSmrPersistentMem);

   if (error != SMR_ERROR_NONE)

  {

     BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--err5--", LEFT_MODE);

     return (AUDIO_ERROR_SMR);

  }

 

   error = smr_getConfig(&smr_dynamic_param, pSmrPersistentMem);

   if (error != SMR_ERROR_NONE)

   {

      BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--err6--", LEFT_MODE);

      return AUDIO_ERROR_SMR;

   }

 

sprintf((char *)str_pwr, "Left mean power: %ld dB",(int32_t)((float)smr_dynamic_param.mean_level_left*0.25));

BSP_LCD_DisplayStringAt(CHAR(0), LINE(10), (uint8_t *)str_pwr, LEFT_MODE);

sprintf((char *)str_pwr, "Right mean power: %ld dB",(int32_t)((float)smr_dynamic_param.mean_level_right*0.25));

BSP_LCD_DisplayStringAt(CHAR(0), LINE(11), (uint8_t *)str_pwr, LEFT_MODE);

}

14 REPLIES 14

@aciuf wrote:

Now I see some correlation from input signal and dB print but still not correct


Again, what exactly does that mean??

Give some specific numbers!

Show the signal that you're applying. Show the results that you're getting. Explain why you think the results are "not correct".

So, ...

no signal -31dB

1.3Vrms  ->  -2dB

0.4Vrms -> -2dB

0.144Vrms -> -2dB

5mVrms -> -5dB

 

 

 

aciuf
Associate II

The problem could be on the evaluation board because the line input is a mic input, so probably I saturate the input with my signal.

I will check tomorrow if this is true.

 

aciuf_0-1722705199228.png

aciuf_1-1722705231327.png

 

I have modified the source WM8994.c to exclude the +30dB gain on input

Now the my signal does not saturate the input but still strange output:

input
   0dB  1.349Vrms   ->   on lcd read  -1dB
 -3dB   0.97Vrms    ->   on lcd read   -7dB
-10dB   0.43Vrms    ->   on lcd read -35dB

disconnected signal on lcd read -96dB

Do I have to apply a conversion to the smr_dynamic_param value ? because in the example there is a * 0.25.

 

    case INPUT_DEVICE_INPUT_LINE_1 :
    	BSP_LCD_DisplayStringAt(10, 80, (uint8_t *)"--INPUT_DEVICE_INPUT_LINE_1--", 0);

      /* IN1LN_TO_IN1L, IN1LP_TO_VMID, IN1RN_TO_IN1R, IN1RP_TO_VMID */
      // 0x0011 = 00010001
      //             |   +- IN1RN_TO_IN1R
      //             +-- IN1LN_TO_IN1L
      counter += CODEC_IO_Write(DeviceAddr, 0x28, 0x0011);

      /* Disable mute on IN1L_TO_MIXINL and +30dB on IN1L PGA output */
      //counter += CODEC_IO_Write(DeviceAddr, 0x29, 0x0035);
      // 0x0035 = 00110101
      //               |||
      //               +++  0dB
      //             + +30dB
      //            + unmute IN1L
      // 0x0025 = 001000101
      //               |||
      //               +++  0dB
      //             +-- 0dB
      //            +--- unmute IN1L
      counter += CODEC_IO_Write(DeviceAddr, 0x29, 0x0025);

      /* Disable mute on IN1R_TO_MIXINL, Gain = +30dB */
      counter += CODEC_IO_Write(DeviceAddr, 0x2A, 0x0025);

 

 

 

 

 

 

If I multiply by 0.25

sprintf((char *)str_pwr, "Left mean power: %ld dB",(int32_t)((float)smr_dynamic_param.mean_level_left*0.25));

I can read after power on -87dB but after applying any signal and stopping it never goes back to -87 and stops at -24 as if the read buffer remains dirty.

Last version here:

 

#define AUDIO_OUT_BUFFER_SIZE                      3840 /* 480 * 2 (half + half) * 2 (stereo) * 2 (bytes x sample) */
//#define AUDIO_OUT_BUFFER_SIZE                       (24 * 1024)
//#define AUDIO_IN_BUFFER_SIZE                        (10 * 2304) /* buffer size in half-word */


void  AUDIO_PLAYER_Init2()
{
	int32_t error = SMR_ERROR_NONE;

	pSmrPersistentMem = malloc(smr_persistent_mem_size); /* smr_persistent_mem_size  0x188 */
	pSmrScratchMem = malloc(smr_scratch_mem_size);       /* smr_scratch_mem_size  0xF04 */


	if (BSP_AUDIO_IN_Init(INPUT_DEVICE_INPUT_LINE_1, 80, AUDIO_FREQUENCY_48K) != 0)
		LCD_LOG_SetHeader((uint8_t *)"Audio SMR Application KO");
	else
		LCD_LOG_SetHeader((uint8_t *)"Audio SMR Application OK");

	 __HAL_RCC_CRC_CLK_ENABLE();

	CRC->CR = CRC_CR_RESET;

	error = smr_reset(pSmrPersistentMem, pSmrScratchMem);
	if (error != SMR_ERROR_NONE)
	{
	 	BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--smr_reset--", LEFT_MODE);
	    return (AUDIO_ERROR_SMR);
	}

	smr_static_param.sampling_rate = AUDIO_FREQUENCY_48K;   /* only sampling rate supported */

	error = smr_setParam(&smr_static_param, pSmrPersistentMem);
	if (error != SMR_ERROR_NONE)
	{
	   BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--smr_setParam--", LEFT_MODE);
	   return (AUDIO_ERROR_SMR);
	}

	smr_dynamic_param.enable = 1;                        /* SMR module enabler */
	smr_dynamic_param.averaging_time = AUDIO_CFG_SMR_AVGTIME_DEFAULT;
	smr_dynamic_param.filter_type = SMR_PREFILTER_AWEIGHTING ; // SMR_PREFILTER_NONE;

	error = smr_setConfig(&smr_dynamic_param, pSmrPersistentMem);
	if (error != SMR_ERROR_NONE)
	{
		BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--smr_setConfig--", LEFT_MODE);
	    return (AUDIO_ERROR_SMR);
	}
	BufferHandler.nb_bytes_per_Sample = 2;			/* 16-bit */
	BufferHandler.nb_channels = 2; 					/* stereo */
	//BufferHandler.data_ptr = PCM_Data_SMR;
	//BufferHandler.buffer_size = 32/2; 		/* just half buffer is process (size per channel) */
	BufferHandler.buffer_size = AUDIO_OUT_BUFFER_SIZE/(2*4);
	BufferHandler.mode = INTERLEAVED;

	BufferCtl.state = BUFFER_OFFSET_NONE;

	error = BSP_AUDIO_IN_Record(&BufferCtl.buff[0], AUDIO_OUT_BUFFER_SIZE/2);
	if (error > 0)
	{
		    BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--BSP_AUDIO_IN_Record-", LEFT_MODE);
	        return (AUDIO_ERROR_SMR);
	}
}

void Audio_Process2()
{
	int32_t error = SMR_ERROR_NONE;

	if (BufferCtl.state == BUFFER_OFFSET_HALF)
	{
		BufferHandler.data_ptr = &BufferCtl.buff[0];

		error = smr_process(pBufferHandler, pBufferHandler, pSmrPersistentMem);
		if (error != SMR_ERROR_NONE)
		{
			BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--smr_process--", LEFT_MODE);
			return (AUDIO_ERROR_SMR);
		}
	    error = smr_getConfig(&smr_dynamic_param, pSmrPersistentMem);
	    if (error != SMR_ERROR_NONE)
	    {
	    	BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--smr_getConfig--", LEFT_MODE);
	        return AUDIO_ERROR_SMR;
	    }
	    sprintf((char *)str_pwr, "Left mean power:  %ld dB",(int32_t)((float)smr_dynamic_param.mean_level_left*0.25));
	    BSP_LCD_DisplayStringAt(10, 100, (uint8_t *)str_pwr, LEFT_MODE);
	    sprintf((char *)str_pwr, "Right mean power:  %ld dB",(int32_t)((float)smr_dynamic_param.mean_level_right*0.25));
	    BSP_LCD_DisplayStringAt(10, 120, (uint8_t *)str_pwr, LEFT_MODE);


	    BufferCtl.state = BUFFER_OFFSET_NONE;
	}
	else if (BufferCtl.state == BUFFER_OFFSET_FULL)
	{
		BufferHandler.data_ptr = &BufferCtl.buff[AUDIO_OUT_BUFFER_SIZE /2];

		error = smr_process(pBufferHandler, pBufferHandler, pSmrPersistentMem);
		if (error != SMR_ERROR_NONE)
		{
			BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--smr_process--", LEFT_MODE);
			return (AUDIO_ERROR_SMR);
		}
		error = smr_getConfig(&smr_dynamic_param, pSmrPersistentMem);
		if (error != SMR_ERROR_NONE)
		{
			BSP_LCD_DisplayStringAt(10, 20, (uint8_t *)"--smr_getConfig--", LEFT_MODE);
			return AUDIO_ERROR_SMR;
		}
		sprintf((char *)str_pwr, "Left mean power:  %ld dB",(int32_t)((float)smr_dynamic_param.mean_level_left*0.25));
		BSP_LCD_DisplayStringAt(10, 100, (uint8_t *)str_pwr, LEFT_MODE);
		sprintf((char *)str_pwr, "Right mean power:  %ld dB",(int32_t)((float)smr_dynamic_param.mean_level_right*0.25));
		BSP_LCD_DisplayStringAt(10, 120, (uint8_t *)str_pwr, LEFT_MODE);

		BufferCtl.state = BUFFER_OFFSET_NONE;
	}
	else
	{

	}
}

void BSP_AUDIO_IN_TransferComplete_CallBack(void)
{
	BufferCtl.state = BUFFER_OFFSET_FULL;
}

void BSP_AUDIO_IN_HalfTransfer_CallBack(void)
{
	BufferCtl.state = BUFFER_OFFSET_HALF;
}