private unsafe static void testPinnedArray() { //to enable fast access to the elements of the array (without bound checking) an PinnedArray<> is created. //you can emulate it by using GCHandle, but this is more convenient way :) int[] arr = new int[] { 1, 2, 3, 4, 5 }; var pinnedArr = new PinnedArray <int>(arr); var data = (int *)pinnedArr.Data; data[1] = 500; pinnedArr.Dispose(); Console.WriteLine("Changed value: " + arr[1]); //discover more properties and extensions!!! }
public double[] FFT(double[] data) { System.Numerics.Complex[] fftComplex = new System.Numerics.Complex[data.Length]; System.Numerics.Complex[] output = new Complex[data.Length]; for (int i = 0; i < data.Length; i++) { fftComplex[i] = data[i]; } var pinIn = new PinnedArray <Complex>(fftComplex); var pinOut = new PinnedArray <Complex>(output); DFT.FFT(pinIn, pinOut); for (int i = 0; i < data.Length; i++) { data[i] = pinOut[i].Magnitude; } pinIn.Dispose(); pinOut.Dispose(); return(data); }
/// <summary> /// Disposes generic image. /// In case if data is allocated it is released. /// If data is shared parent reference (if exists) and parent handle (if exist) is released. /// </summary> public void Dispose() { if (isDisposed) { return; //if this function is called for the first time } if (IsAllocated) //must be disposed { buffer.Dispose(); buffer = null; } else { if (this.parentDestructor != null) { this.parentDestructor(objectReference); } this.parentDestructor = null; this.objectReference = null; } isDisposed = true; }
public void Dispose() { pinIn.Dispose(); pinIn2.Dispose(); pinOut.Dispose(); }
public static double[] ApplyDistortion(double[] samples, double distortionStrength, WaveFormat fmt) { List <double> processedSamples = new List <double>(); //list containing overlapping sound frames List <double> savedTimeDomain = new List <double>(SoundProcessing.Constants.FFT_FRAME_LENGTH); SoundProcessing.PadZeroes(savedTimeDomain, SoundProcessing.Constants.FFT_FRAME_LENGTH);//pad zeroes to have required container size //build a filter mask specified for the distortion double[] filterMask = FilterMasks.DistortionMask( SoundProcessing.Constants.FREQUENCY_DOMAIN_LENGTH, distortionStrength, fmt ); for (int i = 0; i < samples.Length; i += SoundProcessing.Constants.FFT_FRAME_LENGTH) { //cut out a data frame from samples var data = samples.Slice(i, SoundProcessing.Constants.FFT_LENGTH); //prepare a buffer for FFT var complices = new Complex[SoundProcessing.Constants.FREQUENCY_DOMAIN_LENGTH]; //arrays containing the input samples, the result of FFT and the result of IFFT PinnedArray <double> input = new PinnedArray <double>(data); PinnedArray <Complex> frequencyDomain = new PinnedArray <Complex>(complices); PinnedArray <double> timeDomain = new PinnedArray <double>(data.Length); //convert time domain to frequency domain DFT.FFT(input, frequencyDomain); //mask the frequencies using a mask created earlier for (int j = 0; j < frequencyDomain.Length; ++j) { frequencyDomain[j] *= filterMask[j]; } //mask negative frequencies for (int j = frequencyDomain.Length - 1; j > frequencyDomain.Length / 2; --j) { frequencyDomain[j] *= filterMask[frequencyDomain.Length - j]; } //convert back to time domain DFT.IFFT(frequencyDomain, timeDomain); //overlapping of the frames for (int j = 0; j < SoundProcessing.Constants.OVERLAP; ++j) { timeDomain[j] *= (j + 0.0) / SoundProcessing.Constants.OVERLAP; timeDomain[j] += savedTimeDomain[j] * (1.0 - (j + 0.00) / (double)SoundProcessing.Constants.OVERLAP); //overlap with previous samples to avoid stuttering } //add overlapping sound samples savedTimeDomain.Clear(); for (int j = SoundProcessing.Constants.FFT_FRAME_LENGTH; j < timeDomain.Length; ++j) { savedTimeDomain.Add(MathUtils.Clamp(timeDomain[j], -1.0, 1.0)); } //add the actual samples for (int j = 0; j < SoundProcessing.Constants.FFT_FRAME_LENGTH; ++j) { processedSamples.Add(MathUtils.Clamp(timeDomain[j], -1.0, 1.0)); } //dispose of the PinnedArray objects input.Dispose(); frequencyDomain.Dispose(); timeDomain.Dispose(); } return(processedSamples.ToArray()); }