protected override Task <string> Record(Channel Channel) { var tcs = new TaskCompletionSource <string>(); WavSource = new WaveInEvent(); bool IsEqual = false; ProductName = string.Empty; List <Guid> AudioDeviceList = null; switch (Channel) { case Channel.Left: case Channel.Right: AudioDeviceList = ExternalAudioDeviceList; break; case Channel.AudioJack: AudioDeviceList = MachineAudioDeviceList; break; case Channel.HeadSet: AudioDeviceList = DigitalMicDeviceList; break; case Channel.Fan: AudioDeviceList = FanRecordDeviceList; break; } SetupApi.di.Clear(); for (int n = -1; n < WaveInEvent.DeviceCount; n++) { var caps = WaveInEvent.GetCapabilities(n); Console.WriteLine("Record device {0}: {1}", n, caps.ProductName); Console.WriteLine(caps.ManufacturerGuid); Trace.WriteLine(caps.ManufacturerGuid); foreach (var v in AudioDeviceList) { if (caps.ManufacturerGuid.Equals(v)) { DeviceNumber = n; ProductName = caps.ProductName; if (!di.ContainsKey(DeviceNumber)) { di.Add(DeviceNumber, ProductName); } SetupApi.GetLocationInformation(DeviceNumber, ProductName); Console.WriteLine("Find"); } } } switch (Channel) { case Channel.Left: case Channel.Right: IsEqual = ExternalAudioDeviceList.Except(FanRecordDeviceList).Count() == 0; if (IsEqual) { if (SetupApi.di.Count() < 2 || ProductName.Contains(UsbAudioDeviceName)) { throw new Exception("External audio device not found"); } } else { if (string.IsNullOrEmpty(ProductName) || ProductName.Contains(UsbAudioDeviceName)) { throw new Exception("External audio device not found"); } } DeviceNumber = SetupApi.di.OrderBy(e => e.Value).FirstOrDefault().Key; //ProductName = SetupApi.di.OrderBy(e => e.Value).FirstOrDefault().Value; break; case Channel.HeadSet: if (string.IsNullOrEmpty(ProductName)) { throw new Exception("Digital Mic device not found"); } break; case Channel.AudioJack: ProductName = string.Empty; Regex regex = new Regex(RealtekMicrophone); foreach (var v in di) { if (regex.IsMatch(v.Value)) { DeviceNumber = v.Key; ProductName = v.Value; } } if (string.IsNullOrEmpty(ProductName)) { throw new Exception("Audio Jack device not found"); } break; case Channel.Fan: IsEqual = ExternalAudioDeviceList.Except(FanRecordDeviceList).Count() == 0; if (IsEqual) { if (SetupApi.di.Count() < 2) { throw new Exception("Fan record device not found"); } } else { if (string.IsNullOrEmpty(ProductName)) { throw new Exception("Fan record device not found"); } } DeviceNumber = SetupApi.di.OrderBy(e => e.Value).LastOrDefault().Key; //ProductName = SetupApi.di.OrderBy(e => e.Value).LastOrDefault().Value; break; } WavSource.DeviceNumber = DeviceNumber; Console.WriteLine("Record device ###### {0} ######", WaveInEvent.GetCapabilities(DeviceNumber).ProductName); foreach (var item in SetupApi.di.OrderBy(e => e.Value)) { Console.WriteLine(item.Key + " " + item.Value); } WavSource.WaveFormat = new WaveFormat(44100, 1); WavSource.DataAvailable += (sender, e) => { if (WavSourceFile != null) { WavSourceFile.Write(e.Buffer, 0, e.BytesRecorded); WavSourceFile.Flush(); } }; WavSource.RecordingStopped += (sender, e) => { if (WavSource != null) { WavSource.Dispose(); WavSource = null; } if (WavSourceFile != null) { WavSourceFile.Dispose(); WavSourceFile = null; } Thread.Sleep(200); tcs.SetResult("Done"); Console.WriteLine("Record Stopped"); }; switch (Channel) { case Channel.Left: WavSourceFile = new WaveFileWriter(LeftRecordFileName, WavSource.WaveFormat); break; case Channel.Right: WavSourceFile = new WaveFileWriter(RightRecordFileName, WavSource.WaveFormat); break; case Channel.HeadSet: WavSourceFile = new WaveFileWriter(InternalRecordFileName, WavSource.WaveFormat); break; case Channel.AudioJack: WavSourceFile = new WaveFileWriter(AudioJackRecordFileName, WavSource.WaveFormat); break; case Channel.Fan: WavSourceFile = new WaveFileWriter(FanRecordFileName, WavSource.WaveFormat); break; } WavSource.StartRecording(); return(tcs.Task); }
protected override Task <string> Record(Channel Channel) { var tcs = new TaskCompletionSource <string>(); WavSource = new WaveInEvent(); ProductName = string.Empty; List <Guid> AudioDeviceList = null; if (Channel == Channel.HeadSet) { AudioDeviceList = MachineAudioDeviceList; } else if (Channel == Channel.Left || Channel == Channel.Right) { AudioDeviceList = ExternalAudioDeviceList; } for (int n = -1; n < WaveInEvent.DeviceCount; n++) { var caps = WaveInEvent.GetCapabilities(n); Console.WriteLine("Record device {0}: {1}", n, caps.ProductName); Console.WriteLine(caps.ManufacturerGuid); Trace.WriteLine(caps.ManufacturerGuid); foreach (var v in AudioDeviceList) { if (caps.ManufacturerGuid.Equals(v)) { DeviceNumber = n; ProductName = caps.ProductName; Console.WriteLine("Find"); } } } if (string.IsNullOrEmpty(ProductName) && Channel == Channel.HeadSet) { throw new Exception("Machine audio device not found"); } else if (string.IsNullOrEmpty(ProductName) && (Channel == Channel.Left || Channel == Channel.Right)) { throw new Exception("External audio device not found"); } WavSource.DeviceNumber = DeviceNumber; Console.WriteLine("Record device {0}", ProductName); WavSource.WaveFormat = new WaveFormat(44100, 2); WavSource.DataAvailable += (sender, e) => { if (WavSourceFile != null) { WavSourceFile.Write(e.Buffer, 0, e.BytesRecorded); WavSourceFile.Flush(); } }; WavSource.RecordingStopped += (sender, e) => { if (WavSource != null) { WavSource.Dispose(); WavSource = null; } if (WavSourceFile != null) { WavSourceFile.Dispose(); WavSourceFile = null; } Thread.Sleep(200); tcs.SetResult("Done"); Console.WriteLine("Record Stopped"); }; if (Channel == Channel.Left) { WavSourceFile = new WaveFileWriter(LeftRecordFileName, WavSource.WaveFormat); } else if (Channel == Channel.Right) { WavSourceFile = new WaveFileWriter(RightRecordFileName, WavSource.WaveFormat); } else if (Channel == Channel.HeadSet) { WavSourceFile = new WaveFileWriter(InternalRecordFileName, WavSource.WaveFormat); } WavSource.StartRecording(); return(tcs.Task); }