public DirectSound( Form form, int sampleRate, int bufferCount) { if ((sampleRate % 50) != 0) { throw new ArgumentOutOfRangeException("sampleRate", "Sample rate must be a multiple of 50!"); } _sampleRate = sampleRate; var bufferSize = sampleRate / 50; for (int i = 0; i < bufferCount; i++) { _fillQueue.Enqueue(new uint[bufferSize]); } _bufferSize = bufferSize; _bufferCount = bufferCount; _zeroValue = 0; var hWnd = form != null ? form.Handle : IntPtr.Zero; _device = new DirectSound8(); _device.SetCooperativeLevel(hWnd, DSSCL.PRIORITY); // we always use 16 bit stereo (uint per sample) const short channels = 2; const short bitsPerSample = 16; const int sampleSize = 4; // channels * (bitsPerSample / 8); var wf = new WaveFormat(_sampleRate, bitsPerSample, channels); // Create a buffer var bufferDesc = new DSBUFFERDESC(); bufferDesc.dwBufferBytes = _bufferSize * sampleSize * _bufferCount; bufferDesc.dwFlags = DSBCAPS_FLAGS.CTRLVOLUME | DSBCAPS_FLAGS.CTRLPOSITIONNOTIFY | DSBCAPS_FLAGS.GLOBALFOCUS | DSBCAPS_FLAGS.GETCURRENTPOSITION2; bufferDesc.Format = wf; _soundBuffer = new SecondarySoundBuffer(_device, bufferDesc); var posNotify = new DSBPOSITIONNOTIFY[_bufferCount]; for (int i = 0; i < posNotify.Length; i++) { posNotify[i] = new DSBPOSITIONNOTIFY(); posNotify[i].dwOffset = i * _bufferSize * sampleSize; posNotify[i].WaitHandle = _fillEvent; } _soundBuffer.SetNotificationPositions(posNotify); _wavePlayThread = new Thread(WavePlayThreadProc); _wavePlayThread.IsBackground = true; _wavePlayThread.Name = "WavePlay"; _wavePlayThread.Priority = ThreadPriority.Highest; _wavePlayThread.Start(); }
private void InitializeInternal() { //Use Desktophandle as default handle IntPtr handle = DSUtils.GetDesktopWindow(); IntPtr pdsound; DirectSoundException.Try(NativeMethods.DirectSoundCreate8(ref _device, out pdsound, IntPtr.Zero), "DSInterop", "DirectSoundCreate8"); _directSound = new DirectSound8(pdsound); _directSound.SetCooperativeLevel(handle, DSCooperativeLevelType.Normal); //use normal as default if (!_directSound.SupportsFormat(_source.WaveFormat)) { if (_source.WaveFormat.WaveFormatTag == AudioEncoding.IeeeFloat) //directsound does not support ieeefloat { _source = _source.ToSampleSource().ToWaveSource(16); } WaveFormat format16Bit = (WaveFormat)_source.WaveFormat.Clone(); format16Bit.BitsPerSample = 16; WaveFormat format8Bit = (WaveFormat)_source.WaveFormat.Clone(); format8Bit.BitsPerSample = 8; if (_directSound.SupportsFormat(format16Bit)) { _source = _source.ToSampleSource().ToWaveSource(16); } else if (_directSound.SupportsFormat(format8Bit)) { _source = _source.ToSampleSource().ToWaveSource(8); } else { throw new NotSupportedException( "WaveFormat of the source is not supported."); } if (!_directSound.SupportsFormat(_source.WaveFormat)) { throw new NotSupportedException( "WaveFormat of the source is not supported."); } } WaveFormat waveFormat = _source.WaveFormat; var bufferSize = (int)waveFormat.MillisecondsToBytes(_latency); _primaryBuffer = new DirectSoundPrimaryBuffer(_directSound); _secondaryBuffer = new DirectSoundSecondaryBuffer(_directSound, waveFormat, bufferSize * 2); }
private void InitializeInternal() { //Use Desktophandle as default handle IntPtr handle = DSUtils.GetDesktopWindow(); IntPtr pdsound; DirectSoundException.Try(NativeMethods.DirectSoundCreate8(ref _device, out pdsound, IntPtr.Zero), "DSInterop", "DirectSoundCreate8"); _directSound = new DirectSound8(pdsound); _directSound.SetCooperativeLevel(handle, DSCooperativeLevelType.DSSCL_NORMAL); //use normal as default if (!_directSound.SupportsFormat(_source.WaveFormat)) { if (_source.WaveFormat.WaveFormatTag == AudioEncoding.IeeeFloat) { _source = _source.ToSampleSource().ToWaveSource(16); } if (_directSound.SupportsFormat(new WaveFormat(_source.WaveFormat.SampleRate, 16, _source.WaveFormat.Channels, _source.WaveFormat.WaveFormatTag))) { _source = _source.ToSampleSource().ToWaveSource(16); } else if (_directSound.SupportsFormat(new WaveFormat(_source.WaveFormat.SampleRate, 8, _source.WaveFormat.Channels, _source.WaveFormat.WaveFormatTag))) { _source = _source.ToSampleSource().ToWaveSource(8); } else { throw new InvalidOperationException("Invalid WaveFormat. WaveFormat specified by parameter {_source} is not supported by this DirectSound-Device."); } if (!_directSound.SupportsFormat(_source.WaveFormat)) { throw new InvalidOperationException("Invalid WaveFormat. WaveFormat specified by parameter {_source} is not supported by this DirectSound-Device."); } } WaveFormat waveFormat = _source.WaveFormat; int bufferSize = (int)waveFormat.MillisecondsToBytes(_latency); _primaryBuffer = new DirectSoundPrimaryBuffer(_directSound); _secondaryBuffer = new DirectSoundSecondaryBuffer(_directSound, waveFormat, bufferSize); }