|
@@ -12,47 +12,46 @@ import Foundation
|
|
|
class BFVoiceRecordManager: NSObject {
|
|
|
// 录音相关
|
|
|
var audioRecorder: BFRecorderManager?
|
|
|
- //录音结束回调
|
|
|
+ // 录音结束回调
|
|
|
var endRecordHandle: ((PQVoiceModel?, Error?) -> Void)?
|
|
|
- //录音取消回调
|
|
|
+ // 录音取消回调
|
|
|
var cancelRecordHandle: ((PQVoiceModel?) -> Void)?
|
|
|
- //录音进度回调
|
|
|
+ // 录音进度回调
|
|
|
var recorderProgrossHandle: ((Float64) -> Void)?
|
|
|
- //字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
|
|
|
- var subtitleRecordHandle: ((String?,String?) -> Void)?
|
|
|
-
|
|
|
- //字幕服务 dubug信息
|
|
|
+ // 字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
|
|
|
+ var subtitleRecordHandle: ((String?, String?) -> Void)?
|
|
|
+
|
|
|
+ // 字幕服务 dubug信息
|
|
|
var NeoNuiDebugHandle: ((String?) -> Void)?
|
|
|
- //录音机 dubug信息
|
|
|
+ // 录音机 dubug信息
|
|
|
var AudioQueueRecoderDebugHandle: ((String?) -> Void)?
|
|
|
-
|
|
|
- //开始录制时间
|
|
|
+
|
|
|
+ // 开始录制时间
|
|
|
var beginRecordTime: Date = Date()
|
|
|
- //音频文件模型
|
|
|
+ // 音频文件模型
|
|
|
var voiceModel: PQVoiceModel?
|
|
|
- //停止是否为取消操作
|
|
|
- var mIsCancel:Bool = false
|
|
|
- //是否正在停止中,从调用停止方法,到真正停止结束大约要300ms 左右。在这个期间内如果调用在调用了 start 开始时间被重置, 就会触发< 1s的逻辑。
|
|
|
- var isStoping:Bool = false
|
|
|
+ // 停止是否为取消操作
|
|
|
+ var mIsCancel: Bool = false
|
|
|
+ // 是否正在停止中,从调用停止方法,到真正停止结束大约要300ms 左右。在这个期间内如果调用在调用了 start 开始时间被重置, 就会触发< 1s的逻辑。
|
|
|
+ var isStoping: Bool = false
|
|
|
/// 初始化方法
|
|
|
/// - Parameters:
|
|
|
/// - token: NLS token
|
|
|
/// - appid: NLS appid
|
|
|
- public override init() {
|
|
|
+ override public init() {
|
|
|
super.init()
|
|
|
audioRecorder = BFRecorderManager()
|
|
|
audioRecorder?.delegate = self
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// 开始录音
|
|
|
func startRecord() {
|
|
|
-
|
|
|
- if(isStoping){
|
|
|
+ if isStoping {
|
|
|
BFLog(2, message: "正在停止中,这次开始无效.")
|
|
|
return
|
|
|
}
|
|
|
var recorderFilePath = exportAudiosDirectory
|
|
|
-
|
|
|
+
|
|
|
if !directoryIsExists(dicPath: recorderFilePath) {
|
|
|
BFLog(message: "文件夹不存在 \(recorderFilePath)")
|
|
|
createDirectory(path: recorderFilePath)
|
|
@@ -61,9 +60,8 @@ class BFVoiceRecordManager: NSObject {
|
|
|
beginRecordTime = Date()
|
|
|
BFLog(1, message: "开始录音 \(recorderFilePath) 开始时间\(beginRecordTime)")
|
|
|
audioRecorder?.startRecord(recorderFilePath)
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/// 停止录制
|
|
|
/// - Parameter isCancel: 是否为取消 ,取消操作会把录制的文件删除和字幕删除
|
|
|
func stopRecord(isCancel: Bool) {
|
|
@@ -74,43 +72,44 @@ class BFVoiceRecordManager: NSObject {
|
|
|
}
|
|
|
|
|
|
// MARK: - 录音机回调
|
|
|
+
|
|
|
extension BFVoiceRecordManager: BFRecorderManagerDelegate {
|
|
|
public func recorderProgress(_: BFRecorderManager, recoderTime: Double) {
|
|
|
BFLog(message: "录音机进度:\(recoderTime)")
|
|
|
recorderProgrossHandle?(recoderTime)
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public func recorderDidStop(_ outfile: String) {
|
|
|
let duration = Date().timeIntervalSince(beginRecordTime)
|
|
|
BFLog(1, message: "开始录音结束时间\(Date()) 时长\(duration)")
|
|
|
if duration > 1 {
|
|
|
-
|
|
|
- if(mIsCancel){
|
|
|
+ if mIsCancel {
|
|
|
// 删除录制的原文件
|
|
|
- do {
|
|
|
- try FileManager.default.removeItem(atPath: outfile)
|
|
|
- print("取消后删除文件Success to remove recorder file. \(outfile)")
|
|
|
- } catch {
|
|
|
- print("取消后删除文件Failed to remove recorder file. \(outfile)")
|
|
|
+ if FileManager.default.fileExists(atPath: outfile) {
|
|
|
+ do {
|
|
|
+ try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outfile))
|
|
|
+ } catch {
|
|
|
+ BFLog(message: "删除文件出错 == \(error) \(outfile)")
|
|
|
+ }
|
|
|
}
|
|
|
cancelRecordHandle?(nil)
|
|
|
- }else{
|
|
|
-
|
|
|
- var beginRecordTime1 = Date()
|
|
|
-
|
|
|
- //1转wav
|
|
|
+ } else {
|
|
|
+ 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)")
|
|
|
+ BFLog(message: "转 WAV用时\(Date().timeIntervalSince(beginRecordTime1))")
|
|
|
+ // 删除录制的原文件
|
|
|
+ if FileManager.default.fileExists(atPath: outfile) {
|
|
|
+ do {
|
|
|
+ try FileManager.default.removeItem(at: NSURL.fileURL(withPath: outfile))
|
|
|
+ } catch {
|
|
|
+ BFLog(message: "删除文件出错 == \(error) \(outfile)")
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- //2处理降噪
|
|
|
+
|
|
|
+ // 2处理降噪
|
|
|
beginRecordTime1 = Date()
|
|
|
let noiseFilePath = wavFilePath.replacingOccurrences(of: ".wav", with: "_noise.wav")
|
|
|
BFLog(1, message: "降噪后地址:\(noiseFilePath) 原地址:\(wavFilePath)")
|
|
@@ -119,40 +118,40 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
|
|
|
model.wavFilePath = noiseFilePath
|
|
|
model.duration = "\(duration)"
|
|
|
endRecordHandle?(model, nil)
|
|
|
-
|
|
|
- BFLog(message: "降噪用时\( Date().timeIntervalSince(beginRecordTime1))")
|
|
|
-
|
|
|
+
|
|
|
+ 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)")
|
|
|
+ if FileManager.default.fileExists(atPath: wavFilePath) {
|
|
|
+ do {
|
|
|
+ try FileManager.default.removeItem(at: NSURL.fileURL(withPath: wavFilePath))
|
|
|
+ } catch {
|
|
|
+ BFLog(message: "删除文件出错 == \(error) \(wavFilePath)")
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
cShowHUB(superView: nil, msg: "最短录制1秒")
|
|
|
cancelRecordHandle?(voiceModel)
|
|
|
}
|
|
|
-
|
|
|
- //其它逻辑写在上面 保证最后关开关。
|
|
|
+
|
|
|
+ // 其它逻辑写在上面 保证最后关开关。
|
|
|
isStoping = false
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public func eventCallback(_: BFRecorderManager, asrResult: String,audioFilePath:String) {
|
|
|
- //最后输出的文件是降噪后的
|
|
|
+ public func eventCallback(_: BFRecorderManager, asrResult: String, audioFilePath: String) {
|
|
|
+ // 最后输出的文件是降噪后的
|
|
|
let noiseFilePath = audioFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
|
|
|
subtitleRecordHandle?(asrResult, noiseFilePath)
|
|
|
}
|
|
|
- public func neoNuiDebugHandle(_ msg:String){
|
|
|
+
|
|
|
+ public func neoNuiDebugHandle(_ msg: String) {
|
|
|
BFLog(2, message: "neoNuiDebugHandle :\(msg)")
|
|
|
NeoNuiDebugHandle?(msg)
|
|
|
}
|
|
|
- public func audioQueueRecoderDebugHandle(_ msg:String){
|
|
|
+
|
|
|
+ public func audioQueueRecoderDebugHandle(_ msg: String) {
|
|
|
BFLog(2, message: "audioQueueRecoderDebugHandle :\(msg)")
|
|
|
AudioQueueRecoderDebugHandle?(msg)
|
|
|
}
|