using System; using System.Runtime.InteropServices; using BizHawk.Emulation.Cores.Nintendo.N64; namespace BizHawk.Emulation.Cores.Nintendo.N64.NativeApi { class mupen64plusAudioApi { /// /// Handle to native audio plugin /// private IntPtr AudDll; [DllImport("kernel32.dll")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); /// /// Gets the size of the mupen64plus audio buffer /// /// The size of the mupen64plus audio buffer [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int GetBufferSize(); GetBufferSize dllGetBufferSize; /// /// Gets the audio buffer from mupen64plus, and then clears it /// /// The buffer to fill with samples [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void ReadAudioBuffer(short[] dest); ReadAudioBuffer dllReadAudioBuffer; /// /// Gets the current audio rate from mupen64plus /// /// The current audio rate [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate int GetAudioRate(); GetAudioRate dllGetAudioRate; /// /// Loads native functions and attaches itself to the core /// /// Core with loaded core api public mupen64plusAudioApi(mupen64plusApi core) { AudDll = core.AttachPlugin(mupen64plusApi.m64p_plugin_type.M64PLUGIN_AUDIO, "mupen64plus-audio-bkm.dll"); // Connect dll functions dllGetBufferSize = (GetBufferSize)Marshal.GetDelegateForFunctionPointer(GetProcAddress(AudDll, "GetBufferSize"), typeof(GetBufferSize)); dllReadAudioBuffer = (ReadAudioBuffer)Marshal.GetDelegateForFunctionPointer(GetProcAddress(AudDll, "ReadAudioBuffer"), typeof(ReadAudioBuffer)); dllGetAudioRate = (GetAudioRate)Marshal.GetDelegateForFunctionPointer(GetProcAddress(AudDll, "GetAudioRate"), typeof(GetAudioRate)); } /// /// Returns currently used sampling rate /// /// public uint GetSamplingRate() { return (uint)dllGetAudioRate(); } /// /// Returns size of bytes currently in the audio buffer /// /// public int GetAudioBufferSize() { return dllGetBufferSize(); } /// /// Returns bytes currently in the audiobuffer /// Afterwards audio buffer is cleared /// buffer.Length must be greater than GetAudioBufferSize() /// /// public void GetAudioBuffer(short[] buffer) { dllReadAudioBuffer(buffer); } } }