forked from k2-fsa/sherpa-onnx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspoken-language-identification.swift
57 lines (45 loc) · 1.49 KB
/
spoken-language-identification.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import AVFoundation
extension AudioBuffer {
func array() -> [Float] {
return Array(UnsafeBufferPointer(self))
}
}
extension AVAudioPCMBuffer {
func array() -> [Float] {
return self.audioBufferList.pointee.mBuffers.array()
}
}
func run() {
let encoder = "./sherpa-onnx-whisper-tiny/tiny-encoder.int8.onnx"
let decoder = "./sherpa-onnx-whisper-tiny/tiny-decoder.int8.onnx"
let whisperConfig = sherpaOnnxSpokenLanguageIdentificationWhisperConfig(
encoder: encoder,
decoder: decoder
)
var config = sherpaOnnxSpokenLanguageIdentificationConfig(
whisper: whisperConfig,
numThreads: 1,
debug: 1,
provider: "cpu"
)
let filePath = "./sherpa-onnx-whisper-tiny/test_wavs/0.wav"
let slid = SherpaOnnxSpokenLanguageIdentificationWrapper(config: &config)
let fileURL: NSURL = NSURL(fileURLWithPath: filePath)
let audioFile = try! AVAudioFile(forReading: fileURL as URL)
let audioFormat = audioFile.processingFormat
assert(audioFormat.sampleRate == 16000)
assert(audioFormat.channelCount == 1)
assert(audioFormat.commonFormat == AVAudioCommonFormat.pcmFormatFloat32)
let audioFrameCount = UInt32(audioFile.length)
let audioFileBuffer = AVAudioPCMBuffer(pcmFormat: audioFormat, frameCapacity: audioFrameCount)
try! audioFile.read(into: audioFileBuffer!)
let array: [Float]! = audioFileBuffer?.array()
let result = slid.decode(samples: array)
print("\nDetectedllanguage is:\n\(result.lang)")
}
@main
struct App {
static func main() {
run()
}
}