Ejemplo n.º 1
0
        public void UpdateSound(float atten)
        {
            if (!Global.Config.SoundEnabled || !IsStarted || _bufferedProvider == null || _disposed)
            {
                if (_bufferedProvider != null)
                {
                    _bufferedProvider.DiscardSamples();
                }
                return;
            }

            if (atten < 0)
            {
                atten = 0;
            }
            if (atten > 1)
            {
                atten = 1;
            }
            _outputDevice.ApplyVolumeSettings(atten);

            int samplesNeeded = _outputDevice.CalculateSamplesNeeded();

            short[] samples;
            int     sampleOffset;
            int     sampleCount;

            if (atten == 0)
            {
                samples      = new short[samplesNeeded * ChannelCount];
                sampleOffset = 0;
                sampleCount  = samplesNeeded;

                _bufferedProvider.DiscardSamples();
            }
            else if (_bufferedProvider == _outputProvider)
            {
                if (Global.Config.SoundThrottle)
                {
                    _outputProvider.BaseSoundProvider.GetSamplesSync(out samples, out sampleCount);
                    sampleOffset = 0;

                    if (Global.DisableSecondaryThrottling && sampleCount > samplesNeeded)
                    {
                        return;
                    }

                    int samplesPerMs      = SampleRate / 1000;
                    int outputThresholdMs = 20;
                    while (sampleCount > samplesNeeded)
                    {
                        if (samplesNeeded >= outputThresholdMs * samplesPerMs)
                        {
                            // If we were given a large enough number of samples (e.g. larger than the device's
                            // buffer), the device will never need that many samples no matter how long we
                            // wait, so we have to start splitting up the output
                            _outputDevice.WriteSamples(samples, sampleOffset, samplesNeeded);
                            sampleOffset += samplesNeeded;
                            sampleCount  -= samplesNeeded;
                        }
                        else
                        {
                            // Let the audio clock control sleep time
                            Thread.Sleep(Math.Min((sampleCount - samplesNeeded) / samplesPerMs, outputThresholdMs));
                        }
                        samplesNeeded = _outputDevice.CalculateSamplesNeeded();
                    }
                }
                else
                {
                    if (Global.DisableSecondaryThrottling)                     // This indicates rewind or fast-forward
                    {
                        _outputProvider.OnVolatility();
                    }
                    _outputProvider.GetSamples(samplesNeeded, out samples, out sampleCount);
                    sampleOffset = 0;
                }
            }
            else if (_bufferedProvider == _bufferedAsync)
            {
                samples = new short[samplesNeeded * ChannelCount];

                _bufferedAsync.GetSamplesAsync(samples);

                sampleOffset = 0;
                sampleCount  = samplesNeeded;
            }
            else
            {
                return;
            }

            _outputDevice.WriteSamples(samples, sampleOffset, sampleCount);
        }
Ejemplo n.º 2
0
        public void UpdateSound(float atten)
        {
            if (!Global.Config.SoundEnabled || !IsStarted || _bufferedProvider == null || _disposed)
            {
                if (_bufferedProvider != null)
                {
                    _bufferedProvider.DiscardSamples();
                }
                return;
            }

            if (atten < 0)
            {
                atten = 0;
            }
            if (atten > 1)
            {
                atten = 1;
            }
            _outputDevice.ApplyVolumeSettings(atten);

            short[] samples;
            int     samplesNeeded = _outputDevice.CalculateSamplesNeeded();
            int     samplesProvided;

            if (atten == 0)
            {
                samples         = new short[samplesNeeded * ChannelCount];
                samplesProvided = samplesNeeded;

                _bufferedProvider.DiscardSamples();
            }
            else if (_bufferedProvider == _outputProvider)
            {
                if (Global.Config.SoundThrottle)
                {
                    _outputProvider.BaseSoundProvider.GetSamplesSync(out samples, out samplesProvided);

                    if (Global.DisableSecondaryThrottling && samplesProvided > samplesNeeded)
                    {
                        return;
                    }

                    while (samplesProvided > samplesNeeded)
                    {
                        Thread.Sleep((samplesProvided - samplesNeeded) / (SampleRate / 1000));                         // Let the audio clock control sleep time
                        samplesNeeded = _outputDevice.CalculateSamplesNeeded();
                        if (_unjamSoundThrottle)
                        {
                            //may be garbage, but what can we do?
                            samplesProvided = samplesNeeded;
                            break;
                        }
                    }
                    _unjamSoundThrottle = false;
                }
                else
                {
                    if (Global.DisableSecondaryThrottling)                     // This indicates rewind or fast-forward
                    {
                        _outputProvider.OnVolatility();
                    }
                    _outputProvider.GetSamples(samplesNeeded, out samples, out samplesProvided);
                }
            }
            else if (_bufferedProvider == _bufferedAsync)
            {
                samples = new short[samplesNeeded * ChannelCount];

                _bufferedAsync.GetSamplesAsync(samples);

                samplesProvided = samplesNeeded;
            }
            else
            {
                return;
            }

            _outputDevice.WriteSamples(samples, samplesProvided);
        }
Ejemplo n.º 3
0
        public void UpdateSound(float atten)
        {
            if (!Global.Config.SoundEnabled || !IsStarted || _disposed)
            {
                if (_asyncSoundProvider != null)
                {
                    _asyncSoundProvider.DiscardSamples();
                }
                if (_syncSoundProvider != null)
                {
                    _syncSoundProvider.DiscardSamples();
                }
                if (_outputProvider != null)
                {
                    _outputProvider.DiscardSamples();
                }
                return;
            }

            if (atten < 0)
            {
                atten = 0;
            }
            if (atten > 1)
            {
                atten = 1;
            }
            _soundOutput.ApplyVolumeSettings(atten);

            short[] samples;
            int     samplesNeeded = _soundOutput.CalculateSamplesNeeded();
            int     samplesProvided;

            if (atten == 0)
            {
                samples         = new short[samplesNeeded * ChannelCount];
                samplesProvided = samplesNeeded;

                if (_asyncSoundProvider != null)
                {
                    _asyncSoundProvider.DiscardSamples();
                }
                if (_syncSoundProvider != null)
                {
                    _syncSoundProvider.DiscardSamples();
                }
                if (_outputProvider != null)
                {
                    _outputProvider.DiscardSamples();
                }
            }
            else if (_syncSoundProvider != null)
            {
                if (Global.Config.SoundThrottle)
                {
                    _syncSoundProvider.GetSamples(out samples, out samplesProvided);

                    while (samplesNeeded < samplesProvided && !Global.DisableSecondaryThrottling)
                    {
                        Thread.Sleep((samplesProvided - samplesNeeded) / (SampleRate / 1000));                         // Let the audio clock control sleep time
                        samplesNeeded = _soundOutput.CalculateSamplesNeeded();
                    }
                }
                else
                {
                    if (Global.DisableSecondaryThrottling)                     // This indicates rewind or fast-forward
                    {
                        _outputProvider.OnVolatility();
                    }
                    _outputProvider.GetSamples(samplesNeeded, out samples, out samplesProvided);
                }
            }
            else if (_asyncSoundProvider != null)
            {
                samples = new short[samplesNeeded * ChannelCount];

                _semiSync.GetSamples(samples);

                samplesProvided = samplesNeeded;
            }
            else
            {
                return;
            }

            _soundOutput.WriteSamples(samples, samplesProvided);
        }