public DXWavePlayer(int device, int BufferByteSize, DataRequestDelegate fillProc) { if (BufferByteSize < 1000) { throw new ArgumentOutOfRangeException("BufferByteSize", "minimal size of buffer is 1000 bytes"); } _buffersize = BufferByteSize; _requestproc = fillProc; var devices = DirectSound.GetDevices(); if (device <= 0 || device >= devices.Count) { device = 0; } _outputDevice = new DirectSound(devices[device].DriverGuid); System.Windows.Interop.WindowInteropHelper wh = new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow); _outputDevice.SetCooperativeLevel(wh.Handle, CooperativeLevel.Priority); _buffDescription = new SoundBufferDescription(); _buffDescription.Flags = BufferFlags.ControlPositionNotify | BufferFlags.ControlFrequency | BufferFlags.ControlEffects | BufferFlags.GlobalFocus | BufferFlags.GetCurrentPosition2; _buffDescription.BufferBytes = BufferByteSize * InternalBufferSizeMultiplier; WaveFormat format = new WaveFormat(16000, 16, 1); _buffDescription.Format = format; _soundBuffer = new SecondarySoundBuffer(_outputDevice, _buffDescription); _synchronizer = new AutoResetEvent(false); NotificationPosition[] nots = new NotificationPosition[InternalBufferSizeMultiplier]; NotificationPosition not; int bytepos = 800; for (int i = 0; i < InternalBufferSizeMultiplier; i++) { not = new NotificationPosition(); not.Offset = bytepos; not.WaitHandle = _synchronizer; nots[i] = not; bytepos += BufferByteSize; } _soundBuffer.SetNotificationPositions(nots); _waitThread = new Thread(new ThreadStart(DataRequestThread)) { Name = "MyWavePlayer.DataRequestThread" }; _waitThread.Start(); }
public DXWavePlayer(int device, int BufferByteSize, DataRequestDelegate fillProc) { if (BufferByteSize < 1000) { throw new ArgumentOutOfRangeException("BufferByteSize", "minimal size of buffer is 1000 bytes"); } _buffersize = BufferByteSize; _requestproc = fillProc; var devices = DirectSound.GetDevices(); if (device <= 0 || device >= devices.Count) { device = 0; } _outputDevice = new DirectSound(devices[device].DriverGuid); System.Windows.Interop.WindowInteropHelper wh = new System.Windows.Interop.WindowInteropHelper(Application.Current.MainWindow); _outputDevice.SetCooperativeLevel(wh.Handle, CooperativeLevel.Priority); _buffDescription = new SoundBufferDescription(); _buffDescription.Flags = BufferFlags.ControlPositionNotify | BufferFlags.ControlFrequency | BufferFlags.ControlEffects | BufferFlags.GlobalFocus | BufferFlags.GetCurrentPosition2; _buffDescription.BufferBytes = BufferByteSize * InternalBufferSizeMultiplier; WaveFormat format = new WaveFormat(16000, 16, 1); _buffDescription.Format = format; _soundBuffer = new SecondarySoundBuffer(_outputDevice, _buffDescription); _synchronizer = new AutoResetEvent(false); NotificationPosition[] nots = new NotificationPosition[InternalBufferSizeMultiplier]; NotificationPosition not; int bytepos = 800; for (int i = 0; i < InternalBufferSizeMultiplier; i++) { not = new NotificationPosition(); not.Offset = bytepos; not.WaitHandle = _synchronizer; nots[i] = not; bytepos += BufferByteSize; } _soundBuffer.SetNotificationPositions(nots); _waitThread = new Thread(new ThreadStart(DataRequestThread)) { Name = "MyWavePlayer.DataRequestThread" }; _waitThread.Start(); }