-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ArkTS API for speaker identification
- Loading branch information
1 parent
198a381
commit 8fb3d00
Showing
7 changed files
with
188 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
harmony-os/SherpaOnnxHar/sherpa_onnx/src/main/ets/components/SpeakerIdentification.ets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { | ||
createSpeakerEmbeddingExtractor, | ||
createSpeakerEmbeddingManager, | ||
speakerEmbeddingExtractorComputeEmbedding, | ||
speakerEmbeddingExtractorCreateStream, | ||
speakerEmbeddingExtractorDim, | ||
speakerEmbeddingExtractorIsReady, | ||
speakerEmbeddingManagerAdd, | ||
speakerEmbeddingManagerAddListFlattened, | ||
speakerEmbeddingManagerContains, | ||
speakerEmbeddingManagerGetAllSpeakers, | ||
speakerEmbeddingManagerNumSpeakers, | ||
speakerEmbeddingManagerRemove, | ||
speakerEmbeddingManagerSearch, | ||
speakerEmbeddingManagerVerify | ||
} from 'libsherpa_onnx.so'; | ||
import { OnlineStream } from './StreamingAsr'; | ||
|
||
export class SpeakerEmbeddingExtractorConfig { | ||
public model: string = ''; | ||
public numThreads: number = 1; | ||
public debug: boolean = false; | ||
public provider: string = 'cpu'; | ||
} | ||
|
||
export class SpeakerEmbeddingExtractor { | ||
public config: SpeakerEmbeddingExtractorConfig = new SpeakerEmbeddingExtractorConfig(); | ||
public dim: number; | ||
private handle: object; | ||
|
||
constructor(config: SpeakerEmbeddingExtractorConfig, mgr?: object) { | ||
this.handle = createSpeakerEmbeddingExtractor(config, mgr); | ||
this.config = config; | ||
this.dim = speakerEmbeddingExtractorDim(this.handle); | ||
} | ||
|
||
createStream(): OnlineStream { | ||
return new OnlineStream( | ||
speakerEmbeddingExtractorCreateStream(this.handle)); | ||
} | ||
|
||
isReady(stream: OnlineStream): boolean { | ||
return speakerEmbeddingExtractorIsReady(this.handle, stream.handle); | ||
} | ||
|
||
compute(stream: OnlineStream, enableExternalBuffer: boolean = true): Float32Array { | ||
return speakerEmbeddingExtractorComputeEmbedding( | ||
this.handle, stream.handle, enableExternalBuffer); | ||
} | ||
} | ||
|
||
function flatten(arrayList: Float32Array[]): Float32Array { | ||
let n = 0; | ||
for (let i = 0; i < arrayList.length; ++i) { | ||
n += arrayList[i].length; | ||
} | ||
let ans = new Float32Array(n); | ||
|
||
let offset = 0; | ||
for (let i = 0; i < arrayList.length; ++i) { | ||
ans.set(arrayList[i], offset); | ||
offset += arrayList[i].length; | ||
} | ||
return ans; | ||
} | ||
|
||
interface SpeakerNameWithEmbedding { | ||
name: string; | ||
v: Float32Array; | ||
} | ||
|
||
interface SpeakerNameWithEmbeddingList { | ||
name: string; | ||
v: Float32Array[]; | ||
} | ||
|
||
interface SpeakerNameWithEmbeddingN { | ||
name: string; | ||
vv: Float32Array; | ||
n: number; | ||
} | ||
|
||
interface EmbeddingWithThreshold { | ||
v: Float32Array; | ||
threshold: number; | ||
} | ||
|
||
interface SpeakerNameEmbeddingThreshold { | ||
name: string; | ||
v: Float32Array; | ||
threshold: number; | ||
} | ||
|
||
export class SpeakerEmbeddingManager { | ||
public dim: number; | ||
private handle: object; | ||
|
||
constructor(dim: number) { | ||
this.handle = createSpeakerEmbeddingManager(dim); | ||
this.dim = dim; | ||
} | ||
|
||
add(speaker: SpeakerNameWithEmbedding): boolean { | ||
return speakerEmbeddingManagerAdd(this.handle, speaker); | ||
} | ||
|
||
addMulti(speaker: SpeakerNameWithEmbeddingList): boolean { | ||
const c: SpeakerNameWithEmbeddingN = { | ||
name: speaker.name, | ||
vv: flatten(speaker.v), | ||
n: speaker.v.length, | ||
}; | ||
return speakerEmbeddingManagerAddListFlattened(this.handle, c); | ||
} | ||
|
||
remove(name: string): boolean { | ||
return speakerEmbeddingManagerRemove(this.handle, name); | ||
} | ||
|
||
search(obj: EmbeddingWithThreshold): string { | ||
return speakerEmbeddingManagerSearch(this.handle, obj); | ||
} | ||
|
||
verify(obj: SpeakerNameEmbeddingThreshold): boolean { | ||
return speakerEmbeddingManagerVerify(this.handle, obj); | ||
} | ||
|
||
contains(name: string): boolean { | ||
return speakerEmbeddingManagerContains(this.handle, name); | ||
} | ||
|
||
getNumSpeakers(): number { | ||
return speakerEmbeddingManagerNumSpeakers(this.handle); | ||
} | ||
|
||
getAllSpeakerNames(): string[] { | ||
return speakerEmbeddingManagerGetAllSpeakers(this.handle); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters