1. 程式人生 > >C語言建立生成WAV音訊檔案

C語言建立生成WAV音訊檔案

對於WAV檔案,首先明白WAV檔案頭的格式

引用部落格http://blog.csdn.net/yzhouen/article/details/777459有詳細說明。

所以我們一般定義一個結構體來表示頭

typedef struct
{
char chRIFF[4];                 // "RIFF" 標誌  
int  total_Len;                 // 檔案長度      
char chWAVE[4];                 // "WAVE" 標誌  
char chFMT[4];                  // "fmt" 標誌 
int  dwFMTLen;                  // 過渡位元組(不定)  一般為16
short fmt_pcm;                  // 格式類別  
short  channels;                // 聲道數  
int fmt_samplehz;               // 取樣率 
int fmt_bytepsec;               // 位速  
short fmt_bytesample;           // 一個取樣多聲道資料塊大小  
short fmt_bitpsample;   // 一個取樣佔的 bit 數  
char chDATA[4];                 // 資料標記符"data "  
int  dwDATALen;                 // 語音資料的長度,比檔案長度小42一般。這個是計算音訊播放時長的關鍵引數~  
}WaveHeader;

只要標頭檔案結構體這個設定對了,後面的音訊資料直接寫入,一個WAV檔案就生成了。

部分計算公式引用部落格http://blog.csdn.net/misol/article/details/6265614

那音訊的播放時長計算如下:()

  1. 每個取樣點位元組數:bits = channels * BitsPerSample / 8   ( BitsPerSample: 16bit 一般 )
  2. 每秒位元組數: m = nSamplesPerSec * bits;  (nSamplesPerSec:每秒取樣數,也就是取樣率~rate)
  3. 音訊檔案播放時長:time = dlen / m;     ( dlen就是語音資料長度,也就是檔案大小減去42~ )
領附上一個簡單的生成正弦波音訊檔案

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>


#define sin_t       5
#define sin_hz      1000
#define sin_db100
#define size_buf    sin_t*100000


unsigned char buf[size_buf*2];


#if 1
typedef struct
{
char chRIFF[4];                 // "RIFF" 標誌  
int  total_Len;                 // 檔案長度      
char chWAVE[4];                 // "WAVE" 標誌  
char chFMT[4];                  // "fmt" 標誌 
int  dwFMTLen;                  // 過渡位元組(不定)  一般為16
short fmt_pcm;                  // 格式類別  
short  channels;                // 聲道數  
int fmt_samplehz;               // 取樣率 
int fmt_bytepsec;               // 位速  
short fmt_bytesample;           // 一個取樣多聲道資料塊大小  
short fmt_bitpsample;   // 一個取樣佔的 bit 數  
char chDATA[4];                 // 資料標記符"data "  
int  dwDATALen;                 // 語音資料的長度,比檔案長度小42一般。這個是計算音訊播放時長的關鍵引數~  
}WaveHeader;


WaveHeader WavInf = {
"RIFF",
sin_t*500*2+36,
"WAVE",
"fmt ",
16,
1,
2,
100000,
100000,
1,
8,
"data",
500*sin_t*2
};


#else
//將實際資料轉化為記憶體儲存形式
void data2array(unsigned int x,unsigned char a[],unsigned char n)

    unsigned char i;
for(i=0;i<n;i++)

        a[i]=x&0xff;
        x=x>>8;
    }
}


//unsigned char WavFileHeader[42] = {0};
unsigned char wav_header[] = {  
'R', 'I', 'F', 'F',      // "RIFF" 標誌  
0, 0, 0, 0,              // 檔案長度  
'W', 'A', 'V', 'E',      // "WAVE" 標誌  
'f', 'm', 't', ' ',      // "fmt" 標誌  
16, 0, 0, 0,             // 過渡位元組(不定)  
0x01, 0x00,              // 格式類別  
0x01, 0x00,              // 聲道數  
0, 0, 0, 0,              // 取樣率  
0, 0, 0, 0,              // 位速  
0x01, 0x00,              // 一個取樣多聲道資料塊大小  
0x10, 0x00,              // 一個取樣佔的 bit 數  
'd', 'a', 't', 'a',      // 資料標記符"data "  
0, 0, 0, 0               // 語音資料的長度,比檔案長度小42一般。這個是計算音訊播放時長的關鍵引數~  
};  


void InitWavHeader(void)
{
    unsigned long a = sin_t*100000+36;
    data2array(a,&wav_header[4],4);
    a = 100000;
    data2array(a,&wav_header[24],4);
    data2array(a,&wav_header[28],4);
    a = sin_t*100000;
    data2array(a,&wav_header[40],4);
}
#endif


int main()
{
long i;
    int j = 0;
    int ret;
    //InitWavHeader();
for(i=0;i<size_buf;i++)
{
        buf[j++] = sin((size_buf - i)*(3.14159*2)/100000*sin_hz)*128*sin_db/100+128;
        buf[j++] = 0;
}
int fd;
fd = open("./test.wav",O_CREAT|O_RDWR);
if(fd<0)
{
printf("open error\n");
return 0;
}
ret = write(fd,(char *)&WavInf,sizeof(WavInf));
//write(fd,wav_header,44);
ret = write(fd,buf,100000*sin_t*2);
    close(fd);


return 0;
}