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
KnarfB
Principal III

BSP_AUDIO_IN_Init(INPUT_DEVICE_INPUT_LINE_1, 80, AUDIO_FREQUENCY_48K)

doesn't match the implentation in 

32f746gdiscovery-bsp/stm32746g_discovery_audio.c at 64c20f758a907cd873e368502cf16e498934d0ec · STMicroelectronics/32f746gdiscovery-bsp (github.com)

hth

KnarfB

Andrew Neil
Evangelist III

@aciuf wrote:

I would like to read the level of audio input signal


What "audio input signal"? On what board?

https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

 


@aciuf wrote:

but I read always 0dB on both channels.


Have you checked that there is actually any signal present at all?

Is the signal sufficient to read above 0 dB ?

 


@aciuf wrote:

This is my code 


So what testing / investigation / debugging have you done on your code to find what's wrong with it?

I am using a STM32F746-DISCO so INPUT_DEVICE_INPUT_LINE_1 should be the line input.

Now I see some values but still not good

 

 

 

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, 100, AUDIO_FREQUENCY_48K) != 0)
	//if (BSP_AUDIO_IN_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, 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 = 48000;   /* 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 parameters that can be updated here every frame if required */
	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 *)"--smr_setConfig--", LEFT_MODE);
	    return (AUDIO_ERROR_SMR);
	}
	/* I/O buffers settings */
	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;
}

void Audio_Process2()
{
	int32_t error = SMR_ERROR_NONE;

	uint16_t pbuf[100];

    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(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);
}

@aciuf wrote:

Now I see some values


So what did you change?

 


@aciuf wrote:

 but still not good


In what way, "not good"?  Give numbers!

Again, are you sure that the input signal you're providing is adequate?

 


@aciuf wrote:

so INPUT_DEVICE_INPUT_LINE_1 should be the line input.


and are you actually feeding it a good, line-level signal?

aciuf
Associate II

Using this last code the dB print without input signal start from 0dB and decrease

https://youtu.be/PohM2AMRxFI

aciuf
Associate II

In the main I call at the begin

AUDIO_PLAYER_Init2

and in a loop

Audio_Process2

 

 

 

So what did you change?

I have insert only 2 fuctions in the loop

aciuf
Associate II

New version

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

 

void *pSmrPersistentMem = NULL;
void *pSmrScratchMem = NULL;

static  smr_static_param_t smr_static_param;
static  smr_dynamic_param_t smr_dynamic_param;

static  buffer_t BufferHandler;
static  buffer_t *pBufferHandler = &BufferHandler;

static  uint8_t str_pwr[100];

uint16_t PCM_Data_SMR[32]; 		/* PCM data buffer */



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, 100, AUDIO_FREQUENCY_48K) != 0)
	//if (BSP_AUDIO_IN_Init(INPUT_DEVICE_DIGITAL_MICROPHONE_2, 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 parameters that can be updated here every frame if required */
	smr_dynamic_param.enable = 1;                        /* SMR module enabler */
	smr_dynamic_param.averaging_time = 100; //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 *)"--smr_setConfig--", LEFT_MODE);
	    return (AUDIO_ERROR_SMR);
	}
	/* I/O buffers settings */
	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;
}

void Audio_Process2()
{
	int32_t error = SMR_ERROR_NONE;

	BSP_AUDIO_IN_Record(&BufferCtl.buff[0], 32/2);

	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);
}