如何在 C# 中播放提取的波形文件字节数组?

发布于 2024-08-20 11:24:04 字数 1106 浏览 4 评论 0原文

目前,我已成功分离 WAVE 文件的左声道和右声道,并将标题包含在 byte[] 数组中。我的下一步是要播放两个频道。这怎么能做到呢?

这是一个代码片段:

byte[] song_left = new byte[fa.Length];
byte[] song_right = new byte[fa.Length];

int p = 0;

for (int c = 0; c < 43; c++)
{
    song_left[p] = header[c];
    p++;
}

int q = 0;

for (s = startByte; s < length; s = s + 3)
{
    song_left[s] = sLeft[q];
    q++;
    s++;
    song_left[s] = sLeft[q];
    q++;
}

p = 0;

for (int c = 0; c < 43; c++)
{
    song_right[p] = header[c];
    p++;
}

这部分从右通道和光通道读取标题和数据并将其保存到数组 sLeft[] 和 sRight[] 中。这部分工作完美。

获得字节数组后,我执行了以下操作:

System.IO.File.WriteAllBytes("c:\\left.wav", song_left);

System.IO.File.WriteAllBytes("c:\\right.wav", song_right);

添加了一个按钮来播放保存的波形文件:

private void button2_Click(object sender, EventArgs e)
{
    spWave = new SoundPlayer("c:\\left.wav");
    spWave.Play();          
}

一旦我点击播放按钮,就会出现此错误:

“System.InvalidOperationException”类型的未处理异常 发生在System.dll

附加信息:波形标头已损坏。

有什么想法吗?

At the moment i have managed to separate the left and right channel of a WAVE file and have included the header in a byte[] array. My next step is to be about to play both channels. How can this be done?

Here is a code snippet:

byte[] song_left = new byte[fa.Length];
byte[] song_right = new byte[fa.Length];

int p = 0;

for (int c = 0; c < 43; c++)
{
    song_left[p] = header[c];
    p++;
}

int q = 0;

for (s = startByte; s < length; s = s + 3)
{
    song_left[s] = sLeft[q];
    q++;
    s++;
    song_left[s] = sLeft[q];
    q++;
}

p = 0;

for (int c = 0; c < 43; c++)
{
    song_right[p] = header[c];
    p++;
}

This part is reading the header and data from both the right and light channel and saving it to array sLeft[] and sRight[]. This part is working perfectly.

Once I obtained the byte arrays, I did the following:

System.IO.File.WriteAllBytes("c:\\left.wav", song_left);

System.IO.File.WriteAllBytes("c:\\right.wav", song_right);

Added a button to play the saved wave file:

private void button2_Click(object sender, EventArgs e)
{
    spWave = new SoundPlayer("c:\\left.wav");
    spWave.Play();          
}

Once I hit the play button, this error appers:

An unhandled exception of type 'System.InvalidOperationException'
occurred in System.dll

Additional information: The wave header is corrupt.

Any ideas?

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

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

发布评论

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

评论(2

暗藏城府 2024-08-27 11:24:04

对于“分离 WAVE 文件的左声道和右声道”,请使用 http://alvas 中的 AudioCompressionManager.SplitStereo 方法.net/alvas.audio.aspx

下面的代码适合您的目的

        IntPtr formatMono = AudioCompressionManager.CreateFormat(formatArray);
        IntPtr formatStereo = IntPtr.Zero;
        byte[] dataStereo = null;
        AudioCompressionManager.MergeStereo(formatMono, leftData, rightData, ref formatStereo, ref dataStereo);
        PlayerEx plex = new PlayerEx();
        plex.OpenPlayer(formatStereo);
        plex.AddData(dataStereo);
        plex.StartPlay();

For "separate the left and right channel of a WAVE file" use AudioCompressionManager.SplitStereo method from http://alvas.net/alvas.audio.aspx

Code below for your purpose

        IntPtr formatMono = AudioCompressionManager.CreateFormat(formatArray);
        IntPtr formatStereo = IntPtr.Zero;
        byte[] dataStereo = null;
        AudioCompressionManager.MergeStereo(formatMono, leftData, rightData, ref formatStereo, ref dataStereo);
        PlayerEx plex = new PlayerEx();
        plex.OpenPlayer(formatStereo);
        plex.AddData(dataStereo);
        plex.StartPlay();
听风吹 2024-08-27 11:24:04

我在 codeplex 上有一个用于播放波形数据的库。

http://WinMM.codeplex.com

我有一个项目,它在声音加载和播放方面做了很多工作上面的库:

http://files.otac0n.com/ShadySound.zip
(此链接可能需要一段时间才能开始工作,我刚刚创建了该子域。)

它包含一些项目,因为所有内容都是通过插件基础结构加载的,但它确实显示了如何加载 WAV 文件。

不幸的是,对于像这样的 QA 网站来说,您提出的问题有点太大了,但我很乐意与您离线讨论这个问题。

I have a library up on codeplex for playing wave-form data.

http://WinMM.codeplex.com

I have a project that does a lot with sound loading and playing based on the above library:

http://files.otac0n.com/ShadySound.zip
(This link may take a while to start working, i just created that subdomain.)

It has a few projects in it, because everything is loaded through a plug-in infrastructure, but it does show how to load a WAV file.

Unfortunately, the question you have is a little bit too "big" for a QA site like this, but I'd be happy to talk with you offline about this.

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