Beispiel #1
0
        private bool RefillBuffer(byte[] buffer, int sampleOffset, int bufferSize)
        {
            int read;

            if (_secondaryBuffer.IsBufferLost)
            {
                _secondaryBuffer.Restore();
            }

            if (_playbackState == PlaybackState.Paused)
            {
                Array.Clear(buffer, 0, buffer.Length);
                read = bufferSize;
            }
            else
            {
                if (_source != null)
                {
                    read = _source.Read(buffer, 0, bufferSize);
                }
                else
                {
                    return(false);
                }
            }

            if (read > 0)
            {
                return(_secondaryBuffer.Write(buffer, sampleOffset, bufferSize));
            }
            return(false);
        }
Beispiel #2
0
        public void CanPlayBuffers()
        {
            using (var dsound = CreateDirectSound8())
            {
                dsound.SetCooperativeLevel(DSUtils.GetDesktopWindow(), DSCooperativeLevelType.DSSCL_NORMAL);
                WaveFormat waveFormat = new WaveFormat(44100, 16, 2);
                using (var primaryBuffer = new DirectSoundPrimaryBuffer(dsound))
                    using (var secondaryBuffer = new DirectSoundSecondaryBuffer(dsound, waveFormat, (int)waveFormat.MillisecondsToBytes(10000)))
                    {
                        primaryBuffer.Play(DSBPlayFlags.DSBPLAY_LOOPING);
                        var caps = secondaryBuffer.BufferCaps;

                        var data = GenerateData(caps.dwBufferBytes / 2, waveFormat);

                        if (secondaryBuffer.Write(data, 0, data.Length))
                        {
                            secondaryBuffer.Play(DSBPlayFlags.DSBPLAY_LOOPING);
                        }
                        else
                        {
                            Assert.Fail("Could not write data.");
                        }
                        Thread.Sleep(1);
                    }
            }
        }