把音频数据保存 wav文件, save audio data to wav file
2011-03-09 12:12
981 查看
/* this play_buffer holds the digitized audio */
//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned int FOURCC; /* a four character code */
/* flags for 'wFormatTag' field of WAVEFORMAT */
#define WAVE_FORMAT_PCM 1
/* MMIO macros */
#define mmioFOURCC(ch0, ch1, ch2, ch3) /
((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | /
((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
#define FOURCC_RIFF mmioFOURCC ('R', 'I', 'F', 'F')
#define FOURCC_LIST mmioFOURCC ('L', 'I', 'S', 'T')
#define FOURCC_WAVE mmioFOURCC ('W', 'A', 'V', 'E')
#define FOURCC_FMT mmioFOURCC ('f', 'm', 't', ' ')
#define FOURCC_DATA mmioFOURCC ('d', 'a', 't', 'a')
typedef struct CHUNKHDR {
FOURCC ckid; /* chunk ID */
DWORD dwSize; /* chunk size */
} CHUNKHDR;
/* simplified Header for standard WAV files */
typedef struct WAVEHDR {
CHUNKHDR chkRiff;
FOURCC fccWave;
CHUNKHDR chkFmt;
WORD wFormatTag; /* format type */
WORD nChannels; /* number of channels (i.e. mono, stereo, etc.) */
DWORD nSamplesPerSec; /* sample rate */
DWORD nAvgBytesPerSec; /* for play_buffer estimation */
WORD nBlockAlign; /* block size of data */
WORD wBitsPerSample;
CHUNKHDR chkData;
} WAVEHDR;
#if BYTE_ORDER == BIG_ENDIAN
# define cpu_to_le32(x) SWAP4((x))
# define cpu_to_le16(x) SWAP2((x))
# define le32_to_cpu(x) SWAP4((x))
# define le16_to_cpu(x) SWAP2((x))
#else
# define cpu_to_le32(x) (x)
# define cpu_to_le16(x) (x)
# define le32_to_cpu(x) (x)
# define le16_to_cpu(x) (x)
#endif
static void wav_init_header(WAVEHDR *fileheader)
{
/* stolen from cdda2wav */
int nBitsPerSample = 16;
int channels = 1;
int rate = 8000;
unsigned long nBlockAlign = channels * ((nBitsPerSample + 7) / 8);
unsigned long nAvgBytesPerSec = nBlockAlign * rate;
unsigned long temp = /* data length */ 0 +
sizeof(WAVEHDR) - sizeof(CHUNKHDR);
fileheader->chkRiff.ckid = cpu_to_le32(FOURCC_RIFF);
fileheader->fccWave = cpu_to_le32(FOURCC_WAVE);
fileheader->chkFmt.ckid = cpu_to_le32(FOURCC_FMT);
fileheader->chkFmt.dwSize = cpu_to_le32(16);
fileheader->wFormatTag = cpu_to_le16(WAVE_FORMAT_PCM);
fileheader->nChannels = cpu_to_le16(channels);
fileheader->nSamplesPerSec = cpu_to_le32(rate);
fileheader->nAvgBytesPerSec = cpu_to_le32(nAvgBytesPerSec);
fileheader->nBlockAlign = cpu_to_le16(nBlockAlign);
fileheader->wBitsPerSample = cpu_to_le16(nBitsPerSample);
fileheader->chkData.ckid = cpu_to_le32(FOURCC_DATA);
fileheader->chkRiff.dwSize = cpu_to_le32(temp);
fileheader->chkData.dwSize = cpu_to_le32(0 /* data length */);
}
static void wav_start_write(FILE* fd, WAVEHDR *fileheader)
{
wav_init_header(fileheader);
fwrite(fileheader,1, sizeof(WAVEHDR), fd);
}
static void wav_stop_write(FILE* fd, WAVEHDR *fileheader, int wav_size)
{
unsigned long temp = wav_size + sizeof(WAVEHDR) - sizeof(CHUNKHDR);
fileheader->chkRiff.dwSize = cpu_to_le32(temp);
fileheader->chkData.dwSize = cpu_to_le32(wav_size);
fseek(fd,0,SEEK_SET);
fwrite(fileheader,1, sizeof(WAVEHDR), fd);
}
//unsigned char buf[LENGTH*RATE*SIZE*CHANNELS/8];
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef unsigned int FOURCC; /* a four character code */
/* flags for 'wFormatTag' field of WAVEFORMAT */
#define WAVE_FORMAT_PCM 1
/* MMIO macros */
#define mmioFOURCC(ch0, ch1, ch2, ch3) /
((DWORD)(BYTE)(ch0) | ((DWORD)(BYTE)(ch1) << 8) | /
((DWORD)(BYTE)(ch2) << 16) | ((DWORD)(BYTE)(ch3) << 24))
#define FOURCC_RIFF mmioFOURCC ('R', 'I', 'F', 'F')
#define FOURCC_LIST mmioFOURCC ('L', 'I', 'S', 'T')
#define FOURCC_WAVE mmioFOURCC ('W', 'A', 'V', 'E')
#define FOURCC_FMT mmioFOURCC ('f', 'm', 't', ' ')
#define FOURCC_DATA mmioFOURCC ('d', 'a', 't', 'a')
typedef struct CHUNKHDR {
FOURCC ckid; /* chunk ID */
DWORD dwSize; /* chunk size */
} CHUNKHDR;
/* simplified Header for standard WAV files */
typedef struct WAVEHDR {
CHUNKHDR chkRiff;
FOURCC fccWave;
CHUNKHDR chkFmt;
WORD wFormatTag; /* format type */
WORD nChannels; /* number of channels (i.e. mono, stereo, etc.) */
DWORD nSamplesPerSec; /* sample rate */
DWORD nAvgBytesPerSec; /* for play_buffer estimation */
WORD nBlockAlign; /* block size of data */
WORD wBitsPerSample;
CHUNKHDR chkData;
} WAVEHDR;
#if BYTE_ORDER == BIG_ENDIAN
# define cpu_to_le32(x) SWAP4((x))
# define cpu_to_le16(x) SWAP2((x))
# define le32_to_cpu(x) SWAP4((x))
# define le16_to_cpu(x) SWAP2((x))
#else
# define cpu_to_le32(x) (x)
# define cpu_to_le16(x) (x)
# define le32_to_cpu(x) (x)
# define le16_to_cpu(x) (x)
#endif
static void wav_init_header(WAVEHDR *fileheader)
{
/* stolen from cdda2wav */
int nBitsPerSample = 16;
int channels = 1;
int rate = 8000;
unsigned long nBlockAlign = channels * ((nBitsPerSample + 7) / 8);
unsigned long nAvgBytesPerSec = nBlockAlign * rate;
unsigned long temp = /* data length */ 0 +
sizeof(WAVEHDR) - sizeof(CHUNKHDR);
fileheader->chkRiff.ckid = cpu_to_le32(FOURCC_RIFF);
fileheader->fccWave = cpu_to_le32(FOURCC_WAVE);
fileheader->chkFmt.ckid = cpu_to_le32(FOURCC_FMT);
fileheader->chkFmt.dwSize = cpu_to_le32(16);
fileheader->wFormatTag = cpu_to_le16(WAVE_FORMAT_PCM);
fileheader->nChannels = cpu_to_le16(channels);
fileheader->nSamplesPerSec = cpu_to_le32(rate);
fileheader->nAvgBytesPerSec = cpu_to_le32(nAvgBytesPerSec);
fileheader->nBlockAlign = cpu_to_le16(nBlockAlign);
fileheader->wBitsPerSample = cpu_to_le16(nBitsPerSample);
fileheader->chkData.ckid = cpu_to_le32(FOURCC_DATA);
fileheader->chkRiff.dwSize = cpu_to_le32(temp);
fileheader->chkData.dwSize = cpu_to_le32(0 /* data length */);
}
static void wav_start_write(FILE* fd, WAVEHDR *fileheader)
{
wav_init_header(fileheader);
fwrite(fileheader,1, sizeof(WAVEHDR), fd);
}
static void wav_stop_write(FILE* fd, WAVEHDR *fileheader, int wav_size)
{
unsigned long temp = wav_size + sizeof(WAVEHDR) - sizeof(CHUNKHDR);
fileheader->chkRiff.dwSize = cpu_to_le32(temp);
fileheader->chkData.dwSize = cpu_to_le32(wav_size);
fseek(fd,0,SEEK_SET);
fwrite(fileheader,1, sizeof(WAVEHDR), fd);
}
相关文章推荐
- OllyDbg 在可执行文件中无法定位数据(Unable to locate data in executable file)
- Flex保存文件 FileReference.save(data,filename)
- 数组和字典的writeToFile方法——在项目开发中处理网络数据的时候,可以把请求获得的网络数据保存为plist文件,这样更方便开发
- 如何缩小或者扩大数据文件 How to Resize a Datafile (Doc ID 1029252.6)
- Exporting Mailbox Data to a PST File in Exchange 2007 SP1(如何导出邮箱数据为PST文件)
- Flex保存文件 FileReference.save(data,filename)
- OpenCV Save CvRect to File 保存CvRect变量到文件
- Flex保存文件 FileReference.save(data,filename)
- 根据文件data.txt和模板文件template.tmpl,实现将数据文件和模板文件的合并,并保存在输出文件
- 保存文件对话框SaveFileDialog (另存为对话框)
- SaveFileDialog 保存文件示例和一些属性
- iphone利用AudioQueue播放音频文件(mp3,aac,caf,wav等)
- 译文:Silverlight 3.0 新功能之六 : 保存文件对话框(Save File Dialog Box)
- R语言保存文件 Error in save error writing to connection
- C++读取WAV音频文件的头部数据的实现方法
- 如何rename datafile name中存在乱码的数据文件
- Android 将少量的数据文件保存在 data/data 目录下
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- <input id="File_Audio" type="file" /> 一步到位上传图片、音频等文件
- Android开发学习---android下的数据持久化,保存数据到rom文件,android_data目录下文件访问的权限控制