UNPKG

@mymusictaste/async-audiorecorder

Version:

node-audiorecorder with async/await & typescript support

80 lines 3.35 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 }); exports.listRecordingDevices = void 0; const node_child_process_1 = require("node:child_process"); const node_util_1 = require("node:util"); const execAsync = (0, node_util_1.promisify)(node_child_process_1.exec); /** * Executes the appropriate command to list available recording devices, automatically detecting the platform. * You can also pass the platform as an argument to override the automatic detection. */ function listRecordingDevices(platform = process.platform) { return __awaiter(this, void 0, void 0, function* () { let probeCommand; let command; switch (platform) { case "darwin": probeCommand = "which rec"; command = "rec -V6 -n -t coreaudio junkname 2>&1 || true"; break; case "win32": probeCommand = "where rec"; command = "rec -V6 -n -t waveaudio junkname 2>&1 || true"; break; case "linux": probeCommand = "which arecord"; command = "arecord -l"; break; default: throw new Error("Unsupported platform"); } // probe for the command try { yield execAsync(probeCommand); } catch (e) { throw new Error(`Could not find the command ${probeCommand}. Please install it and try again.`); } const { stdout } = yield execAsync(command); return parseOutputByPlatform(stdout, platform); }); } exports.listRecordingDevices = listRecordingDevices; function parseOutputByPlatform(output, platform) { const devices = []; switch (platform) { case "darwin": case "win32": const recRegex = /Found Audio Device "(.+)"\n/g; let recMatch; while ((recMatch = recRegex.exec(output))) { devices.push({ device: recMatch[1], label: recMatch[1], }); } break; case "linux": const arecordRegex = /card (\d+): (.+?) \[.+?\], device (\d+):/g; let arecordMatch; while ((arecordMatch = arecordRegex.exec(output))) { const cardId = arecordMatch[1]; const devId = arecordMatch[3]; const device = `pluginhw:${cardId},${devId}`; const label = arecordMatch[2]; devices.push({ device, label }); } break; } return devices; } //# sourceMappingURL=listDevices.js.map