jsonwang 3 лет назад
Родитель
Сommit
c0d6836810
1 измененных файлов с 35 добавлено и 15 удалено
  1. 35 15
      BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

+ 35 - 15
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -46,7 +46,7 @@ class BFVoiceRecordManager: NSObject {
             BFLog(message: "文件夹不存在 \(recorderFilePath)")
             createDirectory(path: recorderFilePath)
         }
-        recorderFilePath.append("recorder_\(Date().timeIntervalSince1970).wav")
+        recorderFilePath.append("recorder_\(Date().timeIntervalSince1970).pcm")
         BFLog(1, message: "开始录音 \(recorderFilePath)")
         
         BFLog(1, message: "开始录制")
@@ -85,33 +85,53 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
                 cancelRecordHandle?(nil)
             }else{
                 
-                // 处理降噪
-                let noiseFilePath = outfile.replacingOccurrences(of: ".wav", with: "_noise.wav")
-                BFLog(1, message: "降噪后地址:\(noiseFilePath) 原地址:\(outfile)")
-                NXNoiseReduction().denoise(outfile, outFile: noiseFilePath)
+                var  beginRecordTime1 = Date()
+               
+                //1转wav
+                let wavFilePath = outfile.replacingOccurrences(of: ".pcm", with: ".wav")
+                BFPcmToWaveTool().pcmToWav(inFileName: outfile, outFileName: wavFilePath)
+                BFLog(message: "转 WAV用时\( Date().timeIntervalSince(beginRecordTime1))")
+                // 删除录制的pcm文件
+                do {
+                    try FileManager.default.removeItem(atPath: outfile)
+                    print("Success to remove recorder file. \(outfile)")
+                } catch {
+                    print("Failed to remove recorder file. \(outfile)")
+                }
+                
+                //2处理降噪
+                beginRecordTime1 = Date()
+                let noiseFilePath = wavFilePath.replacingOccurrences(of: ".wav", with: "_noise.wav")
+                BFLog(1, message: "降噪后地址:\(noiseFilePath) 原地址:\(wavFilePath)")
+                NXNoiseReduction().denoise(wavFilePath, outFile: noiseFilePath)
                 if let model = voiceModel {
-                    model.wavFilePath = outfile
+                    model.wavFilePath = noiseFilePath
                     model.duration = "\(duration)"
                     endRecordHandle?(model, nil)
                     
+                    BFLog(message: "降噪用时\( Date().timeIntervalSince(beginRecordTime1))")
+                    
+                }
+                // 删除临时 wav 文件
+                do {
+                    try FileManager.default.removeItem(atPath: wavFilePath)
+                    print("Success to remove recorder file. \(wavFilePath)")
+                } catch {
+                    print("Failed to remove recorder file. \(wavFilePath)")
                 }
                 
-                //            // 删除录制的原文件
-                //            do {
-                //                try FileManager.default.removeItem(atPath: outfile)
-                //                print("Success to remove recorder file. \(outfile)")
-                //            } catch {
-                //                print("Failed to remove recorder file. \(outfile)")
-                //            }
+
             }
             
         } else {
-            cShowHUB(superView: nil, msg: "说话时间太短")
+            cShowHUB(superView: nil, msg: "最短录制1秒")
             cancelRecordHandle?(voiceModel)
         }
     }
     
     public func eventCallback(_: BFRecorderManager, asrResult: String,audioFilePath:String) {
-        subtitleRecordHandle?(asrResult, audioFilePath)
+        //最后输出的文件是降噪后的
+        let noiseFilePath = audioFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
+        subtitleRecordHandle?(asrResult, noiseFilePath)
     }
 }