lookatstudy-termux-voice 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/vad.js ADDED
@@ -0,0 +1,134 @@
1
+ /** @typedef {import('./types').CircularBufferHandle} CircularBufferHandle */
2
+ /** @typedef {import('./types').VoiceActivityDetectorHandle} VoiceActivityDetectorHandle */
3
+ /** @typedef {import('./types').SpeechSegment} SpeechSegment */
4
+ /** @typedef {import('./types').VadConfig} VadConfig */
5
+
6
+ const addon = require('./addon.js');
7
+
8
+ /**
9
+ * CircularBuffer stores float32 samples internally.
10
+ */
11
+ class CircularBuffer {
12
+ /**
13
+ * @param {number} capacity - capacity in samples (integer)
14
+ */
15
+ constructor(capacity) {
16
+ this.handle = addon.createCircularBuffer(capacity);
17
+ }
18
+
19
+ /**
20
+ * Push samples into the buffer.
21
+ * @param {Float32Array} samples
22
+ */
23
+ push(samples) {
24
+ addon.circularBufferPush(this.handle, samples);
25
+ }
26
+
27
+ /**
28
+ * Get a slice of samples.
29
+ * @param {number} startIndex
30
+ * @param {number} n
31
+ * @param {boolean} [enableExternalBuffer=true]
32
+ * @returns {Float32Array}
33
+ */
34
+ get(startIndex, n, enableExternalBuffer = true) {
35
+ return addon.circularBufferGet(
36
+ this.handle, startIndex, n, enableExternalBuffer);
37
+ }
38
+
39
+ /**
40
+ * Pop n samples from the buffer.
41
+ * @param {number} n
42
+ */
43
+ pop(n) {
44
+ return addon.circularBufferPop(this.handle, n);
45
+ }
46
+
47
+ /**
48
+ * Get current size in samples.
49
+ * @returns {number}
50
+ */
51
+ size() {
52
+ return addon.circularBufferSize(this.handle);
53
+ }
54
+
55
+ /**
56
+ * Get head index.
57
+ * @returns {number}
58
+ */
59
+ head() {
60
+ return addon.circularBufferHead(this.handle);
61
+ }
62
+
63
+ /** Reset the buffer. */
64
+ reset() {
65
+ addon.circularBufferReset(this.handle);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Voice Activity Detector (VAD).
71
+ */
72
+ class Vad {
73
+ /**
74
+ * @param {VadConfig} config
75
+ * @param {number} bufferSizeInSeconds
76
+ */
77
+ constructor(config, bufferSizeInSeconds) {
78
+ this.handle =
79
+ addon.createVoiceActivityDetector(config, bufferSizeInSeconds);
80
+ this.config = config;
81
+ }
82
+
83
+ /**
84
+ * Accept raw waveform samples.
85
+ * @param {Float32Array} samples
86
+ */
87
+ acceptWaveform(samples) {
88
+ addon.voiceActivityDetectorAcceptWaveform(this.handle, samples);
89
+ }
90
+
91
+ /** @returns {boolean} */
92
+ isEmpty() {
93
+ return addon.voiceActivityDetectorIsEmpty(this.handle);
94
+ }
95
+
96
+ /** @returns {boolean} */
97
+ isDetected() {
98
+ return addon.voiceActivityDetectorIsDetected(this.handle);
99
+ }
100
+
101
+ /** Pop the earliest detected speech segment. */
102
+ pop() {
103
+ addon.voiceActivityDetectorPop(this.handle);
104
+ }
105
+
106
+ /** Clear internal state. */
107
+ clear() {
108
+ addon.voiceActivityDetectorClear(this.handle);
109
+ }
110
+
111
+ /**
112
+ * Get the front speech segment.
113
+ * @param {boolean} [enableExternalBuffer=true]
114
+ * @returns {SpeechSegment}
115
+ */
116
+ front(enableExternalBuffer = true) {
117
+ return addon.voiceActivityDetectorFront(this.handle, enableExternalBuffer);
118
+ }
119
+
120
+ /** Reset detector state. */
121
+ reset() {
122
+ addon.voiceActivityDetectorReset(this.handle);
123
+ }
124
+
125
+ /** Flush pending internal buffer. */
126
+ flush() {
127
+ addon.voiceActivityDetectorFlush(this.handle);
128
+ }
129
+ }
130
+
131
+ module.exports = {
132
+ Vad,
133
+ CircularBuffer,
134
+ }