echogarden 1.6.0 → 1.6.2
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/dist/alignment/SpeechAlignment.js +23 -14
- package/dist/alignment/SpeechAlignment.js.map +1 -1
- package/dist/api/TextTranslation.js +12 -10
- package/dist/api/TextTranslation.js.map +1 -1
- package/dist/recognition/WhisperSTT.js +6 -4
- package/dist/recognition/WhisperSTT.js.map +1 -1
- package/dist/text-translation/GoogleTranslateTextTranslation.d.ts +4 -1
- package/dist/text-translation/GoogleTranslateTextTranslation.js +3 -1
- package/dist/text-translation/GoogleTranslateTextTranslation.js.map +1 -1
- package/docs/Options.md +2 -2
- package/package.json +5 -5
- package/src/alignment/SpeechAlignment.ts +28 -14
- package/src/api/TextTranslation.ts +14 -11
- package/src/recognition/WhisperSTT.ts +10 -6
- package/src/text-translation/GoogleTranslateTextTranslation.ts +5 -2
- package/dist/utilities/BufferFileReadStream.d.ts +0 -20
- package/dist/utilities/BufferFileReadStream.js +0 -81
- package/dist/utilities/BufferFileReadStream.js.map +0 -1
- package/dist/utilities/PVarInt.d.ts +0 -4
- package/dist/utilities/PVarInt.js +0 -166
- package/dist/utilities/PVarInt.js.map +0 -1
- package/dist/utilities/VarInt.d.ts +0 -4
- package/dist/utilities/VarInt.js +0 -166
- package/dist/utilities/VarInt.js.map +0 -1
|
@@ -35,6 +35,7 @@ export async function translateText(inputText: string, options: TextTranslationO
|
|
|
35
35
|
logger.start(`Load ${options.engine} module`)
|
|
36
36
|
|
|
37
37
|
let translationPairs: TranslationPair[]
|
|
38
|
+
let translatedText: string
|
|
38
39
|
|
|
39
40
|
switch (options.engine) {
|
|
40
41
|
case 'nllb': {
|
|
@@ -46,15 +47,25 @@ export async function translateText(inputText: string, options: TextTranslationO
|
|
|
46
47
|
|
|
47
48
|
translationPairs = await NLLBTextTranslation.translateText(inputText, options.sourceLanguage, options.targetLanguage)
|
|
48
49
|
|
|
50
|
+
translatedText = translationPairs.map(pair => {
|
|
51
|
+
const translated = pair.translatedText
|
|
52
|
+
|
|
53
|
+
if (translated.endsWith(' ') || translated.endsWith('\n')) {
|
|
54
|
+
return pair.translatedText
|
|
55
|
+
} else {
|
|
56
|
+
return pair.translatedText + ' '
|
|
57
|
+
}
|
|
58
|
+
}).join('').trim()
|
|
59
|
+
|
|
49
60
|
break
|
|
50
61
|
}
|
|
51
62
|
|
|
52
63
|
case 'google-translate': {
|
|
53
64
|
const GoogleTranslateTextTranslation = await import('../text-translation/GoogleTranslateTextTranslation.js')
|
|
54
65
|
|
|
55
|
-
logger.end()
|
|
66
|
+
logger.end();
|
|
56
67
|
|
|
57
|
-
translationPairs = await GoogleTranslateTextTranslation.translateText(inputText, options.sourceLanguage, options.targetLanguage)
|
|
68
|
+
({ translationPairs, translatedText } = await GoogleTranslateTextTranslation.translateText(inputText, options.sourceLanguage, options.targetLanguage))
|
|
58
69
|
|
|
59
70
|
break
|
|
60
71
|
}
|
|
@@ -67,6 +78,7 @@ export async function translateText(inputText: string, options: TextTranslationO
|
|
|
67
78
|
logger.logTitledMessage(`Warning`, `The deepl engine is currently an early prototype implementation and doesn't work correctly.`, chalk.yellow, 'warning')
|
|
68
79
|
|
|
69
80
|
translationPairs = await DeepLTextTranslation.translateText(inputText, options.sourceLanguage, options.targetLanguage)
|
|
81
|
+
translatedText = ''
|
|
70
82
|
|
|
71
83
|
break
|
|
72
84
|
}
|
|
@@ -76,15 +88,6 @@ export async function translateText(inputText: string, options: TextTranslationO
|
|
|
76
88
|
}
|
|
77
89
|
}
|
|
78
90
|
|
|
79
|
-
const translatedText = translationPairs.map(pair => {
|
|
80
|
-
const translated = pair.translatedText
|
|
81
|
-
|
|
82
|
-
if (translated.endsWith(' ') || translated.endsWith('\n')) {
|
|
83
|
-
return pair.translatedText
|
|
84
|
-
} else {
|
|
85
|
-
return pair.translatedText + ' '
|
|
86
|
-
}
|
|
87
|
-
}).join('').trim()
|
|
88
91
|
|
|
89
92
|
logger.end()
|
|
90
93
|
|
|
@@ -1524,14 +1524,16 @@ export class Whisper {
|
|
|
1524
1524
|
|
|
1525
1525
|
const logger = new Logger()
|
|
1526
1526
|
|
|
1527
|
-
|
|
1527
|
+
const onnxSessionOptions = getOnnxSessionOptions({ executionProviders: this.encoderExecutionProviders })
|
|
1528
|
+
|
|
1529
|
+
const onnxProvidersString = onnxSessionOptions.executionProviders!.length > 0 ? `${ onnxSessionOptions.executionProviders!.join(', ') }` : `default`
|
|
1530
|
+
|
|
1531
|
+
await logger.startAsync(`Create encoder inference session for model '${this.modelName}' (ONNX provider: ${onnxProvidersString})`)
|
|
1528
1532
|
|
|
1529
1533
|
const encoderFilePath = path.join(this.modelDir, 'encoder.onnx')
|
|
1530
1534
|
|
|
1531
1535
|
const Onnx = await import('onnxruntime-node')
|
|
1532
1536
|
|
|
1533
|
-
const onnxSessionOptions = getOnnxSessionOptions({ executionProviders: this.encoderExecutionProviders })
|
|
1534
|
-
|
|
1535
1537
|
this.audioEncoder = await Onnx.InferenceSession.create(encoderFilePath, onnxSessionOptions)
|
|
1536
1538
|
|
|
1537
1539
|
logger.end()
|
|
@@ -1544,14 +1546,16 @@ export class Whisper {
|
|
|
1544
1546
|
|
|
1545
1547
|
const logger = new Logger()
|
|
1546
1548
|
|
|
1547
|
-
|
|
1549
|
+
const onnxSessionOptions = getOnnxSessionOptions({ executionProviders: this.decoderExecutionProviders })
|
|
1550
|
+
|
|
1551
|
+
const onnxProvidersString = onnxSessionOptions.executionProviders!.length > 0 ? `${onnxSessionOptions.executionProviders!.join(', ')}` : `default`
|
|
1552
|
+
|
|
1553
|
+
await logger.startAsync(`Create decoder inference session for model '${this.modelName}' (ONNX provider: ${onnxProvidersString})`)
|
|
1548
1554
|
|
|
1549
1555
|
const decoderFilePath = path.join(this.modelDir, 'decoder.onnx')
|
|
1550
1556
|
|
|
1551
1557
|
const Onnx = await import('onnxruntime-node')
|
|
1552
1558
|
|
|
1553
|
-
const onnxSessionOptions = getOnnxSessionOptions({ executionProviders: this.decoderExecutionProviders })
|
|
1554
|
-
|
|
1555
1559
|
this.textDecoder = await Onnx.InferenceSession.create(decoderFilePath, onnxSessionOptions)
|
|
1556
1560
|
|
|
1557
1561
|
logger.end()
|
|
@@ -3,7 +3,7 @@ import { Logger } from "../utilities/Logger.js"
|
|
|
3
3
|
import { Fragment, splitToFragments, splitToLines, splitToParagraphs, splitToSentences } from "../nlp/Segmentation.js"
|
|
4
4
|
import { TranslationPair } from "../api/TextTranslation.js"
|
|
5
5
|
import { getChromeOnWindowsHeaders, getChromeOnAndroidHeaders } from "../utilities/BrowserRequestHeaders.js"
|
|
6
|
-
import { logToStderr } from "../utilities/Utilities.js"
|
|
6
|
+
import { logToStderr, splitAndPreserveSeparators } from "../utilities/Utilities.js"
|
|
7
7
|
import { getShortLanguageCode } from "../utilities/Locale.js"
|
|
8
8
|
|
|
9
9
|
const log = logToStderr
|
|
@@ -13,6 +13,7 @@ export async function translateText(text: string, sourceLanguage: string, target
|
|
|
13
13
|
|
|
14
14
|
const maxTextLengthPerRequest = 2000
|
|
15
15
|
|
|
16
|
+
//const paragraphs = splitAndPreserveSeparators(text, /(\r?\n)(\r?\n)/g)
|
|
16
17
|
const paragraphs = splitToParagraphs(text, 'double', 'preserve')
|
|
17
18
|
|
|
18
19
|
const fragmentsForParagraph: Fragment[][] = []
|
|
@@ -92,7 +93,9 @@ export async function translateText(text: string, sourceLanguage: string, target
|
|
|
92
93
|
})
|
|
93
94
|
}
|
|
94
95
|
|
|
95
|
-
|
|
96
|
+
const translatedText = translationPairs.map(pair => pair.translatedText).join('\n\n')
|
|
97
|
+
|
|
98
|
+
return { translationPairs, translatedText }
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
export async function translateText_MobileWeb(text: string, sourceLanguage: string, targetLanguage: string, tld = 'com') {
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Readable } from 'stream';
|
|
2
|
-
import { EventEmitter } from 'events';
|
|
3
|
-
import { ReadStream as FsReadStream } from 'fs';
|
|
4
|
-
export declare function createVirtualFileReadStreamFromBuffer(buffer: Buffer, path?: string | Buffer): FsReadStream;
|
|
5
|
-
export declare class VirtualFileReadStream extends Readable {
|
|
6
|
-
private buffer;
|
|
7
|
-
position: number;
|
|
8
|
-
bytesRead: number;
|
|
9
|
-
path: string | Buffer;
|
|
10
|
-
pending: boolean;
|
|
11
|
-
emitter: EventEmitter;
|
|
12
|
-
constructor(buffer: Buffer, path?: string | Buffer);
|
|
13
|
-
_read(size: number): void;
|
|
14
|
-
close(callback?: (err?: NodeJS.ErrnoException | null) => void): void;
|
|
15
|
-
addListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
16
|
-
on(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
17
|
-
once(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
18
|
-
prependListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
19
|
-
prependOnceListener(event: string | symbol, listener: (...args: any[]) => void): this;
|
|
20
|
-
}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { Readable } from 'stream';
|
|
2
|
-
import { EventEmitter } from 'events';
|
|
3
|
-
import { ReadStream as FsReadStream } from 'fs';
|
|
4
|
-
export function createVirtualFileReadStreamFromBuffer(buffer, path) {
|
|
5
|
-
const bufferReadStream = new VirtualFileReadStream(buffer, path);
|
|
6
|
-
const proxyStream = new Proxy(bufferReadStream, {
|
|
7
|
-
get(target, prop) {
|
|
8
|
-
if (prop === Symbol.toStringTag) {
|
|
9
|
-
return 'ReadStream';
|
|
10
|
-
}
|
|
11
|
-
return target[prop];
|
|
12
|
-
},
|
|
13
|
-
getPrototypeOf() {
|
|
14
|
-
return FsReadStream.prototype;
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
return proxyStream;
|
|
18
|
-
}
|
|
19
|
-
export class VirtualFileReadStream extends Readable {
|
|
20
|
-
buffer;
|
|
21
|
-
position;
|
|
22
|
-
bytesRead;
|
|
23
|
-
path;
|
|
24
|
-
pending;
|
|
25
|
-
emitter;
|
|
26
|
-
constructor(buffer, path) {
|
|
27
|
-
super({ autoDestroy: false });
|
|
28
|
-
this.buffer = buffer;
|
|
29
|
-
this.position = 0;
|
|
30
|
-
this.bytesRead = 0;
|
|
31
|
-
this.path = path || '';
|
|
32
|
-
this.pending = true;
|
|
33
|
-
this.emitter = new EventEmitter();
|
|
34
|
-
// Emit 'open' event
|
|
35
|
-
this.emitter.emit('open', 0);
|
|
36
|
-
// Emit 'ready' event
|
|
37
|
-
this.pending = false;
|
|
38
|
-
this.emitter.emit('ready');
|
|
39
|
-
}
|
|
40
|
-
_read(size) {
|
|
41
|
-
if (this.position >= this.buffer.length) {
|
|
42
|
-
this.push(null); // No more data
|
|
43
|
-
this.emitter.emit('end');
|
|
44
|
-
this.emitter.emit('finish');
|
|
45
|
-
this.emitter.emit('close');
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
const chunkSize = Math.min(size, this.buffer.length - this.position);
|
|
49
|
-
const chunk = this.buffer.subarray(this.position, this.position + chunkSize);
|
|
50
|
-
this.position += chunkSize;
|
|
51
|
-
this.bytesRead += chunkSize;
|
|
52
|
-
this.push(chunk);
|
|
53
|
-
}
|
|
54
|
-
close(callback) {
|
|
55
|
-
this.emitter.emit('close');
|
|
56
|
-
if (callback) {
|
|
57
|
-
callback(null);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
addListener(event, listener) {
|
|
61
|
-
this.emitter.addListener(event, listener);
|
|
62
|
-
return this;
|
|
63
|
-
}
|
|
64
|
-
on(event, listener) {
|
|
65
|
-
this.emitter.on(event, listener);
|
|
66
|
-
return this;
|
|
67
|
-
}
|
|
68
|
-
once(event, listener) {
|
|
69
|
-
this.emitter.once(event, listener);
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
prependListener(event, listener) {
|
|
73
|
-
this.emitter.prependListener(event, listener);
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
prependOnceListener(event, listener) {
|
|
77
|
-
this.emitter.prependOnceListener(event, listener);
|
|
78
|
-
return this;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
//# sourceMappingURL=BufferFileReadStream.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"BufferFileReadStream.js","sourceRoot":"","sources":["../../src/utilities/BufferFileReadStream.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAA;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AACrC,OAAO,EAAE,UAAU,IAAI,YAAY,EAAE,MAAM,IAAI,CAAA;AAE/C,MAAM,UAAU,qCAAqC,CAAC,MAAc,EAAE,IAAsB;IAC3F,MAAM,gBAAgB,GAAG,IAAI,qBAAqB,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IAEhE,MAAM,WAAW,GAAG,IAAI,KAAK,CAAe,gBAAgB,EAAE;QAC7D,GAAG,CAAC,MAAM,EAAE,IAAI;YACf,IAAI,IAAI,KAAK,MAAM,CAAC,WAAW,EAAE,CAAC;gBACjC,OAAO,YAAY,CAAA;YACpB,CAAC;YAED,OAAQ,MAAc,CAAC,IAAI,CAAC,CAAA;QAC7B,CAAC;QAED,cAAc;YACb,OAAO,YAAY,CAAC,SAAS,CAAA;QAC9B,CAAC;KACD,CAAC,CAAA;IAEF,OAAO,WAAW,CAAA;AACnB,CAAC;AAED,MAAM,OAAO,qBAAsB,SAAQ,QAAQ;IAC1C,MAAM,CAAQ;IACtB,QAAQ,CAAQ;IAChB,SAAS,CAAQ;IACjB,IAAI,CAAiB;IACrB,OAAO,CAAS;IAChB,OAAO,CAAc;IAErB,YAAY,MAAc,EAAE,IAAsB;QACjD,KAAK,CAAC,EAAE,WAAW,EAAE,KAAK,EAAE,CAAC,CAAA;QAE7B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAA;QACjB,IAA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C,IAAI,CAAC,OAAO,CAAC,CAAA;QAE1B,IAAI,QAAQ,EAAE,CAAC;YACd,QAAQ,CAAC,IAAI,CAAC,CAAA;QACf,CAAC;IACF,CAAC;IAED,WAAW,CAAC,KAAsB,EAAE,QAAkC;QACrE,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAEzC,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,EAAE,CAAC,KAAsB,EAAE,QAAkC;QAC5D,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAEhC,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,IAAI,CAAC,KAAsB,EAAE,QAAkC;QAC9D,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAElC,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,eAAe,CAAC,KAAsB,EAAE,QAAkC;QACzE,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAE7C,OAAO,IAAI,CAAA;IACZ,CAAC;IAED,mBAAmB,CAAC,KAAsB,EAAE,QAAkC;QAC7E,IAAI,CAAC,OAAO,CAAC,mBAAmB,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAA;QAEjD,OAAO,IAAI,CAAA;IACZ,CAAC;CACD"}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export declare function encodeSignedInt32(value: number, outEncodedData: number[]): void;
|
|
2
|
-
export declare function decodeSignedInt32s(encodedData: ArrayLike<number>, outDecodedValues: number[]): void;
|
|
3
|
-
export declare function encodeUnsignedInt31(value: number, outEncodedData: number[]): void;
|
|
4
|
-
export declare function decodeUnsignedInt31s(encodedData: ArrayLike<number>, outDecodedValues: number[]): void;
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
2
|
-
// Encode signed integer
|
|
3
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
4
|
-
export function encodeSignedInt32(value, outEncodedData) {
|
|
5
|
-
//const absValue = Math.abs(value)
|
|
6
|
-
const absMask = value >> 31;
|
|
7
|
-
const absValue = (value ^ absMask) - absMask;
|
|
8
|
-
if (absValue < (2 ** 6)) {
|
|
9
|
-
outEncodedData.push(value & 0b01111111);
|
|
10
|
-
}
|
|
11
|
-
else if (absValue < (2 ** 13)) {
|
|
12
|
-
outEncodedData.push((value & 0b00111111) | 0b10000000, (value >> 6) & 0b11111111);
|
|
13
|
-
}
|
|
14
|
-
else if (absValue < (2 ** 20)) {
|
|
15
|
-
outEncodedData.push((value & 0b00011111) | 0b11000000, (value >> 5) & 0b11111111, (value >> 13) & 0b11111111);
|
|
16
|
-
}
|
|
17
|
-
else if (absValue < (2 ** 27)) {
|
|
18
|
-
outEncodedData.push((value & 0b00001111) | 0b11100000, (value >> 4) & 0b11111111, (value >> 12) & 0b11111111, (value >> 20) & 0b11111111);
|
|
19
|
-
}
|
|
20
|
-
else if (value < (2 ** 31) && value >= -(2 ** 31)) {
|
|
21
|
-
outEncodedData.push(0b11110000, value & 0b11111111, (value >> 8) & 0b11111111, (value >> 16) & 0b11111111, (value >> 24) & 0b11111111);
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
throw new Error(`Input value must be within the range of an int31: between -(2^31) and 2^31 - 1`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
28
|
-
// Decode signed integer
|
|
29
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
30
|
-
export function decodeSignedInt32s(encodedData, outDecodedValues) {
|
|
31
|
-
for (let readIndex = 0; readIndex < encodedData.length;) {
|
|
32
|
-
const byte0 = encodedData[readIndex++];
|
|
33
|
-
if ((byte0 & 0b10000000) === 0) {
|
|
34
|
-
// 1 byte
|
|
35
|
-
let decodedValue = byte0;
|
|
36
|
-
if ((byte0 & 0b01000000) !== 0) {
|
|
37
|
-
decodedValue |= (-1 << 7);
|
|
38
|
-
}
|
|
39
|
-
outDecodedValues.push(decodedValue);
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
const byte1 = encodedData[readIndex++];
|
|
43
|
-
if ((byte0 & 0b01000000) === 0) {
|
|
44
|
-
// 2 bytes
|
|
45
|
-
let decodedValue = (byte0 & 0b00111111) |
|
|
46
|
-
(byte1 << 6);
|
|
47
|
-
if ((byte1 & 0b10000000) !== 0) {
|
|
48
|
-
decodedValue |= (-1 << 14);
|
|
49
|
-
}
|
|
50
|
-
outDecodedValues.push(decodedValue);
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
const byte2 = encodedData[readIndex++];
|
|
54
|
-
if ((byte0 & 0b00100000) === 0) {
|
|
55
|
-
// 3 bytes
|
|
56
|
-
let decodedValue = (byte0 & 0b00011111) |
|
|
57
|
-
(byte1 << 5) |
|
|
58
|
-
(byte2 << 13);
|
|
59
|
-
if ((byte2 & 0b10000000) !== 0) {
|
|
60
|
-
decodedValue |= (-1 << 21);
|
|
61
|
-
}
|
|
62
|
-
outDecodedValues.push(decodedValue);
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
const byte3 = encodedData[readIndex++];
|
|
66
|
-
if ((byte0 & 0b00010000) === 0) {
|
|
67
|
-
// 4 bytes
|
|
68
|
-
let decodedValue = (byte0 & 0b00001111) |
|
|
69
|
-
(byte1 << 4) |
|
|
70
|
-
(byte2 << 12) |
|
|
71
|
-
(byte3 << 20);
|
|
72
|
-
if ((byte3 & 0b10000000) !== 0) {
|
|
73
|
-
decodedValue |= (-1 << 28);
|
|
74
|
-
}
|
|
75
|
-
outDecodedValues.push(decodedValue);
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
const byte4 = encodedData[readIndex++];
|
|
79
|
-
if (byte0 === 0b11110000) {
|
|
80
|
-
// 5 bytes
|
|
81
|
-
let decodedValue = (byte1) |
|
|
82
|
-
(byte2 << 8) |
|
|
83
|
-
(byte3 << 16) |
|
|
84
|
-
(byte4 << 24);
|
|
85
|
-
outDecodedValues.push(decodedValue);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
throw new Error(`Encountered a value that is encoded with more than 5 bytes (beyond int32 range).`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
92
|
-
// Encode signed integer
|
|
93
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
94
|
-
export function encodeUnsignedInt31(value, outEncodedData) {
|
|
95
|
-
if (value < (2 ** 7)) {
|
|
96
|
-
outEncodedData.push(value);
|
|
97
|
-
}
|
|
98
|
-
else if (value < (2 ** 14)) {
|
|
99
|
-
outEncodedData.push((value & 0b00111111) | 0b10000000, (value >>> 6) & 0b11111111);
|
|
100
|
-
}
|
|
101
|
-
else if (value < (2 ** 21)) {
|
|
102
|
-
outEncodedData.push((value & 0b00011111) | 0b11000000, (value >>> 5) & 0b11111111, (value >>> 13) & 0b11111111);
|
|
103
|
-
}
|
|
104
|
-
else if (value < (2 ** 28)) {
|
|
105
|
-
outEncodedData.push((value & 0b00001111) | 0b11100000, (value >>> 4) & 0b11111111, (value >>> 12) & 0b11111111, (value >>> 20) & 0b11111111);
|
|
106
|
-
}
|
|
107
|
-
else if (value < (2 ** 31)) {
|
|
108
|
-
outEncodedData.push(0b11110000, value & 0b11111111, (value >>> 8) & 0b11111111, (value >>> 16) & 0b11111111, (value >>> 24) & 0b11111111);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
throw new Error(`Input value must be within the range of an int31: between 0 and 2^31 - 1.`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
115
|
-
// Decode unsigned integer
|
|
116
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
117
|
-
export function decodeUnsignedInt31s(encodedData, outDecodedValues) {
|
|
118
|
-
for (let readIndex = 0; readIndex < encodedData.length;) {
|
|
119
|
-
const byte0 = encodedData[readIndex++];
|
|
120
|
-
if ((byte0 & 0b10000000) === 0) {
|
|
121
|
-
// 1 byte
|
|
122
|
-
let decodedValue = byte0;
|
|
123
|
-
outDecodedValues.push(decodedValue);
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
const byte1 = encodedData[readIndex++];
|
|
127
|
-
if ((byte0 & 0b01000000) === 0) {
|
|
128
|
-
// 2 bytes
|
|
129
|
-
let decodedValue = (byte0 & 0b00111111) |
|
|
130
|
-
(byte1 << 6);
|
|
131
|
-
outDecodedValues.push(decodedValue);
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
const byte2 = encodedData[readIndex++];
|
|
135
|
-
if ((byte0 & 0b00100000) === 0) {
|
|
136
|
-
// 3 bytes
|
|
137
|
-
let decodedValue = (byte0 & 0b00011111) |
|
|
138
|
-
(byte1 << 5) |
|
|
139
|
-
(byte2 << 13);
|
|
140
|
-
outDecodedValues.push(decodedValue);
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
const byte3 = encodedData[readIndex++];
|
|
144
|
-
if ((byte0 & 0b00010000) === 0) {
|
|
145
|
-
// 4 bytes
|
|
146
|
-
let decodedValue = (byte0 & 0b00001111) |
|
|
147
|
-
(byte1 << 4) |
|
|
148
|
-
(byte2 << 12) |
|
|
149
|
-
(byte3 << 20);
|
|
150
|
-
outDecodedValues.push(decodedValue);
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const byte4 = encodedData[readIndex++];
|
|
154
|
-
if (byte0 === 0b11110000) {
|
|
155
|
-
// 5 bytes
|
|
156
|
-
let decodedValue = (byte1) |
|
|
157
|
-
(byte2 << 8) |
|
|
158
|
-
(byte3 << 16) |
|
|
159
|
-
(byte4 << 24);
|
|
160
|
-
outDecodedValues.push(decodedValue);
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
throw new Error(`Encountered a value that is encoded with more than 5 bytes (beyond uint31 range).`);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
//# sourceMappingURL=PVarInt.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PVarInt.js","sourceRoot":"","sources":["../../src/utilities/PVarInt.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,wBAAwB;AACxB,oFAAoF;AACpF,MAAM,UAAU,iBAAiB,CAAC,KAAa,EAAE,cAAwB;IACxE,kCAAkC;IAElC,MAAM,OAAO,GAAG,KAAK,IAAI,EAAE,CAAA;IAC3B,MAAM,QAAQ,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,OAAO,CAAA;IAE5C,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QACzB,cAAc,CAAC,IAAI,CAClB,KAAK,GAAG,UAAU,CAClB,CAAA;IACF,CAAC;SAAM,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;QACjC,cAAc,CAAC,IAAI,CAClB,CAAC,KAAK,GAAG,UAAU,CAAC,GAAG,UAAU,EACjC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,UAAU,CACzB,CAAA;IACF,CAAC;SAAM,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;QACjC,cAAc,CAAC,IAAI,CAClB,CAAC,KAAK,GAAG,UAAU,CAAC,GAAG,UAAU,EACjC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,UAAU,EACzB,CAAC,KAAK,IAAI,EAAE,CAAC,GAAG,UAAU,CAC1B,CAAA;IACF,CAAC;SAAM,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;QACjC,cAAc,CAAC,IAAI,CAClB,CAAC,KAAK,GAAG,UAAU,CAAC,GAAG,UAAU,EACjC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,UAAU,EACzB,CA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AC,CAAA;YAEnC,SAAQ;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,mFAAmF,CAAC,CAAA;IACrG,CAAC;AACF,CAAC"}
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
export declare function encodeSignedInt32(value: number, outEncodedData: number[]): void;
|
|
2
|
-
export declare function decodeSignedInt32s(encodedData: ArrayLike<number>, outDecodedValues: number[]): void;
|
|
3
|
-
export declare function encodeUnsignedInt31(value: number, outEncodedData: number[]): void;
|
|
4
|
-
export declare function decodeUnsignedInt31s(encodedData: ArrayLike<number>, outDecodedValues: number[]): void;
|
package/dist/utilities/VarInt.js
DELETED
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
2
|
-
// Encode signed integer
|
|
3
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
4
|
-
export function encodeSignedInt32(value, outEncodedData) {
|
|
5
|
-
//const absValue = Math.abs(value)
|
|
6
|
-
const absMask = value >> 31;
|
|
7
|
-
const absValue = (value ^ absMask) - absMask;
|
|
8
|
-
if (absValue < (2 ** 6)) {
|
|
9
|
-
outEncodedData.push(value & 0b01111111);
|
|
10
|
-
}
|
|
11
|
-
else if (absValue < (2 ** 13)) {
|
|
12
|
-
outEncodedData.push((value & 0b00111111) | 0b10000000, (value >> 6) & 0b11111111);
|
|
13
|
-
}
|
|
14
|
-
else if (absValue < (2 ** 20)) {
|
|
15
|
-
outEncodedData.push((value & 0b00011111) | 0b11000000, (value >> 5) & 0b11111111, (value >> 13) & 0b11111111);
|
|
16
|
-
}
|
|
17
|
-
else if (absValue < (2 ** 27)) {
|
|
18
|
-
outEncodedData.push((value & 0b00001111) | 0b11100000, (value >> 4) & 0b11111111, (value >> 12) & 0b11111111, (value >> 20) & 0b11111111);
|
|
19
|
-
}
|
|
20
|
-
else if (value < (2 ** 31) && value >= -(2 ** 31)) {
|
|
21
|
-
outEncodedData.push(0b11110000, value & 0b11111111, (value >> 8) & 0b11111111, (value >> 16) & 0b11111111, (value >> 24) & 0b11111111);
|
|
22
|
-
}
|
|
23
|
-
else {
|
|
24
|
-
throw new Error(`Input value must be within the range of an int31: between -(2^31) and 2^31 - 1`);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
28
|
-
// Decode signed integer
|
|
29
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
30
|
-
export function decodeSignedInt32s(encodedData, outDecodedValues) {
|
|
31
|
-
for (let readIndex = 0; readIndex < encodedData.length;) {
|
|
32
|
-
const byte0 = encodedData[readIndex++];
|
|
33
|
-
if ((byte0 & 0b10000000) === 0) {
|
|
34
|
-
// 1 byte
|
|
35
|
-
let decodedValue = byte0;
|
|
36
|
-
if ((byte0 & 0b01000000) !== 0) {
|
|
37
|
-
decodedValue |= (-1 << 7);
|
|
38
|
-
}
|
|
39
|
-
outDecodedValues.push(decodedValue);
|
|
40
|
-
continue;
|
|
41
|
-
}
|
|
42
|
-
const byte1 = encodedData[readIndex++];
|
|
43
|
-
if ((byte0 & 0b01000000) === 0) {
|
|
44
|
-
// 2 bytes
|
|
45
|
-
let decodedValue = (byte0 & 0b00111111) |
|
|
46
|
-
(byte1 << 6);
|
|
47
|
-
if ((byte1 & 0b10000000) !== 0) {
|
|
48
|
-
decodedValue |= (-1 << 14);
|
|
49
|
-
}
|
|
50
|
-
outDecodedValues.push(decodedValue);
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
const byte2 = encodedData[readIndex++];
|
|
54
|
-
if ((byte0 & 0b00100000) === 0) {
|
|
55
|
-
// 3 bytes
|
|
56
|
-
let decodedValue = (byte0 & 0b00011111) |
|
|
57
|
-
(byte1 << 5) |
|
|
58
|
-
(byte2 << 13);
|
|
59
|
-
if ((byte2 & 0b10000000) !== 0) {
|
|
60
|
-
decodedValue |= (-1 << 21);
|
|
61
|
-
}
|
|
62
|
-
outDecodedValues.push(decodedValue);
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
const byte3 = encodedData[readIndex++];
|
|
66
|
-
if ((byte0 & 0b00010000) === 0) {
|
|
67
|
-
// 4 bytes
|
|
68
|
-
let decodedValue = (byte0 & 0b00001111) |
|
|
69
|
-
(byte1 << 4) |
|
|
70
|
-
(byte2 << 12) |
|
|
71
|
-
(byte3 << 20);
|
|
72
|
-
if ((byte3 & 0b10000000) !== 0) {
|
|
73
|
-
decodedValue |= (-1 << 28);
|
|
74
|
-
}
|
|
75
|
-
outDecodedValues.push(decodedValue);
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
const byte4 = encodedData[readIndex++];
|
|
79
|
-
if (byte0 === 0b11110000) {
|
|
80
|
-
// 5 bytes
|
|
81
|
-
let decodedValue = (byte1) |
|
|
82
|
-
(byte2 << 8) |
|
|
83
|
-
(byte3 << 16) |
|
|
84
|
-
(byte4 << 24);
|
|
85
|
-
outDecodedValues.push(decodedValue);
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
throw new Error(`Encountered a value that is encoded with more than 5 bytes (beyond int32 range).`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
92
|
-
// Encode signed integer
|
|
93
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
94
|
-
export function encodeUnsignedInt31(value, outEncodedData) {
|
|
95
|
-
if (value < (2 ** 7)) {
|
|
96
|
-
outEncodedData.push(value);
|
|
97
|
-
}
|
|
98
|
-
else if (value < (2 ** 14)) {
|
|
99
|
-
outEncodedData.push((value & 0b00111111) | 0b10000000, (value >>> 6) & 0b11111111);
|
|
100
|
-
}
|
|
101
|
-
else if (value < (2 ** 21)) {
|
|
102
|
-
outEncodedData.push((value & 0b00011111) | 0b11000000, (value >>> 5) & 0b11111111, (value >>> 13) & 0b11111111);
|
|
103
|
-
}
|
|
104
|
-
else if (value < (2 ** 28)) {
|
|
105
|
-
outEncodedData.push((value & 0b00001111) | 0b11100000, (value >>> 4) & 0b11111111, (value >>> 12) & 0b11111111, (value >>> 20) & 0b11111111);
|
|
106
|
-
}
|
|
107
|
-
else if (value < (2 ** 31)) {
|
|
108
|
-
outEncodedData.push(0b11110000, value & 0b11111111, (value >>> 8) & 0b11111111, (value >>> 16) & 0b11111111, (value >>> 24) & 0b11111111);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
throw new Error(`Input value must be within the range of an int31: between 0 and 2^31 - 1.`);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
115
|
-
// Decode unsigned integer
|
|
116
|
-
////////////////////////////////////////////////////////////////////////////////////
|
|
117
|
-
export function decodeUnsignedInt31s(encodedData, outDecodedValues) {
|
|
118
|
-
for (let readIndex = 0; readIndex < encodedData.length;) {
|
|
119
|
-
const byte0 = encodedData[readIndex++];
|
|
120
|
-
if ((byte0 & 0b10000000) === 0) {
|
|
121
|
-
// 1 byte
|
|
122
|
-
let decodedValue = byte0;
|
|
123
|
-
outDecodedValues.push(decodedValue);
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
const byte1 = encodedData[readIndex++];
|
|
127
|
-
if ((byte0 & 0b01000000) === 0) {
|
|
128
|
-
// 2 bytes
|
|
129
|
-
let decodedValue = (byte0 & 0b00111111) |
|
|
130
|
-
(byte1 << 6);
|
|
131
|
-
outDecodedValues.push(decodedValue);
|
|
132
|
-
continue;
|
|
133
|
-
}
|
|
134
|
-
const byte2 = encodedData[readIndex++];
|
|
135
|
-
if ((byte0 & 0b00100000) === 0) {
|
|
136
|
-
// 3 bytes
|
|
137
|
-
let decodedValue = (byte0 & 0b00011111) |
|
|
138
|
-
(byte1 << 5) |
|
|
139
|
-
(byte2 << 13);
|
|
140
|
-
outDecodedValues.push(decodedValue);
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
const byte3 = encodedData[readIndex++];
|
|
144
|
-
if ((byte0 & 0b00010000) === 0) {
|
|
145
|
-
// 4 bytes
|
|
146
|
-
let decodedValue = (byte0 & 0b00001111) |
|
|
147
|
-
(byte1 << 4) |
|
|
148
|
-
(byte2 << 12) |
|
|
149
|
-
(byte3 << 20);
|
|
150
|
-
outDecodedValues.push(decodedValue);
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
const byte4 = encodedData[readIndex++];
|
|
154
|
-
if (byte0 === 0b11110000) {
|
|
155
|
-
// 5 bytes
|
|
156
|
-
let decodedValue = (byte1) |
|
|
157
|
-
(byte2 << 8) |
|
|
158
|
-
(byte3 << 16) |
|
|
159
|
-
(byte4 << 24);
|
|
160
|
-
outDecodedValues.push(decodedValue);
|
|
161
|
-
continue;
|
|
162
|
-
}
|
|
163
|
-
throw new Error(`Encountered a value that is encoded with more than 5 bytes (beyond uint31 range).`);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
//# sourceMappingURL=VarInt.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"VarInt.js","sourceRoot":"","sources":["../../src/utilities/VarInt.ts"],"names":[],"mappings":"AAAA,oFAAoF;AACpF,wBAAwB;AACxB,oFAAoF;AACpF,MAAM,UAAU,iBAAiB,CAAC,KAAa,EAAE,cAAwB;IACxE,kCAAkC;IAElC,MAAM,OAAO,GAAG,KAAK,IAAI,EAAE,CAAA;IAC3B,MAAM,QAAQ,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,OAAO,CAAA;IAE5C,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC;QACzB,cAAc,CAAC,IAAI,CAClB,KAAK,GAAG,UAAU,CAClB,CAAA;IACF,CAAC;SAAM,IAAI,QAAQ,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,CAAC;QACjC,cAAc,CAAC,IAAI,CAClB,CAAC,KAAK,GAAG,UAAU,CAAC,GAAG,UAAU,EACjC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG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