Add Swift API for spoken language identification. (#696)

This commit is contained in:
Fangjun Kuang
2024-03-25 16:22:25 +08:00
committed by GitHub
parent ab7cff2513
commit 83a10a55a5
5 changed files with 180 additions and 0 deletions

View File

@@ -713,3 +713,86 @@ class SherpaOnnxOfflineTtsWrapper {
return SherpaOnnxGeneratedAudioWrapper(audio: audio)
}
}
// spoken language identification
func sherpaOnnxSpokenLanguageIdentificationWhisperConfig(
encoder: String,
decoder: String,
tailPaddings: Int = -1
) -> SherpaOnnxSpokenLanguageIdentificationWhisperConfig {
return SherpaOnnxSpokenLanguageIdentificationWhisperConfig(
encoder: toCPointer(encoder),
decoder: toCPointer(decoder),
tail_paddings: Int32(tailPaddings))
}
func sherpaOnnxSpokenLanguageIdentificationConfig(
whisper: SherpaOnnxSpokenLanguageIdentificationWhisperConfig,
numThreads: Int = 1,
debug: Int = 0,
provider: String = "cpu"
) -> SherpaOnnxSpokenLanguageIdentificationConfig {
return SherpaOnnxSpokenLanguageIdentificationConfig(
whisper: whisper,
num_threads: Int32(numThreads),
debug: Int32(debug),
provider: toCPointer(provider))
}
class SherpaOnnxSpokenLanguageIdentificationResultWrapper {
/// A pointer to the underlying counterpart in C
let result: UnsafePointer<SherpaOnnxSpokenLanguageIdentificationResult>!
/// Return the detected language.
/// en for English
/// zh for Chinese
/// es for Spanish
/// de for German
/// etc.
var lang: String {
return String(cString: result.pointee.lang)
}
init(result: UnsafePointer<SherpaOnnxSpokenLanguageIdentificationResult>!) {
self.result = result
}
deinit {
if let result {
SherpaOnnxDestroySpokenLanguageIdentificationResult(result)
}
}
}
class SherpaOnnxSpokenLanguageIdentificationWrapper {
/// A pointer to the underlying counterpart in C
let slid: OpaquePointer!
init(
config: UnsafePointer<SherpaOnnxSpokenLanguageIdentificationConfig>!
) {
slid = SherpaOnnxCreateSpokenLanguageIdentification(config)
}
deinit {
if let slid {
SherpaOnnxDestroySpokenLanguageIdentification(slid)
}
}
func decode(samples: [Float], sampleRate: Int = 16000)
-> SherpaOnnxSpokenLanguageIdentificationResultWrapper
{
let stream: OpaquePointer! = SherpaOnnxSpokenLanguageIdentificationCreateOfflineStream(slid)
AcceptWaveformOffline(stream, Int32(sampleRate), samples, Int32(samples.count))
let result: UnsafePointer<SherpaOnnxSpokenLanguageIdentificationResult>? =
SherpaOnnxSpokenLanguageIdentificationCompute(
slid,
stream)
DestroyOfflineStream(stream)
return SherpaOnnxSpokenLanguageIdentificationResultWrapper(result: result)
}
}