標(biāo)題:
STM32F4開發(fā)源碼—I2S音頻播放
[打印本頁(yè)]
作者:
e_boy
時(shí)間:
2017-4-10 13:51
標(biāo)題:
STM32F4開發(fā)源碼—I2S音頻播放
STM32F4開發(fā)源碼之I2S音頻播放,利用原子STM32F407開發(fā)板進(jìn)行編寫。
0.png
(55.48 KB, 下載次數(shù): 60)
下載附件
2017-4-10 15:41 上傳
完整源碼下載:
(10)I2S音頻播放.zip
(150.58 KB, 下載次數(shù): 61)
2017-4-10 13:50 上傳
點(diǎn)擊文件名下載附件
STM32F4開發(fā)源碼——I2S音頻播放
下載積分: 黑幣 -5
部分源程序預(yù)覽:
#include "wavplayer.h"
#include "cs4334.h"
#include "lcd.h"
#include "led.h"
#include "ff.h"
u16 buffer1[1024]={0x00};
u16 buffer2[1024]={0x00};
__IO u32 WaveLen=0;
__IO u32 XferCplt=0;
__IO u32 DataOffset=0;
__IO u32 WaveCounter;
u8 buffer_switch=1;
WAVE_TypeDef WAVE_Format;
void Wavplay(void)
{
FATFS fatfs;
FIL fileR;
UINT BytesRead;
u8 i=0;
f_mount(0, &fatfs);
f_open(&fileR, "0:/WAV/melody.wav" , FA_READ);
f_read(&fileR, buffer1, 1024, &BytesRead);
while(WaveParsing())LCD_String(20,50,"File read error!",RED);
LCD_String(20,50,"0:/WAV/melody.wav",WHITE);
WaveLen = WAVE_Format.DataSize;
AUDIO_Init(WAVE_Format.SampleRate);
f_lseek(&fileR, WaveCounter);//跳過文件頭
f_read(&fileR, buffer1, 1024, &BytesRead);
f_read(&fileR, buffer2, 1024, &BytesRead);
Audio_MAL_Play((u32)buffer1, 1024);
buffer_switch=1;
XferCplt=0;
while(WaveLen!=0)
{
while(XferCplt==0);//等待DMA傳送完成
XferCplt=0;
if(buffer_switch==0)
{
Audio_MAL_Play((u32)buffer1,1024);//從buffer1播放
f_read(&fileR,buffer2,1024,&BytesRead);//填充buffer2
buffer_switch=1;
}
else
{
Audio_MAL_Play((u32)buffer2,1024);//從buffer2播放
f_read(&fileR,buffer1,1024,&BytesRead);//填充buffer1
buffer_switch=0;
}
i++;
if(i==100)
{
LEDTog(LED1);
i=0;
}
}
}
u8 WaveParsing(void)
{
u32 temp=0x00;
u32 extraformatbytes=0;
temp=ReadUnit((u8*)buffer1,0,4,BigEndian);//讀'RIFF'
if(temp!=CHUNK_ID)return 1;
WAVE_Format.RIFFchunksize=ReadUnit((u8*)buffer1,4,4,LittleEndian);//讀文件長(zhǎng)度
temp=ReadUnit((u8*)buffer1,8,4,BigEndian);//讀'WAVE'
if(temp!=FILE_FORMAT)return 2;
temp=ReadUnit((u8*)buffer1,12,4,BigEndian);//讀'fmt '
if(temp!=FORMAT_ID)return 3;
temp=ReadUnit((u8*)buffer1,16,4,LittleEndian);//讀'fmt'數(shù)據(jù)長(zhǎng)度
if(temp!=0x10)extraformatbytes=1;
WAVE_Format.FormatTag=ReadUnit((u8*)buffer1,20,2,LittleEndian);//讀音頻格式
if(WAVE_Format.FormatTag!=WAVE_FORMAT_PCM)return 4;
WAVE_Format.NumChannels=ReadUnit((u8*)buffer1,22,2,LittleEndian);//讀聲道數(shù)
WAVE_Format.SampleRate=ReadUnit((u8*)buffer1,24,4,LittleEndian);//讀采樣率
WAVE_Format.ByteRate=ReadUnit((u8*)buffer1,28,4,LittleEndian);//讀波特率
WAVE_Format.BlockAlign=ReadUnit((u8*)buffer1,32,2,LittleEndian);//讀塊對(duì)齊
WAVE_Format.BitsPerSample=ReadUnit((u8*)buffer1,34,2,LittleEndian);//讀采樣點(diǎn)位數(shù)
if(WAVE_Format.BitsPerSample!=BITS_PER_SAMPLE_16)return 5;
DataOffset=36;
if(extraformatbytes==1)
{
temp=ReadUnit((u8*)buffer1,36,2,LittleEndian);//讀額外格式字節(jié)
if(temp!=0x00)return 6;
temp=ReadUnit((u8*)buffer1,38,4,BigEndian);//讀'fact'
if(temp!=FACT_ID)return 7;
temp=ReadUnit((u8*)buffer1,42,4,LittleEndian);//讀Fact數(shù)據(jù)大小
DataOffset+=10+temp;
}
temp=ReadUnit((u8*)buffer1,DataOffset,4,BigEndian);//讀'data'
DataOffset+=4;
if(temp!=DATA_ID)return 8;
WAVE_Format.DataSize=ReadUnit((u8*)buffer1,DataOffset,4,LittleEndian);//讀采樣數(shù)據(jù)大小
DataOffset+=4;
WaveCounter=DataOffset;
return 0;
}
u32 ReadUnit(u8 *buffer,u8 idx,u8 NbrOfBytes,Endianness BytesFormat)
{
u32 index=0;
u32 temp=0;
for(index=0;index<NbrOfBytes;index++)temp|=buffer[idx+index]<<(index*8);
if (BytesFormat == BigEndian)temp= __REV(temp);
return temp;
}
void AUDIO_TransferComplete(u32 pBuffer, u32 Size)
{
XferCplt=1;
if(WaveLen)WaveLen-=1024;
if(WaveLen<1024) WaveLen=0;
}
復(fù)制代碼
作者:
e_boy
時(shí)間:
2017-4-10 13:56
自己頂一個(gè)。
作者:
chen0610
時(shí)間:
2022-7-26 15:28
看下能不能用
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1