public bool SetAudioEndpoint(string dev, bool usedefault = false) { System.Collections.ObjectModel.ReadOnlyCollection <DirectSoundDevice> list = DirectSoundDeviceEnumerator.EnumerateDevices(); DirectSoundDevice dsd = null; if (dev != null) // active selection { dsd = list.FirstOrDefault(x => x.Description.Equals(dev)); // find if (dsd == null && !usedefault) // if not found, and don't use the default (used by constructor) { return(false); } } DirectSoundOut dso = new DirectSoundOut(200, System.Threading.ThreadPriority.Highest); // seems good quality at 200 ms latency if (dso == null) // if no DSO, fail.. { return(false); } if (dsd != null) { dso.Device = dsd.Guid; } else { DirectSoundDevice def = DirectSoundDevice.DefaultDevice; dso.Device = def.Guid; // use default GUID } NullWaveSource nullw = new NullWaveSource(10); try { dso.Initialize(nullw); // check it takes it.. may not if no sound devices there.. dso.Stop(); nullw.Dispose(); } catch { nullw.Dispose(); dso.Dispose(); return(false); } if (aout != null) // clean up last { aout.Stopped -= Output_Stopped; aout.Stop(); aout.Dispose(); } aout = dso; aout.Stopped += Output_Stopped; return(true); }
// FROM audio stream public AudioData Generate(System.IO.Stream audioms, SoundEffectSettings effects, bool ensureaudio) { try { audioms.Position = 0; IWaveSource s; if (audioms.Length == 0) { if (ensureaudio) { s = new NullWaveSource(50); } else { return(null); } } else { s = new CSCore.Codecs.WAV.WaveFileReader(audioms); //System.Diagnostics.Debug.WriteLine("oRIGINAL length " + s.Length); if (ensureaudio) { s = s.AppendSource(x => new ExtendWaveSource(x, 100)); // SEEMS to help the click at end.. } } //System.Diagnostics.Debug.WriteLine("Sample length " + s.Length); System.Diagnostics.Debug.Assert(s != null); ApplyEffects(ref s, effects); //System.Diagnostics.Debug.WriteLine(".. to length " + s.Length); System.Diagnostics.Debug.Assert(s != null); return(new AudioData(s)); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception " + ex.Message); if (ensureaudio) { IWaveSource s = new NullWaveSource(100); System.Diagnostics.Debug.Assert(s != null); return(new AudioData(s)); } else { return(null); } } }