@@ -5,8 +5,8 @@
|
|||||||
// Created by knight on 2023/4/5.
|
// Created by knight on 2023/4/5.
|
||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
|
||||||
import AVFoundation
|
import AVFoundation
|
||||||
|
import Foundation
|
||||||
|
|
||||||
enum Status {
|
enum Status {
|
||||||
case stop
|
case stop
|
||||||
@@ -22,6 +22,7 @@ class SherpaOnnxViewModel: ObservableObject {
|
|||||||
|
|
||||||
var audioEngine: AVAudioEngine? = nil
|
var audioEngine: AVAudioEngine? = nil
|
||||||
var recognizer: SherpaOnnxRecognizer! = nil
|
var recognizer: SherpaOnnxRecognizer! = nil
|
||||||
|
private var audioSession: AVAudioSession!
|
||||||
|
|
||||||
var lastSentence: String = ""
|
var lastSentence: String = ""
|
||||||
let maxSentence: Int = 20
|
let maxSentence: Int = 20
|
||||||
@@ -36,11 +37,16 @@ class SherpaOnnxViewModel: ObservableObject {
|
|||||||
|
|
||||||
let start = max(sentences.count - maxSentence, 0)
|
let start = max(sentences.count - maxSentence, 0)
|
||||||
if lastSentence.isEmpty {
|
if lastSentence.isEmpty {
|
||||||
return sentences.enumerated().map { (index, s) in "\(index): \(s.lowercased())" }[start...]
|
return sentences.enumerated().map { (index, s) in
|
||||||
.joined(separator: "\n")
|
"\(index): \(s.lowercased())"
|
||||||
|
}[start...]
|
||||||
|
.joined(separator: "\n")
|
||||||
} else {
|
} else {
|
||||||
return sentences.enumerated().map { (index, s) in "\(index): \(s.lowercased())" }[start...]
|
return sentences.enumerated().map { (index, s) in
|
||||||
.joined(separator: "\n") + "\n\(sentences.count): \(lastSentence.lowercased())"
|
"\(index): \(s.lowercased())"
|
||||||
|
}[start...]
|
||||||
|
.joined(separator: "\n")
|
||||||
|
+ "\n\(sentences.count): \(lastSentence.lowercased())"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,8 +54,20 @@ class SherpaOnnxViewModel: ObservableObject {
|
|||||||
self.subtitles = self.results
|
self.subtitles = self.results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupAudioSession() {
|
||||||
|
audioSession = AVAudioSession.sharedInstance()
|
||||||
|
do {
|
||||||
|
try audioSession.setCategory(
|
||||||
|
.playAndRecord, mode: .default, options: [.defaultToSpeaker])
|
||||||
|
try audioSession.setActive(true)
|
||||||
|
} catch {
|
||||||
|
print("Failed to set up audio session: \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
initRecognizer()
|
initRecognizer()
|
||||||
|
setupAudioSession()
|
||||||
initRecorder()
|
initRecorder()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,8 +134,8 @@ class SherpaOnnxViewModel: ObservableObject {
|
|||||||
pcmFormat: outputFormat,
|
pcmFormat: outputFormat,
|
||||||
frameCapacity:
|
frameCapacity:
|
||||||
AVAudioFrameCount(outputFormat.sampleRate)
|
AVAudioFrameCount(outputFormat.sampleRate)
|
||||||
* buffer.frameLength
|
* buffer.frameLength
|
||||||
/ AVAudioFrameCount(buffer.format.sampleRate))!
|
/ AVAudioFrameCount(buffer.format.sampleRate))!
|
||||||
|
|
||||||
var error: NSError?
|
var error: NSError?
|
||||||
let _ = converter.convert(
|
let _ = converter.convert(
|
||||||
@@ -129,7 +147,7 @@ class SherpaOnnxViewModel: ObservableObject {
|
|||||||
let array = convertedBuffer.array()
|
let array = convertedBuffer.array()
|
||||||
if !array.isEmpty {
|
if !array.isEmpty {
|
||||||
self.recognizer.acceptWaveform(samples: array)
|
self.recognizer.acceptWaveform(samples: array)
|
||||||
while (self.recognizer.isReady()){
|
while self.recognizer.isReady() {
|
||||||
self.recognizer.decode()
|
self.recognizer.decode()
|
||||||
}
|
}
|
||||||
let isEndpoint = self.recognizer.isEndpoint()
|
let isEndpoint = self.recognizer.isEndpoint()
|
||||||
@@ -141,7 +159,7 @@ class SherpaOnnxViewModel: ObservableObject {
|
|||||||
print(text)
|
print(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
if isEndpoint{
|
if isEndpoint {
|
||||||
if !text.isEmpty {
|
if !text.isEmpty {
|
||||||
let tmp = self.lastSentence
|
let tmp = self.lastSentence
|
||||||
self.lastSentence = ""
|
self.lastSentence = ""
|
||||||
@@ -170,7 +188,8 @@ class SherpaOnnxViewModel: ObservableObject {
|
|||||||
do {
|
do {
|
||||||
try self.audioEngine?.start()
|
try self.audioEngine?.start()
|
||||||
} catch let error as NSError {
|
} catch let error as NSError {
|
||||||
print("Got an error starting audioEngine: \(error.domain), \(error)")
|
print(
|
||||||
|
"Got an error starting audioEngine: \(error.domain), \(error)")
|
||||||
}
|
}
|
||||||
print("started")
|
print("started")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user