0

我有一小段代码可以通过 Tao.Sdl 播放音调。它在 Linux 和 Mac 上运行良好(使用 Mono),但它在 Windows7、64 位的 Mono 或 .NET 上崩溃。这是代码:

using System;
using System.Runtime.InteropServices;
using Tao.Sdl;

namespace ConsoleApplication1
{
    class Program
    {
        delegate void AudioSpecCallbackDelegate(IntPtr userData, IntPtr stream, int length);
        static Sdl.SDL_AudioSpec desired;
        static IntPtr specPtr;
        static IntPtr obtained;

        static void Main(string[] args)
        {
            Sdl.SDL_Init(Sdl.SDL_INIT_AUDIO);
            desired = new Sdl.SDL_AudioSpec();
            desired.freq = 22050; 
            desired.format = (short)Sdl.AUDIO_S16LSB;
            desired.channels = 1;
            desired.samples = (short)1000; //(short)2205; 
            desired.callback = Marshal.GetFunctionPointerForDelegate((AudioSpecCallbackDelegate)DoCallback);
            desired.userdata = null;
            specPtr = Marshal.AllocHGlobal(Marshal.SizeOf(desired));
            Marshal.StructureToPtr(desired, specPtr, false);
            obtained = IntPtr.Zero;
            if (Sdl.SDL_OpenAudio((IntPtr)specPtr, (IntPtr)obtained) < 0)
            {
                Console.WriteLine("Error opening sdl_audio (1)");
                Console.WriteLine(Sdl.SDL_GetError());
            }
            Sdl.SDL_PauseAudio(0);
            Sdl.SDL_Delay(1000);
            Sdl.SDL_PauseAudio(1);
        }

        public static void DoCallback(IntPtr userData, IntPtr stream, int len)
        {
            Console.WriteLine("{0}, ", len);
            byte[] buffer = new byte[len];
            for (int i = 0; i < len; i++)
                buffer[i] = 0;
            Marshal.Copy(buffer, 0, stream, len);
        }
    }
}

它第二次尝试调用回调时在 Windows 上崩溃。你能看出我做错了什么吗?也许我没有正确的频率值,或者格式在 Windows 上很重要?

或者,我不知道如何调试低级代码......它只是在 Visual Studio 或 MonoDevelop 中崩溃。或者,如果您有关于使用不同系统重新执行此代码的建议。目标:能够在 Mac、Windows 和 Linux 上用 C# 处理要通过声音系统播放的字节。

4

1 回答 1

1

我最终在 SdlDotNet 中重写了代码,尽管它还有另一个需要存在窗口的错误。我只是创建了一个小窗口,并在打开流后将其关闭。以下适用于 Linux 和 Windows7。需要在 Mac 上测试。我从来没有发现上面的错误是什么......也许它与需要一个窗口有关。下面还有产生音调的实际代码。我在 SDL 中看到了一些非常错误的示例来执行此操作。

public class AudioManager : IDisposable {
    const int playbackFreq = 44100;
    const int samples = 2048;
    const double pi2 = 360 * Math.PI / 180.0;
    private bool disposed = false;
    private bool initialized = false;
    SdlDotNet.Audio.AudioStream stream;
    byte[] buffer8;

    double time = 0;
    double volume;
    double frequency1 = -1;
    double frequency2 = -1;
    SdlDotNet.Audio.AudioCallback audioCallback = new SdlDotNet.Audio.AudioCallback(Callback)

    public AudioManager()
    {
        stream = new SdlDotNet.Audio.AudioStream(playbackFreq, 
                             SdlDotNet.Audio.AudioFormat.Unsigned8, 
                             SdlDotNet.Audio.SoundChannel.Mono, 
                             samples, 
                             audioCallback, 
                             null);
        buffer8 = new byte[samples];
        volume = 1.0;

        // BUG: OpenAudio (or lower) apparently requires a visible screen for some reason:
        SdlDotNet.Graphics.Video.SetVideoMode(1, 1);
        SdlDotNet.Audio.Mixer.OpenAudio(stream);
        // BUG: close (or hide) it
        SdlDotNet.Graphics.Video.Close();
    }

    public void beep(double duration, double freq) {
        frequency1 = freq;
        frequency2 = -1;
        Tao.Sdl.Sdl.SDL_PauseAudio(0);
        Tao.Sdl.Sdl.SDL_Delay((int)(duration * 1000));
        Tao.Sdl.Sdl.SDL_PauseAudio(1);
    }

    void Callback(IntPtr userData, IntPtr stream, int len)
    {
        double slice = 1.0 / playbackFreq * pi2; 
        for (int buf_pos = 0; buf_pos < len; buf_pos++ )
        {
            buffer8[buf_pos] = (byte)(127 + Math.Cos(time) * volume * 127);
            time += frequency1 * slice;
            if (time > pi2)
                time -= pi2;
        }
        Marshal.Copy(buffer8, 0, stream, len);
    }
于 2012-02-19T20:03:36.397 回答