private void button1_Click(object sender, EventArgs e) { // int[] x = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // RMS = 6.20483682299543 // int[] x = new int[] { 10, 9, 8, 9, 10, 9, 8, 9, 10, 9, 8, 9, 10 }; // RMS = 9.10621089823187 // int[] x = new int[] { 10, 9, 8, 10, 9, 8, 10, 9, 8, 10, 9, 8 }; // RMS = 9.03696114115064 Int16[] x = new Int16[] { 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9 }; // RMS = 9 TextboxRmsValue.Text = (AudioMath.RootMeanSquare(x)).ToString(); }
private void DecodePcmAudio() { int bytesReadFromBuffer; int samplesReadFromBuffer; int totalSamplesReadFromBuffer = 0; int framesReadFromBuffer = 0; int totalBytesToRead = bytesPerSample * samplesTotal; int frameCount = samplesTotal / channelCount; int frameSizeBytes = bytesPerSample * channelCount; int frameWindowSize = 1024; int frameWindowIndex = 0; int framesRemaining; int dataIndex = 0; int numberOfSamplesToRead = frameWindowSize * channelCount; int dbfsValue; ISampleProvider decoder = new AudioFileReader(selectedFile); Log.Debug("Beginning decode of PCM audio"); if (CheckboxShowDsp.Checked == true) { float rmsValue = 0f; List <string> rmsList = new List <string>(); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); float[] sampleBuffer = new float[frameWindowSize * channelCount]; while ((totalSamplesReadFromBuffer = decoder.Read(sampleBuffer, 0, sampleBuffer.Length)) > 0) { if (totalSamplesReadFromBuffer < sampleBuffer.Length) // We must be at the end of the file then { rmsValue = AudioMath.RootMeanSquare(sampleBuffer, totalSamplesReadFromBuffer); } else { rmsValue = AudioMath.RootMeanSquare(sampleBuffer); } dbfsValue = AudioMath.ConvertToDbfs(rmsValue); #if DSP_USES_SIGNALS CanPlayAudio.WaitOne(); ThreadPool.QueueUserWorkItem(PlaybackAudioFromBuffer, sampleBuffer); #else PlaybackAudioFromBuffer(sampleBuffer); #endif long elapsedTime = stopWatch.ElapsedMilliseconds; string dbfsString = String.Format("FrameWindow: {0}, Time: {1}ms, dBFS: {2}", frameWindowIndex.ToString(), elapsedTime.ToString(), dbfsValue.ToString()); TextboxRmsValue.Text = dbfsString; rmsList.Add(dbfsString); if (CheckboxReverseList.Checked == true) { ListboxRmsList.Items.Insert(0, dbfsString); } else { ListboxRmsList.Items.Add(dbfsString); } frameWindowIndex++; #if DSP_FORCES_FORM_UPDATE this.Update(); #endif } stopWatch.Stop(); } else { if (outputDevice == null) { var trimmed = new OffsetSampleProvider(decoder) { // DelayBy = TimeSpan.FromSeconds(1), // LeadOut = TimeSpan.FromSeconds(1), // SkipOverSamples = 4096 * 4 }; outputDevice = new WaveOutEvent(); outputDevice.DesiredLatency = 600; outputDevice.PlaybackStopped += OnPlaybackStopped; outputDevice.Init(trimmed); outputDevice.Play(); } } Log.Debug("Finished decode of PCM audio"); }