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); }