wave player - Page 1
Forum section: electronics / general

author: alizare368
author_rank: Regular Contributor
timestamp: November 28, 2019, 02:08:13 pm
content: Hi guysI 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:0Auto reload register :255pwm pulse:%50this program works as a ping pong.this is main code.Code:#include "main.h"#include "fatfs.h"SD_HandleTypeDef hsd;TIM_HandleTypeDef htim1;/* USER CODE BEGIN 0 */#define sample_no 256uint8_t sai_buf;uint32_t br;FATFS SDFatFs; /* File system object for SD disk logical drive */FIL MyFile; /* File object */extern char SDPath; /* 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,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: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];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?

author: wrynczuk
author_rank: Newbie
timestamp: November 29, 2019, 04:10:14 pm
content: 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 channelInstead of reading from the SD card read from a table of constantsOnce, 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 indexingTIM1->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.

author: alizare368
author_rank: Regular Contributor
timestamp: November 30, 2019, 01:32:45 pm
content: Hithank you for help me.I will check out the ways you suggested.

author: Manteroy
author_rank: Newbie
timestamp: December 03, 2019, 07:32:16 am
content: I still do many things. Well not good enough I want everyone's opinions.

author: alizare368
author_rank: Regular Contributor
timestamp: December 03, 2019, 04:15:37 pm
content: I still haven't answered anyone can help me?

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: December 04, 2019, 06:40:09 am
content: Can you measure the speed of reading the wave file chunks? is it fast enough?

author: GromBeestje
author_rank: Frequent Contributor
timestamp: December 04, 2019, 11:47:56 am
content: 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.