public IList <float> Resample(IList <float> data, float sourceRate, float targetRate)
        {
            var unfiltered = _baseResampler.Resample(data, sourceRate, targetRate);

            if (targetRate > sourceRate)
            {
                var filter  = _filterProvider.Get(FilterType.LowPass).First();
                var context = filter.Create(targetRate, sourceRate / 2);
                return(context.Filter(unfiltered));
            }
            else
            {
                return(unfiltered);
            }
        }
Пример #2
0
        public ISound ApplyResampling(ISound sound, IResampler resampler, BigRational rate)
        {
            if (sound == null || !sound.Samples.Any())
            {
                return(null);
            }

            if (rate <= BigRational.Zero ||
                sound[NumericData.Rate] == rate ||
                sound[NumericData.Rate] == 0 ||
                sound.Samples == null ||
                !sound.Samples.Any() ||
                sound.Samples.Any(sa => sa[NumericData.Rate] != null && sa[NumericData.Rate] <= 0))
            {
                return(sound);
            }

            var targetRate = (float)(double)rate;
            var samples    = new List <ISample>(sound.Samples);
            var result     = new Sound
            {
                Samples = samples.Select(s =>
                {
                    var sourceRate = (float)(double)(s[NumericData.Rate] ?? sound[NumericData.Rate]);
                    var sample     = new Sample
                    {
                        Data = resampler.Resample(s.Data, sourceRate, targetRate)
                    };
                    sample.CloneMetadataFrom((Metadata)s);
                    sample[NumericData.Rate] = rate;
                    return((ISample)sample);
                }).ToList()
            };

            result.CloneMetadataFrom((Metadata)sound);
            result[NumericData.Rate] = rate;
            return(result);
        }