/// <summary> /// Detect BPM rate of <paramref name="inFile"/> and adjust tempo /// setting accordingly if necessary. /// </summary> private static void DetectBpm(WavInFile inFile, RunParameters parameters) { var bpm = BpmDetect <TSampleType, TLongSampleType> .NewInstance(inFile.GetNumChannels(), inFile.GetSampleRate()); var sampleBuffer = new TSampleType[BUFF_SIZE]; // detect bpm rate Console.Error.Write("Detecting BPM rate..."); Console.Error.Flush(); int nChannels = inFile.GetNumChannels(); Debug.Assert(BUFF_SIZE % nChannels == 0); // Process the 'inFile' in small blocks, repeat until whole file has // been processed while (inFile.Eof() == false) { // Read sample data from input file int num = inFile.Read(sampleBuffer, BUFF_SIZE); // Enter the new samples to the bpm analyzer class int samples = num / nChannels; bpm.InputSamples(sampleBuffer, samples); } // Now the whole song data has been analyzed. Read the resulting bpm. float bpmValue = bpm.GetBpm(); Console.Error.WriteLine("Done!"); // rewind the file after bpm detection inFile.Rewind(); if (bpmValue > 0) { Console.Error.WriteLine("Detected BPM rate {0:0.0}\n", bpmValue); } else { Console.Error.WriteLine("Couldn't detect BPM rate.\n"); return; } if (parameters.GoalBpm > 0) { // adjust tempo to given bpm parameters.TempoDelta = (parameters.GoalBpm / bpmValue - 1.0f) * 100.0f; Console.Error.WriteLine("The file will be converted to {0:0.0} BPM\n", parameters.GoalBpm); } }