如何在 .Net/Mono 中解码 wav、mp3 和/或 ogg?

发布于 2024-07-17 01:46:40 字数 528 浏览 7 评论 0原文

我正在寻找一种跨平台(Windows、MacOSX 和 Linux 上的 .Net 和 Mono)方法来解码 wav、mp3 或 ogg 文件,以便我可以根据需要通过 DirectSound 或 OpenAL 播放解码的流。

一个可以解码 mp3 或 ogg 的解决方案就足够了——不需要解码两者。 如果它(或其他解决方案)也可以解码 wav,那将是理想的选择,但并不是绝对必要的,因为我可能会将我的 wav 文件转换为 mp3 或 ogg。

只能解码 wav 文件的解决方案只是部分解决方案,但我会采取我能得到的。 ;-)

这是我已经看过的内容:

  • DirectSound 和 Alut 都支持 WAV 解码,但 Alut 已被弃用。
  • ffmpeg 几乎可以解码任何内容,但它似乎没有跨平台的通用稳定版本?
  • 我还没有尝试过 csvorbis,但它已经很旧了,我不确定它当前的状态是什么。

非常欢迎您尝试说服我上述解决方案之一实际上是最佳选择。

谢谢。

I am looking for a cross-platform (.Net and Mono on Windows, MacOSX, and Linux) way to decode wav, mp3, or ogg files such that I can then play the decoded streams through DirectSound or OpenAL as desired.

A solution that can decode either mp3 or ogg would be sufficient -- decoding both is not necessary. If it (or another solution) can decode wav as well, that would be ideal, but isn't strictly necessary since I could potentially convert my wav files to mp3 or ogg.

A solution that can only decode wav files is only a partial solution, but I'll take what I can get. ;-)

Here's what I've already looked at:

  • DirectSound and Alut both support WAV decoding, but Alut has been deprecated.
  • ffmpeg will decode just about anything, but it doesn't seem to have a common stable release across platforms?
  • I have not tried csvorbis yet, but it's very old and I'm not sure what its current status is.

You are more than welcome to try to pursuade me that one of these above solutions is actually the best option.

Thanks.

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

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

发布评论

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

评论(8

我偏爱纯白色 2024-07-24 01:46:41

为了部分回答我自己的问题,事实证明编写自己的 WAV 加载器非常容易。 我使用这篇文章描述了 wav 文件格式:

http://www.sonicspot.com/guide /wavefiles.html

To partially answer my own question, it turns out that it's pretty easy to write your own WAV loader. I used this article that describes the wav file format:

http://www.sonicspot.com/guide/wavefiles.html

旧街凉风 2024-07-24 01:46:41

as always, fmod is your answer...
please feel free to get solution to all your problems on http://www.fmod.org/index.php/download#FMODExProgrammersAPI

初见 2024-07-24 01:46:41

wav 文件有结构
44字节起始音频信号。 如果读取 44 到结束文件音频数据(wav 文件),您可以在 .NET(openl)中播放您的音乐
https://dpaste.com/8VNKMAUU5

public static void playwav()
    {
        Console.WriteLine("Hello World!");

        OpenTK.Audio.OpenAL.ALDevice device = OpenTK.Audio.OpenAL.ALDevice.Null;
        OpenTK.Audio.OpenAL.ALContext ctx = OpenTK.Audio.OpenAL.ALContext.Null;

        string defname = OpenTK.Audio.OpenAL.ALC.GetString(device, OpenTK.Audio.OpenAL.AlcGetString.DefaultDeviceSpecifier);
        Console.WriteLine(defname);
        device = OpenTK.Audio.OpenAL.ALC.OpenDevice(defname);

        if (!OpenTK.Audio.OpenAL.ALC.IsExtensionPresent(device, "ALC_EXT_EFX"))
            return;
        int[] atrributes = { 0, 0, 0, 0 };
        int[] iSends = { 0 };
        atrributes[0] = ((int)OpenTK.Audio.OpenAL.AlcContextAttributes.EfxMaxAuxiliarySends);
        atrributes[1] = 4;

        Console.WriteLine("EFX Extension found!\n");

        ctx = OpenTK.Audio.OpenAL.ALC.CreateContext(device, atrributes);

        OpenTK.Audio.OpenAL.ALC.MakeContextCurrent(ctx);
        OpenTK.Audio.OpenAL.ALC.ProcessContext(ctx);

        OpenTK.Audio.OpenAL.ALC.GetInteger(device, OpenTK.Audio.OpenAL.AlcGetInteger.EfxMaxAuxiliarySends, 1, iSends);


        //OpenTK.Audio.OpenAL.AL.GetProcAddress("LPALGENEFFECTS"); 

        int buffer;

        string str = @"C:\WorkC#\ConsoleApp3\ConsoleApp3\20211222-204048_.wav";
        //Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle("20211222-204048_.wav",true);
        FileStream fileStream = File.OpenRead(str);

        byte[] bufdata = new byte[fileStream.Length];
        //bufdata[0] = 0;
        byte[] RIFF = new byte[4];
        byte[] sizeRid = new byte[4];
        byte[] WAVE = new byte[4];
        byte[] FMT = new byte[4];
        fileStream.Read(RIFF, 0, 4);
        fileStream.Read(sizeRid, 0, 4);
        fileStream.Read(WAVE, 0, 4);
        fileStream.Read(FMT, 0, 4);

        fileStream.Read(bufdata, 44, (int)(fileStream.Length - 45));

        OpenTK.Audio.OpenAL.AL.GenBuffer(out buffer);

        UInt16 value = BitConverter.ToUInt16(RIFF, 0);

        Console.WriteLine(((char)value));
        Console.WriteLine(sizeRid.Length);
        Console.WriteLine(WAVE.Length);
        OpenTK.Audio.OpenAL.AL.BufferData(buffer, OpenTK.Audio.OpenAL.ALFormat.Stereo16, bufdata, 44100);

        Console.WriteLine((bufdata));

        int src;
        OpenTK.Audio.OpenAL.AL.GenSource(out src);
        OpenTK.Audio.OpenAL.AL.Source(src, OpenTK.Audio.OpenAL.ALSourcei.Buffer, buffer);
        OpenTK.Audio.OpenAL.AL.SourcePlay(src);
        int state = 4114;

        while (state == (int)OpenTK.Audio.OpenAL.ALSourceState.Playing)
        {
            OpenTK.Audio.OpenAL.AL.GetSource(src, OpenTK.Audio.OpenAL.ALGetSourcei.SourceState, out state);
            Console.Write(state + "\r");
            //int state = (int)OpenTK.Audio.OpenAL.ALSourceState.Playing;
            //OpenTK.Audio.OpenAL.AL.SourcePlay(src);
        }


        OpenTK.Audio.OpenAL.ALC.DestroyContext(ctx);
        OpenTK.Audio.OpenAL.ALC.CloseDevice(device);



        //OpenTK.Audio.OpenAL.ALC.GetContextsDevice();
    }

wav file have structure
44 byte start audio signal. if read 44-to end file audio data(wav file)your have playing in .NET(opeanl) your music
https://dpaste.com/8VNKMAUU5

public static void playwav()
    {
        Console.WriteLine("Hello World!");

        OpenTK.Audio.OpenAL.ALDevice device = OpenTK.Audio.OpenAL.ALDevice.Null;
        OpenTK.Audio.OpenAL.ALContext ctx = OpenTK.Audio.OpenAL.ALContext.Null;

        string defname = OpenTK.Audio.OpenAL.ALC.GetString(device, OpenTK.Audio.OpenAL.AlcGetString.DefaultDeviceSpecifier);
        Console.WriteLine(defname);
        device = OpenTK.Audio.OpenAL.ALC.OpenDevice(defname);

        if (!OpenTK.Audio.OpenAL.ALC.IsExtensionPresent(device, "ALC_EXT_EFX"))
            return;
        int[] atrributes = { 0, 0, 0, 0 };
        int[] iSends = { 0 };
        atrributes[0] = ((int)OpenTK.Audio.OpenAL.AlcContextAttributes.EfxMaxAuxiliarySends);
        atrributes[1] = 4;

        Console.WriteLine("EFX Extension found!\n");

        ctx = OpenTK.Audio.OpenAL.ALC.CreateContext(device, atrributes);

        OpenTK.Audio.OpenAL.ALC.MakeContextCurrent(ctx);
        OpenTK.Audio.OpenAL.ALC.ProcessContext(ctx);

        OpenTK.Audio.OpenAL.ALC.GetInteger(device, OpenTK.Audio.OpenAL.AlcGetInteger.EfxMaxAuxiliarySends, 1, iSends);


        //OpenTK.Audio.OpenAL.AL.GetProcAddress("LPALGENEFFECTS"); 

        int buffer;

        string str = @"C:\WorkC#\ConsoleApp3\ConsoleApp3\20211222-204048_.wav";
        //Microsoft.Win32.SafeHandles.SafeFileHandle safeFileHandle = new Microsoft.Win32.SafeHandles.SafeFileHandle("20211222-204048_.wav",true);
        FileStream fileStream = File.OpenRead(str);

        byte[] bufdata = new byte[fileStream.Length];
        //bufdata[0] = 0;
        byte[] RIFF = new byte[4];
        byte[] sizeRid = new byte[4];
        byte[] WAVE = new byte[4];
        byte[] FMT = new byte[4];
        fileStream.Read(RIFF, 0, 4);
        fileStream.Read(sizeRid, 0, 4);
        fileStream.Read(WAVE, 0, 4);
        fileStream.Read(FMT, 0, 4);

        fileStream.Read(bufdata, 44, (int)(fileStream.Length - 45));

        OpenTK.Audio.OpenAL.AL.GenBuffer(out buffer);

        UInt16 value = BitConverter.ToUInt16(RIFF, 0);

        Console.WriteLine(((char)value));
        Console.WriteLine(sizeRid.Length);
        Console.WriteLine(WAVE.Length);
        OpenTK.Audio.OpenAL.AL.BufferData(buffer, OpenTK.Audio.OpenAL.ALFormat.Stereo16, bufdata, 44100);

        Console.WriteLine((bufdata));

        int src;
        OpenTK.Audio.OpenAL.AL.GenSource(out src);
        OpenTK.Audio.OpenAL.AL.Source(src, OpenTK.Audio.OpenAL.ALSourcei.Buffer, buffer);
        OpenTK.Audio.OpenAL.AL.SourcePlay(src);
        int state = 4114;

        while (state == (int)OpenTK.Audio.OpenAL.ALSourceState.Playing)
        {
            OpenTK.Audio.OpenAL.AL.GetSource(src, OpenTK.Audio.OpenAL.ALGetSourcei.SourceState, out state);
            Console.Write(state + "\r");
            //int state = (int)OpenTK.Audio.OpenAL.ALSourceState.Playing;
            //OpenTK.Audio.OpenAL.AL.SourcePlay(src);
        }


        OpenTK.Audio.OpenAL.ALC.DestroyContext(ctx);
        OpenTK.Audio.OpenAL.ALC.CloseDevice(device);



        //OpenTK.Audio.OpenAL.ALC.GetContextsDevice();
    }
自找没趣 2024-07-24 01:46:41

https://sites.google.com/site/cobnut3d/

SlimDX.DirectSound - 是一个C# 代码示例。

https://sites.google.com/site/cobnut3d/

SlimDX.DirectSound - is an example of code to C #.

永不分离 2024-07-24 01:46:40

您还没有提到您正在寻找的许可类型。 我使用 FMOD 取得了出色的效果,该软件可免费用于非商业用途项目。 它可以播放多种格式,包括 ogg 和 mp3,并具有广泛的 API 来控制如何播放它们。 它是跨平台的,正如您所需要的。

FMOD很大程度上是针对游戏的; 如果它已经具备出色的 3D 功能,我不会感到惊讶(尽管我不确定)。

正如您所见,我是它的忠实粉丝,这主要是因为第一次获得一个空白的 C# 项目来播放 OGG 文件非常轻松。

You haven't mentioned the sort of licensing you're looking for. I've had excellent results with FMOD, which is available for free for non-commercial projects. It plays a diverse variety of formats, including ogg and mp3, and has an extensive API for controlling how to play them. It's cross-platform, like you require.

FMOD targets games to a large extent; I wouldn't be surprised if it already comes with excellent 3D features (though I don't know for sure).

As you can tell I'm quite a fan, which is mostly because it was surprisingly painless to get a blank C# project to play an OGG file for the first time.

怀里藏娇 2024-07-24 01:46:40

对于 WAV 文件的简单支持,您可以在 mcs/class/System/System.Media/AudioData.cs (http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/System/System.Media/)。

要解码 ogg 音频文件,您可以查看 mono 的 sn 服务器中的 csvorbis 模块:
http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/< /a>

For simple support for WAV files you can look at mono's implementation in mcs/class/System/System.Media/AudioData.cs (http://anonsvn.mono-project.com/viewvc/trunk/mcs/class/System/System.Media/).

For decoding ogg audio files you can look at the csvorbis module in mono's sn server:
http://anonsvn.mono-project.com/viewvc/trunk/csvorbis/

打小就很酷 2024-07-24 01:46:40

您应该能够直接使用 System.Media.SoundPlayer 播放 .wav 文件。 不确定这是否对您的用例有帮助。

You should be able to play back .wav files directly with System.Media.SoundPlayer. Not sure if that helps your use case.

素食主义者 2024-07-24 01:46:40

我使用 ffmpeg 工作,到目前为止我很喜欢它。 是的,各个版本之间几乎没有问题,但它是所有版本中最有效的。

I worked with ffmpeg and so far I like it. Yea there are few issues across releases, but it is the most efficient of all.

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