基于STM32的WAV音频格式播放器 (4)(转)
- UID
- 1029342
- 性别
- 男
|
基于STM32的WAV音频格式播放器 (4)(转)
WAV初始化:
u8 WAV_Init(u8* wav_buf)
{
if(Check_Ifo(wav_buf,"RIFF"))
return1;
wav1.wavlen=Get_num(wav_buf+4,4);
printf("\n\rwav1.wavlen= %ld\n\r",wav1.wavlen);
//if(Check_Ifo(wav_buf+8,"WAVE"))return 2;//WAVE错误标志
//if(Check_Ifo(wav_buf+12,"fmt "))return 3;//fmt错误标志
wav1.formart=Get_num(wav_buf+20,2);//格式类别
printf("\n\rwav1.formart= %d\n\r",wav1.formart);
wav1.CHnum=Get_num(wav_buf+22,2);//通道数
printf("\n\rwav1.CHnum= %d\n\r",wav1.CHnum);
CHanalnum=wav1.CHnum;
wav1.SampleRate=Get_num(wav_buf+24,4);//采样率
printf("\n\rwav1.SampleRate= %ld\n\r",wav1.SampleRate);
wav1.speed=Get_num(wav_buf+28,4);//音频转换数率
printf("\n\rwav1.speed= %ld\n\r",wav1.speed);
wav1.ajust=Get_num(wav_buf+32,2);//数据块调速数
printf("\n\rwav1.ajust= %d\n\r",wav1.ajust);
wav1.SampleBits=Get_num(wav_buf+34,2);//样本数据位数
printf("\n\rwav1.SampleBits= %d\n\r",wav1.SampleBits);
Bitnum=wav1.SampleBits;
//if(Check_Ifo(wav_buf+36,"data"))return 4;//数据标志错误
wav1.DATAlen=Get_num(wav_buf+40,4);//数据长度
printf("\n\rwav1.DATAlen= %d\n\r",wav1.DATAlen);
if(wav1.wavlen<0x100000)
{
printf("\n\rwav1.wavlen= %dkb\n\r",(wav1.wavlen)>>10);
}
else
{
printf("\n\rwav1.wavlen= %dMb\n\r",(wav1.wavlen)>>20);
}
if(wav1.formart==1)
printf("\n\rWAVPCM\n\r");
if(wav1.CHnum==1)
printf("\n\rsingle\n\r");
else
printf("\n\rstereo\n\r");
printf("\n\rwav1.SampleRate= %dkHz\n\r",(wav1.SampleRate)/1000);
printf("\n\rwav1.speed= %dbps\n\r",(wav1.speed)/1000);
printf("\n\rwav1.SampleBits= %dbit\n\r",wav1.SampleBits);
return0;
}
u8 Check_Ifo(u8* pbuf1,u8* pbuf2)
{
u8i;
for(i=0;i<4;i++)
if(pbuf1!=pbuf2)
return1;
return0;
}
u32 Get_num(u8* pbuf,u8 len)
{
u32 num;
if(len==2)num=(pbuf[1]<<8)|pbuf[0];
elseif(len==4)num=(pbuf[3]<<24)|(pbuf[2]<<16)|(pbuf[1]<<8)|pbuf[0];
returnnum;
}
WAV播放:
u8 Playwav(char *file)
{
FILfwav;
FRESULTRes;
UINTBR;
unsignedchar i;
unsignedint times;
Res= f_open(&fwav, file, FA_OPEN_EXISTING | FA_READ);
if(Res!= FR_OK)
{
printf("\n\ropenfile error : %d\n\r",Res);
}
else
{
Res = f_read(&fwav, wav_buf,sizeof(wav_buf), &BR); /* Read achunk of src file */
if(Res==FR_OK)
{
WAV_Init(wav_buf);
DACdone=0;
DApc=44;//跳过头信息
Timerx_Init(1000000/wav1.SampleRate,72);//定时器初始化
times=(wav1.DATAlen>>10)-1;//计算数据大小
for(i=0;i<times/32;i++)//循环一次转换32KB数据
{
while(!DACdone);//等待前面16384字节转换完成 DACdone=0;
Res= f_read(&fwav, wav_buf, 16384, &BR);
while(!DACdone);//
等待前面16384字节转换完成
DACdone=0;
Res= f_read(&fwav, wav_buf, 16384, &BR);//读取数据
}
}
else
{
printf("\n\rreadfile error : %d\n\r",Res);
}
f_close(&fwav);
}
return 0;
} |
|
|
|
|
|