#region --- OpenTK.OpenAL License --- /* AlutFunctions.cs * C header: \freealut-1.1.0-src\include\AL\Alut.h * Spec: http://www.openal.org/openal_webstf/specs/alut.html * Copyright (c) 2008 Christoph Brandtner and Stefanos Apostolopoulos * See license.txt for license details * http://www.OpenTK.net */ #endregion using System; using System.Runtime.InteropServices; using System.Security; namespace OpenTK.OpenAL { /// Alut, FreeAlut = Free Audio Library Utilities public static class Alut { #region Constants private const string Lib = "alut.dll"; private const CallingConvention Style = CallingConvention.Cdecl; #endregion Constants #region Init/Exit /// Alut.Init initializes the ALUT internals and creates an OpenAL context on the default device and makes it the current OpenAL context. If you want something more complex than that (e.g. running on a non-default device or opening multiple contexts on multiple devices), you can use alutInitWithoutContext instead. alutInit examines the commandline arguments passed to it and remove those it recognizes. It is acceptable to pass two NULL pointers in settings where no useful information can be obtained from argc and argv. /// Application Main Parameters. Can be IntPtr.Zero. /// Application Main Parameters. Can be IntPtr.Zero. /// Success. [DllImport(Alut.Lib, EntryPoint = "alutInit", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern bool Init([In] IntPtr argcp, [In] IntPtr argv); // ALUT_API ALboolean ALUT_APIENTRY alutInit (int *argcp, char **argv); /// Parameterless function for convenience. Internally passes IntPtr.Zero as parameters. /// Success. public static bool Init() // overload for convenience { return Init(IntPtr.Zero, IntPtr.Zero); } /// Alut.InitWithoutContext initializes the ALUT internals. It does not create any OpenAL context or device, so this has to be done via the usual ALC calls. alutInitWithoutContext examines the commandline arguments passed to it and remove those it recognizes. It is acceptable to pass two NULL pointers in settings where no useful information can be obtained from argc and argv. /// Application Main Parameters /// Application Main Parameters /// Success. [DllImport(Alut.Lib, EntryPoint = "alutInitWithoutContext", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern bool InitWithoutContext([In] IntPtr argcp, [In] IntPtr argv); // ALUT_API ALboolean ALUT_APIENTRY alutInitWithoutContext (int *argcp, char **argv); /// Alut.InitWithoutContext initializes the ALUT internals. It does not create any OpenAL context or device, so this has to be done via the usual ALC calls. alutInitWithoutContext examines the commandline arguments passed to it and remove those it recognizes. It is acceptable to pass two NULL pointers in settings where no useful information can be obtained from argc and argv. /// Success. public static bool InitWithoutContext() // overload for convenience { return InitWithoutContext(IntPtr.Zero, IntPtr.Zero); } /// When the application has finished playing audio, it should shut down ALUT using Alut.Exit. This closes any OpenAL device/context that ALUT may have created in alutInit (but not any that the application created using ALC). After calling alutExit, you may subsequently call alutInit or alutInitWithoutContext again. Note that under well-behaved operating systems, it should be acceptable to simply exit from your program without bothering to call alutExit, relying on the OS to clean up after you. However, it is dangerous to rely on this behavior if portable operation is expected. /// Success. [DllImport(Alut.Lib, EntryPoint = "alutExit", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern bool Exit(); // ALUT_API ALboolean ALUT_APIENTRY alutExit (void); #endregion Init/Exit #region Error Checking /// Any ALUT routine that fails will return AL_FALSE / AL_NONE / NULL and set the global error state. If a subsequent error occurs while there is still an error recorded internally, the second error will simply be ignored. Calling alutGetError will reset the error code to ALUT_ERROR_NO_ERROR. Note that the error state is not cleared by other successful ALUT calls. Alut.GetError can be called in any ALUT state and will never fail. /// [DllImport(Alut.Lib, EntryPoint = "alutGetError", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern Enums.AlutError GetError(); // ALUT_API ALenum ALUT_APIENTRY alutGetError (void); [DllImport(Alut.Lib, EntryPoint = "alutGetErrorString", ExactSpelling = true, CallingConvention = Alut.Style, CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity()] private static extern IntPtr GetErrorStringPrivate(Enums.AlutError error); // ALUT_API const char *ALUT_APIENTRY alutGetErrorString (ALenum error); /// Alut.GetErrorString can be used to convert an error code into a human-readable description. The precise text of these descriptions may vary from implementation to implementation and should not be relied upon by the application. /// Retrieve first occured error with Alut.GetError /// A human-readable description of the Error. public static string GetErrorString(Enums.AlutError error) { return Marshal.PtrToStringAnsi(GetErrorStringPrivate(error)); } #endregion Error Checking #region File Loading /// Alut.CreateBufferFromFile tries to guess the sound data format by looking at the filename and/or the file contents and loads the sound data into an OpenAL buffer. /// The file to be loaded /// OpenAL Buffer, 0 on failure. [CLSCompliant(false), DllImport(Alut.Lib, EntryPoint = "alutCreateBufferFromFile", ExactSpelling = true, CallingConvention = Alut.Style, CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity()] public static extern uint CreateBufferFromFile([In] string filename); // ALUT_API ALuint ALUT_APIENTRY alutCreateBufferFromFile (const char *fileName); /// Alut.CreateBufferFromFileImage tries to guess the sound data format by looking at the contents of the memory region given as parameters and loads the sound data into an OpenAL buffer. /// A Pointer to the sound data in memory. /// Size in Bytes of the sound data. /// OpenAL Buffer, 0 on failure. [CLSCompliant(false), DllImport(Alut.Lib, EntryPoint = "alutCreateBufferFromFileImage", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern uint CreateBufferFromFileImage([In] IntPtr data, int length); // ALUT_API ALuint ALUT_APIENTRY alutCreateBufferFromFileImage (const ALvoid *data, ALsizei length); /// Alut.CreateBufferHelloWorld returns a handle to an OpenAL buffer containing the sound of someone saying 'Hello, world!'. /// OpenAL Buffer, 0 on failure. [CLSCompliant(false), DllImport(Alut.Lib, EntryPoint = "alutCreateBufferHelloWorld", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern uint CreateBufferHelloWorld(); //ALUT_API ALuint ALUT_APIENTRY alutCreateBufferHelloWorld (void); /// Alut.CreateBufferWaveform returns a handle to an OpenAL buffer containing a snippet of audio with the specified waveshape at the specified frequency (in Hertz), phase (in degrees: -180 to +180) and duration (in seconds). /// /// Frequency in Hertz [Hz]. /// Phase (in degrees: -180 to +180) /// Duration (in seconds) /// OpenAL Buffer, 0 on failure. [CLSCompliant(false), DllImport(Alut.Lib, EntryPoint = "alutCreateBufferWaveform", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern uint CreateBufferWaveform(Enums.AlutWaveform waveshape, float frequency, float phase, float duration); // ALUT_API ALuint ALUT_APIENTRY alutCreateBufferWaveform (ALenum waveshape, ALfloat frequency, ALfloat phase, ALfloat duration); // Warning: these leak memory if not properly free'd // ALUT_API ALvoid *ALUT_APIENTRY alutLoadMemoryFromFile (const char *fileName, ALenum *format, ALsizei *size, ALfloat *frequency); // ALUT_API ALvoid *ALUT_APIENTRY alutLoadMemoryFromFileImage (const ALvoid *data, ALsizei length, ALenum *format, ALsizei *size, ALfloat *frequency); // ALUT_API ALvoid *ALUT_APIENTRY alutLoadMemoryHelloWorld (ALenum *format, ALsizei *size, ALfloat *frequency); // ALUT_API ALvoid *ALUT_APIENTRY alutLoadMemoryWaveform (ALenum waveshape, ALfloat frequency, ALfloat phase, ALfloat duration, ALenum *format, ALsizei *size, ALfloat *freq); #endregion File Loading #region Misc [DllImport(Alut.Lib, EntryPoint = "alutGetMIMETypes", ExactSpelling = true, CallingConvention = Alut.Style, CharSet = CharSet.Ansi), SuppressUnmanagedCodeSecurity()] private static extern IntPtr GetMIMETypesPrivate(Enums.AlutLoader loader); // ALUT_API const char *ALUT_APIENTRY alutGetMIMETypes (ALenum loader); /// Alut.GetMIMETypes returns a comma-separated list of supported MIME types for the given loader type, e.g. something like "audio/basic,audio/mpeg,audio/x-wav". /// It is possible that Enums.AlutLoader.Memory loaders will be unable to support some file types that Enums.AlutLoader.Buffer loaders can support (although the reverse is never the case). Furthermore, it is possible that for some file types (notably audio/x-wav) the support may be only for a few sub-formats. For example, an implementation may advertise that audio/x-wav is supported when in fact it only supports uncompressed (i.e. PCM) WAV files and not any of the compressed subformats. In this event, the various ALUT loaders may return an error and set ALUT_ERROR_UNSUPPORTED_FILE_SUBTYPE rather than ALUT_ERROR_UNSUPPORTED_FILE_TYPE which would indicate that no files of this type are allowed. /// /// /// A comma-separated list of supported MIME types. public static string GetMIMETypes(Enums.AlutLoader loader) { return Marshal.PtrToStringAnsi(GetMIMETypesPrivate(loader)); } /// Alut.GetMajorVersion returns the major version number of the ALUT in use, which will match the major version number of the corresponding ALUT specification document. Can be compared using Enums.AlutVersions. /// Major Version Number. [DllImport(Alut.Lib, EntryPoint = "alutGetMajorVersion", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern int GetMajorVersion(); // ALUT_API ALint ALUT_APIENTRY alutGetMajorVersion (void); /// Alut.GetMinorVersion returns the minor version number of the ALUT in use, which will match the minor version number of the corresponding ALUT specification document. Can be compared using Enums.AlutVersions. /// Minor Version Number. [DllImport(Alut.Lib, EntryPoint = "alutGetMinorVersion", ExactSpelling = true, CallingConvention = Alut.Style), SuppressUnmanagedCodeSecurity()] public static extern int GetMinorVersion(); // ALUT_API ALint ALUT_APIENTRY alutGetMinorVersion (void); /* /// Alut.Sleep will delay the execution of the current thread for at least the given amount of seconds. It will only return earlier if a signal has been delivered to the thread, but this does not count as an error. Note that sleeping for zero seconds will give other runnable threads a chance to run. /// Having a general utility function like the following in an audio-related toolkit might seem strange at first, but sleeping is a common task in a lot of audio demos and it can't be done portably without cluttering the source code with #ifdefs. /// /// Number of seconds. May not be negative. /// Success. [DllImport( Alut.Lib, EntryPoint = "alutSleep", ExactSpelling = true, CallingConvention = Alut.Style ), SuppressUnmanagedCodeSecurity( )] public static extern bool Sleep( float duration ); // ALUT_API ALboolean ALUT_APIENTRY alutSleep (ALfloat duration); */ #endregion Misc } }