Add vad clear api for better performance (#366)
* Add vad clear api for better performance * rename to make naming consistent and remove macro * Fix linker error * Fix Vad.kt
This commit is contained in:
@@ -551,7 +551,7 @@ class SherpaOnnxVoiceActivityDetectorWrapper {
|
||||
return SherpaOnnxVoiceActivityDetectorEmpty(vad) == 1
|
||||
}
|
||||
|
||||
func isDetected() -> Bool {
|
||||
func isSpeechDetected() -> Bool {
|
||||
return SherpaOnnxVoiceActivityDetectorDetected(vad) == 1
|
||||
}
|
||||
|
||||
@@ -559,6 +559,10 @@ class SherpaOnnxVoiceActivityDetectorWrapper {
|
||||
SherpaOnnxVoiceActivityDetectorPop(vad)
|
||||
}
|
||||
|
||||
func clear() {
|
||||
SherpaOnnxVoiceActivityDetectorClear(vad)
|
||||
}
|
||||
|
||||
func front() -> SherpaOnnxSpeechSegmentWrapper {
|
||||
let p: UnsafePointer<SherpaOnnxSpeechSegment>? = SherpaOnnxVoiceActivityDetectorFront(vad)
|
||||
return SherpaOnnxSpeechSegmentWrapper(p: p)
|
||||
|
||||
@@ -174,32 +174,31 @@ func run() {
|
||||
|
||||
var segments: [SpeechSegment] = []
|
||||
|
||||
while array.count > windowSize {
|
||||
// todo(fangjun): avoid extra copies here
|
||||
vad.acceptWaveform(samples: [Float](array[0..<windowSize]))
|
||||
array = [Float](array[windowSize..<array.count])
|
||||
|
||||
while !vad.isEmpty() {
|
||||
let s = vad.front()
|
||||
vad.pop()
|
||||
let result = recognizer.decode(samples: s.samples)
|
||||
|
||||
segments.append(
|
||||
SpeechSegment(
|
||||
start: Float(s.start) / Float(sampleRate),
|
||||
duration: Float(s.samples.count) / Float(sampleRate),
|
||||
text: result.text))
|
||||
|
||||
print(segments.last!)
|
||||
|
||||
}
|
||||
for offset in stride(from: 0, to: array.count, by: windowSize) {
|
||||
let end = min(offset + windowSize, array.count)
|
||||
vad.acceptWaveform(samples: [Float](array[offset ..< end]))
|
||||
}
|
||||
|
||||
let srt = zip(segments.indices, segments).map { (index, element) in
|
||||
var index: Int = 0
|
||||
while !vad.isEmpty() {
|
||||
let s = vad.front()
|
||||
vad.pop()
|
||||
let result = recognizer.decode(samples: s.samples)
|
||||
|
||||
segments.append(
|
||||
SpeechSegment(
|
||||
start: Float(s.start) / Float(sampleRate),
|
||||
duration: Float(s.samples.count) / Float(sampleRate),
|
||||
text: result.text))
|
||||
|
||||
print(segments.last!)
|
||||
}
|
||||
|
||||
let srt: String = zip(segments.indices, segments).map { (index, element) in
|
||||
return "\(index+1)\n\(element)"
|
||||
}.joined(separator: "\n\n")
|
||||
|
||||
let srtFilename = filePath.stringByDeletingPathExtension + ".srt"
|
||||
let srtFilename: String = filePath.stringByDeletingPathExtension + ".srt"
|
||||
do {
|
||||
try srt.write(to: srtFilename.fileURL, atomically: true, encoding: .utf8)
|
||||
} catch {
|
||||
|
||||
Reference in New Issue
Block a user