@pexip/media-processor 16.7.1 → 17.1.0

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.
Files changed (69) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/README.md +1 -7
  3. package/dist/main/audio.d.ts +123 -0
  4. package/dist/main/audio.js +653 -0
  5. package/dist/main/benchUtils.d.ts +12 -0
  6. package/dist/main/benchUtils.js +34 -0
  7. package/dist/main/generator.d.ts +4 -0
  8. package/dist/main/generator.js +4 -0
  9. package/dist/main/index.d.ts +17 -0
  10. package/dist/main/index.js +17 -0
  11. package/dist/main/math.d.ts +41 -0
  12. package/dist/main/math.js +57 -0
  13. package/dist/main/path.d.ts +102 -0
  14. package/dist/main/path.js +103 -0
  15. package/dist/main/process.d.ts +239 -0
  16. package/dist/main/process.js +364 -0
  17. package/dist/main/processor.d.ts +5 -0
  18. package/dist/main/processor.js +4 -0
  19. package/dist/main/transformer.d.ts +1 -0
  20. package/dist/main/transformer.js +4 -0
  21. package/dist/main/tsconfig.tsbuildinfo +1 -0
  22. package/dist/main/typeGuards.d.ts +5 -0
  23. package/dist/main/typeGuards.js +24 -0
  24. package/dist/main/types.d.ts +342 -0
  25. package/dist/main/types.js +1 -0
  26. package/dist/main/utils.d.ts +173 -0
  27. package/dist/main/utils.js +364 -0
  28. package/dist/main/video/canvasRenderUtils.d.ts +22 -0
  29. package/dist/main/video/canvasRenderUtils.js +198 -0
  30. package/dist/main/video/canvasTransform.d.ts +8 -0
  31. package/dist/main/video/canvasTransform.js +173 -0
  32. package/dist/main/video/constants.d.ts +10 -0
  33. package/dist/main/video/constants.js +12 -0
  34. package/dist/main/video/index.d.ts +9 -0
  35. package/dist/main/video/index.js +9 -0
  36. package/dist/main/video/load.d.ts +17 -0
  37. package/dist/main/video/load.js +47 -0
  38. package/dist/main/video/segmenters/index.d.ts +1 -0
  39. package/dist/main/video/segmenters/index.js +1 -0
  40. package/dist/main/video/segmenters/mediapipe.d.ts +13 -0
  41. package/dist/main/video/segmenters/mediapipe.js +85 -0
  42. package/dist/main/video/transformer.d.ts +10 -0
  43. package/dist/main/video/transformer.js +56 -0
  44. package/dist/main/video/typeGuards.d.ts +2 -0
  45. package/dist/main/video/typeGuards.js +13 -0
  46. package/dist/main/video/types.d.ts +98 -0
  47. package/dist/main/video/types.js +20 -0
  48. package/dist/main/video/utils.d.ts +102 -0
  49. package/dist/main/video/utils.js +476 -0
  50. package/dist/main/video/video.d.ts +19 -0
  51. package/dist/main/video/video.js +48 -0
  52. package/dist/main/video/videoStreamTrackProcessor.d.ts +14 -0
  53. package/dist/main/video/videoStreamTrackProcessor.js +82 -0
  54. package/dist/main/visual.d.ts +80 -0
  55. package/dist/main/visual.js +135 -0
  56. package/dist/main/workletNodes.d.ts +2 -0
  57. package/dist/main/workletNodes.js +3 -0
  58. package/dist/workers/index.d.ts +0 -0
  59. package/dist/workers/index.js +1 -0
  60. package/dist/workers/tsconfig.tsbuildinfo +1 -0
  61. package/dist/worklets/denoise.worklet.d.ts +1 -0
  62. package/dist/worklets/denoise.worklet.js +1 -2
  63. package/dist/worklets/tsconfig.tsbuildinfo +1 -0
  64. package/dist/worklets/types.d.ts +52 -0
  65. package/dist/worklets/types.js +0 -0
  66. package/package.json +11 -9
  67. package/dist/index.d.ts +0 -1129
  68. package/dist/index.mjs +0 -2441
  69. package/dist/worklets/denoise.worklet.js.map +0 -7
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A library for media analysis using Web APIs.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export * from './audio';
7
+ export * from './video';
8
+ export * from './visual';
9
+ export * from './types';
10
+ export * from './math';
11
+ export * from './process';
12
+ export * from './typeGuards';
13
+ export { createAsyncCallbackLoop } from './utils';
14
+ export * from './benchUtils';
15
+ export declare const urls: {
16
+ denoise: () => URL;
17
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A library for media analysis using Web APIs.
3
+ *
4
+ * @packageDocumentation
5
+ */
6
+ export * from './audio';
7
+ export * from './video';
8
+ export * from './visual';
9
+ export * from './types';
10
+ export * from './math';
11
+ export * from './process';
12
+ export * from './typeGuards';
13
+ export { createAsyncCallbackLoop } from './utils';
14
+ export * from './benchUtils';
15
+ export const urls = {
16
+ denoise: () => new URL('../worklets/denoise.worklet.js', import.meta.url),
17
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Sum an array of numbers
3
+ *
4
+ * @param nums - An array of numbers
5
+ */
6
+ export declare const sum: (nums: number[]) => number;
7
+ /**
8
+ * Average an array of numbers
9
+ *
10
+ * @param nums - An array of numbers
11
+ */
12
+ export declare const avg: (nums: number[]) => number;
13
+ /**
14
+ * pow function from Math in functional form `number -> number -> number`
15
+ *
16
+ * @param exponent - The exponent used for the expression
17
+ * @param base - The base value to be powered
18
+ *
19
+ * @returns Math.pow(base, exponent)
20
+ */
21
+ export declare const pow: (exponent: number) => (base: number) => number;
22
+ /**
23
+ * Calculate the Root Mean Square from provided numbers
24
+ *
25
+ * @param nums - An array of numbers
26
+ */
27
+ export declare const rms: (nums: number[]) => number;
28
+ /**
29
+ * Round the floating point number away from zero, which is different from
30
+ * `Math.round`
31
+ *
32
+ * @param num - The number to round
33
+ *
34
+ * @example
35
+ *
36
+ * ```typescript
37
+ * round(0.5) // 1
38
+ * round(-0.5) // -1
39
+ * ```
40
+ */
41
+ export declare const round: (num: number) => number;
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Sum an array of numbers
3
+ *
4
+ * @param nums - An array of numbers
5
+ */
6
+ export const sum = (nums) => nums.reduce((accm, num) => accm + num, 0);
7
+ /**
8
+ * Average an array of numbers
9
+ *
10
+ * @param nums - An array of numbers
11
+ */
12
+ export const avg = (nums) => {
13
+ if (nums.length === 0) {
14
+ return 0;
15
+ }
16
+ if (nums.length === 1) {
17
+ return nums[0] ?? 0;
18
+ }
19
+ return sum(nums) / nums.length;
20
+ };
21
+ /**
22
+ * pow function from Math in functional form `number -> number -> number`
23
+ *
24
+ * @param exponent - The exponent used for the expression
25
+ * @param base - The base value to be powered
26
+ *
27
+ * @returns Math.pow(base, exponent)
28
+ */
29
+ export const pow = (exponent) => (base) => Math.pow(base, exponent);
30
+ /**
31
+ * Calculate the Root Mean Square from provided numbers
32
+ *
33
+ * @param nums - An array of numbers
34
+ */
35
+ export const rms = (nums) => {
36
+ if (!nums || !Array.isArray(nums) || !nums.length) {
37
+ return 0;
38
+ }
39
+ if (nums.length === 1 && nums[0] !== undefined) {
40
+ return Math.abs(nums[0]);
41
+ }
42
+ return Math.sqrt(sum(nums.map(pow(2))) / nums.length);
43
+ };
44
+ /**
45
+ * Round the floating point number away from zero, which is different from
46
+ * `Math.round`
47
+ *
48
+ * @param num - The number to round
49
+ *
50
+ * @example
51
+ *
52
+ * ```typescript
53
+ * round(0.5) // 1
54
+ * round(-0.5) // -1
55
+ * ```
56
+ */
57
+ export const round = (num) => num ? Math.round(num) : -Math.round(-num);
@@ -0,0 +1,102 @@
1
+ /**
2
+ * SVG path command functions
3
+ *
4
+ * https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
5
+ */
6
+ import type { Point } from './types';
7
+ /**
8
+ * Coordinates string
9
+ *
10
+ * @example
11
+ *
12
+ * ```
13
+ * 30,90
14
+ * ```
15
+ */
16
+ export declare function toCoordinateString(p?: Point): string;
17
+ /**
18
+ * MoveTo Path Command
19
+ *
20
+ * @example
21
+ *
22
+ * ```
23
+ * M 10,20
24
+ * ```
25
+ */
26
+ export declare function moveTo(p?: Point): string;
27
+ /**
28
+ * Line to Path Command
29
+ *
30
+ * @example
31
+ *
32
+ * ```
33
+ * L 10,20
34
+ * ```
35
+ */
36
+ export declare function lineTo(p: Point): string;
37
+ /**
38
+ * Draw a horizontal line from the current point to the end point, which is
39
+ * specified by the x parameter and the current point's y coordinate.
40
+ *
41
+ * @example
42
+ *
43
+ * ```
44
+ * H 10
45
+ * ```
46
+ */
47
+ export declare function horizontalLineTo(x: number): string;
48
+ /**
49
+ * Draw a vertical line from the current point to the end point, which is
50
+ * specified by the y parameter and the current point's x coordinate.
51
+ *
52
+ * @example
53
+ *
54
+ * ```
55
+ * V 10
56
+ * ```
57
+ */
58
+ export declare function verticalLineTo(y: number): string;
59
+ /**
60
+ * Cubic Bezier Curve parameters
61
+ *
62
+ * scp: start control point
63
+ * ecp: end control point
64
+ * ep: end point
65
+ */
66
+ interface CubicCurveParams {
67
+ scp: Point;
68
+ ecp: Point;
69
+ ep: Point;
70
+ }
71
+ /**
72
+ * Cubic Bezier Curve Path Command
73
+ *
74
+ * @example
75
+ *
76
+ * ```
77
+ * C 30,90 25,10 50,10
78
+ * ```
79
+ */
80
+ export declare function cubicCurveTo({ scp, ecp, ep }: CubicCurveParams): string;
81
+ /**
82
+ * Smooth Cubic Bezier Curve Path Command
83
+ *
84
+ * @example
85
+ *
86
+ * ```
87
+ * S 25,10 50,10
88
+ * ```
89
+ */
90
+ export declare function smoothCubicCurveTo({ ecp, ep, }: Pick<CubicCurveParams, 'ecp' | 'ep'>): string;
91
+ /**
92
+ * Close the current subpath by connecting the last point of the path with its
93
+ * initial point.
94
+ *
95
+ * @example
96
+ *
97
+ * ```
98
+ * Z
99
+ * ```
100
+ */
101
+ export declare function closePath(): string;
102
+ export {};
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Coordinates string
3
+ *
4
+ * @example
5
+ *
6
+ * ```
7
+ * 30,90
8
+ * ```
9
+ */
10
+ export function toCoordinateString(p) {
11
+ return p ? [p.x, p.y].join(',') : '';
12
+ }
13
+ /**
14
+ * MoveTo Path Command
15
+ *
16
+ * @example
17
+ *
18
+ * ```
19
+ * M 10,20
20
+ * ```
21
+ */
22
+ export function moveTo(p) {
23
+ return p ? ['M', toCoordinateString(p)].join(' ') : '';
24
+ }
25
+ /**
26
+ * Line to Path Command
27
+ *
28
+ * @example
29
+ *
30
+ * ```
31
+ * L 10,20
32
+ * ```
33
+ */
34
+ export function lineTo(p) {
35
+ return p ? ['L', toCoordinateString(p)].join(' ') : '';
36
+ }
37
+ /**
38
+ * Draw a horizontal line from the current point to the end point, which is
39
+ * specified by the x parameter and the current point's y coordinate.
40
+ *
41
+ * @example
42
+ *
43
+ * ```
44
+ * H 10
45
+ * ```
46
+ */
47
+ export function horizontalLineTo(x) {
48
+ return `H ${x}`;
49
+ }
50
+ /**
51
+ * Draw a vertical line from the current point to the end point, which is
52
+ * specified by the y parameter and the current point's x coordinate.
53
+ *
54
+ * @example
55
+ *
56
+ * ```
57
+ * V 10
58
+ * ```
59
+ */
60
+ export function verticalLineTo(y) {
61
+ return `V ${y}`;
62
+ }
63
+ /**
64
+ * Cubic Bezier Curve Path Command
65
+ *
66
+ * @example
67
+ *
68
+ * ```
69
+ * C 30,90 25,10 50,10
70
+ * ```
71
+ */
72
+ export function cubicCurveTo({ scp, ecp, ep }) {
73
+ return scp && ecp && ep
74
+ ? ['C', ...[scp, ecp, ep].map(toCoordinateString)].join(' ')
75
+ : '';
76
+ }
77
+ /**
78
+ * Smooth Cubic Bezier Curve Path Command
79
+ *
80
+ * @example
81
+ *
82
+ * ```
83
+ * S 25,10 50,10
84
+ * ```
85
+ */
86
+ export function smoothCubicCurveTo({ ecp, ep, }) {
87
+ return ecp && ep
88
+ ? ['S', ...[ecp, ep].map(toCoordinateString)].join(' ')
89
+ : '';
90
+ }
91
+ /**
92
+ * Close the current subpath by connecting the last point of the path with its
93
+ * initial point.
94
+ *
95
+ * @example
96
+ *
97
+ * ```
98
+ * Z
99
+ * ```
100
+ */
101
+ export function closePath() {
102
+ return 'Z';
103
+ }
@@ -0,0 +1,239 @@
1
+ import type { Queue } from '@pexip/utils';
2
+ import type { StatsOptions, AudioSamples, AudioStats, Rect, Clock, IsVoice, ThrottleOptions } from './types';
3
+ /**
4
+ * Default silent threshold
5
+ * At least one LSB 16-bit data (compare is on absolute value).
6
+ */
7
+ export declare const SILENT_THRESHOLD: number;
8
+ /**
9
+ * Default mono detection threshold
10
+ * Data must be identical within one LSB 16-bit to be identified as mono.
11
+ */
12
+ export declare const MONO_THRESHOLD: number;
13
+ /**
14
+ * Default low volume detection threshold
15
+ */
16
+ export declare const LOW_VOLUME_THRESHOLD = -60;
17
+ /**
18
+ * Default clipping detection threshold
19
+ */
20
+ export declare const CLIP_THRESHOLD = 0.98;
21
+ /**
22
+ * Default Voice probability threshold
23
+ */
24
+ export declare const VOICE_PROBABILITY_THRESHOLD = 0.3;
25
+ /**
26
+ * Default clipping count threshold
27
+ * Number of consecutive clipThreshold level samples that indicate clipping.
28
+ */
29
+ export declare const CLIP_COUNT_THRESHOLD = 6;
30
+ /**
31
+ * AudioStats builder
32
+ *
33
+ * @param stats - overwrite the default attributes
34
+ * @param options - `silentThreshold`, `lowVolumeThreshold` and
35
+ * `clipCountThreshold`
36
+ */
37
+ export declare const createAudioStats: (stats?: Partial<AudioStats>, { silentThreshold, lowVolumeThreshold, clipCountThreshold, }?: {
38
+ silentThreshold?: number | undefined;
39
+ lowVolumeThreshold?: number | undefined;
40
+ clipCountThreshold?: number | undefined;
41
+ }) => AudioStats;
42
+ /**
43
+ * Convert a byte to float, according to web audio spec
44
+ *
45
+ * Floating point audio sample number is defined as: non-interleaved IEEE754
46
+ * 32-bit linear PCM with a nominal range between -1 and +1, that is, 32bits
47
+ * floating point buffer, with each samples between -1.0 and 1.0
48
+ * https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer
49
+ *
50
+ * Byte samples are represented as follows:
51
+ * 128 is silence, 0 is negative max, 256 is positive max
52
+ *
53
+ * @param value - The byte value to convert to float
54
+ *
55
+ * @remarks
56
+ * Ref. https://www.w3.org/TR/webaudio/#dom-analysernode-getbytetimedomaindata
57
+ */
58
+ export declare const fromByteToFloat: (value: number) => number;
59
+ /**
60
+ * Convert a float to byte, according to web audio spec
61
+ *
62
+ * Floating point audio sample number is defined as: non-interleaved IEEE754
63
+ * 32-bit linear PCM with a nominal range between -1 and +1, that is, 32bits
64
+ * floating point buffer, with each samples between -1.0 and 1.0
65
+ * https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer
66
+ *
67
+ * Byte samples are represented as follows:
68
+ * 128 is silence, 0 is negative max, 256 is positive max
69
+ *
70
+ * @param value - The float value to convert to byte
71
+ *
72
+ * @remarks
73
+ * Ref. https://www.w3.org/TR/webaudio/#dom-analysernode-getbytetimedomaindata
74
+ */
75
+ export declare const fromFloatToByte: (value: number) => number;
76
+ /**
77
+ * Copy data from Uint8Array buffer to Float32Array buffer with byte to float conversion
78
+ *
79
+ * @param bytes - The source Byte buffer
80
+ * @param floats - The destination buffer
81
+ */
82
+ export declare const copyByteBufferToFloatBuffer: (bytes: Uint8Array, floats: Float32Array) => void;
83
+ /**
84
+ * Convert a floating point gain value into a dB representation without any
85
+ * reference, dBFS, https://en.wikipedia.org/wiki/DBFS
86
+ *
87
+ * See https://www.w3.org/TR/webaudio#conversion-to-db
88
+ *
89
+ * @param amplitude - Expected a value in (0, 1]
90
+ */
91
+ export declare const toDecibel: (gain: number) => number;
92
+ /**
93
+ * Calculate the averaged volume using Root Mean Square, assuming the data is in
94
+ * float form
95
+ *
96
+ * @param data - Audio Frequency data
97
+ *
98
+ * @alpha
99
+ */
100
+ export declare const processAverageVolume: (data: number[]) => number;
101
+ /**
102
+ * Simple silent detection to only check the first and last bit from the sample
103
+ *
104
+ * @param samples - Audio sample data, this could be in a form of floating number
105
+ * of a byte number as long as the `threshold` value is given accordingly.
106
+ * @param threshold - Silent threshold
107
+ *
108
+ * @defaultValue
109
+ * `1.0 / 32767` assuming the sample is float value
110
+ *
111
+ * @returns
112
+ * `true` when it is silent
113
+ */
114
+ export declare const isSilent: (samples: AudioSamples, threshold?: number) => boolean;
115
+ /**
116
+ * Check if the provided gain above the low volume threshold, which is
117
+ * considered as low volume.
118
+ *
119
+ * @param gain - Floating point representation of the gain number
120
+ *
121
+ * @returns
122
+ * `true` if the `gain` is lower than the threshold
123
+ */
124
+ export declare const isLowVolume: (gain: number, threshold?: number) => boolean;
125
+ /**
126
+ * Check if there is clipping
127
+ *
128
+ * @param clipCount - Number of consecutive clip
129
+ *
130
+ * @returns
131
+ * `true` if the `clipCount` is above the threshold, aka clipping
132
+ */
133
+ export declare const isClipping: (clipCount: number, threshold?: number) => boolean;
134
+ /**
135
+ * Check if provided channels are mono or stereo
136
+ *
137
+ * @param channels - Audio channels and assuming the inputs are in floating
138
+ * point form
139
+ * @param threshold - Mono detection threshold, default to floating point form
140
+ *
141
+ * @defaultValue
142
+ * `1.0 / 32767`
143
+ *
144
+ * @returns
145
+ * `true` if they are mono, otherwise stereo
146
+ */
147
+ export declare const isMono: (channels: AudioSamples[], threshold?: number) => boolean;
148
+ /**
149
+ * Calculate the audio stats, expected the samples are in float form
150
+ *
151
+ * @param options - See StatsOptions
152
+ *
153
+ * @remarks
154
+ * http://www.rossbencina.com/code/real-time-audio-programming-101-time-waits-for-nothing
155
+ */
156
+ export declare const getAudioStats: ({ samples, baseStats, clipThreshold, }: StatsOptions) => AudioStats;
157
+ /**
158
+ * VAD options
159
+ */
160
+ interface VAOptions {
161
+ /**
162
+ * the RMS threshold used to compare with the input RMS
163
+ */
164
+ volumeThreshold?: number;
165
+ /**
166
+ * The threshold for a voice pulse in terms of time, in millisecond
167
+ */
168
+ VADTimeThreshold?: number;
169
+ /**
170
+ * The clock, can be used for testing
171
+ *
172
+ * @defaultValue
173
+ * `performance`
174
+ */
175
+ clock?: Clock;
176
+ }
177
+ /**
178
+ * A Naive Voice activity detection
179
+ *
180
+ * @param options - See `VAOptions`
181
+ *
182
+ * @returns `(volume: number) => boolean`, `true` if there is voice
183
+ */
184
+ export declare const isVoiceActivity: ({ volumeThreshold, VADTimeThreshold, clock, }?: VAOptions) => (volume: number) => boolean;
185
+ /**
186
+ * Compare the provided width and height to see if they are the same
187
+ *
188
+ * @param widthA - The width of A
189
+ * @param heightA - The height of A
190
+ * @param widthB - The width of B
191
+ * @param heightB - The height of B
192
+ */
193
+ export declare const isEqualSize: (widthA: number, heightA: number, widthB: number, heightB: number) => boolean;
194
+ /**
195
+ * Convert the source size to destination size when necessary based on the
196
+ * height
197
+ *
198
+ * @param sw - Source width
199
+ * @param sh - Source height
200
+ * @param dw - destination width
201
+ * @param dh - destination height
202
+ */
203
+ export declare const fitDestinationSize: (sw: number, sh: number, dw: number, dh: number) => Rect;
204
+ /**
205
+ * A function to check provided time series data is considered as voice activity
206
+ *
207
+ * @param options - @see VAOptions
208
+ */
209
+ export declare const createVoiceDetectorFromTimeData: (options?: VAOptions) => IsVoice<number[]>;
210
+ /**
211
+ * A function to check the provided probability is considered as voice activity
212
+ *
213
+ * @param voiceThreshold - A threshold of the probability to be considered as
214
+ * voice activity
215
+ */
216
+ export declare const createVoiceDetectorFromProbability: (voiceThreshold?: number) => IsVoice<number>;
217
+ /**
218
+ * Create a voice detector based on provided params
219
+ *
220
+ * @param onDetected - When there is voice activity, this callback will be called
221
+ * @param shouldDetect - When return `true`, voice activity will function, otherwise, not function
222
+ * @param options - @see ThrottleOptions
223
+ */
224
+ export declare const createVADetector: (onDetected: () => void, shouldDetect: () => boolean, options?: ThrottleOptions) => <T>(isVoice: IsVoice<T>) => (data: T) => void;
225
+ /**
226
+ * Create a function to process the AudioStats and check if silent
227
+ * `onSignalDetected` callback is called under 2 situations:
228
+ *
229
+ * ```
230
+ * Logic
231
+ * lastCheck | silent | should call onSignalDetected
232
+ * 0 | 0 | 0
233
+ * 0 | 1 | 1
234
+ * 1 | 0 | 1
235
+ * 1 | 1 | 0
236
+ * ```
237
+ */
238
+ export declare const createAudioSignalDetector: (shouldDetect: () => boolean, onDetected: (silent: boolean) => void) => (buffer: Queue<number[]>, threshold?: number) => (samples: number[]) => void;
239
+ export {};