Beispiel #1
0
        public void TestMinimumDistance()
        {
            double[] input = { 1, 2, 2.1, 3, 4, 10 };
            Assert.AreEqual(Dsp.MinimumDistance(input), .1, 1e-14);

            Assert.Throws <ArgumentNullException>(() => Dsp.MinimumDistance(null));
        }
Beispiel #2
0
        public void TestInterpolateComplex()
        {
            double[] x = { 1, 2, 3, 4 };

            Complex[] y =
            {
                Complex.FromPolarCoordinates(1, 1),
                Complex.FromPolarCoordinates(2, 2),
                Complex.FromPolarCoordinates(3, 3),
                Complex.FromPolarCoordinates(4, 4)
            };

            double[] x2 = { 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5 };

            var result = Dsp.InterpolateComplex(x, y, x2).ToReadOnlyList();

            Assert.That(result.Count == x2.Length);
            FilterAssert.ListIsMonotonouslyRising(result.Select(c => c.Magnitude));
            Assert.GreaterOrEqual(result[0].Magnitude, 0);
            Assert.GreaterOrEqual(result[result.Count - 1].Magnitude, 4);
            Assert.LessOrEqual(result[0].Magnitude, 1);
            Assert.LessOrEqual(result[result.Count - 1].Magnitude, 5);

            Assert.Throws <ArgumentNullException>(() => Dsp.InterpolateComplex(null, y, x2).ToReadOnlyList());
            Assert.Throws <ArgumentNullException>(() => Dsp.InterpolateComplex(x, null, x2).ToReadOnlyList());
            Assert.Throws <ArgumentNullException>(() => Dsp.InterpolateComplex(x, y, null).ToReadOnlyList());
            Assert.Catch <Exception>(() => Dsp.InterpolateComplex(x2, y, x2).ToReadOnlyList());
        }
Beispiel #3
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is Phaser p)
     {
     }
 }
Beispiel #4
0
        public void TestModBessel0()
        {
            double[] input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            double[] target =
            {
                1,
                1.26606587775201e+000,
                2.27958530233607e+000,
                4.88079258586502e+000,
                11.3019219521363e+000,
                27.2398718236044e+000,
                67.2344069764780e+000,
                168.593908510290e+000,
                427.564115721805e+000,
                1.09358835451137e+003,
                2.81571662846625e+003
            };

            var result = input.Select(Dsp.ModBessel0).ToReadOnlyList();

            FilterAssert.ListsAreReasonablyClose(target, result, 1e-13);

            Assert.Catch <ArgumentOutOfRangeException>(() => Dsp.ModBessel0(-1));
        }
Beispiel #5
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="Sinc" /> Class.
 /// </summary>
 /// <param name="from">The start frequency.</param>
 /// <param name="to">The stop frequency.</param>
 /// <param name="length">The length in seconds.</param>
 /// <param name="samplerate">The samplerate.</param>
 public LogSweep(double from, double to, double length, double samplerate)
     : base(Dsp.LogSweep(from, to, length, samplerate).ToReadOnlyList(), samplerate)
 {
     this.From        = @from;
     this.To          = to;
     this.DisplayName = "logarithmic sweep";
 }
        private void AddParameterControl(ref Dsp dsp, int index)
        {
            var info = dsp.GetParameterInfo(index);

            switch (info.Type)
            {
            case DspParameterType.Float:
                AddFloatControl(dsp, ref info, index);
                break;

            case DspParameterType.Int:
                AddIntControl(dsp, ref info, index);
                break;

            case DspParameterType.Bool:
                AddBoolControl(dsp, ref info, index);
                break;

            case DspParameterType.Data:
                AddDataControl(dsp, ref info, index);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Beispiel #7
0
        IDisposable onChanged(object sender, string name, NotifyCollectionChangedEventArgs e, bool delay)
        {
            IDisposable blocker;
            Dsp         dsp;

            if (Block(name, out blocker))               //ブロックできた場合
            {
                if (delay)
                {
                    dsp = new Dsp(() => {
                        Raise(sender, name, e);
                        blocker.Dispose();
                    });
                }
                else
                {
                    Raise(sender, name, e);
                    dsp = new Dsp(() => {
                        blocker.Dispose();
                    });
                }
            }
            else
            {
                dsp = new Dsp(null);
            }
            return(dsp);
        }
Beispiel #8
0
        /// <summary>
        ///     Applies a circular shift to the provided vector.
        /// </summary>
        /// <param name="series">The vector.</param>
        /// <param name="amount">The shift amount.</param>
        /// <returns></returns>
        public static IEnumerable <double> CircularShift(this IEnumerable <double> series, int amount)
        {
            var list = series.ToReadOnlyList();

            amount = Dsp.Mod(amount, list.Count);
            return(list.Skip(amount).Concat(list.Take(amount)));
        }
Beispiel #9
0
        public override ISignal CreateSignal()
        {
            var fi = new FileInfo(this.FileName);

            if (!fi.Exists)
            {
                return(null);
            }

            var file = File.ReadLines(this.FileName);

            var frequencies = new List <double>();
            var values      = new List <Complex>();

            double frequency;
            double mag;
            double phase;

            foreach (var line in file)
            {
                var fields = line.Split(',');

                if (fields.Length < 2)
                {
                    continue;
                }

                if (!double.TryParse(fields[0], NumberStyles.Any, CultureInfo.InvariantCulture, out frequency))
                {
                    continue;
                }

                if (!double.TryParse(fields[1], NumberStyles.Any, CultureInfo.InvariantCulture, out mag))
                {
                    continue;
                }

                if (fields.Length == 2)
                {
                    frequencies.Add(frequency);
                    values.Add(mag);
                }
                else if (fields.Length == 3)
                {
                    if (!double.TryParse(fields[1], NumberStyles.Any, CultureInfo.InvariantCulture, out phase))
                    {
                        continue;
                    }

                    frequencies.Add(frequency);
                    values.Add(Complex.FromPolarCoordinates(mag, phase));
                }
            }

            var series = new FftSeries(this.SampleRate, this.SignalLength);
            var ret    = Dsp.AdaptiveInterpolation(frequencies, values, series.Values.ToReadOnlyList(), false).ToReadOnlyList();

            return(new FiniteSignal(new FftSpectrum(series, ret), this.TimeOffset));
        }
Beispiel #10
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is TapeStop ts)
     {
         ts.Duration = Duration.Sample(alpha);
     }
 }
Beispiel #11
0
        public void TestLinSeries()
        {
            double[] target = { 1, 2, 3, 4, 5 };
            var      result = Dsp.LinSeries(1, 5, 5).ToReadOnlyList();

            FilterAssert.ListsAreReasonablyClose(target, result);
            Assert.Catch <ArgumentOutOfRangeException>(() => Dsp.LinSeries(1, 5, 1));
        }
Beispiel #12
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="WhiteNoise" /> class.
 /// </summary>
 /// <param name="sampleRate">The sample rate.</param>
 /// <param name="mean">The mean.</param>
 /// <param name="variance">The variance.</param>
 public WhiteNoise(double sampleRate, double mean = 0, double variance = 1) : base(sampleRate)
 {
     this.Mean        = mean;
     this.Sigma       = Math.Sqrt(variance);
     this.Variance    = variance;
     this.NoiseSource = Dsp.WhiteNoise().GetEnumerator();
     this.DisplayName = "white noise, µ = " + mean + ",σ² = " + variance;
 }
Beispiel #13
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is Wobble wobble)
     {
         wobble.SetPeriod(Period.Sample(alpha) * qnDur.Seconds * 4);
     }
 }
Beispiel #14
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is BitCrusher bitCrusher)
     {
         bitCrusher.Reduction = Reduction.Sample(alpha);
     }
 }
Beispiel #15
0
        public Dsp.Dsp CreateDspByType(Dsp.Type type)
        {
            IntPtr DspHandle = IntPtr.Zero;

            Error.Code ReturnCode = CreateDspByType (this.DangerousGetHandle (), type, ref DspHandle);
            Error.Errors.ThrowError (ReturnCode);

            return new Dsp.Dsp (DspHandle);
        }
Beispiel #16
0
        public Dsp.Dsp CreateDSP(ref Dsp.Description description)
        {
            IntPtr DspHandle = IntPtr.Zero;

            Error.Code ReturnCode = CreateDSP (this.DangerousGetHandle (), ref description, ref DspHandle);
            Error.Errors.ThrowError (ReturnCode);

            return new Dsp.Dsp (DspHandle);
        }
Beispiel #17
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is SideChain sc)
     {
         sc.Amount   = Amount.Sample(alpha);
         sc.Duration = Duration.Sample(alpha) * qnDur.Seconds * 4;
     }
 }
Beispiel #18
0
        public Dsp.Connection AddDsp(Dsp.Dsp dsp)
        {
            IntPtr ConnectionHandle = IntPtr.Zero;

            Error.Code ReturnCode = AddDSP (this.DangerousGetHandle (), dsp.DangerousGetHandle (), ref ConnectionHandle);
            Error.Errors.ThrowError (ReturnCode);

            return new Dsp.Connection (ConnectionHandle);
        }
Beispiel #19
0
        public void TestWhiteNoise()
        {
            var noise  = Dsp.WhiteNoise().Take(1000000).ToReadOnlyList();
            var noise1 = Dsp.WhiteNoise().Take(1).ToReadOnlyList();

            Assert.That(noise1[0] != noise[0]);
            Assert.AreEqual(noise.Average(), 0, 1e-2);
            Assert.AreEqual(noise.Variance(), 1, 1e-2);
        }
Beispiel #20
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is Flanger flanger)
     {
         flanger.SetDelay(Delay.Sample(alpha));
         flanger.SetDelayRange(Offset.Sample(alpha), Depth.Sample(alpha));
     }
 }
Beispiel #21
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is Gate gate)
     {
         gate.SetGating(Gating.Sample(alpha));
         gate.SetGateDuration(GateDuration.Sample(alpha) * qnDur.Seconds * 4);
     }
 }
Beispiel #22
0
        public override void Initialize()
        {
            base.Initialize();

            System.Init(32);

            dsp = System.CreateDSPByType(DSPType.Oscillator);

            dsp.SetParameterFloat(1, 440.0f);
        }
Beispiel #23
0
        private void AddBoolControl(Dsp dsp, ref DspParameterDesc info, int index)
        {
            var boolPanel = new IntParameterPanel();

            boolPanel.ComboBox.Items.AddRange(new object[] { "False", "True" });
            boolPanel.Description   = info.Description;
            boolPanel.Anchor        = AnchorStyles.Left | AnchorStyles.Right;
            boolPanel.ValueChanged += (s, e) => dsp.SetParameterBool(index, boolPanel.Value == 1);
            flowPanel.Controls.Add(boolPanel, 0, index);
        }
Beispiel #24
0
 public void Remove()
 {
     if (_dspEq != null)
     {
         var result = _fChannel?.RemoveDsp(_dspEq);
         result = _dspEq.Release();
         _dspEq = null;
     }
     IsActive = false;
 }
Beispiel #25
0
 public override void ApplyToDsp(Dsp effect, time_t qnDur, float alpha = 0)
 {
     base.ApplyToDsp(effect, qnDur, alpha);
     if (effect is Retrigger retrigger)
     {
         retrigger.Mix      = Mix.Sample(alpha);
         retrigger.Gating   = Gating.Sample(alpha);
         retrigger.Duration = GateDuration.Sample(alpha) * qnDur.Seconds * 4;
     }
 }
Beispiel #26
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="UniformSeries" /> class.
 /// </summary>
 /// <param name="from">The first value of the series.</param>
 /// <param name="to">The last value of the series.</param>
 /// <param name="length">Determines how many values (including first and last) the series should contain.</param>
 /// <param name="logarithmic">Determines whether the series should be on a linear or a logarithmic scale.</param>
 public UniformSeries(double from, double to, int length, bool logarithmic = false) :
     base(logarithmic
         ? Dsp.LogSeries(from, to, length)
         : Dsp.LinSeries(from, to, length),
          logarithmic)
 {
     this.From   = from;
     this.To     = to;
     this.Length = length;
 }
Beispiel #27
0
 /// <summary>ブロックできたら解除Keyを返す</summary>
 private bool Block(string name, out IDisposable dispose)
 {
     if (blockingName.Add(name))
     {
         dispose = new Dsp(() => blockingName.Remove(name));
         return(true);
     }
     dispose = new Dsp(null);
     return(false);
 }
Beispiel #28
0
        public void TestDegToRad()
        {
            var input  = new[] { 0.0, -90, 180, 270 };
            var target = new[] { 0, -Math.PI * 0.5, Math.PI, 1.5 * Math.PI };

            var result = Dsp.DegToRad(input).ToReadOnlyList();

            FilterAssert.ListsAreReasonablyClose(target, result);
            Assert.That(Dsp.DegToRad(Enumerable.Empty <double>()).ToReadOnlyList().Count == 0);
            Assert.Throws <ArgumentNullException>(() => Dsp.DegToRad(null).ToReadOnlyList());
        }
Beispiel #29
0
        public void TestLinearToDb()
        {
            var input  = new[] { 1.0, 10, 2, 0 };
            var target = new[] { 0, 20, 6.020599913279624, -100 };

            var result = Dsp.LinearToDb(input, -100).ToReadOnlyList();

            FilterAssert.ListsAreReasonablyClose(target, result);
            Assert.That(Dsp.LinearToDb(Enumerable.Empty <double>()).ToReadOnlyList().Count == 0);
            Assert.Throws <ArgumentNullException>(() => Dsp.LinearToDb(null).ToReadOnlyList());
        }
Beispiel #30
0
        /// <summary>
        ///     Convolves the specified finite signal with an enumerable signal.
        /// </summary>
        /// <param name="s1">The finite signal.</param>
        /// <param name="s2">The enumerable signal.</param>
        /// <returns></returns>
        /// <exception cref="SamplerateMismatchException"></exception>
        public static IEnumerableSignal Convolve(this IFiniteSignal s1, IEnumerableSignal s2)
        {
            if (s1.SampleRate != s2.SampleRate)
            {
                throw new SamplerateMismatchException();
            }

            return(new EnumerableSignal(Dsp.Convolve(s2.Signal, s1.Signal), s1.SampleRate, s1.Start + s2.Start)
            {
                DisplayName = "convolution result"
            });
        }
Beispiel #31
0
        /// <summary>
        ///     Computes the cross-correlation of a finite and a enumerable signal.
        /// </summary>
        /// <param name="s1">The finite signal.</param>
        /// <param name="s2">The enumerable signal.</param>
        /// <returns></returns>
        /// <exception cref="SamplerateMismatchException"></exception>
        public static IEnumerableSignal CrossCorrelate(this IFiniteSignal s1, IEnumerableSignal s2)
        {
            if (s1.SampleRate != s2.SampleRate)
            {
                throw new SamplerateMismatchException();
            }

            return(new EnumerableSignal(Dsp.CrossCorrelate(s2.Signal, s1.Signal), s1.SampleRate, s1.Start - s2.Start)
            {
                DisplayName = "cross correlation"
            });
        }
Beispiel #32
0
        public void TestAmplitudeToComplex()
        {
            var input  = new[] { 1.0, 2, 3 };
            var output = Dsp.AmplitudeToComplex(input).ToReadOnlyList();

            Complex[] target = { Complex.One, 2, 3 };
            FilterAssert.ListsAreEqual(output, target);

            Assert.That(Dsp.AmplitudeToComplex(new List <double>()).ToReadOnlyList().Count == 0);

            Assert.Throws <ArgumentNullException>(() => Dsp.AmplitudeToComplex(null));
        }
Beispiel #33
0
        private void AddIntControl(Dsp dsp, ref DspParameterDesc info, int index)
        {
            var intPanel = new IntParameterPanel();

            // ReSharper disable once CoVariantArrayConversion
            intPanel.ComboBox.Items.AddRange(info.IntDescription.ValueNames);
            intPanel.Description   = info.Description;
            intPanel.Anchor        = AnchorStyles.Left | AnchorStyles.Right;
            intPanel.Value         = dsp.GetParameterInt(index);
            intPanel.ValueChanged += (s, e) => dsp.SetParameterInt(index, intPanel.Value);
            flowPanel.Controls.Add(intPanel, 0, index);
        }
Beispiel #34
0
        public Channel.Channel PlayDsp(Dsp.Dsp dsp, bool paused)
        {
            IntPtr ChannelHandle = IntPtr.Zero;

            Error.Code ReturnCode = PlayDsp (this.DangerousGetHandle (), Channel.Index.Free, dsp.DangerousGetHandle (), paused, ref ChannelHandle);
            Error.Errors.ThrowError (ReturnCode);

            return new Channel.Channel (ChannelHandle);
        }
Beispiel #35
0
 public Channel.Channel PlayDsp(Dsp.Dsp dsp)
 {
     return PlayDsp (dsp, false);
 }
Beispiel #36
0
 private static extern Error.Code CreateDspByType(IntPtr system, Dsp.Type type, ref IntPtr dsp);
Beispiel #37
0
 private static extern Error.Code CreateDSP(IntPtr system, ref Dsp.Description description, ref IntPtr dsp);
Beispiel #38
0
 private static extern Error.Code GetSpectrum(IntPtr channel, [MarshalAs(UnmanagedType.LPArray)] float[] spectrumarray, int numvalues, int channeloffset, Dsp.FFTWindow windowtype);
Beispiel #39
0
 public void GetSpectrum(float[] spectrumarray, int numvalues, int channeloffset, Dsp.FFTWindow windowtype)
 {
     GetSpectrum(this.DangerousGetHandle(), spectrumarray, numvalues, channeloffset, windowtype);
 }
Beispiel #40
0
 public float[] GetSpectrum(int numvalues, int channeloffset, Dsp.FFTWindow windowtype)
 {
     float[] SpectrumArray = new float[numvalues];
     this.GetSpectrum (SpectrumArray, numvalues, channeloffset, windowtype);
     return SpectrumArray;
 }
Beispiel #41
0
        public void PlayDsp(Dsp.Dsp dsp, bool paused, Channel.Channel chn)
        {
            IntPtr channel = chn.DangerousGetHandle ();

            Error.Code ReturnCode = PlayDsp (this.DangerousGetHandle (), Channel.Index.Reuse, dsp.DangerousGetHandle (), paused, ref channel);
            Error.Errors.ThrowError (ReturnCode);

            //This can't really happend.
            //Check just in case...
            if(chn.DangerousGetHandle () != channel)
                throw new Exception("Channel handle got changed by Fmod.");
        }