UNPKG

ai-code-writer

Version:

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

104 lines (102 loc) 3.6 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SoxRecordingFactory = exports.Recording = void 0; const assert_1 = __importDefault(require("assert")); const child_process_1 = require("child_process"); const debug = require('debug')('record'); class Recording { constructor(options) { this.cmd = ''; const defaults = { sampleRate: 16000, bitRate: 16, channels: 1, threshold: 0.5, thresholdStart: null, thresholdEnd: null, silence: '1.0', endOnSilence: false, audioType: 'wav' }; this.options = Object.assign(defaults, options); this.cmd = 'sox'; this.args = [ '--default-device', '--no-show-progress', '--rate', String(options.sampleRate), '--channels', String(options.channels), '--encoding', 'signed-integer', // sample encoding '--bits', String(options.bitRate), '--type', String(options.audioType), '-', 'silence', '1', '0.1', String(options.thresholdStart || options.threshold) + '%', '1', String(options.silence), String(options.thresholdEnd || options.threshold) + '%' ]; // noinspection SpellCheckingInspection this.cmdOptions = { encoding: 'binary', stdio: 'pipe', env: Object.assign(Object.assign({}, process.env), { AUDIODRIVER: 'waveaudio' }) }; debug(`Started recording`); debug(this.options); debug(` ${this.cmd} ${this.args.join(' ')}`); return this.start(); } start() { // @ts-ignore const cp = (0, child_process_1.spawn)(this.cmd, this.args, this.cmdOptions); const rec = cp.stdout; const err = cp.stderr; this.process = cp; // expose child process this._stream = rec; // expose output stream cp.on('close', (code) => { if (code === 0) return; rec.emit('error', `${this.cmd} has exited with error code ${code}. Enable debugging with the environment variable DEBUG=record.`); }); err.on('data', (chunk) => { debug(`STDERR: ${chunk}`); }); rec.on('data', (chunk) => { debug(`Recording ${chunk.length} bytes`); }); rec.on('end', () => { debug('Recording ended'); }); return this; } stop() { (0, assert_1.default)(this.process, 'Recording not yet started'); this.process.kill(); } pause() { (0, assert_1.default)(this.process, 'Recording not yet started'); this.process.kill('SIGSTOP'); this._stream.pause(); debug('Paused recording'); } resume() { (0, assert_1.default)(this.process, 'Recording not yet started'); this.process.kill('SIGCONT'); this._stream.resume(); debug('Resumed recording'); } isPaused() { (0, assert_1.default)(this.process, 'Recording not yet started'); return this._stream.isPaused(); } stream() { (0, assert_1.default)(this._stream, 'Recording not yet started'); return this._stream; } } exports.Recording = Recording; function SoxRecordingFactory(options) { return new Recording(options); } exports.SoxRecordingFactory = SoxRecordingFactory;