File size: 3,063 Bytes
b50f2a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * This file is part of TEN Framework, an open source project.
 * Licensed under the Apache License, Version 2.0.
 * See the LICENSE file for more information.
 * 
 * TEN VAD (Voice Activity Detection) WebAssembly Module
 * TypeScript type definitions
 */

export interface TenVADModule {
  /**
   * Create and initialize a VAD instance
   * @param handlePtr Pointer to store the VAD handle
   * @param hopSize Number of samples between consecutive analysis frames (e.g., 256)
   * @param threshold VAD detection threshold [0.0, 1.0]
   * @returns 0 on success, -1 on error
   */
  _ten_vad_create(handlePtr: number, hopSize: number, threshold: number): number;

  /**
   * Process audio frame for voice activity detection
   * @param handle Valid VAD handle from ten_vad_create
   * @param audioDataPtr Pointer to int16 audio samples array
   * @param audioDataLength Length of audio data (should equal hopSize)
   * @param outProbabilityPtr Pointer to output probability [0.0, 1.0]
   * @param outFlagPtr Pointer to output flag (0: no voice, 1: voice detected)
   * @returns 0 on success, -1 on error
   */
  _ten_vad_process(
    handle: number,
    audioDataPtr: number,
    audioDataLength: number,
    outProbabilityPtr: number,
    outFlagPtr: number
  ): number;

  /**
   * Destroy VAD instance and release resources
   * @param handlePtr Pointer to the VAD handle
   * @returns 0 on success, -1 on error
   */
  _ten_vad_destroy(handlePtr: number): number;

  /**
   * Get library version string
   * @returns Version string pointer
   */
  _ten_vad_get_version(): number;

  // WebAssembly Memory Management
  _malloc(size: number): number;
  _free(ptr: number): void;

  // Memory access helpers
  HEAP16: Int16Array;
  HEAPF32: Float32Array;
  HEAP32: Int32Array;
  HEAPU8: Uint8Array;

  // Value access methods
  getValue(ptr: number, type: 'i8' | 'i16' | 'i32' | 'float' | 'double'): number;
  setValue(ptr: number, value: number, type: 'i8' | 'i16' | 'i32' | 'float' | 'double'): void;

  // String utilities
  UTF8ToString(ptr: number): string;
  lengthBytesUTF8(str: string): number;
  stringToUTF8(str: string, outPtr: number, maxBytesToWrite: number): void;
}

/**
 * High-level TypeScript wrapper for TEN VAD
 */
export class TenVAD {
  private module: TenVADModule;
  private handle: number | null;
  private hopSize: number;

  constructor(module: TenVADModule, hopSize: number, threshold: number);

  /**
   * Process audio samples for voice activity detection
   * @param audioData Int16Array of audio samples (length must equal hopSize)
   * @returns Object with probability and voice detection flag
   */
  process(audioData: Int16Array): {
    probability: number;
    isVoice: boolean;
  } | null;

  /**
   * Get library version
   */
  getVersion(): string;

  /**
   * Destroy VAD instance
   */
  destroy(): void;

  /**
   * Check if VAD instance is valid
   */
  isValid(): boolean;
}

/**
 * Create TEN VAD WebAssembly module
 */
declare function createVADModule(): Promise<TenVADModule>;

export default createVADModule;