static AlDevice() { var ptr = ALC10.alcGetString(IntPtr.Zero, ALC10.ALC_DEFAULT_DEVICE_SPECIFIER); if (ptr != IntPtr.Zero) { DefaultDeviceName = Marshal.PtrToStringAnsi(ptr); } }
/// <summary> /// Get the available devices. /// </summary> public static IEnumerable <string> GetDevices() { var deviceList = ALC11.alcIsExtensionPresent(IntPtr.Zero, "ALC_ENUMERATE_ALL_EXT") ? ALC10.alcGetString(IntPtr.Zero, ALC11.ALC_ALL_DEVICES_SPECIFIER) : ALC10.alcGetString(IntPtr.Zero, ALC10.ALC_DEVICE_SPECIFIER); var curString = Marshal.PtrToStringAnsi(deviceList); while (!string.IsNullOrEmpty(curString)) { yield return(curString); deviceList += curString.Length + 1; curString = Marshal.PtrToStringAnsi(deviceList); } }
public ReadOnlyCollection <Microphone> GetCaptureDevices() { IntPtr deviceList = ALC10.alcGetString(IntPtr.Zero, ALC11.ALC_CAPTURE_DEVICE_SPECIFIER); List <Microphone> microphones = new List <Microphone>(); string curString = Marshal.PtrToStringAnsi(deviceList); while (!String.IsNullOrEmpty(curString)) { microphones.Add(new Microphone(curString)); deviceList += curString.Length + 1; curString = Marshal.PtrToStringAnsi(deviceList); } return(new ReadOnlyCollection <Microphone>(microphones)); }
public ReadOnlyCollection <RendererDetail> GetDevices() { IntPtr deviceList = ALC10.alcGetString(IntPtr.Zero, ALC11.ALC_ALL_DEVICES_SPECIFIER); List <RendererDetail> renderers = new List <RendererDetail>(); int i = 0; string curString = Marshal.PtrToStringAnsi(deviceList); while (!String.IsNullOrEmpty(curString)) { renderers.Add(new RendererDetail( curString, i.ToString() )); i += 1; deviceList += curString.Length + 1; curString = Marshal.PtrToStringAnsi(deviceList); } return(new ReadOnlyCollection <RendererDetail>(renderers)); }
static string[] QueryDevices(string label, int type) { // Clear error bit AL10.alGetError(); // Returns a null separated list of strings, terminated by two nulls. var devicesPtr = ALC10.alcGetString(IntPtr.Zero, type); if (devicesPtr == IntPtr.Zero || AL10.alGetError() != AL10.AL_NO_ERROR) { Log.Write("sound", "Failed to query OpenAL device list using {0}", label); return(new string[0]); } var devices = new List <string>(); var buffer = new List <byte>(); var offset = 0; while (true) { var b = Marshal.ReadByte(devicesPtr, offset++); if (b != 0) { buffer.Add(b); continue; } // A null indicates termination of that string, so add that to our list. devices.Add(Encoding.UTF8.GetString(buffer.ToArray())); buffer.Clear(); // Two successive nulls indicates the end of the list. if (Marshal.ReadByte(devicesPtr, offset) == 0) { break; } } return(devices.ToArray()); }
// Helper function to call alcGetString() and translate the result // Passing in In32.MaxValue for device will use the current device, or pass any other value to use that device pointer public static string GetALCString(int param, int device = Int32.MaxValue) { var sPtr = ALC10.alcGetString((device == Int32.MaxValue) ? AudioEngine.Device : (IntPtr)device, param); CheckALCError($"could not get ALC string param {param}"); if (sPtr == IntPtr.Zero) { return(null); } // We need to handle the string lists in a different way, as they dont play well with Marshal.PtrToStringAnsi() // Scan through until a double null terminator is found, and return as a newline separated list if (device == 0 && (param == ALC10.ALC_DEVICE_SPECIFIER || param == ALC11.ALC_CAPTURE_DEVICE_SPECIFIER || param == ALC11.ALC_ALL_DEVICES_SPECIFIER)) { // Copy the string data to managed memory byte[] charData = new byte[GetStringListPtrLength(sPtr)]; Marshal.Copy(sPtr, charData, 0, charData.Length); // Find split indices for null characters var splits = charData.Select((b, i) => b == 0 ? i : -1).Where(i => i != -1).ToList(); splits.Insert(0, -1); // Add the beginning of the first string (pos = 0) splits.RemoveAt(splits.Count - 1); // Remove the secondary null terminator at the very end // Create list of strings List <string> strList = new List <string>(); for (int i = 0; i < (splits.Count - 1); ++i) { strList.Add(Encoding.ASCII.GetString(charData, splits[i] + 1, splits[i + 1] - splits[i] - 1)); } // Return newline separated list return(String.Join("\n", strList)); } else { return(Marshal.PtrToStringAnsi(sPtr)); } }
static string[] QueryDevices(string label, int type) { // Clear error bit AL10.alGetError(); var devices = new List <string>(); var next = ALC10.alcGetString(IntPtr.Zero, type); if (next == IntPtr.Zero || AL10.alGetError() != AL10.AL_NO_ERROR) { Log.Write("sound", "Failed to query OpenAL device list using {0}", label); return(new string[] { }); } do { var str = Marshal.PtrToStringAuto(next); next += UnicodeEncoding.Default.GetByteCount(str) + 1; devices.Add(str); } while (Marshal.ReadByte(next) != 0); return(devices.ToArray()); }