2008-01-17 15:07:22 +01:00
|
|
|
|
#region --- License ---
|
2008-04-04 21:46:08 +02:00
|
|
|
|
/* Licensed under the MIT/X11 license.
|
|
|
|
|
* Copyright (c) 2006-2008 the OpenTK Team.
|
|
|
|
|
* This notice may not be removed from any source distribution.
|
|
|
|
|
* See license.txt for licensing details.
|
2008-01-17 15:07:22 +01:00
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2008-01-18 14:56:27 +01:00
|
|
|
|
using System.Diagnostics;
|
2008-01-17 15:07:22 +01:00
|
|
|
|
|
2008-04-04 21:46:08 +02:00
|
|
|
|
using OpenTK.Audio;
|
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
namespace OpenTK.Audio
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides methods to create and use an audio context.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class AudioContext : IDisposable
|
|
|
|
|
{
|
|
|
|
|
#region --- Fields ---
|
|
|
|
|
|
|
|
|
|
bool disposed;
|
2008-01-18 15:15:03 +01:00
|
|
|
|
bool is_processing;
|
2008-11-23 21:17:50 +01:00
|
|
|
|
IntPtr device_handle;
|
|
|
|
|
ContextHandle context_handle;
|
2008-04-13 21:46:42 +02:00
|
|
|
|
bool context_exists;
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
string device_name;
|
2008-01-17 15:07:22 +01:00
|
|
|
|
static object audio_context_lock = new object();
|
2008-01-18 14:56:27 +01:00
|
|
|
|
static List<string> available_devices = new List<string>();
|
|
|
|
|
static Dictionary<ContextHandle, AudioContext> available_contexts = new Dictionary<ContextHandle, AudioContext>();
|
2008-04-13 17:50:22 +02:00
|
|
|
|
static bool openal_supported = true;
|
2008-04-13 21:46:42 +02:00
|
|
|
|
static Version version;
|
|
|
|
|
|
|
|
|
|
private enum Version
|
|
|
|
|
{
|
|
|
|
|
OpenAL10,
|
|
|
|
|
OpenAL11
|
|
|
|
|
}
|
2008-01-17 15:07:22 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#region static AudioContext()
|
|
|
|
|
|
|
|
|
|
/// <private />
|
|
|
|
|
/// <static />
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs before the actual class constructor, to load available devices.
|
|
|
|
|
/// </summary>
|
2008-01-31 15:16:26 +01:00
|
|
|
|
static AudioContext()
|
|
|
|
|
{
|
|
|
|
|
LoadAvailableDevices();
|
|
|
|
|
}
|
2008-01-17 15:07:22 +01:00
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public AudioContext()
|
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
/// <summary>Constructs a new AudioContext, using the default audio device.</summary>
|
|
|
|
|
/// <exception cref="NotSupportedException">Occurs when no audio devices are available.</exception>
|
2008-03-16 11:07:55 +01:00
|
|
|
|
public AudioContext()// : this(available_devices.Count > 0 ? available_devices[0] : null, 0, 0, false, 0) { }
|
|
|
|
|
{
|
2008-04-13 17:50:22 +02:00
|
|
|
|
if (!openal_supported)
|
|
|
|
|
throw new DllNotFoundException("openal32.dll");
|
2008-03-16 11:07:55 +01:00
|
|
|
|
CreateContext(null, 0, 0, false, 0);
|
|
|
|
|
}
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
2008-03-16 11:07:55 +01:00
|
|
|
|
#if false
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#region public AudioContext(string device)
|
2008-01-17 15:07:22 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>Constructs a new AudioContext, using the specified audio device.</summary>
|
|
|
|
|
/// <param name="device">The name of the audio device to use.</param>
|
|
|
|
|
/// <remarks>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// Use AudioContext.AvailableDevices to obtain a list of all available audio devices.
|
|
|
|
|
/// devices.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public AudioContext(string device) : this(device, 0, 0, false, 0) { }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public AudioContext(string device, int freq)
|
|
|
|
|
|
|
|
|
|
/// <summary>Constructs a new AudioContext, using the specified audio device and device parameters.</summary>
|
|
|
|
|
/// <param name="device">The name of the audio device to use.</param>
|
|
|
|
|
/// <param name="freq">Frequency for mixing output buffer, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Use AudioContext.AvailableDevices to obtain a list of all available audio devices.
|
2008-01-17 15:07:22 +01:00
|
|
|
|
/// devices.
|
|
|
|
|
/// </remarks>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <see cref="AudioContext(string device, int freq, int refresh, bool sync, int maxEfxSends)"/>
|
|
|
|
|
public AudioContext(string device, int freq) : this(device, freq, 0, false, 0) { }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public AudioContext(string device, int freq, int refresh)
|
|
|
|
|
|
|
|
|
|
/// <summary>Constructs a new AudioContext, using the specified audio device and device parameters.</summary>
|
|
|
|
|
/// <param name="device">The name of the audio device to use.</param>
|
|
|
|
|
/// <param name="freq">Frequency for mixing output buffer, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <param name="refresh">Refresh intervals, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Use AudioContext.AvailableDevices to obtain a list of all available audio devices.
|
|
|
|
|
/// devices.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <see cref="AudioContext(string device, int freq, int refresh, bool sync, int maxEfxSends)"/>
|
|
|
|
|
public AudioContext(string device, int freq, int refresh)
|
|
|
|
|
: this(device, freq, refresh, false, 0) { }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public AudioContext(string device)
|
|
|
|
|
|
|
|
|
|
/// <summary>Constructs a new AudioContext, using the specified audio device and device parameters.</summary>
|
|
|
|
|
/// <param name="device">The name of the audio device to use.</param>
|
|
|
|
|
/// <param name="freq">Frequency for mixing output buffer, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <param name="refresh">Refresh intervals, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <param name="sync">Flag, indicating a synchronous context.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Use AudioContext.AvailableDevices to obtain a list of all available audio devices.
|
|
|
|
|
/// devices.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <see cref="AudioContext(string device, int freq, int refresh, bool sync, int maxEfxSends)"/>
|
|
|
|
|
public AudioContext(string device, int freq, int refresh, bool sync)
|
|
|
|
|
: this(available_devices[0], freq, refresh, sync, 0) { }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-31 15:16:26 +01:00
|
|
|
|
#region public AudioContext(string device, int freq, int refresh, bool sync, int maxSends)
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>Creates the audio context using the specified device and device parameters.</summary>
|
|
|
|
|
/// <param name="device">The device descriptor obtained through AudioContext.AvailableDevices.</param>
|
|
|
|
|
/// <param name="freq">Frequency for mixing output buffer, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <param name="refresh">Refresh intervals, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <param name="sync">Flag, indicating a synchronous context.</param>
|
2008-01-31 15:16:26 +01:00
|
|
|
|
/// <param name="maxSends">Number of auxilliary send slots for the EFX extensions. Can be 0 (use driver default) or higher.</param>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <exception cref="ArgumentNullException">Occurs when the device string is invalid.</exception>
|
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">Occurs when a specified parameter is invalid.</exception>
|
|
|
|
|
/// <exception cref="InvalidAudioDeviceException">
|
|
|
|
|
/// Occurs when the specified device is not available, or is in use by another program.
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <exception cref="InvalidAudioContextException">
|
|
|
|
|
/// Occurs when an audio context could not be created with the specified parameters.
|
|
|
|
|
/// </exception>
|
2008-01-31 15:16:26 +01:00
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
|
/// Occurs when an AudioContext already exists.</exception>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <remarks>
|
2008-01-31 15:16:26 +01:00
|
|
|
|
/// <para>For maximum compatibility, you are strongly recommended to use the default constructor.</para>
|
|
|
|
|
/// <para>Multiple AudioContexts are not supported at this point.</para>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <para>
|
|
|
|
|
/// The number of auxilliary EFX sends depends on the audio hardware and drivers. Most Realtek devices, as well
|
|
|
|
|
/// as the Creative SB Live!, support 1 auxilliary send. Creative's Audigy and X-Fi series support 4 sends.
|
2008-01-31 15:16:26 +01:00
|
|
|
|
/// Values higher than supported will be clamped by the driver.
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
2008-01-31 15:16:26 +01:00
|
|
|
|
public AudioContext(string device, int freq, int refresh, bool sync, int maxSends)
|
2008-01-17 15:07:22 +01:00
|
|
|
|
{
|
2008-01-31 15:16:26 +01:00
|
|
|
|
CreateContext(device, freq, refresh, sync, maxSends);
|
2008-01-17 15:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2008-03-16 11:07:55 +01:00
|
|
|
|
#endif
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-03-16 11:07:55 +01:00
|
|
|
|
#region --- Private Methods ---
|
2008-01-17 15:07:22 +01:00
|
|
|
|
|
|
|
|
|
#region static void LoadAvailableDevices()
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <private />
|
|
|
|
|
/// <static />
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads all available audio devices into the available_devices array.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Only called by the static AudioContext constructor.
|
|
|
|
|
/// </remarks>
|
2008-01-17 15:07:22 +01:00
|
|
|
|
static void LoadAvailableDevices()
|
|
|
|
|
{
|
|
|
|
|
lock (audio_context_lock)
|
|
|
|
|
{
|
2008-01-18 14:56:27 +01:00
|
|
|
|
if (available_devices.Count == 0)
|
2008-01-17 15:07:22 +01:00
|
|
|
|
{
|
2008-03-16 11:07:55 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine("Enumerating audio devices.");
|
|
|
|
|
Debug.Indent();
|
|
|
|
|
|
|
|
|
|
if (Alc.IsExtensionPresent(IntPtr.Zero, "ALC_ENUMERATION_EXT"))
|
2008-04-13 21:46:42 +02:00
|
|
|
|
{
|
|
|
|
|
version = Version.OpenAL11;
|
2008-03-16 11:07:55 +01:00
|
|
|
|
available_devices.AddRange(Alc.GetString(IntPtr.Zero, AlcGetStringList.DeviceSpecifier));
|
2008-04-13 21:46:42 +02:00
|
|
|
|
}
|
2008-03-16 11:07:55 +01:00
|
|
|
|
else
|
2008-04-13 21:46:42 +02:00
|
|
|
|
{
|
|
|
|
|
version = Version.OpenAL10;
|
2008-03-16 11:07:55 +01:00
|
|
|
|
Debug.Print("Device enumeration extension not available. Failed to enumerate devices.");
|
2008-04-13 21:46:42 +02:00
|
|
|
|
}
|
2008-03-16 11:07:55 +01:00
|
|
|
|
|
|
|
|
|
foreach (string s in available_devices)
|
|
|
|
|
Debug.WriteLine(s);
|
|
|
|
|
}
|
2008-04-13 17:50:22 +02:00
|
|
|
|
catch (DllNotFoundException e)
|
|
|
|
|
{
|
|
|
|
|
Debug.WriteLine(e.ToString());
|
|
|
|
|
openal_supported = false;
|
|
|
|
|
}
|
2008-03-16 11:07:55 +01:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Debug.Unindent();
|
|
|
|
|
}
|
2008-01-17 15:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region void CreateContext(string device)
|
|
|
|
|
|
|
|
|
|
/// <private />
|
|
|
|
|
/// <summary>Creates the audio context using the specified device.</summary>
|
2008-01-20 21:56:38 +01:00
|
|
|
|
/// <param name="device">The device descriptor obtained through AudioContext.AvailableDevices, or null for the default device.</param>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <param name="freq">Frequency for mixing output buffer, in units of Hz. Pass 0 for driver default.</param>
|
|
|
|
|
/// <param name="refresh">Refresh intervals, in units of Hz. Pass 0 for driver default.</param>
|
2008-01-17 15:07:22 +01:00
|
|
|
|
/// <param name="sync">Flag, indicating a synchronous context.</param>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <param name="maxEfxSends">Number of auxilliary send slots for the EFX extensions. Can be 0 (use driver default) or higher.</param>
|
|
|
|
|
/// /// <exception cref="ArgumentOutOfRangeException">Occurs when a specified parameter is invalid.</exception>
|
2008-03-16 11:07:55 +01:00
|
|
|
|
/// <exception cref="AudioDeviceException">
|
2008-01-17 15:07:22 +01:00
|
|
|
|
/// Occurs when the specified device is not available, or is in use by another program.
|
|
|
|
|
/// </exception>
|
2008-03-16 11:07:55 +01:00
|
|
|
|
/// <exception cref="AudioDeviceException">
|
2008-01-17 15:07:22 +01:00
|
|
|
|
/// Occurs when an audio context could not be created with the specified parameters.
|
|
|
|
|
/// </exception>
|
2008-01-31 15:16:26 +01:00
|
|
|
|
/// <exception cref="NotSupportedException">
|
|
|
|
|
/// Occurs when an AudioContext already exists.</exception>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <remarks>
|
2008-01-31 15:16:26 +01:00
|
|
|
|
/// <para>For maximum compatibility, you are strongly recommended to use the default constructor.</para>
|
|
|
|
|
/// <para>Multiple AudioContexts are not supported at this point.</para>
|
|
|
|
|
/// <para>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// The number of auxilliary EFX sends depends on the audio hardware and drivers. Most Realtek devices, as well
|
|
|
|
|
/// as the Creative SB Live!, support 1 auxilliary send. Creative's Audigy and X-Fi series support 4 sends.
|
|
|
|
|
/// Values higher than supported will be clamped by the driver.
|
2008-01-31 15:16:26 +01:00
|
|
|
|
/// </para>
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// </remarks>
|
|
|
|
|
void CreateContext(string device, int freq, int refresh, bool sync, int maxEfxSends)
|
2008-01-17 15:07:22 +01:00
|
|
|
|
{
|
2008-04-13 21:46:42 +02:00
|
|
|
|
if (version == Version.OpenAL11 && available_devices.Count == 0) // Version.OpenAL10 does not support device enumeration.
|
|
|
|
|
throw new NotSupportedException("No audio hardware is available.");
|
2008-03-16 11:07:55 +01:00
|
|
|
|
if (context_exists) throw new NotSupportedException("Multiple AudioContexts are not supported.");
|
2008-01-18 14:56:27 +01:00
|
|
|
|
if (freq < 0) throw new ArgumentOutOfRangeException("freq", freq, "Should be greater than zero.");
|
|
|
|
|
if (refresh < 0) throw new ArgumentOutOfRangeException("refresh", refresh, "Should be greater than zero.");
|
|
|
|
|
if (maxEfxSends < 0) throw new ArgumentOutOfRangeException("maxEfxSends", maxEfxSends, "Should be greater than zero.");
|
2008-01-17 15:07:22 +01:00
|
|
|
|
|
2008-03-16 11:07:55 +01:00
|
|
|
|
if (!String.IsNullOrEmpty(device))
|
|
|
|
|
device_handle = Alc.OpenDevice(device);
|
2008-06-22 20:06:32 +02:00
|
|
|
|
if (device_handle == IntPtr.Zero)
|
|
|
|
|
device_handle = Alc.OpenDevice(null);
|
2008-03-16 11:07:55 +01:00
|
|
|
|
if (device_handle == IntPtr.Zero)
|
|
|
|
|
Alc.OpenDevice(Alc.GetString(IntPtr.Zero, AlcGetString.DefaultDeviceSpecifier));
|
|
|
|
|
if (device_handle == IntPtr.Zero && available_devices.Count > 0)
|
|
|
|
|
device_handle = Alc.OpenDevice(available_devices[0]);
|
2008-01-17 15:07:22 +01:00
|
|
|
|
if (device_handle == IntPtr.Zero)
|
2008-04-04 21:46:08 +02:00
|
|
|
|
throw new AudioDeviceException(String.Format("Audio device '{0}' does not exist or is tied up by another application.",
|
|
|
|
|
String.IsNullOrEmpty(device) ? "default" : device));
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
2008-01-20 23:42:49 +01:00
|
|
|
|
CheckForAlcErrors();
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
device_name = device;
|
2008-01-22 22:18:47 +01:00
|
|
|
|
|
2008-01-20 22:00:59 +01:00
|
|
|
|
// Build the attribute list
|
2008-01-17 15:07:22 +01:00
|
|
|
|
List<int> attributes = new List<int>();
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
if (freq != 0)
|
|
|
|
|
{
|
|
|
|
|
attributes.Add((int)AlcContextAttributes.Frequency);
|
|
|
|
|
attributes.Add(freq);
|
|
|
|
|
}
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
if (refresh != 0)
|
|
|
|
|
{
|
|
|
|
|
attributes.Add((int)AlcContextAttributes.Refresh);
|
|
|
|
|
attributes.Add(refresh);
|
|
|
|
|
}
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
attributes.Add((int)AlcContextAttributes.Frequency);
|
|
|
|
|
attributes.Add(sync ? 1 : 0);
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
if (maxEfxSends > 0)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
//if (Alc.IsExtensionPresent(device_handle, "ALC_EXT_EFX"))
|
|
|
|
|
//attributes.Add((int)AlcContextAttributes.MaxAuxilliarySends);
|
|
|
|
|
//attributes.Add(maxEfxSends);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
context_handle = Alc.CreateContext(device_handle, attributes.ToArray());
|
2008-01-22 22:18:47 +01:00
|
|
|
|
|
2008-11-23 21:17:50 +01:00
|
|
|
|
if (context_handle == ContextHandle.Zero)
|
2008-01-17 15:07:22 +01:00
|
|
|
|
{
|
|
|
|
|
Alc.CloseDevice(device_handle);
|
2008-01-18 14:56:27 +01:00
|
|
|
|
throw new AudioContextException("The audio context could not be created with the specified parameters.");
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-20 23:42:49 +01:00
|
|
|
|
CheckForAlcErrors();
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
2008-04-20 19:46:20 +02:00
|
|
|
|
// HACK: OpenAL SI on Linux/ALSA crashes on MakeCurrent. This hack avoids calling MakeCurrent when
|
|
|
|
|
// an old OpenAL version is detect - it may affect outdated OpenAL versions different than OpenAL SI,
|
|
|
|
|
// but it looks like a good compromise for now.
|
|
|
|
|
if (available_devices.Count > 0)
|
|
|
|
|
MakeCurrent();
|
2008-01-18 15:15:03 +01:00
|
|
|
|
|
2008-01-20 23:42:49 +01:00
|
|
|
|
CheckForAlcErrors();
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
//device_name = Alc.GetString(device_handle, AlcGetString.DeviceSpecifier);
|
|
|
|
|
//Debug.Print(device_name);
|
|
|
|
|
lock (audio_context_lock)
|
|
|
|
|
{
|
|
|
|
|
available_contexts.Add(this.context_handle, this);
|
2008-01-31 15:16:26 +01:00
|
|
|
|
context_exists = true;
|
2008-01-17 15:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-20 23:42:49 +01:00
|
|
|
|
#region void CheckForAlcErrors()
|
2008-03-16 11:07:55 +01:00
|
|
|
|
|
2008-01-20 23:42:49 +01:00
|
|
|
|
void CheckForAlcErrors()
|
|
|
|
|
{
|
|
|
|
|
AlcError err = Alc.GetError(device_handle);
|
|
|
|
|
if (err != AlcError.NoError)
|
|
|
|
|
throw new AudioContextException(err.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#region static void MakeCurrent(AudioContext context)
|
|
|
|
|
|
|
|
|
|
/// <private />
|
|
|
|
|
/// <summary>Makes the specified AudioContext current in the calling thread.</summary>
|
|
|
|
|
/// <param name="context">The OpenTK.Audio.AudioContext to make current, or null.</param>
|
|
|
|
|
/// <exception cref="ObjectDisposedException">
|
|
|
|
|
/// Occurs if this function is called after the AudioContext has been disposed.
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <exception cref="AudioContextException">
|
|
|
|
|
/// Occurs when the AudioContext could not be made current.
|
|
|
|
|
/// </exception>
|
|
|
|
|
static void MakeCurrent(AudioContext context)
|
|
|
|
|
{
|
2008-01-20 23:42:49 +01:00
|
|
|
|
lock (audio_context_lock)
|
2008-01-18 14:56:27 +01:00
|
|
|
|
{
|
2008-11-23 21:17:50 +01:00
|
|
|
|
if (!Alc.MakeContextCurrent(context != null ? context.context_handle : ContextHandle.Zero))
|
2008-04-06 03:28:12 +02:00
|
|
|
|
throw new AudioContextException(String.Format("ALC {0} error detected at {1}.",
|
|
|
|
|
Alc.GetError(context != null ? (IntPtr)context.context_handle : IntPtr.Zero).ToString(),
|
|
|
|
|
context != null ? context.ToString() : "null"));
|
2008-01-18 14:56:27 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-01-17 15:07:22 +01:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-02-02 14:11:42 +01:00
|
|
|
|
#region internal bool IsCurrent
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a System.Boolean indicating whether the AudioContext
|
|
|
|
|
/// is current.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Only one AudioContext can be current in the application at any time,
|
|
|
|
|
/// <b>regardless of the number of threads</b>.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
internal bool IsCurrent
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
lock (audio_context_lock)
|
|
|
|
|
{
|
|
|
|
|
if (available_contexts.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return AudioContext.CurrentContext == this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value) AudioContext.MakeCurrent(this);
|
|
|
|
|
else AudioContext.MakeCurrent(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-04-13 21:42:37 +02:00
|
|
|
|
#region IntPtr Device
|
|
|
|
|
|
2008-11-23 21:17:50 +01:00
|
|
|
|
IntPtr Device { get { return device_handle; } }
|
2008-04-13 21:42:37 +02:00
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
2008-04-13 21:42:37 +02:00
|
|
|
|
#endregion
|
2008-01-20 18:55:55 +01:00
|
|
|
|
|
2008-02-02 14:11:42 +01:00
|
|
|
|
#region --- Public Members ---
|
|
|
|
|
|
2008-04-06 03:28:12 +02:00
|
|
|
|
#region internal void MakeCurrent()
|
|
|
|
|
|
|
|
|
|
/// <summary>Makes the AudioContext current in the calling thread.</summary>
|
|
|
|
|
/// <exception cref="ObjectDisposedException">
|
|
|
|
|
/// Occurs if this function is called after the AudioContext has been disposed.
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <exception cref="AudioContextException">
|
|
|
|
|
/// Occurs when the AudioContext could not be made current.
|
|
|
|
|
/// </exception>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Only one AudioContext can be current in the application at any time,
|
|
|
|
|
/// <b>regardless of the number of threads</b>.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void MakeCurrent()
|
|
|
|
|
{
|
|
|
|
|
AudioContext.MakeCurrent(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-18 15:15:03 +01:00
|
|
|
|
#region public bool IsProcessing
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a System.Boolean indicating whether the AudioContext is
|
|
|
|
|
/// currently processing audio events.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <seealso cref="Process"/>
|
|
|
|
|
/// <seealso cref="Suspend"/>
|
|
|
|
|
public bool IsProcessing
|
|
|
|
|
{
|
|
|
|
|
get { return is_processing; }
|
|
|
|
|
private set { is_processing = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#region public void Process
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes queued audio events.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// If AudioContext.IsSynchronized is true, this function will resume
|
|
|
|
|
/// the internal audio processing thread. If AudioContext.IsSynchronized is false,
|
|
|
|
|
/// you will need to call this function multiple times per second to process
|
|
|
|
|
/// audio events.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// In some implementations this function may have no effect.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <exception cref="ObjectDisposedException">Occurs when this function is called after the AudioContext had been disposed.</exception>
|
|
|
|
|
/// <seealso cref="Suspend"/>
|
|
|
|
|
/// <seealso cref="IsSuspended"/>
|
|
|
|
|
/// <seealso cref="IsSynchronized"/>
|
|
|
|
|
public void Process()
|
|
|
|
|
{
|
|
|
|
|
if (disposed) throw new ObjectDisposedException(this.ToString());
|
|
|
|
|
Alc.ProcessContext(this.context_handle);
|
2008-01-18 15:15:03 +01:00
|
|
|
|
IsProcessing = true;
|
2008-01-18 14:56:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public void Suspend
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Suspends processing of audio events.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// To avoid audio artifacts when calling this function, set audio gain to zero before
|
|
|
|
|
/// suspending an AudioContext.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// In some implementations, it can be faster to suspend processing before changing
|
|
|
|
|
/// AudioContext state.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// In some implementations this function may have no effect.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <exception cref="ObjectDisposedException">Occurs when this function is called after the AudioContext had been disposed.</exception>
|
|
|
|
|
/// <seealso cref="Process"/>
|
|
|
|
|
/// <seealso cref="IsSuspended"/>
|
|
|
|
|
/// <seealso cref="IsSynchronized"/>
|
|
|
|
|
public void Suspend()
|
|
|
|
|
{
|
|
|
|
|
if (disposed) throw new ObjectDisposedException(this.ToString());
|
|
|
|
|
Alc.SuspendContext(this.context_handle);
|
2008-01-18 15:15:03 +01:00
|
|
|
|
IsProcessing = false;
|
2008-01-18 14:56:27 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-04-13 21:42:37 +02:00
|
|
|
|
#region public bool SupportsExtension(string extension)
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks whether the specified OpenAL extension is supported.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="extension">The name of the extension to check (e.g. "ALC_EXT_EFX").</param>
|
|
|
|
|
/// <returns>true if the extension is supported; false otherwise.</returns>
|
|
|
|
|
public bool SupportsExtension(string extension)
|
|
|
|
|
{
|
|
|
|
|
if (disposed) throw new ObjectDisposedException(this.GetType().FullName);
|
|
|
|
|
return Alc.IsExtensionPresent(this.Device, extension);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
#region public static AudioContext CurrentContext
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the OpenTK.Audio.AudioContext which is current in the application.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Only one AudioContext can be current in the application at any time,
|
|
|
|
|
/// <b>regardless of the number of threads</b>.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public static AudioContext CurrentContext
|
2008-01-17 15:07:22 +01:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2008-01-18 14:56:27 +01:00
|
|
|
|
lock (audio_context_lock)
|
|
|
|
|
{
|
|
|
|
|
if (available_contexts.Count == 0)
|
|
|
|
|
return null;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
AudioContext context;
|
|
|
|
|
AudioContext.available_contexts.TryGetValue(
|
|
|
|
|
(ContextHandle)Alc.GetCurrentContext(),
|
|
|
|
|
out context);
|
|
|
|
|
return context;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-01-17 15:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-31 15:16:26 +01:00
|
|
|
|
#region public static string[] AvailableDevices
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a System.String array containing all available audio devices.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>This property allocates memory.</remarks>
|
|
|
|
|
public static string[] AvailableDevices
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (available_devices.Count == 0)
|
|
|
|
|
LoadAvailableDevices();
|
|
|
|
|
return available_devices.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
#region --- IDisposable Members ---
|
|
|
|
|
|
2008-01-18 14:56:27 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes of the AudioContext, cleaning up all resources consumed by it.
|
|
|
|
|
/// </summary>
|
2008-01-17 15:07:22 +01:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Dispose(bool manual)
|
|
|
|
|
{
|
|
|
|
|
if (!disposed)
|
|
|
|
|
{
|
2008-01-18 14:56:27 +01:00
|
|
|
|
if (this.IsCurrent)
|
|
|
|
|
this.IsCurrent = false;
|
|
|
|
|
|
2008-11-23 21:17:50 +01:00
|
|
|
|
if (context_handle != ContextHandle.Zero)
|
2008-03-16 11:07:55 +01:00
|
|
|
|
{
|
|
|
|
|
available_contexts.Remove(context_handle);
|
2008-01-18 14:56:27 +01:00
|
|
|
|
Alc.DestroyContext(context_handle);
|
2008-03-16 11:07:55 +01:00
|
|
|
|
}
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
|
|
|
|
if (device_handle != IntPtr.Zero)
|
|
|
|
|
Alc.CloseDevice(device_handle);
|
|
|
|
|
|
2008-01-17 15:07:22 +01:00
|
|
|
|
if (manual)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~AudioContext()
|
|
|
|
|
{
|
|
|
|
|
this.Dispose(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2008-01-18 14:56:27 +01:00
|
|
|
|
|
|
|
|
|
#region --- Overrides ---
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return base.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
return base.Equals(obj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return String.Format("{0} (handle: {1}, device: {2})",
|
|
|
|
|
this.device_name, this.context_handle, this.device_handle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2008-01-17 15:07:22 +01:00
|
|
|
|
}
|
|
|
|
|
}
|