Skip to content

Commit 7e15974

Browse files
feat: 实现 4 个 NAPI 包 — modifiers/image-processor/audio-capture/url-handler
- modifiers-napi: 使用 Bun FFI 调用 macOS CGEventSourceFlagsState 检测修饰键 - image-processor-napi: 集成 sharp 库,macOS 剪贴板图像读取 (osascript) - audio-capture-napi: 基于 SoX/arecord 的跨平台音频录制 - url-handler-napi: 完善函数签名(保持 null fallback) - 修复 image-processor 类型兼容性问题 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent fac9341 commit 7e15974

8 files changed

Lines changed: 398 additions & 23 deletions

File tree

bun.lock

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 143 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,151 @@
1-
// Auto-generated stub — replace with real implementation
1+
// audio-capture-napi: cross-platform audio capture using SoX (rec) on macOS
2+
// and arecord (ALSA) on Linux. Replaces the original cpal-based native module.
3+
4+
import { type ChildProcess, spawn, spawnSync } from 'child_process'
5+
6+
// ─── State ───────────────────────────────────────────────────────────
7+
8+
let recordingProcess: ChildProcess | null = null
9+
let availabilityCache: boolean | null = null
10+
11+
// ─── Helpers ─────────────────────────────────────────────────────────
12+
13+
function commandExists(cmd: string): boolean {
14+
const result = spawnSync(cmd, ['--version'], {
15+
stdio: 'ignore',
16+
timeout: 3000,
17+
})
18+
return result.error === undefined
19+
}
20+
21+
// ─── Public API ──────────────────────────────────────────────────────
22+
23+
/**
24+
* Check whether a supported audio recording command is available.
25+
* Returns true if `rec` (SoX) is found on macOS, or `arecord` (ALSA) on Linux.
26+
* Windows is not supported and always returns false.
27+
*/
228
export function isNativeAudioAvailable(): boolean {
29+
if (availabilityCache !== null) {
30+
return availabilityCache
31+
}
32+
33+
if (process.platform === 'win32') {
34+
availabilityCache = false
35+
return false
36+
}
37+
38+
if (process.platform === 'darwin') {
39+
// macOS: use SoX rec
40+
availabilityCache = commandExists('rec')
41+
return availabilityCache
42+
}
43+
44+
if (process.platform === 'linux') {
45+
// Linux: prefer arecord, fall back to rec
46+
availabilityCache = commandExists('arecord') || commandExists('rec')
47+
return availabilityCache
48+
}
49+
50+
availabilityCache = false
351
return false
452
}
53+
54+
/**
55+
* Check whether a recording is currently in progress.
56+
*/
557
export function isNativeRecordingActive(): boolean {
6-
return false
58+
return recordingProcess !== null && !recordingProcess.killed
759
}
8-
export function stopNativeRecording(): void {}
60+
61+
/**
62+
* Stop the active recording process, if any.
63+
*/
64+
export function stopNativeRecording(): void {
65+
if (recordingProcess) {
66+
const proc = recordingProcess
67+
recordingProcess = null
68+
if (!proc.killed) {
69+
proc.kill('SIGTERM')
70+
}
71+
}
72+
}
73+
74+
/**
75+
* Start recording audio. Raw PCM data (16kHz, 16-bit signed, mono) is
76+
* streamed via the onData callback. onEnd is called when recording stops
77+
* (either from silence detection or process termination).
78+
*
79+
* Returns true if recording started successfully, false otherwise.
80+
*/
981
export function startNativeRecording(
10-
_onData: (data: Buffer) => void,
11-
_onEnd: () => void,
82+
onData: (data: Buffer) => void,
83+
onEnd: () => void,
1284
): boolean {
13-
return false
85+
// Don't start if already recording
86+
if (isNativeRecordingActive()) {
87+
stopNativeRecording()
88+
}
89+
90+
if (!isNativeAudioAvailable()) {
91+
return false
92+
}
93+
94+
let child: ChildProcess
95+
96+
if (process.platform === 'darwin' || (process.platform === 'linux' && commandExists('rec'))) {
97+
// Use SoX rec: output raw PCM 16kHz 16-bit signed mono to stdout
98+
child = spawn(
99+
'rec',
100+
[
101+
'-q', // quiet
102+
'--buffer',
103+
'1024', // small buffer for low latency
104+
'-t', 'raw', // raw PCM output
105+
'-r', '16000', // 16kHz sample rate
106+
'-e', 'signed', // signed integer encoding
107+
'-b', '16', // 16-bit
108+
'-c', '1', // mono
109+
'-', // output to stdout
110+
],
111+
{ stdio: ['pipe', 'pipe', 'pipe'] },
112+
)
113+
} else if (process.platform === 'linux' && commandExists('arecord')) {
114+
// Use arecord: output raw PCM 16kHz 16-bit signed LE mono to stdout
115+
child = spawn(
116+
'arecord',
117+
[
118+
'-f', 'S16_LE', // signed 16-bit little-endian
119+
'-r', '16000', // 16kHz sample rate
120+
'-c', '1', // mono
121+
'-t', 'raw', // raw PCM, no header
122+
'-q', // quiet
123+
'-', // output to stdout
124+
],
125+
{ stdio: ['pipe', 'pipe', 'pipe'] },
126+
)
127+
} else {
128+
return false
129+
}
130+
131+
recordingProcess = child
132+
133+
child.stdout?.on('data', (chunk: Buffer) => {
134+
onData(chunk)
135+
})
136+
137+
// Consume stderr to prevent backpressure
138+
child.stderr?.on('data', () => {})
139+
140+
child.on('close', () => {
141+
recordingProcess = null
142+
onEnd()
143+
})
144+
145+
child.on('error', () => {
146+
recordingProcess = null
147+
onEnd()
148+
})
149+
150+
return true
14151
}

packages/image-processor-napi/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"private": true,
55
"type": "module",
66
"main": "./src/index.ts",
7-
"types": "./src/index.ts"
7+
"types": "./src/index.ts",
8+
"dependencies": {
9+
"sharp": "^0.33.5"
10+
}
811
}

0 commit comments

Comments
 (0)