#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define uchar unsigned char
#define uint unsigned int
#define maxsize 10240
//wav 文件数据分别存储进入不同数组
uchar riff[4]={'R','I','F','F'};
uchar size_total[4];
uchar wave[4]={'W','A','V','E'};
uchar fmt[4]={"fmt"};
uchar fmt_size[4];
uchar fmt_pcm[2]={0x01,0x00};
uchar fmt_way[2];
uchar fmt_samplehz[4];
uchar fmt_bytepsec[4];
uchar fmt_bytepsample[2];
uchar fmt_bitpsample[2];
uchar fmt_nouse[2];
uchar data[4] = {'d','a','t','a'};
uchar data_size[4];
uchar *mp;
//wave文件实际数据
uint d_totalsize;
uint d_fmtsize;
uint d_way;
uint d_samplehz;
uint d_bytepsec;
uint d_bytepsample;
uint d_bitpsample;
uint d_datasize;
//创建sin波形参数
uint sin_t;
uint sin_hz;
uint sin_db;
//其他全局变量
uchar namei[15];
uchar nameo[15];
FILE *fp;
FILE *fq;
void data2array(uint x,uchar a[],uchar n)
//将实际数据转化为内存存储形式
{
uchar i;
for(i=0;i<n;i++)
{
a[i] = x&0xff;
x=x>>8;
}
}
uint array2data(uchar a[],uchar n)
//将数组内信息还原为实际数据
{
uchar i;
uint x=0;
for(i=0;i<n;i++)
{
x=x*256+a[n-1-i];
}
return x;
}
//读取功能函数
uchar readfromfile(uchar namein[])
//读取文件说明部分内容
{
uint nn;
printf("\nthe tool is reading the file %s\n",namei);
if((fp=fopen(namein,"rb"))==0)
{
printf("open erro\n");
return 0;
}
if(fread(riff,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
else if(riff[0]!='r'&&riff[0]!='R')
{
printf("reading error\n");
return 0;
}
else if(fread(size_total,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(wave,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
else if(wave[0]!='w'&&wave[0]!='W')
{
printf("write error\n");
return 0;
}
if(fread(fmt,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(fmt_size,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(fmt_pcm,1,2,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(fmt_way,1,2,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(fmt_samplehz,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(fmt_bytepsec,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(fmt_bytepsample,1,2,fp)==0)
{
printf("reading error\n");
return 0;
}
if(fread(fmt_bitpsample,1,2,fp)==0)
{
printf("reading error\n");
return 0;
}
if(array2data(fmt_size,4)==18)
{
if(fread(fmt_nouse,1,2,fp)==0)
{
printf("reading error\n");
return 0;
}
}
if(fread(data,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
else if(data[0]!='d'&&wave[0]!='D')
{
printf("the file is illegal!\n");
return 0;
}
if(fread(data_size,1,4,fp)==0)
{
printf("reading error\n");
return 0;
}
mp=(uchar*)malloc(array2data(data_size,4));
if(fread(mp,1,array2data(data_size,4),fp)==0)
{
printf("reading erroe\n");
return 0;
}
fclose(fp);
return 1;
}
void convert_array2data()
{
d_totalsize = array2data(size_total,4);
d_fmtsize = array2data(fmt_size,4);
d_way = array2data(fmt_way,2);
d_samplehz = array2data(fmt_samplehz,4);
d_bytepsample = array2data(fmt_bytepsample,2);
d_bitpsample = array2data(fmt_bitpsample,2);
d_datasize = array2data(data_size,4);
}
uchar writetofile(uchar nameout[])
//读取文件说明部分内容
{
if((fp=fopen(nameout,"wb"))==0)
{
printf("creat erro\n");
return 0;
}
if(fwrite(riff,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
else if(riff[0]!='r'&&riff[0]!='R')
{
printf("the fie is ilegal!\n");
return 0;
}
else if(fwrite(size_total,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(wave,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
else if(wave[0]!='w'&&wave[0]!='W')
{
printf("the file is illegal!\n");
return 0;
}
if(fwrite(fmt,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(fmt_size,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(fmt_pcm,1,2,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(fmt_way,1,2,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(fmt_samplehz,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(fmt_bytepsec,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(fmt_bytepsample,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(fmt_bitpsample,1,2,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(data,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
else if(data[0]!='d'&&wave[0]!='D')
{
printf("the file is illegal!\n");
return 0;
}
if(fwrite(data_size,1,4,fp)==0)
{
printf("write error\n");
return 0;
}
if(fwrite(mp,1,array2data(data_size,4),fp)==0)
{
printf("write erroe\n");
return 0;
}
fclose(fp);
return 1;
}
void creatsin()
{
uint i;
double n;
printf("please input the parameter of the sin wave\n");
printf("\ntotal time(s): ");
scanf("%d",&sin_t);
printf("\n\nfrequncy(hz): ");
scanf("%d",&sin_hz);
printf("\n\namplification(1-100): ");
scanf("%d",&sin_db);
printf("\n");
d_way = 1;
d_samplehz = 100000;
d_bitpsample = 8;
d_bytepsample = 1;
d_bytepsec = 100000;
d_datasize = 100000* sin_t;
d_fmtsize = 16;
d_totalsize = sin_t * 100000+36;
data2array(d_way,fmt_way,2);
data2array(d_samplehz,fmt_samplehz,4);
data2array(d_bytepsample,fmt_bytepsample,2);
data2array(d_bitpsample,fmt_bitpsample,2);
data2array(d_bytepsec,fmt_bytepsec,4);
data2array(d_datasize,data_size,4);
data2array(d_fmtsize,fmt_size,4);
data2array(d_totalsize,size_total,4);
mp = (uchar*)malloc(d_datasize);
for(i=0;i<d_datasize;i++)
{
n = sin(3.14159 * 2 * sin_hz/100000 * i);
mp[i] = 128 + n * 128 * sin_db/100;
}
system("cls");
}
void display_filescript()
{
printf("---------------------------------------------------\n");
printf("\n\nthe total size: %dB\n\n",d_totalsize);
printf("the fmt chunk size: %dB\n\n",d_fmtsize);
printf("the number of ways: %d\n\n",d_way);
printf("the frequency oof sample: %dhz\n\n",d_samplehz);
printf("the fluency: %dB/s\n\n",d_bytepsec);
printf("the size per sample: %dB\n\n",d_bytepsample);
printf("the sampling bits: %d\n\n",d_datasize);
printf("--------------------------------------------------\n\n\n");
}
int main(void)
{
uchar s;
while(1)
{
printf("\n\n*************************************************\n\n");
printf("1.have a view of a new file\n\n\n");
printf("2.creat a wave file of sin(t)\n\n\n");
printf("3.exit\n");
printf("\n\n*************************************************\n\n");
printf("select the function: ");
scanf("%d",&s);
if(s==1)
{
system("cls");
printf("***notice:the file would better to be set in the same menu as the tool!***\n\ninput the name of the target file: ");
scanf("%s",namei);
convert_array2data();
printf("\n\nthe data are as following \n\n");
display_filescript();
system("pause");
}
else if(s==2)
{
system("cls");
creatsin();
printf("the file you creat has been ready!\n");
display_filescript();
printf("\ninput the name of the output file");
scanf("%s",nameo);
writetofile(nameo);
printf("n\nmission complete!\n\n");
system("pause");
}
else if(s==3)
{
system("cls");
exit(1);
}
system("cls");
}
return 0;
}
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。