需要帮助在字节级别操作 WAV (RIFF) 文件

发布于 2024-08-28 03:06:13 字数 1482 浏览 3 评论 0原文

我正在用 C# 编写一个应用程序,它将录制音频文件 (*.wav) 并自动标记和命名它们。波形文件是 RIFF 文件(如 AVI),除了波形数据块之外,还可以包含元数据块。所以现在我试图弄清楚如何从录制的波形文件中读取和写入 RIFF 元数据。

我使用 NAudio 来录制文件,并在他们的论坛 作为很好,了解如何读取和写入 RIFF 标签。虽然我收到了许多好的答案,但没有一个解决方案可以像我希望的那样轻松地读取和写入 RIFF 块。

但更重要的是,我对处理字节级文件的经验很少,并且认为这可能是一个很好的学习机会。所以现在我想尝试编写自己的类,它可以读取 RIFF 文件并允许从文件中读取和写入元数据。

我在 C# 中使用过流,但总是同时使用整个流。所以现在我有点迷失了,我必须逐字节地考虑文件。具体来说,我将如何在文件中间删除或插入字节?我尝试通过 FileStream 将文件读入字节数组 (byte[]),如下面的代码所示。

System.IO.FileStream waveFileStream = System.IO.File.OpenRead(@"C:\sound.wav");
byte[] waveBytes = new byte[waveFileStream.Length];
waveFileStream.Read(waveBytes, 0, waveBytes.Length);

我可以通过 Visual Studio 调试器看到前四个字节是文件的 RIFF 标头。 alt text

但是,在执行更改其大小的操作(例如插入或删除值)时,数组很难处理。所以我想我可以将 byte[] 放入这样的列表中。

List<byte> list = waveBytes.ToList<byte>();

这将使对文件逐字节的任何操作变得更加容易,但我担心我可能会丢失诸如 System.IO 命名空间中的类之类的东西,这将使这一切变得更加容易。我走在正确的道路上吗,或者有更好的方法吗?我还应该提到,我不太关心性能,并且不想处理指针或不安全的代码块,例如 这个人

如果有帮助的话,这里有一篇关于 RIFF/WAV 文件的好文章格式。

I'm writing an an application in C# that will record audio files (*.wav) and automatically tag and name them. Wave files are RIFF files (like AVI) which can contain meta data chunks in addition to the waveform data chunks. So now I'm trying to figure out how to read and write the RIFF meta data to and from recorded wave files.

I'm using NAudio for recording the files, and asked on their forums as well on SO for way to read and write RIFF tags. While I received a number of good answers, none of the solutions allowed for reading and writing RIFF chunks as easily as I would like.

But more importantly I have very little experience dealing with files at a byte level, and think this could be a good opportunity to learn. So now I want to try writing my own class(es) that can read in a RIFF file and allow meta data to be read, and written from the file.

I've used streams in C#, but always with the entire stream at once. So now I'm little lost that I have to consider a file byte by byte. Specifically how would I go about removing or inserting bytes to and from the middle of a file? I've tried reading a file through a FileStream into a byte array (byte[]) as shown in the code below.

System.IO.FileStream waveFileStream = System.IO.File.OpenRead(@"C:\sound.wav");
byte[] waveBytes = new byte[waveFileStream.Length];
waveFileStream.Read(waveBytes, 0, waveBytes.Length);

And I could see through the Visual Studio debugger that the first four byte are the RIFF header of the file.
alt text

But arrays are a pain to deal with when performing actions that change their size like inserting or removing values. So I was thinking I could then to the byte[] into a List like this.

List<byte> list = waveBytes.ToList<byte>();

Which would make any manipulation of the file byte by byte a whole lot easier, but I'm worried I might be missing something like a class in the System.IO name-space that would make all this even easier. Am I on the right track, or is there a better way to do this? I should also mention that I'm not hugely concerned with performance, and would prefer not to deal with pointers or unsafe code blocks like this guy.

If it helps at all here is a good article on the RIFF/WAV file format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2024-09-04 03:06:13

我不是用 C# 编写的,但从我的角度来看,可以指出一些不好的地方:

1) 不要读取内存中的整个 WAV 文件,除非这些文件是您自己的文件并且故意尺寸很小。

2)不需要在内存中插入数据。例如,您可以简单地执行以下操作:分析源文件、存储块的偏移量以及读取内存中的元数据;在对话框中呈现用于编辑的元数据;在保存写入RIFF-WAV标头、fmt块的同时,从源文件传输音频数据(通过读写块),添加元数据;更新 RIFF-WAV 标头。

3)尝试将元数据保存在文件尾部。这将导致仅交替标记不需要重写整个文件。

此处似乎提供了一些有关在 C# 中使用 RIFF 文件的资料。

I did not write in C#, but can point on some places which are bad from my point of view:

1) Do not read whole WAV files in memory unless the files are your own files and knowingly have small size.

2) There is no need to insert a data in memory. You can simply for example do about the following: Analyze source file, store offsets of chunks, and read metadata in memory; present the metadata for editing in a dialog; while saving write RIFF-WAV header, fmt chunk, transfer audio data from source file (by reading and writing blocks), add metadata; update RIFF-WAV header.

3) Try save metadata in the tail of file. This will results in alternating only tag will not require re-writing of whole file.

It seems some sources regarding working with RIFF files in C# are present here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文