语音转文字支持从手机选择文件
authorliangzhenying <867174124@qq.com>
Sat, 3 Aug 2024 03:04:55 +0000 (11:04 +0800)
committerliangzhenying <867174124@qq.com>
Sat, 3 Aug 2024 03:04:55 +0000 (11:04 +0800)
MixSdk/Example/Example/Sbc2PcmViewController.swift
MixSdk/Example/Example/TtsFileViewController.swift
MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
MixSdk/MixSdk/MixSdk/MixSdk.swift

index 8cfefbc..8e2dee8 100644 (file)
@@ -11,6 +11,7 @@ import SnapKit
 import Toast_Swift
 class Sbc2PcmViewController: UIViewController{
     let textView = UITextView()
+    var sbcPath:String?
     
     override func viewDidLoad() {
         super.viewDidLoad()
@@ -21,6 +22,7 @@ class Sbc2PcmViewController: UIViewController{
     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)
@@ -50,13 +52,16 @@ class Sbc2PcmViewController: UIViewController{
                 //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)
             }
@@ -66,6 +71,11 @@ class Sbc2PcmViewController: UIViewController{
 
 extension Sbc2PcmViewController{
     @objc func recordAction(button:UIButton){
+        if sbcPath == nil{
+            self.view.makeToast("请先选择sbc文件")
+            return
+        }
+        
         button.isSelected = !button.isSelected
         if button.isSelected {
             //1
@@ -78,12 +88,13 @@ extension Sbc2PcmViewController{
     }
     //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(){
@@ -100,3 +111,33 @@ extension Sbc2PcmViewController{
    }
 }
 
+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)
+    }
+    
+}
index 102fb1c..3834abf 100644 (file)
@@ -44,25 +44,48 @@ class SttFileViewController: UIViewController{
             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}
@@ -79,3 +102,13 @@ extension SttFileViewController{
         }
     }
 }
+
+
+extension SttFileViewController:UIDocumentPickerDelegate{
+    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
+        if let url = urls.first {
+            selectedFile(fileURL:url)
+        }
+        controller.dismiss(animated: true, completion: nil)
+    }
+}
index 6f74e5e..99af428 100644 (file)
Binary files a/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MixSdk/MixSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ
index fa9bf98..351d240 100644 (file)
@@ -137,7 +137,7 @@ public class MixSdk:NSObject {
     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 }
@@ -180,8 +180,6 @@ public class MixSdk:NSObject {
     /// - 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
@@ -201,30 +199,7 @@ public class MixSdk:NSObject {
 //    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()