Author Topic: wave player  (Read 1965 times)

0 Members and 1 Guest are viewing this topic.

Offline alizare368Topic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: de
wave player
« on: November 28, 2019, 02:08:13 pm »
Hi guys
I want to make a wave player whit stm32f4, read the wave files from sdcard and into PWM CH_1 and CH_2.
and timer1_ch1 and timer1_ch2 pins are connected to RC filter(res: 1M,3.9k & c:4.7nf), ch_lo connected to 1M and ch_hi connected to 3.9k.
but the filter output is consistently noisy.

the MCU clock  is 84MHZ and timer configuration is :
prescaler:0
Auto reload register :255
pwm pulse:%50

this program works as a ping pong.

this is main code.

Code: [Select]
#include "main.h"
#include "fatfs.h"

SD_HandleTypeDef hsd;
TIM_HandleTypeDef htim1;

/* USER CODE BEGIN 0 */


#define sample_no 256
uint8_t sai_buf[2][sample_no];
uint32_t br;
FATFS SDFatFs;  /* File system object for SD disk logical drive */
FIL MyFile;     /* File object */
extern char SDPath[4]; /* SD disk logical drive path */
uint8_t refill=0;
uint32_t index_buf=0;
static FRESULT res;
uint32_t block_size;
/* USER CODE END 0 */

int main(void)
{
/* USER CODE BEGIN 2 */
uint8_t count=0;
int t=1;
res=f_mount(&SDFatFs, (TCHAR const*)SDPath, 0);
if(res==FR_OK)
{
res=f_open(&MyFile,"6.wav",FA_READ);
block_size=f_size(&MyFile);

if(res==FR_OK)
{
res=f_read(&MyFile,&sai_buf[0],sample_no,&br);

}

}

HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2);
HAL_TIM_Base_Start_IT(&htim1);

  /* USER CODE END 2 */

while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
if(refill)
{
index_buf++;
res=f_read(&MyFile,&sai_buf[(index_buf)%2],sample_no,&br);
refill=0;


}


  }
  /* USER CODE END 3 */
}


this is a ISR code:
Code: [Select]
void TIM1_UP_TIM10_IRQHandler(void)
{
  /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 0 */

  /* USER CODE END TIM1_UP_TIM10_IRQn 0 */
  HAL_TIM_IRQHandler(&htim1);
  /* USER CODE BEGIN TIM1_UP_TIM10_IRQn 1 */
wcnt++;

if(wcnt>7)
{
wcnt=0;


TIM1->CCR1=sai_buf[index_buf%2][i];
TIM1->CCR2=sai_buf[index_buf%2][i+1];
i+=3;

if(i>=(sample_no-1))
{
i=0;
refill =1;
}



}


  /* USER CODE END TIM1_UP_TIM10_IRQn 1 */
}


someone know what is the problem? |O |O
 
« Last Edit: November 30, 2019, 03:23:45 pm by alizare368 »
 

Offline wrynczuk

  • Newbie
  • Posts: 7
  • Country: pl
Re: wave player
« Reply #1 on: November 29, 2019, 04:10:14 pm »
The design seems very simplistic to me. Without looking into it really deeply it is hard to guess what could really go wrong.

The ideas to try:
  • Simplify it to use just a single channel
  • Instead of reading from the SD card read from a table of constants

Once, you get it done, up and running than start complicating things.

What seems suspicious to me in the design is the assumption that f_read() completes before the timer updating PWMs expires. Maybe it would be worth verifying the correctness of the assumption.

In the interrupt handler, the indexing TIM1->CCR1=sai_buf[....]; seems also suspicious to me.

In general, the current design is asynchronous which might lead to a couple of issues. One way of resolving them would be to go synchronous  - ping pong implementation here tries to deal with it. Another solution, would be adding some circular buffer which you could fill with the read data and empty it once feeding PWMs. At least you could easily detect the buffer overflow or underflow condition.
« Last Edit: November 29, 2019, 08:36:50 pm by wrynczuk »
 
The following users thanked this post: I wanted a rude username

Offline alizare368Topic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: de
Re: wave player
« Reply #2 on: November 30, 2019, 01:32:45 pm »
Hi
thank you for help me.
I will check out the ways you suggested. :-+
 

Offline Manteroy

  • Newbie
  • Posts: 2
  • Country: at
Re: wave player
« Reply #3 on: December 03, 2019, 07:32:16 am »
I still do many things. Well not good enough I want everyone's opinions.

Offline alizare368Topic starter

  • Regular Contributor
  • *
  • Posts: 56
  • Country: de
Re: wave player
« Reply #4 on: December 03, 2019, 04:15:37 pm »
I still haven't answered anyone can help me? :-[
 

Offline ali_asadzadeh

  • Super Contributor
  • ***
  • Posts: 2147
  • Country: ca
Re: wave player
« Reply #5 on: December 04, 2019, 06:40:09 am »
Can you measure the speed of reading the wave file chunks? is it fast enough?
ASiDesigner, Stands for Application specific intelligent devices
I'm a Digital Expert from 8-bits to 64-bits
 

Offline GromBeestje

  • Frequent Contributor
  • **
  • Posts: 310
  • Country: nl
Re: wave player
« Reply #6 on: December 04, 2019, 11:47:56 am »
Am I missing something or are you reading a .wav file as if it would contain RAW PCM data?
There is a header in front, and there it specifies how the data is formatted ( whether it is uncompressed PCM, or some compression is applied, sample rate. number of channels, etc)

In the best case, ignoring the header, knowing how the data is encoded, will result in a digital blip at the start of the sound.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->