UNPKG

@mymusictaste/async-audiorecorder

Version:

node-audiorecorder with async/await & typescript support

65 lines 2.28 kB
import { exec } from "node:child_process"; import { promisify } from "node:util"; const execAsync = promisify(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. */ export async function listRecordingDevices(platform = process.platform) { 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 { await execAsync(probeCommand); } catch (e) { throw new Error(`Could not find the command ${probeCommand}. Please install it and try again.`); } const { stdout } = await execAsync(command); return parseOutputByPlatform(stdout, platform); } 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