//private static int Main(string[] args) //{ // var parameters = new RunParameters(); // parameters.TempoDelta = 50; // parameters.PitchDelta = 5; // parameters.Speech = true; // var inBytes = System.IO.File.ReadAllBytes("Test.wav"); // var inStream = new MemoryStream(inBytes); // var outStream = new MemoryStream(); // Process(inStream, outStream, parameters); // var outBytes = outStream.ToArray(); // File.WriteAllBytes("Test_Pith_Tempo_ms.wav", outBytes); // return 0; //} public static void Process(Stream inStream, Stream outStream, RunParameters parameters) { var soundTouch = new SoundTouch <TSampleType, TLongSampleType>(); // Open Input file. var inFile = new WavInFile(inStream); //var inFile = new WavInFile("Test.wav"); int bits = inFile.GetNumBits(); int samplerate = inFile.GetSampleRate(); int channels = inFile.GetNumChannels(); // Open output file. var outFile = new WavOutFile(outStream, samplerate, bits, channels); //var outFile = new WavOutFile("Test_Pith_Tempo.wav", samplerate, bits, channels); if (parameters.DetectBpm) { // detect sound BPM (and adjust processing parameters // accordingly if necessary) DetectBpm(inFile, parameters); } // Setup the 'SoundTouch' object for processing the sound int sampleRate = inFile.GetSampleRate(); soundTouch.SetSampleRate(sampleRate); soundTouch.SetChannels(channels); soundTouch.SetTempoChange(parameters.TempoDelta); soundTouch.SetPitchSemiTones(parameters.PitchDelta); soundTouch.SetRateChange(parameters.RateDelta); soundTouch.SetSetting(SettingId.UseQuickseek, parameters.Quick); soundTouch.SetSetting(SettingId.UseAntiAliasFilter, (parameters.NoAntiAlias == 1) ? 0 : 1); if (parameters.Speech) { // use settings for speech processing soundTouch.SetSetting(SettingId.SequenceDurationMs, 40); soundTouch.SetSetting(SettingId.SeekwindowDurationMs, 15); soundTouch.SetSetting(SettingId.OverlapDurationMs, 8); } // Process the sound Process(soundTouch, inFile, outFile); if (inFile != null) { inFile.Dispose(); } if (outFile != null) { outFile.Dispose(); } }
private static void OpenFiles(out WavInFile inFile, out WavOutFile outFile, RunParameters parameters) { inFile = parameters.InFileName == "stdin" ? new WavInFile(Console.OpenStandardInput()) : new WavInFile(parameters.InFileName); // ... open output file with same sound parameters int bits = (inFile).GetNumBits(); int samplerate = (inFile).GetSampleRate(); int channels = (inFile).GetNumChannels(); if (parameters.OutFileName != null) { outFile = parameters.OutFileName == "stdout" ? new WavOutFile(Console.OpenStandardOutput(), samplerate, bits, channels) : new WavOutFile(parameters.OutFileName, samplerate, bits, channels); } else { outFile = null; } }
/// <summary> /// Processes the sound. /// </summary> private static void Process(SoundTouch <TSampleType, TLongSampleType> pSoundTouch, WavInFile inFile, WavOutFile outFile) { int nSamples; var sampleBuffer = new TSampleType[BUFF_SIZE]; if ((inFile == null) || (outFile == null)) { return; // nothing to do. } int nChannels = inFile.GetNumChannels(); Debug.Assert(nChannels > 0); int buffSizeSamples = BUFF_SIZE / nChannels; // Process samples read from the input file while (!inFile.Eof()) { // Read a chunk of samples from the input file int num = inFile.Read(sampleBuffer, BUFF_SIZE); nSamples = num / inFile.GetNumChannels(); // Feed the samples into SoundTouch processor pSoundTouch.PutSamples(sampleBuffer, nSamples); // Read ready samples from SoundTouch processor & write them output file. // NOTES: // - 'receiveSamples' doesn't necessarily return any samples at all // during some rounds! // - On the other hand, during some round 'receiveSamples' may have more // ready samples than would fit into 'sampleBuffer', and for this reason // the 'receiveSamples' call is iterated for as many times as it // outputs samples. do { nSamples = pSoundTouch.ReceiveSamples(sampleBuffer, buffSizeSamples); outFile.Write(sampleBuffer, nSamples * nChannels); } while (nSamples != 0); } // Now the input file is processed, yet 'flush' few last samples that are // hiding in the SoundTouch's internal processing pipeline. pSoundTouch.Flush(); do { nSamples = pSoundTouch.ReceiveSamples(sampleBuffer, buffSizeSamples); outFile.Write(sampleBuffer, nSamples * nChannels); } while (nSamples != 0); }