BFVoiceRecordManager.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // BFVoiceRecordManager.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by 胡志强 on 2021/11/24.
  6. //
  7. import BFCommonKit
  8. import BFMediaKit
  9. import Foundation
  10. class BFVoiceRecordManager: NSObject {
  11. let debugHeader:String = "debugHeaderManger"
  12. // 录音相关
  13. var audioRecorder: BFRecorderManager?
  14. // 录音结束回调
  15. var endRecordHandle: ((PQVoiceModel?, Error?) -> Void)?
  16. // 录音取消回调
  17. var cancelRecordHandle: ((PQVoiceModel?) -> Void)?
  18. // 录音进度回调
  19. var recorderProgrossHandle: ((Float64) -> Void)?
  20. // 字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
  21. var subtitleRecordHandle: ((String?, String?) -> Void)?
  22. // 字幕服务 dubug信息
  23. var NeoNuiDebugHandle: ((String?) -> Void)?
  24. // 录音机 dubug信息
  25. var AudioQueueRecoderDebugHandle: ((String?) -> Void)?
  26. // 音频文件模型
  27. var voiceModel: PQVoiceModel?
  28. // 停止是否为取消操作
  29. var mIsCancel: Bool = false
  30. // 是否正在停止中,从调用停止方法,到真正停止结束大约要300ms 左右。在这个期间内如果调用在调用了 start 开始时间被重置, 就会触发< 1s的逻辑。
  31. var isStoping: Bool = false
  32. /// 初始化方法
  33. /// - Parameters:
  34. /// - token: NLS token
  35. /// - appid: NLS appid
  36. override public init() {
  37. super.init()
  38. audioRecorder = BFRecorderManager()
  39. audioRecorder?.delegate = self
  40. }
  41. deinit{
  42. audioRecorder?.delegate = nil
  43. }
  44. /// 开始录音
  45. func startRecord() {
  46. BFLog(2, message: "\(debugHeader)开始录音::: \(Date().timeIntervalSince1970)")
  47. if isStoping {
  48. AudioQueueRecoderDebugHandle?("正在停止中,这次开始无效.")
  49. BFLog(2, message: "\(debugHeader)正在停止中,这次开始无效.")
  50. return
  51. }
  52. var recorderFilePath = exportAudiosDirectory
  53. if !directoryIsExists(dicPath: recorderFilePath) {
  54. BFLog(message: "\(debugHeader)文件夹不存在 \(recorderFilePath)")
  55. createDirectory(path: recorderFilePath)
  56. }
  57. recorderFilePath.append("recorder_\(Date().timeIntervalSince1970).pcm")
  58. let noiseFilePath = recorderFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
  59. voiceModel?.wavFilePath = noiseFilePath
  60. BFLog(1, message: "\(debugHeader)开始录音::: \(recorderFilePath)")
  61. audioRecorder?.startRecord(recorderFilePath)
  62. }
  63. func cancelTitleService() {
  64. audioRecorder?.stopNui_dialog(true)
  65. isStoping = false
  66. }
  67. /// 停止录制
  68. /// - Parameter isCancel: 是否为取消 ,取消操作会把录制的文件删除和字幕删除
  69. func stopRecord(isCancel: Bool) {
  70. BFLog(2, message: "\(debugHeader)结束录音::: \(Date().timeIntervalSince1970)")
  71. isStoping = true
  72. mIsCancel = isCancel
  73. //停止录音机录音
  74. audioRecorder?.voiceRecorder.stop(true)
  75. //停止字幕服务
  76. audioRecorder?.stopNui_dialog(false)
  77. }
  78. }
  79. // MARK: - 录音机回调
  80. extension BFVoiceRecordManager: BFRecorderManagerDelegate {
  81. public func recorderProgress(_: BFRecorderManager, recoderTime: Double) {
  82. // BFLog(message: "录音机进度:\(recoderTime)")
  83. recorderProgrossHandle?(recoderTime)
  84. }
  85. public func recorderDidStop(_ outfile: String) {
  86. if mIsCancel {
  87. // 删除录制的原文件
  88. deleteFile(outfile: outfile)
  89. cancelRecordHandle?(nil)
  90. } else {
  91. var beginRecordTime1 = Date()
  92. // 1转wav
  93. let wavFilePath = outfile.replacingOccurrences(of: ".pcm", with: ".wav")
  94. BFPcmToWaveTool().pcmToWav(inFileName: outfile, outFileName: wavFilePath)
  95. BFLog(message: "\(debugHeader)转 WAV用时\(Date().timeIntervalSince(beginRecordTime1))")
  96. // 删除录制的原文件
  97. deleteFile(outfile: outfile)
  98. // 2处理降噪
  99. beginRecordTime1 = Date()
  100. let noiseFilePath = wavFilePath.replacingOccurrences(of: ".wav", with: "_noise.wav")
  101. BFLog(1, message: "\(debugHeader)降噪后地址:\(noiseFilePath) 原地址:\(wavFilePath)")
  102. NXNoiseReduction().denoise(wavFilePath, outFile: noiseFilePath)
  103. if let model = voiceModel {
  104. model.wavFilePath = noiseFilePath
  105. endRecordHandle?(model, nil)
  106. BFLog(message: "\(debugHeader)降噪用时\(Date().timeIntervalSince(beginRecordTime1))")
  107. }
  108. // 删除临时 wav 文件
  109. deleteFile(outfile: wavFilePath)
  110. }
  111. // 其它逻辑写在上面 保证最后关开关。
  112. isStoping = false
  113. }
  114. /// 删除文件
  115. /// - Parameter outfile: <#outfile description#>
  116. public func deleteFile(outfile:String) {
  117. if FileManager.default.fileExists(atPath: outfile) {
  118. do {
  119. try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outfile))
  120. } catch {
  121. BFLog(message: "删除文件出错 == \(error) \(outfile)")
  122. }
  123. }
  124. }
  125. //字幕返回
  126. public func eventCallback(_: BFRecorderManager, asrResult: String, audioFilePath: String) {
  127. // 最后输出的文件是降噪后的
  128. let noiseFilePath = audioFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
  129. subtitleRecordHandle?(asrResult, noiseFilePath)
  130. }
  131. public func neoNuiDebugHandle(_ msg: String) {
  132. BFLog(2, message: "neoNuiDebugHandle :\(msg)")
  133. NeoNuiDebugHandle?(msg)
  134. }
  135. public func audioQueueRecoderDebugHandle(_ msg: String) {
  136. BFLog(2, message: "audioQueueRecoderDebugHandle :\(msg)")
  137. AudioQueueRecoderDebugHandle?(msg)
  138. }
  139. }