如何在C#一波STREAM出原始音频样本? [英] How to create a wave STREAM out of raw audio samples in C#?

查看:319
本文介绍了如何在C#一波STREAM出原始音频样本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#中创建一个波STREAM出原始音频采样的?

How to create a wave STREAM out of raw audio samples in C#?

推荐答案

下面是在C#读写WAV文件的好样本项目:

Here is a good sample project for reading and writing WAV files in C#:

HTTP://www.$c$ cproject.com/KB/audio-video/Concatenation%5FWave%5FFiles.aspx

假设你的原始音频是短的(2字节)整数数组,这是一个简单的任务。 WAV文件的标头为44个字节(见注),所以你写出来的头一个(使用样本中的code),然后是数据。

Assuming that your "raw audio" is an array of short (2-byte) integers, this is a simple task. The header of a WAV file is 44 bytes (see note), so you write out the header first (using the code in the sample) followed by the data.

注意:不是所有的WAV文件是规范,这意味着它们不都有一个44字节的头后面的数据。 WAV格式实际上是一个RIFF格式,这意味着它们可以包含各种不同的数据,并且首标不一定是在开始时。然而,这一切都不重要,因为你只是的WAV文件。

Note: not all WAV files are "canonical", which means they don't all have a 44-byte header followed by the data. The WAV format is actually a RIFF format, which means they can contain all kinds of different data and the header isn't necessarily at the beginning. However, none of this matters since you're just writing WAV files.

更新:如果语音识别程序期待一个流(而不是一个文件路径),可以很容易地创建一个的MemoryStream 像这样的:

Update: If the voice recognition program is expecting a stream (as opposed to a file path), it's easy to create a MemoryStream like this:

byte[] bytes = System.IO.File.ReadAllBytes("c:\whatever.wav"); 
System.IO.MemoryStream stream = new System.IO.MemoryStream(bytes); 

或者,你能避免文件I / O完全并创建WAV文件作为首位在内存中的字节数组,并从创建的MemoryStream

这篇关于如何在C#一波STREAM出原始音频样本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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