//using System; //using System.Collections; //using System.Collections.Generic; //using System.Data; //using System.Diagnostics; //using NAudio.Wave; ///// ///// Author: Freefall ///// Date: 05.08.16 ///// Based on: the port of Stephan M. Bernsee“s pitch shifting class ///// Port site: https://sites.google.com/site/mikescoderama/pitch-shifting ///// Test application and github site: https://github.com/Freefall63/NAudio-Pitchshifter ///// ///// UPDATE: Added a simple Limiter based on the pydirac implementation. ///// https://github.com/echonest/remix/blob/master/external/pydirac225/source/Dirac_LE.cpp ///// //public class SMBPitchShiftingSampleProvider : ISampleProvider //{ // //Shifter objects // private ISampleProvider SourceStream = null; // private WaveFormat WFormat = null; // private float Pitch = 1f; // private int _FFTSize; // private long _osamp; // private SMBPitchShifter ShifterLeft = new SMBPitchShifter(); // private SMBPitchShifter ShifterRight = new SMBPitchShifter(); // public SMBPitchShiftingSampleProvider(ISampleProvider SourceProvider) : this(SourceProvider, 4096, 4L, 1f) { } // public SMBPitchShiftingSampleProvider(ISampleProvider SourceProvider, int FFTSize, long osamp, float InitialPitch) // { // SourceStream = SourceProvider; // WFormat = SourceProvider.WaveFormat; // _FFTSize = FFTSize; // _osamp = osamp; // PitchFactor = InitialPitch; // } // public int Read(float[] buffer, int offset, int count) // { // int SampRead = SourceStream.Read(buffer, offset, count); // if (Pitch == 1f) //Nothing to do. // return SampRead; // if (WFormat.Channels == 1) { // float[] Mono = new float[SampRead]; // int index = 0; // for (int sample = offset; sample <= SampRead + offset - 1; sample++) // { // Mono[index] = buffer[sample]; // index += 1; // } // ShifterLeft.PitchShift(Pitch, SampRead, _FFTSize, _osamp, WFormat.SampleRate, Mono); // index = 0; // for (int sample = offset; sample <= SampRead + offset - 1; sample++) // { // buffer[sample] = ShifterLeft.Limiter(Mono[index]); // index += 1; // } // return SampRead; // } // //else if (WFormat.Channels == 2) // //{ // // float[] Left = new float[(SampRead >> 1)]; // // float[] Right = new float[(SampRead >> 1)]; // // int index = 0; // // for (int sample = offset; sample <= SampRead + offset - 1; sample += 2) { // // Left[index] = buffer[sample]; // // Right[index] = buffer[sample + 1]; // // index += 1; // // } // // ShifterLeft.PitchShift(Pitch, SampRead >> 1, _FFTSize, _osamp, WFormat.SampleRate, Left); // // ShifterRight.PitchShift(Pitch, SampRead >> 1, _FFTSize, _osamp, WFormat.SampleRate, Right); // // index = 0; // // for (int sample = offset; sample <= SampRead + offset - 1; sample += 2) { // // //buffer[sample] = ShifterLeft.Limiter(Left[index]); // // //buffer[sample + 1] = ShifterLeft.Limiter(Right[index]); // // buffer[sample] = Limiter(Left[index]); // // buffer[sample + 1] = Limiter(Right[index]); // // index += 1; // // } // // return SampRead; // //} else { // // throw new Exception("Shifting of more than 2 channels is currently not supported."); // //} // } // public NAudio.Wave.WaveFormat WaveFormat { get { return WFormat; }} // public float PitchFactor { get { return Pitch; } set { Pitch = value; } } // //Limiter constants // const float LIM_THRESH = 0.95f; // const float LIM_RANGE = (1f - LIM_THRESH); // const float M_PI_2 = (float)(Math.PI / 2); // private float Limiter(float Sample) // { // float res = 0f; // if ((LIM_THRESH < Sample)) // { // res = (Sample - LIM_THRESH) / LIM_RANGE; // res = (float)((Math.Atan(res) / M_PI_2) * LIM_RANGE + LIM_THRESH); // } // else if ((Sample < -LIM_THRESH)) // { // res = -(Sample + LIM_THRESH) / LIM_RANGE; // res = -(float)((Math.Atan(res) / M_PI_2) * LIM_RANGE + LIM_THRESH); // } // else // res = Sample; // return res; // } // //private void trackBar1_Scroll(object sender, EventArgs e) // //{ // // if (trackBar1.Value > 11) // // { // // Pitch = (float)((trackBar1.Value - 1) / 10f); // // } // // else if (trackBar1.Value < 11) // // { // // Pitch = (float)(((trackBar1.Value - 1) / 10f * 0.5f) + 0.5f); // // } // // else // // { // // Pitch = 1f; // // } // // button1.Text = Pitch.ToString(); // // if (!Object.ReferenceEquals(null, SMB)) // // { // // SMB.PitchFactor = Pitch; // // } // //} //}