Exemplo n.º 1
0
        private void CleanupResources()
        {
            if (_directSoundNotify != null)
            {
                _directSoundNotify.Dispose();
                _directSoundNotify = null;
            }
            if (_secondaryBuffer != null)
            {
                _secondaryBuffer.Stop();
                _secondaryBuffer.Dispose();
                _secondaryBuffer = null;
            }
            if (_primaryBuffer != null)
            {
                _primaryBuffer.Stop();
                _primaryBuffer.Dispose();
                _primaryBuffer = null;
            }

            if (_directSound != null)
            {
                _directSound.Dispose();
                _directSound = null;
            }

            _isInitialized = false;
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="owner">owner window object</param>
        public Performance(IWin32Window owner)
        {
            handle      = DirectAudio.dx.DirectMusicPerformanceCreate();
            this.handle = handle;

            DMUS_AUDIOPARAMS param   = new DMUS_AUDIOPARAMS();
            DirectSound8     nullDs8 = null;

            // TODO: learn more about this initialization process
            handle.InitAudio(owner.Handle.ToInt32(),
                             CONST_DMUS_AUDIO.DMUS_AUDIOF_ALL,
                             ref param,
                             ref nullDs8,
                             CONST_DMUSIC_STANDARD_AUDIO_PATH.DMUS_APATH_DYNAMIC_STEREO, 16);
        }
Exemplo n.º 5
0
        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);
        }
Exemplo n.º 6
0
 public void CanCreateDirectSound8()
 {
     DirectSound8.Create8((Guid)DirectSoundDevice.DefaultDevice).Dispose();
 }
Exemplo n.º 7
0
 private DirectSound8 CreateDirectSound8()
 {
     return(DirectSound8.Create8((Guid)DirectSoundDevice.DefaultDevice));
 }