sofya.transcription
Version:
a JavaScript library that provides a robust and flexible solution for real-time audio transcription. It is designed to transcribe audio streams and can be easily integrated into web applications.
330 lines • 17.2 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WhisperTranscriptionAdapter = void 0;
var eventemitter3_1 = __importDefault(require("eventemitter3"));
var WhisperTranscriptionAdapter = /** @class */ (function (_super) {
__extends(WhisperTranscriptionAdapter, _super);
function WhisperTranscriptionAdapter(config) {
var _this = _super.call(this) || this;
_this.websocket = null;
_this.audioContext = null;
_this.mediaStream = null;
_this.recordState = "PAUSED";
_this.websocketState = WebSocket.CLOSED;
_this.recordingNode = undefined;
_this.postMessage = function (sampleData) {
var _a;
var outputSampleRate = 16000;
var decreaseResultBuffer = _this.decreaseSampleRate(sampleData, 44100, outputSampleRate);
var audioData = _this.convertFloat32ToInt16(decreaseResultBuffer);
if (_this.websocket && ((_a = _this.websocket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
_this.websocket.send(audioData);
}
};
_this.connectWebsocketHandler = function (ws) {
var _a;
if (_this.websocket && ((_a = _this.websocket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
_this.websocketState = WebSocket.OPEN;
// this.sendAudioConfig(this.config?.language);
_this.websocket.onclose = function (event) {
console.log("WebSocket connection closed", event);
};
_this.websocket.onmessage = function (event) {
var _a, _b, _c;
var transcript_data = JSON.parse(event.data);
if (!transcript_data.is_partial && ((_a = transcript_data === null || transcript_data === void 0 ? void 0 : transcript_data.data) === null || _a === void 0 ? void 0 : _a.text)) {
_this.emit("recognized", (_b = transcript_data === null || transcript_data === void 0 ? void 0 : transcript_data.data) === null || _b === void 0 ? void 0 : _b.text);
_this.tempPartialTranscription = undefined;
}
if (transcript_data.is_partial && ((_c = transcript_data === null || transcript_data === void 0 ? void 0 : transcript_data.data) === null || _c === void 0 ? void 0 : _c.text)) {
_this.tempPartialTranscription = transcript_data.data.text;
_this.emit("recognizing", transcript_data.data.text);
}
};
}
};
_this.languageSelector = function (language) {
switch (language) {
case "pt-BR":
return "portuguese";
case "en-US":
return "english";
case "es-ES":
return "spanish";
default:
return "multilingual";
}
};
_this.sendAudioConfig = function (language) {
var _a;
if (language === void 0) { language = "multilingual"; }
var processingArgs = {};
var chunk_length_seconds = "3";
var chunk_offset_seconds = "0.1";
var selectedStrategy = "silence_at_end_of_chunk";
if (selectedStrategy === "silence_at_end_of_chunk") {
processingArgs = {
chunk_length_seconds: parseFloat(chunk_length_seconds),
chunk_offset_seconds: parseFloat(chunk_offset_seconds),
};
}
var audioConfig = {
type: "config",
data: {
sampleRate: 44100,
channels: 1,
language: _this.languageSelector(language),
processing_strategy: selectedStrategy,
processing_args: processingArgs,
},
};
if (_this.websocket && ((_a = _this.websocket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
_this.websocket.send(JSON.stringify(audioConfig));
}
};
_this.config = config;
_this.websocket = config.ws;
_this.connectWebsocketHandler(config.ws);
_this.audioContext = new AudioContext({ sampleRate: 44100 });
return _this;
}
WhisperTranscriptionAdapter.prototype.startTranscription = function (mediaStream) {
return __awaiter(this, void 0, void 0, function () {
var _a, error_1;
var _this = this;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
if (!navigator.mediaDevices.getUserMedia) {
throw new Error("getUserMedia not supported on your browser!");
}
this.mediaStream = mediaStream;
_c.label = 1;
case 1:
_c.trys.push([1, 3, , 4]);
this.input = (_b = this.audioContext) === null || _b === void 0 ? void 0 : _b.createMediaStreamSource(this.mediaStream);
_a = this;
return [4 /*yield*/, this.setupRecordingWorkletNode()];
case 2:
_a.recordingNode = _c.sent();
this.recordingNode.port.onmessage = function (event) {
if (_this.recordState !== "PAUSED") {
_this.postMessage(event.data);
}
};
if (this.input) {
this.input.connect(this.recordingNode);
this.recordState = "RECORDING";
}
else {
console.error("Unable to createMediaStreamSource");
}
return [3 /*break*/, 4];
case 3:
error_1 = _c.sent();
console.error("Erro no onmessage: ", error_1);
return [3 /*break*/, 4];
case 4: return [2 /*return*/];
}
});
});
};
WhisperTranscriptionAdapter.prototype.setupRecordingWorkletNode = function () {
return __awaiter(this, void 0, void 0, function () {
var workletCode, blob, workletURL;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
workletCode = "\n class RealtimeAudioProcessor extends AudioWorkletProcessor {\n constructor(options) {\n super();\n }\n\n process(inputs, outputs, params) {\n // ASR and VAD models typically require a mono audio.\n this.port.postMessage(inputs[0][0]);\n return true;\n }\n }\n\n registerProcessor('realtime-audio-processor', RealtimeAudioProcessor);\n ";
blob = new Blob([workletCode], { type: "application/javascript" });
workletURL = URL.createObjectURL(blob);
return [4 /*yield*/, this.audioContext.audioWorklet.addModule(workletURL)];
case 1:
_a.sent();
return [2 /*return*/, new AudioWorkletNode(this.audioContext, "realtime-audio-processor")];
}
});
});
};
WhisperTranscriptionAdapter.prototype.decreaseSampleRate = function (buffer, inputSampleRate, outputSampleRate) {
if (inputSampleRate < outputSampleRate) {
console.error("Sample rate too small.");
return;
}
else if (inputSampleRate === outputSampleRate) {
return;
}
var sampleRateRatio = inputSampleRate / outputSampleRate;
var newLength = Math.ceil(buffer.length / sampleRateRatio);
var result = new Float32Array(newLength);
var offsetResult = 0;
var offsetBuffer = 0;
while (offsetResult < result.length) {
var nextOffsetBuffer = Math.round((offsetResult + 1) * sampleRateRatio);
var accum = 0, count = 0;
for (var i = offsetBuffer; i < nextOffsetBuffer && i < buffer.length; i++) {
accum += buffer[i];
count++;
}
result[offsetResult] = accum / count;
offsetResult++;
offsetBuffer = nextOffsetBuffer;
}
return result;
};
WhisperTranscriptionAdapter.prototype.convertFloat32ToInt16 = function (buffer) {
var l = buffer.length;
var buf = new Int16Array(l);
while (l--) {
buf[l] = Math.min(1, buffer[l]) * 0x7fff;
}
return buf.buffer;
};
WhisperTranscriptionAdapter.prototype.pauseTranscription = function () {
this.recordState = "PAUSED";
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
this.websocket.send(JSON.stringify({ action: "finish" }));
}
if (this.input) {
this.input.disconnect();
}
};
WhisperTranscriptionAdapter.prototype.resumeTranscription = function () {
if (this.input && this.recordingNode) {
this.input.connect(this.recordingNode);
}
this.recordState = "RECORDING";
};
WhisperTranscriptionAdapter.prototype.stopTranscription = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.mediaStream) {
this.mediaStream.getTracks().forEach(function (track) { return track.stop(); });
}
return [4 /*yield*/, new Promise(function (resolve) {
var resolved = false;
var timeout = setTimeout(function () {
var _a;
if (!resolved) {
resolved = true;
if ((_a = _this.tempPartialTranscription) === null || _a === void 0 ? void 0 : _a.length) {
_this.emit("recognized", _this.tempPartialTranscription);
}
cleanup();
resolve();
}
}, 5000);
var cleanup = function () {
clearTimeout(timeout);
if (_this.websocket) {
_this.websocket.removeEventListener("message", onMessage);
}
};
var onMessage = function (event) {
try {
var data = JSON.parse(event.data);
if (!data.is_partial) {
if (!resolved) {
resolved = true;
cleanup();
resolve();
}
}
}
catch (e) {
console.error("264: ", e);
}
};
if (_this.websocket) {
_this.websocket.addEventListener("message", onMessage);
// Send finish action
if (_this.websocket.readyState === WebSocket.OPEN) {
_this.websocketState = WebSocket.CLOSING;
_this.websocket.send(JSON.stringify({ action: "finish" }));
}
}
else {
cleanup();
resolve();
}
})];
case 1:
_a.sent();
if (!this.audioContext) return [3 /*break*/, 3];
return [4 /*yield*/, this.audioContext.close()];
case 2:
_a.sent();
this.audioContext = null;
_a.label = 3;
case 3:
if (this.websocket && this.websocket.readyState === WebSocket.OPEN) {
this.websocket.close();
this.websocketState = WebSocket.CLOSED;
}
return [2 /*return*/];
}
});
});
};
return WhisperTranscriptionAdapter;
}(eventemitter3_1.default));
exports.WhisperTranscriptionAdapter = WhisperTranscriptionAdapter;
//# sourceMappingURL=WhisperTranscriptionAdapter.js.map