/// 把指定的 指定的 Wave 头文件,字节 流式化.
/// 需要写文件的话, 再调用文件相关 *** 作来 写入文件即可
/// </summary>
/// <param name="wh">指定的 头内容</param>
/// <returns>字节流后的头数据.</returns>
public byte[] wav_header_stream(WAVE_Header wh)
{
/// Header 总长 44, 核算一哈.
byte[] re = new byte[44]
/// RIFF_ID
string str = "RIFF"
int i = 0
for (i = 0i <str.Lengthi++) re[i] = (byte)str[i]
/// File_Size
re[i++] = (byte)wh.File_Sizere[i++] = (byte)(wh.File_Size >>8)
re[i++] = (byte)(wh.File_Size >>16)re[i++] = (byte)(wh.File_Size >>24)
///RIFF_Type
str = "WAVE"
for (i <i + str.Lengthi++) re[i] = (byte)str[i]
///FMT_ID
str = "fmt"
for (i <i + str.Lengthi++) re[i] = (byte)str[i]
/// FMT_Size
re[i++] = (byte)wh.FMT_Sizere[i++] = (byte)(wh.FMT_Size >>8)
re[i++] = (byte)(wh.FMT_Size >>16)re[i++] = (byte)(wh.FMT_Size >>24)
///FMT_Tag
re[i++] = (byte)wh.FMT_Tagre[i++] = (byte)(wh.FMT_Tag >>8)
///FMT_Channel
re[i++] = (byte)wh.FMT_Channelre[i++] = (byte)(wh.FMT_Channel >>8)
/// FMT_SamplesPerSec
re[i++] = (byte)wh.FMT_SamplesPerSecre[i++] = (byte)(wh.FMT_SamplesPerSec >>8)
re[i++] = (byte)(wh.FMT_SamplesPerSec >>16)re[i++] = (byte)(wh.FMT_SamplesPerSec >>24)
/// AvgBytesPerSec
re[i++] = (byte)wh.AvgBytesPerSecre[i++] = (byte)(wh.AvgBytesPerSec >>8)
re[i++] = (byte)(wh.AvgBytesPerSec >>16)re[i++] = (byte)(wh.AvgBytesPerSec >>24)
/// BlockAlign
re[i++] = (byte)wh.BlockAlignre[i++] = (byte)(wh.BlockAlign >>8)
/// BitsPerSample
re[i++] = (byte)wh.BitsPerSamplere[i++] = (byte)(wh.BitsPerSample >>8)
/// DATA_ID
str = "data"
for (i <i + str.Lengthi++) re[i] = (byte)str[i]
/// DATA_Size
re[i++] = (byte)wh.DATA_Sizere[i++] = (byte)(wh.DATA_Size >>8)
re[i++] = (byte)(wh.DATA_Size >>16)re[i++] = (byte)(wh.DATA_Size >>24)
return re
}
其中 字节流的顺序,你可以在生成的 byte[] 缓冲区中 观察一下,是否
是你需要的,如果不是 再调整, 数值的对应顺序就可以了.wave文件头我过去看过,但是我已经忘记很多了···没什么印象了
具体你要写的数据是一个什么样的?你有几个实例给我看看吗?文件 *** 作过去写多了,没有什么问题
但是wav这个格式我现在忘记的差不多了,你复制个合适你写的这个文件头的wav文件的文件头数据16进制的给我看看
用UE之类的,我看看可以给你写一个函数吗
(文件 *** 作我比较有兴趣)
http://www.hao123.com/
C# code
public struct WAVE_Header
{
public string RIFF_ID//4 byte , 'RIFF'
public long File_Size//4 byte , 文件长度
public string RIFF_Type//4 byte , 'WAVE'
public string FMT_ID//4 byte , 'fmt'
public long FMT_Size//4 byte , 数值为16或18,18则最后又附加信息
public short FMT_Tag//2 byte , 编码方式,一般为0x0001
public ushort FMT_Channel//2 byte , 声道数目,1--单声道;2--双声道
public ulong FMT_SamplesPerSec//4 byte , 采样频率
public ulong AvgBytesPerSec//4 byte , 每秒所需字节数,记录每秒的数据量
public ushort BlockAlign//2 byte , 数据块对齐单位(每个采样需要的字节数)
public ushort BitsPerSample//2 byte , 每个采样需要的bit数
public string DATA_ID//4 byte , 'data'
public long DATA_Size//4 byte ,
}
WAVE_Header wav_Header = new WAVE_Header()
public void Ini_wav_Header(long data_len)
{
wav_Header.RIFF_ID = "RIFF"
wav_Header.File_Size = data_len + 36
wav_Header.RIFF_Type = "WAVE"
wav_Header.FMT_ID = "fmt"
wav_Header.FMT_Size = 16
wav_Header.FMT_Tag = 0x0001
wav_Header.FMT_Channel = 1//单声道
wav_Header.FMT_SamplesPerSec = 32000//采样频率
wav_Header.AvgBytesPerSec = 32000//每秒所需字节数
wav_Header.BlockAlign = 1//每个采样1个字节
wav_Header.BitsPerSample = 8//每个采样8bit
wav_Header.DATA_ID = "data"
wav_Header.DATA_Size = data_len
}
/*** pcm2wav.c
* add wav head for pcm data
*/
#include <stdio.h>
#include <string.h>
//wav头的结构如下所示:
typedef struct {
char fccID[4]
unsigned long dwSize
char fccType[4]
}HEADER
typedef struct {
char fccID[4]
unsigned long dwSize
unsigned short wFormatTag
unsigned short wChannels
unsigned long dwSamplesPerSec
unsigned long dwAvgBytesPerSec
unsigned short wBlockAlign
unsigned short uiBitsPerSample
}FMT
typedef struct {
char fccID[4]
unsigned long dwSize
}DATA
void show_usage()
{
printf("PCM 2 WAV usage\n eg:pcm2wav pcm_file wav_file channels sample_rate bits\n")
}
int main(int argc, char **argv)
{
char src_file[128] = {0}
char dst_file[128] = {0}
int channels = 1
int bits = 16
int sample_rate = 22050
//以下是为了建立.wav头而准备的变量
HEADER pcmHEADER
FMT pcmFMT
DATA pcmDATA
unsigned short m_pcmData
FILE *fp,*fpCpy
if (argc <5)
{
show_usage()
return -1
}
sscanf(argv[1], "%s", src_file)
sscanf(argv[2], "%s", dst_file)
sscanf(argv[3], "%d", &channels)
sscanf(argv[4], "%d", &sample_rate)
if (argc>6)
{
sscanf(argv[5], "%d", &bits)
}
printf("parameter analyse succeess\n")
if((fp=fopen(src_file, "rb")) == NULL) //读取文件
{
printf("open pcm file %s error\n", argv[1])
return -1
}
if((fpCpy=fopen(dst_file, "wb+")) == NULL) //为转换建立一个新文件
{
printf("create wav file error\n")
return -1
}
//以下是创建wav头的HEADER但.dwsize未定,因为不知道Data的长度。
strcpy(pcmHEADER.fccID,"RIFF")
strcpy(pcmHEADER.fccType,"WAVE")
fseek(fpCpy,sizeof(HEADER),1)//跳过HEADER的长度,以便下面继续写入wav文件的数据
//以上是创建wav头的HEADER
if(ferror(fpCpy))
{
printf("error\n")
}
//以下是创建wav头的FMT
pcmFMT.dwSamplesPerSec=sample_rate
pcmFMT.dwAvgBytesPerSec=pcmFMT.dwSamplesPerSec*sizeof(m_pcmData)
pcmFMT.uiBitsPerSample=bits
strcpy(pcmFMT.fccID,"fmt ")
pcmFMT.dwSize=16
pcmFMT.wBlockAlign=2
pcmFMT.wChannels=channels
pcmFMT.wFormatTag=1
//以上是创建wav头的FMT
fwrite(&pcmFMT,sizeof(FMT),1,fpCpy)//将FMT写入.wav文件
//以下是创建wav头的DATA 但由于DATA.dwsize未知所以不能写入.wav文件
strcpy(pcmDATA.fccID,"data")
pcmDATA.dwSize=0//给pcmDATA.dwsize 0以便于下面给它赋值
fseek(fpCpy,sizeof(DATA),1)//跳过DATA的长度,以便以后再写入wav头的DATA
fread(&m_pcmData,sizeof(unsigned short),1,fp)//从.pcm中读入数据
while(!feof(fp)) //在.pcm文件结束前将他的数据转化并赋给.wav
{
pcmDATA.dwSize+=2//计算数据的长度;每读入一个数据,长度就加一;
fwrite(&m_pcmData,sizeof(unsigned short),1,fpCpy)//将数据写入.wav文件
fread(&m_pcmData,sizeof(unsigned short),1,fp)//从.pcm中读入数据
}
fclose(fp)//关闭文件
pcmHEADER.dwSize=44+pcmDATA.dwSize //根据pcmDATA.dwsize得出pcmHEADER.dwsize的值
rewind(fpCpy)//将fpCpy变为.wav的头,以便于写入HEADER和DATA
fwrite(&pcmHEADER,sizeof(HEADER),1,fpCpy)//写入HEADER
fseek(fpCpy,sizeof(FMT),1)//跳过FMT,因为FMT已经写入
fwrite(&pcmDATA,sizeof(DATA),1,fpCpy) //写入DATA
fclose(fpCpy) //关闭文件
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)