public SoundDevice[] GetDevices() { // Create a DEVICE object to get device info WSSoundInterop.DEVICE device = new WSSoundInterop.DEVICE(); device.nMaxDesc = WSSoundInterop.WS_MAXNAMELEN; device.nMaxId = WSSoundInterop.WS_MAXNAMELEN; device.nMaxName = WSSoundInterop.WS_MAXNAMELEN; // Create device list to add devices into it during enumeration callbackDevices = new List <WSSoundInterop.DEVICE>(); // Enumerate devices int hr = WSSoundInterop.WSEnumDevices(this.pws, ref device, this.deviceCallback); if (hr != 0) { throw new SoundException("WSGetNumDevices", hr); } // Convert returned DEVICEs to SoundDevice int nDevices = callbackDevices.Count; SoundDevice[] soundDevices = new SoundDevice[nDevices]; for (int i = 0; i < nDevices; i++) { device = callbackDevices[i]; // Is device input or ouput (loopback) WSSoundInterop.DeviceType type = (WSSoundInterop.DeviceType)device.dwType; bool isLoopback = type == WSSoundInterop.DeviceType.Output; // Create Sound Device SoundDevice soundDevice = new SoundDevice(device.szId, device.szName, isLoopback, device.szDescription); soundDevices.SetValue(soundDevice, i); } callbackDevices.Clear(); return(soundDevices); }
private void UninitWS() { if (this.pws != IntPtr.Zero) { WSSoundInterop.WSUninit(this.pws); this.pws = IntPtr.Zero; } }
private void InitWS() { int hr = WSSoundInterop.WSInit(ref this.pws); if (hr != 0) { throw new SoundException("WSOpen", hr); } }
public void Close() { if (this.state == RecordingState.Recording) { this.Stop(); } if (this.state == RecordingState.Opened) { WSSoundInterop.WSClose(this.pws); this.state = RecordingState.Closed; } }
public void Stop() { if (this.state == RecordingState.Opened || this.state == RecordingState.Closed) { throw new InvalidOperationException(); } int hr = WSSoundInterop.WSStop(this.pws); if (hr != 0) { throw new SoundException("WSStop", hr); } this.state = RecordingState.Opened; }
public void Start() { if (this.state != RecordingState.Opened) { throw new InvalidOperationException(); } int hr = WSSoundInterop.WSStart(this.pws); if (hr != 0) { throw new SoundException("WSStart", hr); } this.state = RecordingState.Recording; }
public int Read(byte[] buffer, int offset, int length, bool isEnd) { if (this.state != RecordingState.Recording) { throw new InvalidOperationException(); } uint bytesRead; int hr = WSSoundInterop.WSRead(this.pws, buffer, (uint)offset, (uint)length, isEnd, out bytesRead); if (hr != 0) { throw new SoundException("WSRead", hr); } return((int)bytesRead); }
public SoundFormat[] GetDeviceFormats(string deviceId) { if (string.IsNullOrEmpty(deviceId)) { throw new ArgumentNullException("deviceId"); } IntPtr pwfx = Marshal.AllocHGlobal(maxFormatSize); try { callbackDeviceFormats = new List <SoundFormat>(); int hr = WSSoundInterop.WSEnumDeviceFormats(this.pws, deviceId, pwfx, maxFormatSize, deviceFormatCallback); if (hr != 0) { throw new SoundException("WSEnumDeviceFormats", hr); } } finally { Marshal.FreeHGlobal(pwfx); } SoundFormat[] formats = callbackDeviceFormats.ToArray(); callbackDeviceFormats = null; return(formats); }
public void Open() { if (this.state != RecordingState.Closed) { throw new InvalidOperationException(); } int hr = WSSoundInterop.WSOpen(this.pws); if (hr != 0) { throw new SoundException("WSOpen", hr); } hr = WSSoundInterop.WSGetBufferLength(this.pws, out this.bufferLength); if (hr != 0) { throw new SoundException("WSGetBufferLength", hr); } hr = WSSoundInterop.WSGetPacketLength(this.pws, ref this.packetLength); if (hr != 0) { throw new SoundException("WSGetBufferLength", hr); } this.state = RecordingState.Opened; }
private bool EnumDevicesCallback(IntPtr pws, ref WSSoundInterop.DEVICE device) { callbackDevices.Add(device); return true; }