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

Setting Thingy52 Audio rate

The customer's goal is to play a tone on an external speaker with a data rate of 11.05KHz.

The "default" demo code for the Thingy52 @ https://github.com/NordicSemiconductor/Nordic-Thingy52-FW has a PWM driver that runs at 8KHz. 

 In drv_speaker.c SEQ_REPEATES is define as 7 with a comment indicating a 8kHz sample rate. It not clear what changing this define would do the sample rate. Can you example this?

What is the recommended methodology to achieve a playback sample rate of 11.05 KHz?

BTW - REPEATES should be REPEATS.

Cheers,

Jeff

 

  • Hi Jeff

    The PWM runs on a 16MHz clock, with a duty cycle of 250ms. The SEQ_REPEATES is defined as 7 because that is how many times the duty cycle sequence is repeated (after the initial cycle), which is how the PWM driver runs at 8kHz. To make the PWM driver operate at 11049.72Hz, which is the closest I got to 11050Hz by trial and error , you will have to modify the .top_value in nrf_drv_pwm_config_t which is set to 250 down to 181.

    If you do this you will also have to alter the sound files played so that they play correctly on the modified PWM driver. As I do not know which sound file is played I can just give you general pointers here. If you are playing some of the sounds already on the Thingy:52 you will have to go through the sounds.h file and multiply all values by 0.724 (181/250) to ensure that the sound files doesn't come out warped when playing them on a modified PWM driver. If the sound files in question are being sent by another device, the easiest thing would be to modify the sound files being before sending them to the Thingy. Alternatively, it might work if you multiply the p_sound[i] with 0.724 in lines 360, 390, and 401, in the drv_speaker.c file, however I am not completely sure this is correct.

    Note: It might be possible to get closer to the desired value by altering both the SEQ_REPEATES value as well as the duty cycle value. Exactly 11.05kHz will not be achieveable on a 16MHz clock.

    Best regards,

    Simon

  • Hi Simon, 

    Please confirm that 250mS does not set the duty cycle,it sets the period or top value. In this case the period is 8 times faster then the targeted 8KHz sample rate, hence the SEQ_REPEATS value. So effectively, there are 8 sub-periods within a single sample period. 

    When using p_sound[i], i represents a portion of top or duty cycle of each sub-period. 

    If I understand this correctly the following timing diagrams should be relatively accurate. Please confirm.

    Thanks,

    Jeff

  • Hi Jeff

    Yes, yes and yes. It seems you have understood it. Best of luck!

    Kind regards,

    Simon

Related