创建C#中的.wav文件 [英] Creating a .wav File in C#

查看:275
本文介绍了创建C#中的.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为借口,学习C#,我一直在尝试编写一个简单的项目:创建音频文件。一开始,我要确保我可以编写符合WAVE格式的文件。我研究的格式在线(例如,这里),但每当我试图回放文件,它将无法正确打开。这里是我的代码。 ?在发生遗漏或不正确

  UINT NUMSAMPLES = 44100; 
USHORT numchannels = 1;
USHORT samplelength = 1; //以字节为单位
UINT采样率= 22050;

的FileStream F =新的FileStream(a.wav,FileMode.Create);
的BinaryWriter WR =新的BinaryWriter(F);

wr.Write(RIFF);
wr.Write(36 + NUMSAMPLES * numchannels * samplelength);
wr.Write(WAVEfmt);
wr.Write(16);
wr.Write((USHORT)1);
wr.Write(numchannels);
wr.Write(采样率);
wr.Write(采样率* samplelength * numchannels);
wr.Write(samplelength * numchannels);
wr.Write((USHORT)(8 * samplelength));
wr.Write(数据);
wr.Write(NUMSAMPLES * samplelength);

//现在,只是一个方波
波形A =新的波形(440,50);

双T = 0.0;
的for(int i = 0; I< NUMSAMPLES;我+ +,T + = 1.0 /采样率)
{
wr.Write((字节)((a.sample(T)+ (samplelength == 1 128:0))及0xff的));
}


解决方案

的主要问题是:



BinaryWriter.Write(串) 写道的前缀是它的长度为 BinaryReader 读回。它不是喜欢你的情况下使用。您需要直接写的,而不是使用字节 BinaryWriter.Write(串)



你是什么应该做的:



将字符串转换成字节,然后直接写入字节

 字节[]数据= System.Text.Encoding.ASCII.GetBytes(RIFF); 
binaryWriter.Write(数据);

或使其一行:

  binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes(RIFF)); 






有也可以是其它的问题,如整数你正在写的要求可能不相同的大小。你应该仔细检查它们。



至于字节顺序,链接你放指出,数据在小尾数和的BinaryWriter 使用little-endian的,所以这不应该是一个问题。


As an excuse to learn C#, I have been trying to code a simple project: creating audio files. To start, I want to make sure that I can write files that meet the WAVE format. I have researched the format online (for example, here), but whenever I try to play back a file, it won't open correctly. Here is my code. Is something missing or incorrect?

uint numsamples = 44100;
ushort numchannels = 1;
ushort samplelength = 1; // in bytes
uint samplerate = 22050;

FileStream f = new FileStream("a.wav", FileMode.Create);
BinaryWriter wr = new BinaryWriter(f);

wr.Write("RIFF");
wr.Write(36 + numsamples * numchannels * samplelength);
wr.Write("WAVEfmt ");
wr.Write(16);
wr.Write((ushort)1);
wr.Write(numchannels);
wr.Write(samplerate);
wr.Write(samplerate * samplelength * numchannels);
wr.Write(samplelength * numchannels);
wr.Write((ushort)(8 * samplelength));
wr.Write("data");
wr.Write(numsamples * samplelength);

// for now, just a square wave
Waveform a = new Waveform(440, 50);

double t = 0.0;
for (int i = 0; i < numsamples; i++, t += 1.0 / samplerate)
{
    wr.Write((byte)((a.sample(t) + (samplelength == 1 ? 128 : 0)) & 0xff));
}

解决方案

The major problem is:

BinaryWriter.Write(string) writes a string that is prefixed with it's length for BinaryReader to read it back. It is not intended to be used like your case. You need to write the bytes directly instead of using BinaryWriter.Write(string).

What you should do:

Convert the string into bytes and then write the bytes directly.

byte[] data = System.Text.Encoding.ASCII.GetBytes("RIFF");
binaryWriter.Write(data);

or make it one line:

binaryWriter.Write(System.Text.Encoding.ASCII.GetBytes("RIFF"));


There may also be other problems, like the integers you are writing may not be of the same size as required. You should check them carefully.

As for endianess, the link you put states that data are in little-endian and BinaryWriter uses little-endian, so this should not be a problem.

这篇关于创建C#中的.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆