S3C2440-IIS放音
下面這個程序完成從WAV音頻文件中提取出數(shù)組。
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include <memory.h>
int main(){
FILE *stream, *fp;
unsigned char *music;
char ch[5];
int start, end, num, t, i, j, r;
if(((stream = fopen("D:music.wav","rb")) == NULL) || ((fp = fopen("D:music.h","wb+")) == NULL)){
printf("%s","cannot open output file.");
return 1;
}
fseek(stream,0,SEEK_SET);
start = ftell(stream); //獲得文件的起始地址
fseek(stream,0,SEEK_END);
end = ftell(stream); //獲得文件的結(jié)束地址
fseek(stream,0,SEEK_SET);
music = (unsigned char *)malloc(end-start); //動態(tài)分配一個buffer
fwrite("unsigned char music[",1,20,fp);
fwrite("]={",1,3,fp);
fwrite("", 2, 1, fp);
num = (end - start - 0x2c) / 16; //m每行16個
t = (end - start - 0x2c) % 16;
fread(music, 1, end - start, stream);
for(i = 0; i < num; i++){
for(j = 0; j < 16; j++){
memset(ch,0,5);
r = (int)music[i*16+j+0x2c]; //從0x2c開始是音頻數(shù)據(jù)
r &= 0xff;
sprintf(ch,"0x%02x",r); //先轉(zhuǎn)化成一定格式
fwrite(ch,sizeof(ch),1,fp);
fwrite(",",1,1,fp);
}
fwrite("",1,2,fp);
}
for(i = 0; i < t; i++){
memset(ch,0,5);
r = (int)music[i*16+j+0x2c];
r &= 0xff;
sprintf(ch,"0x%02x",r);
fwrite(ch,sizeof(ch),1,fp);
if(i != t-1)
fwrite(",",1,1,fp);
}
fwrite("};",1,2,fp);
fclose(stream);
fclose(fp);
system("pause");
return 0;
}
評論