augnitorecorder
Advanced tools
Comparing version 1.0.16 to 1.0.17
@@ -23,4 +23,5 @@ export declare class AugnitoRecorder { | ||
bufferInterval?: number; | ||
EOS_Message?: any; | ||
socketTimeoutInterval?: any; | ||
EOS_Message?: string; | ||
socketTimeoutInterval?: number; | ||
shouldSendAudioDataSequence?: boolean; | ||
}, | ||
@@ -27,0 +28,0 @@ heavyOp?: any |
@@ -387,3 +387,3 @@ function _regeneratorRuntime() { | ||
var Executor = /*#__PURE__*/function () { | ||
function Executor(wsUrl, enableLogs, eosMessage, socketTimeoutInterval, onFinalResult, onPartialResult, onError, onSessionEvent, onOtherResults) { | ||
function Executor(wsUrl, enableLogs, eosMessage, socketTimeoutInterval, shouldSendAudioDataSequence, onFinalResult, onPartialResult, onError, onSessionEvent, onOtherResults) { | ||
_classCallCheck(this, Executor); | ||
@@ -398,2 +398,4 @@ this.worker; | ||
this.lastSent; | ||
this.packetSeqId = 0; | ||
this.shouldSendAudioDataSequence = shouldSendAudioDataSequence; | ||
this.heavyOp; | ||
@@ -431,3 +433,5 @@ this.idleLoop; | ||
// const asrText = document.getElementById("asrText"); | ||
if (eventData.type == "final") _this.onFinalResultCallback(eventData.data);else if (eventData.type == "partial") _this.onPartialResultCallback(eventData.data);else if (eventData.type == "meta") _this.onSessionEventCallback(eventData.data);else if (eventData.type == "error") _this.onErrorCallback(eventData.data);else if (eventData.type == "other") _this.onOtherResultsCallback(eventData.data); | ||
if (eventData.type == "final") _this.onFinalResultCallback(eventData.data);else if (eventData.type == "partial") _this.onPartialResultCallback(eventData.data);else if (eventData.type == "meta") _this.onSessionEventCallback(eventData.data);else if (eventData.type == "error") _this.onErrorCallback(eventData.data);else if (eventData.type == "ping") { | ||
_this.lastSent = +new Date(); | ||
} else if (eventData.type == "other") _this.onOtherResultsCallback(eventData.data); | ||
}; | ||
@@ -446,3 +450,45 @@ // main-thread | ||
} | ||
}, { | ||
key: "CreateAudioPacketHeader", | ||
value: function CreateAudioPacketHeader(isLastPacket) { | ||
this.packetSeqId++; | ||
var headerSize = 16; // 21 bytes in total | ||
// Create a buffer of the required size | ||
var buffer = new ArrayBuffer(headerSize); | ||
var view = new DataView(buffer); | ||
// Write '@BSR' as string | ||
for (var i = 0; i < 3; i++) { | ||
view.setUint8(i, "@BSR".charCodeAt(i)); | ||
} | ||
// Write packet ID (int64) | ||
view.setBigInt64(3, BigInt(this.packetSeqId), false); // false for big endian | ||
// Write boolean value | ||
view.setUint8(11, isLastPacket ? 1 : 0); | ||
console.log(buffer); | ||
return buffer; | ||
} | ||
}, { | ||
key: "appendAudioData", | ||
value: function appendAudioData(arrayBuffer, audioDataBytes) { | ||
// Calculate the total length of the new buffer | ||
var totalLength = arrayBuffer.byteLength + audioDataBytes.byteLength; | ||
// Create a new ArrayBuffer with the combined length | ||
var combinedBuffer = new ArrayBuffer(totalLength); | ||
// Create a new Uint8Array for the combined buffer | ||
var combinedArray = new Uint8Array(combinedBuffer); | ||
// Copy the binary header into the combined buffer | ||
combinedArray.set(new Uint8Array(arrayBuffer), 0); | ||
// Copy the audio data bytes into the combined buffer after the binary header | ||
combinedArray.set(new Uint8Array(audioDataBytes), arrayBuffer.byteLength); | ||
return combinedBuffer; | ||
} | ||
// main-thread | ||
@@ -454,10 +500,23 @@ }, { | ||
if (data === DONE_MSG) { | ||
var _this$worker; | ||
// message sent to web-worker | ||
(_this$worker = this.worker) === null || _this$worker === void 0 || _this$worker.postMessage(DONE_MSG); | ||
if (this.shouldSendAudioDataSequence) { | ||
var _this$worker; | ||
var binaryHeader = this.CreateAudioPacketHeader(true); | ||
(_this$worker = this.worker) === null || _this$worker === void 0 || _this$worker.postMessage(binaryHeader); | ||
} else { | ||
var _this$worker2; | ||
(_this$worker2 = this.worker) === null || _this$worker2 === void 0 || _this$worker2.postMessage(DONE_MSG); | ||
} | ||
clearInterval(this.idleLoop); | ||
} else { | ||
var _this$worker2; | ||
var _this$worker3; | ||
var audioData = []; | ||
if (this.shouldSendAudioDataSequence) { | ||
var binaryHeader = this.CreateAudioPacketHeader(false); | ||
audioData = this.appendAudioData(binaryHeader, data); | ||
} else { | ||
audioData = data; | ||
} | ||
// message sent to web-worker - transferrable | ||
(_this$worker2 = this.worker) === null || _this$worker2 === void 0 || _this$worker2.postMessage(data, [data]); | ||
(_this$worker3 = this.worker) === null || _this$worker3 === void 0 || _this$worker3.postMessage(audioData, [audioData]); | ||
} | ||
@@ -583,6 +642,13 @@ } | ||
} catch (e) { | ||
self.postMessage({ | ||
type: "error", | ||
data: "invalid response" | ||
}); | ||
if (message.data === "PING") { | ||
self.postMessage({ | ||
type: "ping", | ||
data: message.data | ||
}); | ||
} else { | ||
self.postMessage({ | ||
type: "error", | ||
data: "invalid response" | ||
}); | ||
} | ||
} | ||
@@ -631,11 +697,11 @@ } | ||
// worklet-thread | ||
var worklet = "class MyAudioWorkletProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.accumulator = [];\n this.reset();\n this.isProcessing = true;\n // message received from main-thread\n this.port.onmessage = (e) => {\n console.log(\"Worklet received event: \", e.data);\n if (this.sampleSize > 0) {\n this.accumulator.push(this.sampleVal / this.sampleSize);\n }\n if (e.data == \"PAUSE\") {\n // append silence to get last word ASR.\n const silenceSize = 16000 * 2;\n for (let i = 0; i < silenceSize; i++) {\n this.accumulator.push(0);\n }\n }\n this.send();\n if (e.data == \"STOP\") {\n // message sent to main-thread\n this.port.postMessage(\"DONE\");\n this.isProcessing = false;\n }\n };\n }\n\n static get parameterDescriptors() {\n return [\n {\n name: \"scale\",\n defaultValue: 1,\n minValue: 1,\n maxValue: 6,\n },\n {\n name: \"bufferSizeInterval\",\n defaultValue: 1,\n minValue: 1,\n maxValue: 100,\n },\n ];\n }\n\n // 128 frames\n process(inputList, outputList, params) {\n const input = inputList[0];\n if (input && input.length && input[0].length) {\n const output = outputList[0];\n const scale = params.scale[0];\n const bufferSizeInterval = params[\"bufferSizeInterval\"][0];\n // console.log(\"BufferSizeInterval\", bufferSizeInterval);\n // Jackpot\n input[0].forEach((float32Element) => {\n const int16Element = Math.min(1, Math.max(-1, float32Element)) * 0x7fff;\n this.sampleVal += int16Element;\n this.sampleSize++;\n if (this.sampleSize == scale) {\n this.accumulator.push(this.sampleVal / this.sampleSize);\n this.reset();\n }\n\n // Comment this when streaming microphone audio\n // output[0][index] = float32Element;\n });\n if (this.accumulator.length >= 125 * 128 * bufferSizeInterval) {\n this.send();\n }\n }\n return this.isProcessing;\n }\n\n send() {\n if (this.accumulator.length == 0) return;\n const audioData = new Int16Array(this.accumulator);\n // message sent to main-thread - transferrable\n this.port.postMessage(audioData.buffer, [audioData.buffer]);\n this.accumulator = [];\n this.reset();\n }\n\n reset() {\n this.sampleVal = 0;\n this.sampleSize = 0;\n }\n}\n\nregisterProcessor(\"worklet-processor\", MyAudioWorkletProcessor);"; | ||
var worklet = "class MyAudioWorkletProcessor extends AudioWorkletProcessor {\n constructor() {\n super();\n this.accumulator = [];\n this.reset();\n this.isProcessing = true;\n // message received from main-thread\n this.port.onmessage = (e) => {\n console.log(\"Worklet received event: \", e.data);\n if (this.sampleSize > 0) {\n this.accumulator.push(this.sampleVal / this.sampleSize);\n }\n if (e.data == \"PAUSE\" || e.data == \"STOP\") {\n // append silence to get last word ASR.\n const silenceSize = 16000 * 2;\n for (let i = 0; i < silenceSize; i++) {\n this.accumulator.push(0);\n }\n }\n this.send();\n if (e.data == \"STOP\") {\n // message sent to main-thread\n this.port.postMessage(\"DONE\");\n this.isProcessing = false;\n }\n };\n }\n\n static get parameterDescriptors() {\n return [\n {\n name: \"scale\",\n defaultValue: 1,\n minValue: 1,\n maxValue: 6,\n },\n {\n name: \"bufferSizeInterval\",\n defaultValue: 1,\n minValue: 1,\n maxValue: 100,\n },\n ];\n }\n\n // 128 frames\n process(inputList, outputList, params) {\n const input = inputList[0];\n if (input && input.length && input[0].length) {\n const output = outputList[0];\n const scale = params.scale[0];\n const bufferSizeInterval = params[\"bufferSizeInterval\"][0];\n // console.log(\"BufferSizeInterval\", bufferSizeInterval);\n // Jackpot\n input[0].forEach((float32Element) => {\n const int16Element = Math.min(1, Math.max(-1, float32Element)) * 0x7fff;\n this.sampleVal += int16Element;\n this.sampleSize++;\n if (this.sampleSize == scale) {\n this.accumulator.push(this.sampleVal / this.sampleSize);\n this.reset();\n }\n\n // Comment this when streaming microphone audio\n // output[0][index] = float32Element;\n });\n if (this.accumulator.length >= 125 * 128 * bufferSizeInterval) {\n this.send();\n }\n }\n return this.isProcessing;\n }\n\n send() {\n if (this.accumulator.length == 0) return;\n const audioData = new Int16Array(this.accumulator);\n // message sent to main-thread - transferrable\n this.port.postMessage(audioData.buffer, [audioData.buffer]);\n this.accumulator = [];\n this.reset();\n }\n\n reset() {\n this.sampleVal = 0;\n this.sampleSize = 0;\n }\n}\n\nregisterProcessor(\"worklet-processor\", MyAudioWorkletProcessor);"; | ||
// main-thread | ||
var Streamer = /*#__PURE__*/function () { | ||
function Streamer(wsUrl, enableLogs, isDebug, bufferSizeInterval, eosMessage, socketTimeoutInterval, heavyOp, onFinalResult, onPartialResult, onError, onStateChanged, onSessionEvent, onOtherResults) { | ||
function Streamer(wsUrl, enableLogs, isDebug, bufferSizeInterval, eosMessage, socketTimeoutInterval, shouldSendAudioDataSequence, heavyOp, onFinalResult, onPartialResult, onError, onStateChanged, onSessionEvent, onOtherResults) { | ||
_classCallCheck(this, Streamer); | ||
this.audioContext = new AudioContext(); | ||
if (wsUrl !== "") { | ||
this.executor = new Executor(wsUrl, enableLogs, eosMessage, socketTimeoutInterval, onFinalResult, onPartialResult, onError, onSessionEvent, onOtherResults); | ||
this.executor = new Executor(wsUrl, enableLogs, eosMessage, socketTimeoutInterval, shouldSendAudioDataSequence, onFinalResult, onPartialResult, onError, onSessionEvent, onOtherResults); | ||
this.executor.HeavyOp = heavyOp; | ||
@@ -1042,3 +1108,4 @@ this.executor.Start(); | ||
EOS_Message: undefined, | ||
socketTimeoutInterval: undefined | ||
socketTimeoutInterval: undefined, | ||
shouldSendAudioDataSequence: false | ||
}; | ||
@@ -1055,2 +1122,3 @@ var heavyOp = arguments.length > 1 ? arguments[1] : undefined; | ||
this.socketTimeoutInterval = config.socketTimeoutInterval; | ||
this.shouldSendAudioDataSequence = config.shouldSendAudioDataSequence; | ||
} | ||
@@ -1082,3 +1150,3 @@ return _createClass(AugnitoRecorder, [{ | ||
value: function startAudio() { | ||
this.streamer = new Streamer(this.WebsocketURL, this.enableLogs, this.isDebug, this.bufferInterval, this.eosMessage, this.socketTimeoutInterval, this.heavyOp, this.onFinalResultCallback.bind(this), this.onPartialResultCallback.bind(this), this.onErrorCallback.bind(this), this.onStateChangedCallback.bind(this), this.onSessionEventCallback.bind(this), this.onOtherResultCallback.bind(this)); | ||
this.streamer = new Streamer(this.WebsocketURL, this.enableLogs, this.isDebug, this.bufferInterval, this.eosMessage, this.socketTimeoutInterval, this.shouldSendAudioDataSequence, this.heavyOp, this.onFinalResultCallback.bind(this), this.onPartialResultCallback.bind(this), this.onErrorCallback.bind(this), this.onStateChangedCallback.bind(this), this.onSessionEventCallback.bind(this), this.onOtherResultCallback.bind(this)); | ||
this.streamer.StartStream(); | ||
@@ -1085,0 +1153,0 @@ this.log("Stream Started..."); |
{ | ||
"name": "augnitorecorder", | ||
"version": "1.0.16", | ||
"version": "1.0.17", | ||
"description": "Audio recorder and streamer compatible with any browser", | ||
@@ -5,0 +5,0 @@ "main": "dist/augnitoRecorder.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119695
1349