File size: 5,712 Bytes
94f44a2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 | import { createRequire } from 'node:module'
import { dirname, resolve, sep } from 'node:path'
import { fileURLToPath } from 'node:url'
// createRequire works in both Bun and Node.js ESM contexts.
// Needed because this package is "type": "module" but uses require() for
// loading native .node addons β bare require is not available in Node.js ESM.
const nodeRequire = createRequire(import.meta.url)
/**
* Resolve the "vendor root" directory where native .node binaries live.
*
* - Dev mode: import.meta.url β packages/audio-capture-napi/src/index.ts
* β vendor root = <project>/vendor/
* - Bun build: import.meta.url β dist/chunk-xxx.js
* β vendor root = <project>/dist/vendor/
* - Vite build: import.meta.url β dist/chunks/chunk-xxx.js
* β vendor root = <project>/dist/vendor/
*/
function getVendorRoot(): string {
const filePath = fileURLToPath(import.meta.url)
const dir = dirname(filePath)
const parts = dir.split(sep)
const distIdx = parts.lastIndexOf('dist')
if (distIdx !== -1) {
return parts.slice(0, distIdx + 1).join(sep) + sep + 'vendor'
}
// Dev mode β go up from packages/audio-capture-napi/src/ to project root
return resolve(dir, '..', '..', '..', 'vendor')
}
type AudioCaptureNapi = {
startRecording(onData: (data: Buffer) => void, onEnd: () => void): boolean
stopRecording(): void
isRecording(): boolean
startPlayback(sampleRate: number, channels: number): boolean
writePlaybackData(data: Buffer): void
stopPlayback(): void
isPlaying(): boolean
// TCC microphone authorization status (macOS only):
// 0 = notDetermined, 1 = restricted, 2 = denied, 3 = authorized.
// Linux: always returns 3 (authorized) β no system-level microphone permission API.
// Windows: returns 3 (authorized) if registry key absent or allowed,
// 2 (denied) if microphone access is explicitly denied.
microphoneAuthorizationStatus?(): number
}
let cachedModule: AudioCaptureNapi | null = null
let loadAttempted = false
function loadModule(): AudioCaptureNapi | null {
if (loadAttempted) {
return cachedModule
}
loadAttempted = true
// Supported platforms: macOS (darwin), Linux, Windows (win32)
const platform = process.platform
if (platform !== 'darwin' && platform !== 'linux' && platform !== 'win32') {
return null
}
// Candidate 1: native-embed path (bun compile). AUDIO_CAPTURE_NODE_PATH is
// defined at build time in build-with-plugins.ts for native builds only β the
// define resolves it to the static literal "../../audio-capture.node" so bun
// compile can rewrite it to /$bunfs/root/audio-capture.node. MUST stay a
// direct require(env var) β bun cannot analyze require(variable) from a loop.
if (process.env.AUDIO_CAPTURE_NODE_PATH) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
cachedModule = nodeRequire(
process.env.AUDIO_CAPTURE_NODE_PATH,
) as AudioCaptureNapi
return cachedModule
} catch {
// fall through to runtime fallbacks below
}
}
// Candidates 2-5: resolved vendor path + relative fallbacks.
// The primary candidate uses getVendorRoot() to find the correct dist root
// regardless of chunk nesting depth. Relative fallbacks cover edge cases.
const platformDir = `${process.arch}-${platform}`
const binaryRel = `audio-capture/${platformDir}/audio-capture.node`
const vendorRoot = getVendorRoot()
const fallbacks = [
resolve(vendorRoot, binaryRel),
`./vendor/${binaryRel}`,
`../vendor/${binaryRel}`,
`../../vendor/${binaryRel}`,
`${process.cwd()}/vendor/${binaryRel}`,
]
for (const p of fallbacks) {
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
cachedModule = nodeRequire(p) as AudioCaptureNapi
return cachedModule
} catch {
// try next
}
}
return null
}
export function isNativeAudioAvailable(): boolean {
return loadModule() !== null
}
export function startNativeRecording(
onData: (data: Buffer) => void,
onEnd: () => void,
): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.startRecording(onData, onEnd)
}
export function stopNativeRecording(): void {
const mod = loadModule()
if (!mod) {
return
}
mod.stopRecording()
}
export function isNativeRecordingActive(): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.isRecording()
}
export function startNativePlayback(
sampleRate: number,
channels: number,
): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.startPlayback(sampleRate, channels)
}
export function writeNativePlaybackData(data: Buffer): void {
const mod = loadModule()
if (!mod) {
return
}
mod.writePlaybackData(data)
}
export function stopNativePlayback(): void {
const mod = loadModule()
if (!mod) {
return
}
mod.stopPlayback()
}
export function isNativePlaying(): boolean {
const mod = loadModule()
if (!mod) {
return false
}
return mod.isPlaying()
}
// Returns the microphone authorization status.
// On macOS, returns the TCC status: 0=notDetermined, 1=restricted, 2=denied, 3=authorized.
// On Linux, always returns 3 (authorized) β no system-level mic permission API.
// On Windows, returns 3 (authorized) if registry key absent or allowed, 2 (denied) if explicitly denied.
// Returns 0 (notDetermined) if the native module is unavailable.
export function microphoneAuthorizationStatus(): number {
const mod = loadModule()
if (!mod || !mod.microphoneAuthorizationStatus) {
return 0
}
return mod.microphoneAuthorizationStatus()
}
|