Пример #1
0
        private void InitializeInternal()
        {
            _directSound = new DirectSound();

            _directSound.SetCooperativeLevel(Win32.NativeMethods.GetDesktopWindow(), CooperativeLevel.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);

            IntPtr dsBufferOut;

            _directSound.CreateSoundBuffer(DefaultPrimaryBufferDescription, out dsBufferOut, null);
            _primaryBuffer = new SoundBuffer(dsBufferOut);

            SoundBufferDescription secondaryBufferDesc = new SoundBufferDescription()
            {
                BufferBytes = bufferSize * 2,
                Flags       = BufferFlags.ControlFrequency | BufferFlags.ControlPan |
                              BufferFlags.ControlVolume | BufferFlags.ControlPositionNotify |
                              BufferFlags.GetCurrentPosition2 | BufferFlags.GlobalFocus |
                              BufferFlags.StickyFocus,
                Reserved       = 0,
                AlgorithmFor3D = Guid.Empty
            };

            secondaryBufferDesc.Format = SharpDX.Multimedia.WaveFormat.CreateCustomFormat(
                (SharpDX.Multimedia.WaveFormatEncoding)(short) waveFormat.WaveFormatTag,
                waveFormat.SampleRate,
                waveFormat.Channels,
                waveFormat.BytesPerSecond,
                waveFormat.BlockAlign,
                waveFormat.BitsPerSample
                );
            _directSound.CreateSoundBuffer(secondaryBufferDesc, out dsBufferOut, null);
            _secondaryBuffer = new SoundBuffer(dsBufferOut);
        }