
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
system-audio-loopback-prebuilt
Advanced tools
Loop system audio on macOS and windows to other applications. Includes prebuilt binaries.
本项目采用模块化架构。核心功能是启动录音子进程,然后把音频流传给 STT 识别。
src/
├── core/ # 核心功能模块
│ ├── AudioRecorder.js # 录音进程管理
│ ├── SpeechRecognizer.js # 语音识别管理
│ └── SystemAudioRecognition.js # 主控制器
├── audio/ # 音频处理模块
│ ├── AudioStream.js # 音频流管理
│ ├── AudioStreamFactory.js # 音频流工厂
│ ├── RiffPcmEncoder.js # PCM编码器
│ └── WavFileReader.js # WAV文件读取
├── utils/ # 工具模块
│ ├── config.js # 配置管理
│ ├── permissions.js # 权限检查
│ └── platform.js # 平台检测
├── index.js # 新的入口文件
└── stt.js # 重构后的STT接口
core/
)SystemAudioRecognition.js
- 主控制器AudioRecorder.js
- 录音进程管理SpeechRecognizer.js
- 语音识别管理audio/
)AudioStream.js
- 音频流管理AudioStreamFactory.js
- 音频流工厂RiffPcmEncoder.js
- PCM编码器WavFileReader.js
- WAV文件读取utils/
)platform.js
- 平台检测permissions.js
- 权限检查config.js
- 配置管理import { startSystemAudioRecognition } from './src/index.js';
const recognition = await startSystemAudioRecognition({
apiKey: 'your-azure-api-key',
region: 'your-azure-region',
language: 'zh-CN',
output: './audio.pcm', // 可选,默认为 ./cache.pcm
// 新增:可选的事件处理器
eventHandlers: {
onRecognized: (s, e) => {
if (e.result.reason === 3 /* sdk.ResultReason.RecognizedSpeech */ && e.result.text) {
console.log(`[README示例] 已识别: ${e.result.text}`);
}
},
onSessionStopped: (s, e) => {
console.log("[README示例] 语音识别会话已停止。");
},
onCanceled: (s, e) => {
console.error(`[README示例] 识别被取消或出错: ${e.errorDetails}`);
}
}
});
// recognition 是 SystemAudioRecognition 的实例
// 你可以在需要的时候调用 recognition.stop()
// 例如,在一个按钮点击事件或者超时之后
// setTimeout(async () => {
// if (recognition.running) {
// console.log("[README示例] 正在停止识别...");
// await recognition.stop();
// }
// }, 60000); // 示例:60秒后停止
// startSystemAudioRecognition 返回的 Promise 会在识别会话结束后 resolve
// 或者在启动过程中发生无法恢复的错误时 reject
// 注意:如果提供了 onSessionStopped,Promise 的 resolve 与该事件的触发紧密相关
重要提示: 如果您提供了自定义的事件处理器 (如 onRecognized
, onCanceled
, onSessionStopped
等),它们将覆盖库内部的默认行为(例如,默认的控制台日志记录)。错误和识别结果应在您提供的回调中处理。
SystemAudioRecognition
类)import { SystemAudioRecognition } from './src/index.js';
// import * as sdk from "microsoft-cognitiveservices-speech-sdk"; // 如果需要访问 sdk.ResultReason 等
const recognition = new SystemAudioRecognition();
const options = {
apiKey: 'your-azure-api-key',
region: 'your-azure-region',
language: 'zh-CN',
output: './audio.pcm',
eventHandlers: {
onRecognized: (s, e) => {
// 假设 sdk.ResultReason.RecognizedSpeech 的值为 3
if (e.result.reason === 3 && e.result.text) {
console.log(`[高级示例] 已识别: ${e.result.text}`);
}
},
onSessionStarted: (s, e) => {
console.log("[高级示例] 会话开始");
},
onSessionStopped: (s, e) => {
console.log("[高级示例] 会话停止");
},
onCanceled: (s, e) => {
console.error(`[高级示例] 取消/错误: ${e.errorDetails}`);
}
}
};
(async () => {
try {
await recognition.start(options);
console.log("[高级示例] SystemAudioRecognition.start 完成 (通常意味着会话已结束)");
} catch (error) {
console.error('[高级示例] 启动或识别过程中出错:', error);
}
})();
// 在适当的时候停止
// setTimeout(async () => {
// if (recognition.running) {
// await recognition.stop();
// }
// }, 30000);
SpeechRecognizer
)适用于不涉及系统录音,仅处理现有音频文件(如 .pcm
或 .wav
)的场景。
import { SpeechRecognizer } from './src/stt.js'; // 或者从 './core/SpeechRecognizer.js'
// import * as sdk from "microsoft-cognitiveservices-speech-sdk";
const config = {
apiKey: 'your-azure-api-key',
region: 'your-azure-region',
language: 'zh-CN'
};
const eventHandlers = {
onRecognized: (s, e) => {
// 假设 sdk.ResultReason.RecognizedSpeech 的值为 3
if (e.result.reason === 3 && e.result.text) {
console.log(`[SpeechRecognizer示例] 已识别: ${e.result.text}`);
}
},
onSessionStopped: (s, e) => {
console.log("[SpeechRecognizer示例] 会话已停止。");
},
onCanceled: (s, e) => {
console.error(`[SpeechRecognizer示例] 取消/错误: ${e.errorDetails}`);
}
};
const recognizer = new SpeechRecognizer(config);
(async () => {
try {
// 假设 audio.pcm 是一个有效的PCM音频文件
// audioConfig 是必需的,至少包含 sampleRate, bitsPerSample, channels
const audioConfig = { sampleRate: 16000, bitsPerSample: 16, channels: 1 };
await recognizer.start('audio.pcm', audioConfig, eventHandlers);
console.log('[SpeechRecognizer示例] 识别流程已启动并结束。');
} catch (error) {
console.error('[SpeechRecognizer示例] 识别失败:', error);
} finally {
if (recognizer.running) {
await recognizer.stop();
}
}
})();
eventHandlers
增强灵活性eventHandlers
来定制化处理识别过程中的各个事件FAQs
Loop system audio on macOS and windows to other applications. Includes prebuilt binaries.
We found that system-audio-loopback-prebuilt demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.