import Toast_Swift
class Sbc2PcmViewController: UIViewController{
let textView = UITextView()
+ var sbcPath:String?
override func viewDidLoad() {
super.viewDidLoad()
func setupViews(){
self.title = "sbc2pcm"
self.view.backgroundColor = .white
+ self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "选择文件", style: .done, target: self, action: #selector(selectFileAction))
let recordBtn = UIButton(type: .custom)
recordBtn.setTitle("start record", for: .normal)
//2
self.startTask()
}
+ else if event.event == "Changed",let text = event.result?.result{
+ self.textView.text = text
+ }
else if event.event == "TranscriptionCompleted"{
//6
self.endTask()
}
else if event.event == "SentenceEnd",let text = event.result?.result{
//4
- self.textView.text += text + "\n"
+ self.textView.text = text
let range = NSMakeRange(textView.text.count - 1, 1)
self.textView.scrollRangeToVisible(range)
}
extension Sbc2PcmViewController{
@objc func recordAction(button:UIButton){
+ if sbcPath == nil{
+ self.view.makeToast("请先选择sbc文件")
+ return
+ }
+
button.isSelected = !button.isSelected
if button.isSelected {
//1
}
//3
@objc func startTask(){
- guard let path = Bundle.main.path(forResource: "AIAudio", ofType: "sbc") else {return}
+ self.textView.text = ""
+ guard let path = sbcPath else { return }
let data = readFile(from: path)!
-// MSAudioConvert.sbcTopcm(data){ pcm in
-// MixSdk.shared.stt.sendVoice(pcmData: pcm)
-// }
- MixSdk.shared.stt.sendVoice(sbcData: data)
+ MSAudioConvert.sbcTopcm(data){ pcm in
+ MixSdk.shared.stt.sendVoice(pcmData: pcm)
+ }
+// MixSdk.shared.stt.sendVoice(sbcData: data)
}
//7
@objc func endTask(){
}
}
+extension Sbc2PcmViewController{
+ @objc func selectFileAction(){
+ let documentTypes = [
+ "public.item",
+ ]
+ let document = UIDocumentPickerViewController.init(documentTypes: documentTypes, in: .import)
+ document.delegate = self
+ document.modalPresentationStyle = .fullScreen
+ self.present(document, animated:true, completion:nil)
+ }
+ func selectedFile(fileURL:URL){
+ if fileURL.pathExtension == "sbc"{
+ sbcPath = fileURL.path
+ }
+ else{
+ self.view.makeToast("该文件格式不支持")
+ return
+ }
+ }
+}
+
+extension Sbc2PcmViewController:UIDocumentPickerDelegate{
+ func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
+ if let url = urls.first {
+ selectedFile(fileURL:url)
+ }
+ controller.dismiss(animated: true, completion: nil)
+ }
+
+}
make.bottom.equalTo(fileBtn.snp.top)
}
}
-}
-
-
-extension SttFileViewController{
+
@objc func selectFileAction() {
-
+ let documentTypes = [
+ "public.item",
+ ]
+ let document = UIDocumentPickerViewController.init(documentTypes: documentTypes, in: .import)
+ document.delegate = self
+ document.modalPresentationStyle = .fullScreen
+ self.present(document, animated:true, completion:nil)
// guard let sbcPath = Bundle.main.path(forResource: "AIAudio", ofType: "sbc") else {return}
// let pcmPath = NSTemporaryDirectory() + "temp.pcm"
// let mp3Path = NSTemporaryDirectory() + "temp.mp3"
-//
+//
// var result1 = MSAudioConvert.sbcToPcm(sbcPath: sbcPath, pcmPath: pcmPath)
// var result2 = MSAudioConvert.sbcToMp3(sbcPath: sbcPath, mp3Path: mp3Path)
// print(result1)
// print(pcmPath)
-//
+//
// print(result2)
// print(mp3Path)
+ }
+
+ func selectedFile(fileURL:URL){
+ var pcmPath = ""
+ let filePath = fileURL.path
- guard let pcmPath = Bundle.main.path(forResource: "1", ofType: "pcm") else {return}
+ if fileURL.pathExtension == "pcm"{
+ pcmPath = filePath
+ }
+ else if fileURL.pathExtension == "sbc" {
+ pcmPath = NSTemporaryDirectory() + "temp.pcm"
+ let result = MSAudioConvert.sbcToPcm(sbcPath: filePath, pcmPath: pcmPath)
+ if result == false{
+ self.view.makeToast("无法识别该文件")
+ return
+ }
+ }
+ else{
+ self.view.makeToast("该文件格式不支持")
+ return
+ }
+
view.makeToastActivity(.center)
MixSdk.shared.stt.sentenceRecognize(filePath: pcmPath,format:"pcm",sample_rate:"16000",lang: "zh-cn") {[weak self] text, error in
guard let self = self else {return}
}
}
}
+
+
+extension SttFileViewController:UIDocumentPickerDelegate{
+ func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
+ if let url = urls.first {
+ selectedFile(fileURL:url)
+ }
+ controller.dismiss(animated: true, completion: nil)
+ }
+}
public var logs: [String] { Logger.logs }
/// SDK 版本号
- public let version: String = "1.0.12"
+ public let version: String = "1.0.13"
/// 默认为 false
public var isDebug: Bool = false {
didSet { Logger.isDebug = isDebug }
/// - Parameters:
/// - platform: 使用的平台
/// - key: key值
-// public func setPlatform(_ platform:MSPlatform,key:String? = nil,completeHandler: @escaping MSPlatformChangeCallBack){
-// changePlatformCallBack = completeHandler
public func setPlatform(_ platform:MSPlatform,key:String? = nil){
Logger.log("\nplatform = \(platform) \n key= \(key ?? "")")
aiPlatform = platform
// public lazy var auth:MSAuth = {
// return MSAuth()
// }()
-//
-// /// 翻译
-// public lazy var translate = MSTranslate()
-//
-// /// 人工智能AIGC
-// public lazy var aigc:MSAigc = {
-// return MSAigc()
-// }()
-//
-// /// 大模型对话
-// public lazy var chat:MSChat = {
-// return MSChat()
-// }()
-//
-// /// 文字转语音
-// public lazy var tts:MSTts = {
-// return MSTts()
-// }()
-//
-// /// 语音转文字
-// public lazy var stt:MSStt = {
-// return MSStt()
-// }()
-
+
func setupWebsoket(){
MixSdk.shared.stt.setupWebSocket()
MixSdk.shared.tts.setupWebSocket()