//-------------对外接口------------------ public void initWithNew( string strAudioPath, string outPutPath, ProcessCb cb = null, CallBackZero finishCb = null) { /* * 如果文件夹已经存在则删除 */ if (Directory.Exists(strOutPutPath_ + "\\" + FLODER_NAME_FIRST_CUT)) { Directory.Delete(strOutPutPath_ + "\\" + FLODER_NAME_FIRST_CUT, true); } /* * 初始化 */ strAudioPath_ = strAudioPath; strOutPutPath_ = outPutPath; processCb_ = cb; finishCb_ = finishCb; listAudioParts_ = new List <AudioPart>(); initFolder(); initBuffer(strAudioPath); /* * 大规模工作集合 */ isProcessing_ = true; initListAudioPart(buffer_, onInitListAudioPartCallBack); }
public void Dispose() { corCut = null; corTrans = null; nowProcess_ = 0; finishCb_ = null; processCb_ = null; callBackTime = 0; AudioIndex = 0; buffer_ = null; listAudioParts_.Clear(); }
private void initListAudioPart(byte[] buffer, CallBackZero cb = null) { corCut = StartCoroutine(cutAudio(listAudioParts_, buffer, 4, cb)); }
private IEnumerator cutAudio(List <AudioPart> listAudioParts, byte[] buffer, int cutTime, CallBackZero cb = null) { short[] intBuffer = new short[buffer.Length / 2]; int index = 0; byte[] arr = new byte[2]; float batchTime = 100000.0f; for (long i = 0; i < buffer.Length - 1; ++i) { if (i % 2 == 1) { arr[0] = buffer[i - 1]; arr[1] = buffer[i]; intBuffer[index] = BitConverter.ToInt16(arr, 0); index++; } /* * 更新进度 */ if (i % (int)batchTime == 0) { NowProcess += batchTime / (float)(buffer.Length - 1) * 0.2f; yield return(new WaitForEndOfFrame()); } } NowProcess = 0.2f; index = 0; long dotOneIndex = 0; long value = 0; long[] testDotOneTimeValue = new long[buffer.Length / (2 * SAMPLE_RATE / 10)]; for (long i = 0; i < intBuffer.Length; ++i) { value += Math.Abs(intBuffer[i]); index++; if (index >= SAMPLE_RATE / 10) { index = 0; testDotOneTimeValue[dotOneIndex] = value; value = 0; dotOneIndex++; } if (i % (int)batchTime == 0) { NowProcess += batchTime / (float)(intBuffer.Length) * 0.2f; yield return(new WaitForEndOfFrame()); } } NowProcess = 0.4f; /* * 裁剪音频 */ bool isOnRecorder = false; int lowerTime = 0; long markStartIndex = 0; long markEndIndex = 0; for (long i = 0; i < testDotOneTimeValue.Length; ++i) { if (isOnRecorder == false) { if (testDotOneTimeValue[i] > LOWER_VOICE) { markStartIndex = i; isOnRecorder = true; } } else { if (testDotOneTimeValue[i] < LOWER_VOICE) { lowerTime++; if (lowerTime >= cutTime || i >= testDotOneTimeValue.Length - 1) { /*打断*/ isOnRecorder = false; lowerTime = 0; markEndIndex = i; cutAudioByDotOneTime(buffer, markStartIndex, markEndIndex, listAudioParts); } } else { /* * 出现回升就打断 */ lowerTime = 0; } } if (i % (int)batchTime == 0) { NowProcess += batchTime / (float)(testDotOneTimeValue.Length) * 0.2f; yield return(new WaitForEndOfFrame()); } } NowProcess = 0.5f; if (cb != null) { cb(); } }