cancel
Showing results for 
Search instead for 
Did you mean: 

WAV File to DAC Problem

GIkar
Associate III

Hello,

i read the ST instructions and looked in the EVAL Board example. There they used a audio jack via SPI but in want to play the file direct on DAC.

First to check if the SD Card works i write and read a file to it (works). Then I call the "play_sound" Funktion.

My problems to solve:

  1. Open the file don't work like the text file -> What's the problem
  2. How can I transfer the value of buffer one and two to the DAC. May be via DMA

Settings of SD Card and DACs0690X00000AQrPuQAL.png0690X00000AQrPkQAL.png

void play_sound(void)

{

     uint32_t WaveDataLength=0;

     /* Get Data from SD */

    res=f_open(&myFile, "open_sound_mono.wav", FA_READ); // Don't work. SD Card is already mounted and TXT File write and read worked properly

     res=f_lseek(&myFile, f_size(&myFile));

     res=f_read (&myFile, buffer1, _MAX_SS, &BytesRead);

     res=f_read (&myFile, buffer2, _MAX_SS, &BytesRead);

      res=f_close(&myFile);

     /* Start playing wave */

   //  Play buffer1 on DAC0  HOWTO ???

     buffer_switch = 1;

     while((WaveDataLength != 0) )

     {

         /* wait for DMA transfert complete */

         if(buffer_switch == 0)

         {

           /* Play data from buffer1 */

            //  Play buffer1 on DAC0  HOWTO ???

           /* Store data in buffer2 */

           f_read (&myFile , buffer2, _MAX_SS, &BytesRead);

           buffer_switch = 1;

         }

         else

         {

           /* Play data from buffer2 */

            //  Play buffer2 on DAC0  HOWTO ???

           /* Store data in buffer1 */

           f_read (&myFile, buffer1, _MAX_SS, &BytesRead);

           buffer_switch = 0;

         }

       }

          // Wiedergabe Stoppen

         // WavePlayerStop ???

     WaveDataLength = 0;

}

void DAC_setup()

{

   HAL_DAC_Start(&hdac, DAC_CHANNEL_1);

   HAL_DAC_Start(&hdac, DAC_CHANNEL_2);

   //HAL_DAC_Start_DMA(&hdac, DAC1_CHANNEL_1 , pData, Length, Alignment)

   HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)AUDIO_SAMPLE, 32, DAC_ALIGN_12B_R); //Test with AUDIO_SAMPLE from EVAL Board sample code

}

7 REPLIES 7

Going to be hard to read any data beyond the end of the file.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I gues you mean I have to read the file step by step 512byts to the end?

I solved the problem of the output

      HAL_TIM_Base_Start(&htim6);

      HAL_DAC_Start(&hdac, DAC_CHANNEL_1);

      HAL_DAC_Start(&hdac, DAC_CHANNEL_2);

  HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)AUDIO_SAMPLE, 32, DAC_ALIGN_12B_R);

  HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_2, (uint32_t*)sine_wave_array, 32, DAC_ALIGN_12B_R);

now I only need to replace the array with the data of the wave file. Do you have a nice idee for this?

Better when i close my file at the end :D

GIkar
Associate III

         FATFS *fs;    /* Ponter to the filesystem object */

         fs = malloc(sizeof (FATFS));

      res=f_mount(fs ,"", 1);

      if(res!=FR_OK)

      {

         __NOP();

      }

        res = f_open(&myFile, "close_sound_mono.wav", FA_OPEN_EXISTING | FA_READ); --> Why this does not work

         if(res!=FR_OK)

            __NOP();

      res=f_open(&myFile, "test.txt", FA_WRITE | FA_CREATE_ALWAYS); --> this work

      if(res!=FR_OK)

      {

         __NOP();

      }

GIkar
Associate III

now I can open my wave file. And my function looks like that. ;)

Problem now. DAC only keeps the first value.

void play_sound(void)

{

   res = f_open(&myAudioFile, "SOUND.WAV", FA_READ); //FA_OPEN_EXISTING |

   if(res!=FR_OK)

      __NOP();

      WaveDataLength=f_size(&myAudioFile);

     while((WaveDataLength != 0) )

     {

         // wait for DMA transfert complete

         if(buffer_switch == 0)

         {

           // Play data from buffer1

            //  Play buffer1 on DAC0  HOWTO ???

            HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)buffer1, 32, DAC_ALIGN_12B_R);

            f_lseek(&myAudioFile, f_tell(&myAudioFile)+_MAX_SS);//move 4096 bits

            // Store data in buffer2

           f_read (&myAudioFile , buffer2, _MAX_SS, &BytesRead);

           WaveDataLength=WaveDataLength-_MAX_SS;

           buffer_switch = 1;

         }

         else

         {

           // Play data from buffer2

            //  Play buffer2 on DAC0  HOWTO ???

           HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_1, (uint32_t*)buffer2, 32, DAC_ALIGN_12B_R);

           f_lseek(&myAudioFile, f_tell(&myAudioFile)+_MAX_SS);//move 4096 bits

           // Store data in buffer1

           f_read (&myAudioFile, buffer1, _MAX_SS, &BytesRead);

           WaveDataLength=WaveDataLength-_MAX_SS;

           buffer_switch = 0;

         }

       }

     WaveDataLength = 0;

          __NOP();

      res = f_close(&myAudioFile);

}

GIkar
Associate III

Okay File is loaded step by step to DAC but no proper sound, may the speed is wrong?

CPU Frq = 72MHz

static void MX_TIM6_Init(void) // Trigger for DAC DMA

{

 /* USER CODE BEGIN TIM6_Init 0 */

 /* USER CODE END TIM6_Init 0 */

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 /* USER CODE BEGIN TIM6_Init 1 */

 /* USER CODE END TIM6_Init 1 */

 htim6.Instance = TIM6;

 htim6.Init.Prescaler = 0;

 htim6.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim6.Init.Period = 32768;

 htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_Base_Init(&htim6) != HAL_OK)

 {

   Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)

 {

   Error_Handler();

 }

 /* USER CODE BEGIN TIM6_Init 2 */

 /* USER CODE END TIM6_Init 2 */

}