UNPKG

ai-code-writer

Version:

An AI code writer application using OpenAI APIs for audio transcription and chat completion.

85 lines (84 loc) 3.12 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const stream_1 = require("stream"); class RecordingHandler { constructor(recordingStream, resolve, reject) { this.recordingStream = recordingStream; this.resolve = resolve; this.reject = reject; this.chunks = []; this.isStopped = false; this.passThrough = new stream_1.PassThrough(); this.registerEvents(); this.resetTimeout(); } registerEvents() { this.passThrough.on('data', this.onData.bind(this)); this.passThrough.on('end', this.onEnd.bind(this)); this.passThrough.on('error', this.onError.bind(this)); const stream = this.recordingStream.stream(); stream.on('error', this.onError.bind(this)); stream.pipe(this.passThrough); } resetTimeout() { if (this.timeoutId) { clearTimeout(this.timeoutId); } this.timeoutId = setTimeout(() => this.stopRecording(), 5000); } onData(chunk) { this.chunks.push(chunk); this.resetTimeout(); } onEnd() { if (!this.isStopped) { this.resolve(Buffer.concat(this.chunks)); this.isStopped = true; } } onError(error) { if (!this.isStopped) { this.reject(error); this.isStopped = true; } } stopRecording() { if (!this.isStopped) { this.recordingStream.stream().unpipe(this.passThrough); this.recordingStream.stop(); this.isStopped = true; this.resolve(Buffer.concat(this.chunks)); // Sicherstellen, dass der Buffer immer zurückgegeben wird } } } class NodeAudioRecorder { constructor(soxFactory) { this.soxFactory = soxFactory; } startRecording() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const recordingStream = this.soxFactory({ sampleRate: 16000, channels: 1, threshold: 1.1, silence: '1.5', endOnSilence: true, audioType: 'wav', bitRate: 16 }); new RecordingHandler(recordingStream, resolve, reject); }); }); } } exports.default = NodeAudioRecorder;