private void ApplyPhraseDetectionOnTheFly(AudioLib.AudioRecorder.PcmDataBufferAvailableEventArgs e) { if (!m_Settings.Audio_EnableLivePhraseDetection) { return; //letsverify with on the fly phrase detection first } int overlapLength = 0; int msLentth = Convert.ToInt32(mRecorder.RecordingPCMFormat.SampleRate * mRecorder.RecordingPCMFormat.BlockAlign * 40); // 40 seconds if (mRecorder.RecordingPCMFormat != null) { //@overlap: overlapLength = Convert.ToInt32(0.250 * (mRecorder.RecordingPCMFormat.BlockAlign * mRecorder.RecordingPCMFormat.SampleRate)); //@overlap: if (e != null) overlapLength = (Math.Abs(overlapLength / e.PcmDataBufferLength) * e.PcmDataBufferLength); //msLentth = e != null? Convert.ToInt32( e.PcmDataBufferLength * 480): //m_MemStreamArray != null? m_MemStreamArray.Length: 0; } if ((m_MemStreamArray == null || e == null || m_PhDetectionMemStreamPosition > (msLentth - e.PcmDataBufferLength)) && mRecorder.RecordingPCMFormat != null) { //1 if (m_MemStreamArray != null) { //2 long threshold = (long)m_Settings.Audio_DefaultThreshold; long GapLength = (long)m_Settings.Audio_DefaultGap; long before = (long)m_Settings.Audio_DefaultLeadingSilence; //Console.WriteLine("on the fly ph detection parameters " + threshold + " : " + GapLength); AudioLib.AudioLibPCMFormat audioPCMFormat = new AudioLib.AudioLibPCMFormat(mRecorder.RecordingPCMFormat.NumberOfChannels, mRecorder.RecordingPCMFormat.SampleRate, mRecorder.RecordingPCMFormat.BitDepth); List <long> timingList = AudioLib.PhraseDetection.Apply(new System.IO.MemoryStream(m_MemStreamArray), audioPCMFormat, threshold, (long)GapLength * AudioLib.AudioLibPCMFormat.TIME_UNIT, (long)before * AudioLib.AudioLibPCMFormat.TIME_UNIT); if (timingList != null) {//3 //Console.WriteLine("timingList " + timingList.Count); double overlapTime = mRecorder.RecordingPCMFormat.ConvertBytesToTime(overlapLength); //System.Media.SystemSounds.Asterisk.Play(); foreach (double d in timingList) {//4 //Console.WriteLine("Overlap time and list time " + (overlapTime / AudioLibPCMFormat.TIME_UNIT) + " : " + (d / AudioLibPCMFormat.TIME_UNIT)); if (d >= overlapTime) {//5 double phraseTime = d - overlapTime; double timeInSession = (mRecorder.RecordingPCMFormat.ConvertBytesToTime(m_PhDetectorEstimatedBytesRecorded - msLentth) + d) / AudioLib.AudioLibPCMFormat.TIME_UNIT; //Console.WriteLine("phrase time: " + phraseTime + " : " + timeInSession); if (phraseTime != 0) { m_PhraseMarksOnTheFly.Add(timeInSession); } } //-5 } //-4 } //-3 else {//3 //Console.WriteLine("timing list is null "); } //-3 } //-2 : MemArray null check ends if (e == null) { return; } // continue with iteration at interval at which MemArray is complete byte[] overlapData = null; if (m_MemStreamArray != null) {//2 m_MemStreamArray = new byte[msLentth + overlapLength]; }//-2 else {//2 overlapLength = 0; m_MemStreamArray = new byte[msLentth]; }//-2 //Console.WriteLine("newMemStream length " + m_MemStreamArray.Length); //Console.WriteLine("Total bytes received from recorder: " + m_PhDetectorBytesReceivedFromRecorder); //Console.WriteLine("Recording bytes estimated: " + m_PhDetectorEstimatedBytesRecorded); long difference = m_PhDetectorEstimatedBytesRecorded - m_PhDetectorBytesReceivedFromRecorder; Console.WriteLine("Difference in bytes: " + difference + " in MS: " + (difference * 1000 / mRecorder.RecordingPCMFormat.ByteRate)); m_PhDetectionMemStreamPosition = overlapLength; if (m_PhDetectorEstimatedBytesRecorded != m_PhDetectorBytesReceivedFromRecorder) { m_PhDetectorEstimatedBytesRecorded = m_PhDetectorBytesReceivedFromRecorder; } m_PhDetectorEstimatedBytesRecorded += msLentth; //e.PcmDataBuffer.CopyTo(m_MemStreamArray, m_PhDetectionMemStreamPosition); Array.Copy(e.PcmDataBuffer, 0, m_MemStreamArray, m_PhDetectionMemStreamPosition, e.PcmDataBufferLength); m_PhDetectionMemStreamPosition += e.PcmDataBufferLength; //Console.WriteLine("first writing of recorder buffer " + m_PhDetectionMemStreamPosition); }//-1 else if (m_MemStreamArray != null && e.PcmDataBuffer != null) {//1 int leftOverLength = Convert.ToInt32(msLentth - m_PhDetectionMemStreamPosition); if (leftOverLength > e.PcmDataBufferLength) { leftOverLength = e.PcmDataBufferLength; } //Console.WriteLine("length:position:leftOver " + m_MemStreamArray.Length + " : " + m_PhDetectionMemStreamPosition + " : " + leftOverLength + " : " + e.PcmDataBuffer.Length); if (m_MemStreamArray.Length - m_PhDetectionMemStreamPosition > e.PcmDataBufferLength) { //e.PcmDataBuffer.CopyTo(m_MemStreamArray, m_PhDetectionMemStreamPosition); Array.Copy(e.PcmDataBuffer, 0, m_MemStreamArray, m_PhDetectionMemStreamPosition, e.PcmDataBufferLength); m_PhDetectionMemStreamPosition += e.PcmDataBufferLength; } else { m_PhDetectionMemStreamPosition = m_MemStreamArray.Length - 1; } //Console.WriteLine("writing recorder buffer " + m_PhDetectionMemStreamPosition); }//-1 if (e != null) { m_PhDetectorBytesReceivedFromRecorder += e.PcmDataBufferLength; } }
public void CopyFrom(AudioLibPCMFormat pcmFormat) { NumberOfChannels = pcmFormat.NumberOfChannels; SampleRate = pcmFormat.SampleRate; BitDepth = pcmFormat.BitDepth; }