因此,我一直在遵循类似的教程来尝试构建.wav文件。但是,我似乎无法使它正常工作,因为wav文件将正确打开,但被列为0秒,并且不会播放任何内容。
http://www.topherlee.com/software/pcm-tut-wavformat.html
http://soundfile.sapp.org/doc/WaveFormat/
代码(很糟糕,因为它只是尝试使其运行的测试):
using System;
using System.IO;
using System.Linq;
using System.Text;
namespace Namespace
{
class Program
{
static void Main()
{
StringBuilder SB = new StringBuilder();
for (int e = 0; e < 200000; e++)
{
SB.Append(" ff");
}
int Size = SB.Length / 3;
StringBuilder SBHexSize = new StringBuilder(Convert.ToString(Size, 16));
while (SBHexSize.Length < 8)
{
SBHexSize.Append("0");
}
string HexSize = SBHexSize.ToString();
const string RIFF = "52 49 46 46";
const string RestOfHeader = "57 41 56 45 66 6d 74 20 10 00 00 00 01 00 02 00 22 56 00 00 88 58 01 00 04 00 10 00 64 61 74 61 00 08 00 00";
//Console.WriteLine($"{RIFF} {HexSize[6..8]} {HexSize[4..6]} {HexSize[2..4]} {HexSize[..2]} {RestOfHeader}{SB.ToString()}");
//System.Threading.Thread.Sleep(-1);
//ByteArray bytes = new ByteArray($"{RIFF} {HexSize[6..8]} {HexSize[4..6]} {HexSize[2..4]} {HexSize[..2]} {RestOfHeader}{SB.ToString()}");
ByteArray bytes = new ByteArray($"{RIFF} FF FF FF FF {RestOfHeader}{SB.ToString()}");
bytes.Write();
}
}
class ByteArray
{
private byte[] array;
public ByteArray(string hex)
{
array = Enumerable.Range(0, hex.Length)
.Where(x => x % 3 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public void Write()
{
File.WriteAllBytes(@"C:\Users\name\source\repos\MusicCreator\MusicCreator\musictest.wav", array);
}
}
}
代码是否有问题,例如我尝试写字节的方式,还是它们本身是字节?如果您有任何想法,请告诉我。我对数字音频非常陌生,因此希望获得任何反馈!
WAV
文件的标题必须包含数据的长度:
37-40 "data" "data" chunk header. Marks the beginning of the data section.
41-44 File size (data) Size of the data section.
您具有硬编码的长度:64 61 74 61
(“数据”)00 08 00 00
(长度)
但是您应该在此处写入数据的长度,例如30 D4 00 00
这就是为什么在链接的示例中,有一个“ finalize”部分,在那里他们将数据的长度写回到了标头中。