找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5276|回复: 2
打印 上一主题 下一主题
收起左侧

STM32F4开发源码—I2S音频播放

[复制链接]
跳转到指定楼层
楼主
STM32F4开发源码之I2S音频播放,利用原子STM32F407开发板进行编写。

完整源码下载:
(10)I2S音频播放.zip (150.58 KB, 下载次数: 61)




部分源程序预览:
  1. #include "wavplayer.h"
  2. #include "cs4334.h"
  3. #include "lcd.h"
  4. #include "led.h"
  5. #include "ff.h"






  6. u16 buffer1[1024]={0x00};
  7. u16 buffer2[1024]={0x00};         
  8. __IO u32 WaveLen=0;
  9. __IO u32 XferCplt=0;
  10. __IO u32 DataOffset=0;
  11. __IO u32 WaveCounter;
  12. u8 buffer_switch=1;
  13. WAVE_TypeDef WAVE_Format;

  14. void Wavplay(void)
  15. {
  16.         FATFS fatfs;            
  17.         FIL fileR;
  18.         UINT BytesRead;       
  19.         u8 i=0;

  20.           f_mount(0, &fatfs);
  21.           f_open(&fileR, "0:/WAV/melody.wav" , FA_READ);
  22.         f_read(&fileR, buffer1, 1024, &BytesRead);
  23.         while(WaveParsing())LCD_String(20,50,"File read error!",RED);
  24.         LCD_String(20,50,"0:/WAV/melody.wav",WHITE);
  25.           WaveLen = WAVE_Format.DataSize;
  26.           AUDIO_Init(WAVE_Format.SampleRate);
  27.           f_lseek(&fileR, WaveCounter);//跳过文件头
  28.           f_read(&fileR, buffer1, 1024, &BytesRead);
  29.           f_read(&fileR, buffer2, 1024, &BytesRead);
  30.           Audio_MAL_Play((u32)buffer1, 1024);
  31.           buffer_switch=1;
  32.           XferCplt=0;  
  33.           while(WaveLen!=0)
  34.           {
  35.               while(XferCplt==0);//等待DMA传送完成
  36.               XferCplt=0;
  37.               if(buffer_switch==0)
  38.               {
  39.                 Audio_MAL_Play((u32)buffer1,1024);//从buffer1播放
  40.                 f_read(&fileR,buffer2,1024,&BytesRead);//填充buffer2
  41.                 buffer_switch=1;
  42.               }
  43.               else
  44.               {   
  45.                 Audio_MAL_Play((u32)buffer2,1024);//从buffer2播放
  46.                 f_read(&fileR,buffer1,1024,&BytesRead);//填充buffer1
  47.                 buffer_switch=0;
  48.               }
  49.                 i++;
  50.                 if(i==100)
  51.                 {
  52.                         LEDTog(LED1);
  53.                         i=0;
  54.                 }
  55.           }
  56. }

  57. u8 WaveParsing(void)
  58. {
  59.           u32 temp=0x00;
  60.           u32 extraformatbytes=0;

  61.           temp=ReadUnit((u8*)buffer1,0,4,BigEndian);//读'RIFF'
  62.           if(temp!=CHUNK_ID)return 1;
  63.           WAVE_Format.RIFFchunksize=ReadUnit((u8*)buffer1,4,4,LittleEndian);//读文件长度
  64.           temp=ReadUnit((u8*)buffer1,8,4,BigEndian);//读'WAVE'
  65.           if(temp!=FILE_FORMAT)return 2;
  66.           temp=ReadUnit((u8*)buffer1,12,4,BigEndian);//读'fmt '
  67.           if(temp!=FORMAT_ID)return 3;
  68.           temp=ReadUnit((u8*)buffer1,16,4,LittleEndian);//读'fmt'数据长度
  69.           if(temp!=0x10)extraformatbytes=1;
  70.           WAVE_Format.FormatTag=ReadUnit((u8*)buffer1,20,2,LittleEndian);//读音频格式
  71.           if(WAVE_Format.FormatTag!=WAVE_FORMAT_PCM)return 4;  
  72.           WAVE_Format.NumChannels=ReadUnit((u8*)buffer1,22,2,LittleEndian);//读声道数
  73.         WAVE_Format.SampleRate=ReadUnit((u8*)buffer1,24,4,LittleEndian);//读采样率
  74.         WAVE_Format.ByteRate=ReadUnit((u8*)buffer1,28,4,LittleEndian);//读波特率
  75.         WAVE_Format.BlockAlign=ReadUnit((u8*)buffer1,32,2,LittleEndian);//读块对齐
  76.         WAVE_Format.BitsPerSample=ReadUnit((u8*)buffer1,34,2,LittleEndian);//读采样点位数
  77.         if(WAVE_Format.BitsPerSample!=BITS_PER_SAMPLE_16)return 5;
  78.         DataOffset=36;
  79.         if(extraformatbytes==1)
  80.         {
  81.                 temp=ReadUnit((u8*)buffer1,36,2,LittleEndian);//读额外格式字节
  82.                 if(temp!=0x00)return 6;
  83.                 temp=ReadUnit((u8*)buffer1,38,4,BigEndian);//读'fact'
  84.                 if(temp!=FACT_ID)return 7;
  85.                 temp=ReadUnit((u8*)buffer1,42,4,LittleEndian);//读Fact数据大小
  86.                 DataOffset+=10+temp;
  87.         }
  88.         temp=ReadUnit((u8*)buffer1,DataOffset,4,BigEndian);//读'data'
  89.         DataOffset+=4;
  90.         if(temp!=DATA_ID)return 8;
  91.         WAVE_Format.DataSize=ReadUnit((u8*)buffer1,DataOffset,4,LittleEndian);//读采样数据大小
  92.         DataOffset+=4;
  93.         WaveCounter=DataOffset;
  94.         return 0;
  95. }

  96. u32 ReadUnit(u8 *buffer,u8 idx,u8 NbrOfBytes,Endianness BytesFormat)
  97. {
  98.           u32 index=0;
  99.           u32 temp=0;
  100.   
  101.           for(index=0;index<NbrOfBytes;index++)temp|=buffer[idx+index]<<(index*8);
  102.           if (BytesFormat == BigEndian)temp= __REV(temp);
  103.           return temp;
  104. }

  105. void AUDIO_TransferComplete(u32 pBuffer, u32 Size)
  106. {  
  107.           XferCplt=1;
  108.           if(WaveLen)WaveLen-=1024;
  109.           if(WaveLen<1024) WaveLen=0;
  110. }


复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:188033 发表于 2017-4-10 13:56 | 只看该作者
自己顶一个。
回复

使用道具 举报

板凳
ID:1040575 发表于 2022-7-26 15:28 | 只看该作者
看下能不能用
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表