Committed by
GitHub
Initialize the audio session for iOS ASR example (#1786)
Fixes #1784
正在显示
1 个修改的文件
包含
26 行增加
和
7 行删除
| @@ -5,8 +5,8 @@ | @@ -5,8 +5,8 @@ | ||
| 5 | // Created by knight on 2023/4/5. | 5 | // Created by knight on 2023/4/5. |
| 6 | // | 6 | // |
| 7 | 7 | ||
| 8 | -import Foundation | ||
| 9 | import AVFoundation | 8 | import AVFoundation |
| 9 | +import Foundation | ||
| 10 | 10 | ||
| 11 | enum Status { | 11 | enum Status { |
| 12 | case stop | 12 | case stop |
| @@ -22,6 +22,7 @@ class SherpaOnnxViewModel: ObservableObject { | @@ -22,6 +22,7 @@ class SherpaOnnxViewModel: ObservableObject { | ||
| 22 | 22 | ||
| 23 | var audioEngine: AVAudioEngine? = nil | 23 | var audioEngine: AVAudioEngine? = nil |
| 24 | var recognizer: SherpaOnnxRecognizer! = nil | 24 | var recognizer: SherpaOnnxRecognizer! = nil |
| 25 | + private var audioSession: AVAudioSession! | ||
| 25 | 26 | ||
| 26 | var lastSentence: String = "" | 27 | var lastSentence: String = "" |
| 27 | let maxSentence: Int = 20 | 28 | let maxSentence: Int = 20 |
| @@ -36,11 +37,16 @@ class SherpaOnnxViewModel: ObservableObject { | @@ -36,11 +37,16 @@ class SherpaOnnxViewModel: ObservableObject { | ||
| 36 | 37 | ||
| 37 | let start = max(sentences.count - maxSentence, 0) | 38 | let start = max(sentences.count - maxSentence, 0) |
| 38 | if lastSentence.isEmpty { | 39 | if lastSentence.isEmpty { |
| 39 | - return sentences.enumerated().map { (index, s) in "\(index): \(s.lowercased())" }[start...] | 40 | + return sentences.enumerated().map { (index, s) in |
| 41 | + "\(index): \(s.lowercased())" | ||
| 42 | + }[start...] | ||
| 40 | .joined(separator: "\n") | 43 | .joined(separator: "\n") |
| 41 | } else { | 44 | } else { |
| 42 | - return sentences.enumerated().map { (index, s) in "\(index): \(s.lowercased())" }[start...] | ||
| 43 | - .joined(separator: "\n") + "\n\(sentences.count): \(lastSentence.lowercased())" | 45 | + return sentences.enumerated().map { (index, s) in |
| 46 | + "\(index): \(s.lowercased())" | ||
| 47 | + }[start...] | ||
| 48 | + .joined(separator: "\n") | ||
| 49 | + + "\n\(sentences.count): \(lastSentence.lowercased())" | ||
| 44 | } | 50 | } |
| 45 | } | 51 | } |
| 46 | 52 | ||
| @@ -48,8 +54,20 @@ class SherpaOnnxViewModel: ObservableObject { | @@ -48,8 +54,20 @@ class SherpaOnnxViewModel: ObservableObject { | ||
| 48 | self.subtitles = self.results | 54 | self.subtitles = self.results |
| 49 | } | 55 | } |
| 50 | 56 | ||
| 57 | + func setupAudioSession() { | ||
| 58 | + audioSession = AVAudioSession.sharedInstance() | ||
| 59 | + do { | ||
| 60 | + try audioSession.setCategory( | ||
| 61 | + .playAndRecord, mode: .default, options: [.defaultToSpeaker]) | ||
| 62 | + try audioSession.setActive(true) | ||
| 63 | + } catch { | ||
| 64 | + print("Failed to set up audio session: \(error)") | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + | ||
| 51 | init() { | 68 | init() { |
| 52 | initRecognizer() | 69 | initRecognizer() |
| 70 | + setupAudioSession() | ||
| 53 | initRecorder() | 71 | initRecorder() |
| 54 | } | 72 | } |
| 55 | 73 | ||
| @@ -129,7 +147,7 @@ class SherpaOnnxViewModel: ObservableObject { | @@ -129,7 +147,7 @@ class SherpaOnnxViewModel: ObservableObject { | ||
| 129 | let array = convertedBuffer.array() | 147 | let array = convertedBuffer.array() |
| 130 | if !array.isEmpty { | 148 | if !array.isEmpty { |
| 131 | self.recognizer.acceptWaveform(samples: array) | 149 | self.recognizer.acceptWaveform(samples: array) |
| 132 | - while (self.recognizer.isReady()){ | 150 | + while self.recognizer.isReady() { |
| 133 | self.recognizer.decode() | 151 | self.recognizer.decode() |
| 134 | } | 152 | } |
| 135 | let isEndpoint = self.recognizer.isEndpoint() | 153 | let isEndpoint = self.recognizer.isEndpoint() |
| @@ -141,7 +159,7 @@ class SherpaOnnxViewModel: ObservableObject { | @@ -141,7 +159,7 @@ class SherpaOnnxViewModel: ObservableObject { | ||
| 141 | print(text) | 159 | print(text) |
| 142 | } | 160 | } |
| 143 | 161 | ||
| 144 | - if isEndpoint{ | 162 | + if isEndpoint { |
| 145 | if !text.isEmpty { | 163 | if !text.isEmpty { |
| 146 | let tmp = self.lastSentence | 164 | let tmp = self.lastSentence |
| 147 | self.lastSentence = "" | 165 | self.lastSentence = "" |
| @@ -170,7 +188,8 @@ class SherpaOnnxViewModel: ObservableObject { | @@ -170,7 +188,8 @@ class SherpaOnnxViewModel: ObservableObject { | ||
| 170 | do { | 188 | do { |
| 171 | try self.audioEngine?.start() | 189 | try self.audioEngine?.start() |
| 172 | } catch let error as NSError { | 190 | } catch let error as NSError { |
| 173 | - print("Got an error starting audioEngine: \(error.domain), \(error)") | 191 | + print( |
| 192 | + "Got an error starting audioEngine: \(error.domain), \(error)") | ||
| 174 | } | 193 | } |
| 175 | print("started") | 194 | print("started") |
| 176 | } | 195 | } |
-
请 注册 或 登录 后发表评论