mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-29 12:22:44 +08:00
编排新增音频分类,支持朗读文本、播放音频、录制音频、播放系统音效
This commit is contained in:
parent
378ae7f92f
commit
f9a1aefff6
@ -7,6 +7,7 @@ const quickcomposer = {
|
||||
coding: require("./quickcomposer/coding"),
|
||||
math: require("./quickcomposer/math"),
|
||||
ui: require("./quickcomposer/ui"),
|
||||
audio: require("./quickcomposer/audio"),
|
||||
};
|
||||
|
||||
module.exports = quickcomposer;
|
||||
|
9
plugin/lib/quickcomposer/audio/index.js
Normal file
9
plugin/lib/quickcomposer/audio/index.js
Normal file
@ -0,0 +1,9 @@
|
||||
const speech = require("./speech");
|
||||
const media = require("./media");
|
||||
const record = require("./record");
|
||||
|
||||
module.exports = {
|
||||
speech,
|
||||
media,
|
||||
...record,
|
||||
};
|
164
plugin/lib/quickcomposer/audio/media.js
Normal file
164
plugin/lib/quickcomposer/audio/media.js
Normal file
@ -0,0 +1,164 @@
|
||||
const { spawn } = require("child_process");
|
||||
const fs = require("fs");
|
||||
|
||||
// 存储当前播放的音频实例
|
||||
let currentAudio = null;
|
||||
|
||||
// 系统音效映射
|
||||
const SYSTEM_SOUNDS = {
|
||||
beep: {
|
||||
win: "Beep",
|
||||
mac: "Ping.aiff",
|
||||
},
|
||||
error: {
|
||||
win: "Asterisk",
|
||||
mac: "Basso.aiff",
|
||||
},
|
||||
warning: {
|
||||
win: "Exclamation",
|
||||
mac: "Sosumi.aiff",
|
||||
},
|
||||
notification: {
|
||||
win: "Notification",
|
||||
mac: "Glass.aiff",
|
||||
},
|
||||
complete: {
|
||||
win: "SystemAsterisk",
|
||||
mac: "Hero.aiff",
|
||||
},
|
||||
click: {
|
||||
win: "MenuCommand",
|
||||
mac: "Tink.aiff",
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* 播放音频文件
|
||||
* @param {string} file 音频文件路径
|
||||
* @param {number} volume 音量 (0-1)
|
||||
* @param {boolean} loop 是否循环播放
|
||||
* @param {boolean} autoplay 是否自动播放
|
||||
*/
|
||||
async function play(file, volume = 1, loop = false, autoplay = true) {
|
||||
// 停止当前音频
|
||||
stop();
|
||||
|
||||
// 检查文件是否存在
|
||||
if (!fs.existsSync(file)) {
|
||||
throw new Error(`音频文件不存在: ${file}`);
|
||||
}
|
||||
|
||||
// 创建新的音频实例
|
||||
const audio = new Audio();
|
||||
audio.src = `file://${file}`;
|
||||
audio.volume = parseFloat(volume) || 1;
|
||||
audio.loop = !!loop;
|
||||
|
||||
// 保存当前实例
|
||||
currentAudio = audio;
|
||||
|
||||
// 如果设置了自动播放
|
||||
if (autoplay !== false) {
|
||||
audio.play();
|
||||
}
|
||||
|
||||
// 返回 Promise,在播放结束时 resolve
|
||||
return new Promise((resolve, reject) => {
|
||||
audio.onended = () => {
|
||||
if (!audio.loop) {
|
||||
currentAudio = null;
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
audio.onerror = (error) => {
|
||||
currentAudio = null;
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 播放系统音效
|
||||
* @param {string} type 音效类型
|
||||
* @param {number} volume 音量 (0-1)
|
||||
*/
|
||||
async function beep(type = "beep", volume = 1) {
|
||||
// 在 Windows 上使用 PowerShell 播放系统音效
|
||||
if (process.platform === "win32") {
|
||||
const soundName = SYSTEM_SOUNDS[type]?.win || SYSTEM_SOUNDS.beep.win;
|
||||
// 使用系统音效
|
||||
const script = `[System.Media.SystemSounds]::${soundName}.Play()`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const ps = spawn("powershell", ["-Command", script]);
|
||||
ps.on("close", (code) => {
|
||||
if (code === 0) resolve();
|
||||
else reject(new Error(`PowerShell 命令执行失败,退出码: ${code}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
// 在 macOS 上使用 afplay 播放系统音效
|
||||
else if (process.platform === "darwin") {
|
||||
const soundName = SYSTEM_SOUNDS[type]?.mac || SYSTEM_SOUNDS.beep.mac;
|
||||
volume = parseFloat(volume) || 1;
|
||||
return new Promise((resolve, reject) => {
|
||||
const afplay = spawn("afplay", [
|
||||
`/System/Library/Sounds/${soundName}`,
|
||||
"-v",
|
||||
volume,
|
||||
]);
|
||||
afplay.on("close", (code) => {
|
||||
if (code === 0) resolve();
|
||||
else reject(new Error(`afplay 命令执行失败,退出码: ${code}`));
|
||||
});
|
||||
});
|
||||
}
|
||||
// 在其他平台上使用 utools.shellBeep
|
||||
else {
|
||||
utools.shellBeep();
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止音频播放
|
||||
*/
|
||||
function stop() {
|
||||
if (currentAudio) {
|
||||
currentAudio.pause();
|
||||
currentAudio.currentTime = 0;
|
||||
currentAudio = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分析音频文件
|
||||
* @param {string} file 音频文件路径
|
||||
* @returns {Promise<Object>} 音频信息
|
||||
*/
|
||||
async function analyze(file) {
|
||||
if (!fs.existsSync(file)) {
|
||||
throw new Error(`音频文件不存在: ${file}`);
|
||||
}
|
||||
|
||||
const audio = new Audio();
|
||||
audio.src = `file://${file}`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
audio.onloadedmetadata = () => {
|
||||
resolve({
|
||||
duration: audio.duration,
|
||||
channels: audio.mozChannels || 2,
|
||||
sampleRate: audio.mozSampleRate || 44100,
|
||||
});
|
||||
};
|
||||
audio.onerror = reject;
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
play,
|
||||
beep,
|
||||
stop,
|
||||
analyze,
|
||||
};
|
44
plugin/lib/quickcomposer/audio/record.js
Normal file
44
plugin/lib/quickcomposer/audio/record.js
Normal file
@ -0,0 +1,44 @@
|
||||
const fs = require("fs");
|
||||
|
||||
/**
|
||||
* 录制音频
|
||||
* @param {number} duration 录制时长(ms)
|
||||
* @param {string} savePath 保存路径
|
||||
*/
|
||||
async function record(duration = 5000, savePath) {
|
||||
const format = "audio/webm";
|
||||
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
const mediaRecorder = new MediaRecorder(stream, { mimeType: format });
|
||||
const chunks = [];
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
mediaRecorder.ondataavailable = (e) => chunks.push(e.data);
|
||||
mediaRecorder.onstop = async () => {
|
||||
try {
|
||||
const blob = new Blob(chunks, { type: format });
|
||||
if (savePath) {
|
||||
// 使用 FileReader 读取 blob 数据
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => {
|
||||
const buffer = Buffer.from(reader.result);
|
||||
fs.writeFileSync(savePath, buffer);
|
||||
};
|
||||
reader.readAsArrayBuffer(blob);
|
||||
}
|
||||
stream.getTracks().forEach((track) => track.stop());
|
||||
resolve(blob);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
};
|
||||
mediaRecorder.onerror = reject;
|
||||
|
||||
mediaRecorder.start();
|
||||
setTimeout(() => mediaRecorder.stop(), duration);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
record,
|
||||
};
|
59
plugin/lib/quickcomposer/audio/speech.js
Normal file
59
plugin/lib/quickcomposer/audio/speech.js
Normal file
@ -0,0 +1,59 @@
|
||||
// 存储当前朗读实例
|
||||
let currentSpeech = null;
|
||||
|
||||
/**
|
||||
* 朗读文本
|
||||
* @param {string} text 要朗读的文本
|
||||
* @param {Object} options 朗读选项
|
||||
* @param {number} options.rate 语速 (0.1-10)
|
||||
* @param {number} options.pitch 音调 (0-2)
|
||||
* @param {number} options.volume 音量 (0-1)
|
||||
* @param {string} options.lang 语言
|
||||
*/
|
||||
async function speak(text, options = {}) {
|
||||
// 停止当前朗读
|
||||
stop();
|
||||
|
||||
// 创建新的语音合成实例
|
||||
const speech = new window.SpeechSynthesisUtterance();
|
||||
speech.text = text;
|
||||
speech.rate = parseFloat(options.rate) || 1;
|
||||
speech.pitch = parseFloat(options.pitch) || 1;
|
||||
speech.volume = parseFloat(options.volume) || 1;
|
||||
speech.lang = options.lang || "zh-CN";
|
||||
|
||||
// 保存当前实例
|
||||
currentSpeech = speech;
|
||||
|
||||
// 开始朗读
|
||||
window.speechSynthesis.speak(speech);
|
||||
|
||||
// 返回 Promise,在朗读结束时 resolve
|
||||
return new Promise((resolve, reject) => {
|
||||
speech.onend = () => {
|
||||
currentSpeech = null;
|
||||
resolve();
|
||||
};
|
||||
speech.onerror = (error) => {
|
||||
currentSpeech = null;
|
||||
reject(error);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止文本朗读
|
||||
*/
|
||||
function stop() {
|
||||
if (window.speechSynthesis) {
|
||||
window.speechSynthesis.cancel();
|
||||
}
|
||||
if (currentSpeech) {
|
||||
currentSpeech = null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
speak,
|
||||
stop,
|
||||
};
|
@ -45,6 +45,7 @@
|
||||
<q-select
|
||||
v-else-if="config.type === 'select'"
|
||||
filled
|
||||
dense
|
||||
emit-value
|
||||
map-options
|
||||
:model-value="values[index]"
|
||||
@ -58,6 +59,7 @@
|
||||
<q-input
|
||||
v-else-if="config.type === 'input'"
|
||||
filled
|
||||
dense
|
||||
:model-value="values[index]"
|
||||
@update:model-value="$emit('update', index, $event)"
|
||||
v-bind="config"
|
||||
|
227
src/js/composer/commands/audioCommands.js
Normal file
227
src/js/composer/commands/audioCommands.js
Normal file
@ -0,0 +1,227 @@
|
||||
import { newVarInputVal } from "js/composer/varInputValManager";
|
||||
|
||||
// 系统音效选项
|
||||
const SYSTEM_SOUNDS = [
|
||||
{ label: "提示音", value: "beep" },
|
||||
{ label: "错误音", value: "error" },
|
||||
{ label: "警告音", value: "warning" },
|
||||
{ label: "通知音", value: "notification" },
|
||||
{ label: "完成音", value: "complete" },
|
||||
{ label: "点击音", value: "click" },
|
||||
];
|
||||
|
||||
// 语音朗读配置
|
||||
const SPEECH_CONFIG = {
|
||||
label: "朗读配置",
|
||||
type: "dictEditor",
|
||||
icon: "settings",
|
||||
width: 12,
|
||||
defaultValue: {
|
||||
rate: newVarInputVal("var", "1"),
|
||||
pitch: newVarInputVal("var", "1"),
|
||||
volume: newVarInputVal("var", "1"),
|
||||
lang: newVarInputVal("str", "zh-CN"),
|
||||
},
|
||||
options: {
|
||||
fixedKeys: [
|
||||
{ value: "rate", label: "语速(0.1-10)" },
|
||||
{ value: "pitch", label: "音调(0-2)" },
|
||||
{ value: "volume", label: "音量(0-1)" },
|
||||
{ value: "lang", label: "语言" },
|
||||
],
|
||||
disableAdd: true,
|
||||
},
|
||||
};
|
||||
|
||||
export const audioCommands = {
|
||||
label: "音频操作",
|
||||
icon: "volume_up",
|
||||
defaultOpened: false,
|
||||
commands: [
|
||||
{
|
||||
value: "quickcomposer.audio.speech.speak",
|
||||
label: "文本朗读",
|
||||
desc: "系统语音朗读",
|
||||
icon: "record_voice_over",
|
||||
subCommands: [
|
||||
{
|
||||
value: "quickcomposer.audio.speech.speak",
|
||||
label: "朗读文本",
|
||||
icon: "record_voice_over",
|
||||
config: [
|
||||
{
|
||||
label: "朗读文本",
|
||||
type: "varInput",
|
||||
icon: "text_fields",
|
||||
width: 12,
|
||||
},
|
||||
SPEECH_CONFIG,
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "quickcomposer.audio.speech.stop",
|
||||
label: "停止朗读",
|
||||
icon: "voice_over_off",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "quickcomposer.audio.media.play",
|
||||
label: "音频播放",
|
||||
desc: "播放音频文件",
|
||||
icon: "music_note",
|
||||
subCommands: [
|
||||
{
|
||||
value: "quickcomposer.audio.media.play",
|
||||
label: "播放音频",
|
||||
icon: "play_circle",
|
||||
config: [
|
||||
{
|
||||
label: "音频文件路径",
|
||||
type: "varInput",
|
||||
icon: "audio_file",
|
||||
width: 12,
|
||||
options: {
|
||||
dialog: {
|
||||
type: "open",
|
||||
options: {
|
||||
title: "选择音频文件",
|
||||
filters: [
|
||||
{
|
||||
name: "音频文件",
|
||||
extensions: ["mp3", "wav", "ogg", "m4a", "aac"],
|
||||
},
|
||||
],
|
||||
properties: ["openFile", "showHiddenFiles"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "音量",
|
||||
type: "numInput",
|
||||
icon: "volume_up",
|
||||
width: 4,
|
||||
defaultValue: 1,
|
||||
min: 0,
|
||||
max: 1,
|
||||
step: 0.1,
|
||||
},
|
||||
{
|
||||
label: "循环播放",
|
||||
type: "switch",
|
||||
icon: "repeat",
|
||||
width: 4,
|
||||
defaultValue: false,
|
||||
},
|
||||
{
|
||||
label: "自动播放",
|
||||
type: "switch",
|
||||
icon: "play_circle",
|
||||
width: 4,
|
||||
defaultValue: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "quickcomposer.audio.media.stop",
|
||||
label: "停止播放",
|
||||
icon: "stop",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "quickcomposer.audio.record",
|
||||
label: "音频录制",
|
||||
desc: "录制系统音频",
|
||||
icon: "mic",
|
||||
config: [
|
||||
{
|
||||
label: "录制时长(ms)",
|
||||
type: "numInput",
|
||||
icon: "timer",
|
||||
width: 6,
|
||||
defaultValue: 5000,
|
||||
min: 1000,
|
||||
step: 1000,
|
||||
},
|
||||
{
|
||||
label: "保存路径",
|
||||
type: "varInput",
|
||||
icon: "save",
|
||||
width: 6,
|
||||
options: {
|
||||
dialog: {
|
||||
type: "save",
|
||||
options: {
|
||||
title: "保存录音",
|
||||
filters: [
|
||||
{
|
||||
name: "音频文件",
|
||||
extensions: ["webm"],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "quickcomposer.audio.media.beep",
|
||||
label: "系统音效",
|
||||
desc: "播放系统内置音效",
|
||||
icon: "notifications_active",
|
||||
config: [
|
||||
{
|
||||
label: "音效类型",
|
||||
type: "select",
|
||||
icon: "music_note",
|
||||
width: 6,
|
||||
options: SYSTEM_SOUNDS,
|
||||
defaultValue: "beep",
|
||||
},
|
||||
{
|
||||
label: "音量",
|
||||
type: "numInput",
|
||||
icon: "volume_up",
|
||||
width: 6,
|
||||
defaultValue: 1,
|
||||
min: 0,
|
||||
max: 1,
|
||||
step: 0.1,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "quickcomposer.audio.media.analyze",
|
||||
label: "音频信息",
|
||||
desc: "分析音频文件信息",
|
||||
icon: "analytics",
|
||||
isAsync: true,
|
||||
config: [
|
||||
{
|
||||
label: "音频文件",
|
||||
type: "varInput",
|
||||
icon: "audio_file",
|
||||
width: 12,
|
||||
options: {
|
||||
dialog: {
|
||||
type: "open",
|
||||
options: {
|
||||
title: "选择音频文件",
|
||||
filters: [
|
||||
{
|
||||
name: "音频文件",
|
||||
extensions: ["mp3", "wav", "ogg", "m4a", "aac"],
|
||||
},
|
||||
],
|
||||
properties: ["openFile", "showHiddenFiles"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
@ -67,5 +67,24 @@ export const fileCommands = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
value: "utools.shellTrashItem",
|
||||
label: "删除文件到回收站",
|
||||
icon: "delete",
|
||||
config: [
|
||||
{
|
||||
key: "path",
|
||||
label: "文件或文件夹的绝对路径",
|
||||
type: "varInput",
|
||||
icon: "folder_open",
|
||||
options: {
|
||||
dialog: {
|
||||
type: "open",
|
||||
options: {},
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
@ -12,11 +12,14 @@ import { mathCommands } from "./mathCommands";
|
||||
import { userdataCommands } from "./userdataCommands";
|
||||
import { utoolsCommands } from "./utoolsCommand";
|
||||
import { screenCommands } from "./screenCommands";
|
||||
import { audioCommands } from "./audioCommands";
|
||||
console.log(audioCommands);
|
||||
|
||||
export const commandCategories = [
|
||||
fileCommands,
|
||||
networkCommands,
|
||||
systemCommands,
|
||||
audioCommands,
|
||||
notifyCommands,
|
||||
utoolsCommands,
|
||||
dataCommands,
|
||||
|
@ -371,14 +371,21 @@ export const systemCommands = {
|
||||
label: "进程ID",
|
||||
type: "numInput",
|
||||
icon: "developer_board",
|
||||
width: 6,
|
||||
width: 7,
|
||||
},
|
||||
{
|
||||
label: "信号",
|
||||
type: "varInput",
|
||||
type: "select",
|
||||
icon: "signal_cellular_alt",
|
||||
defaultValue: newVarInputVal("str", "SIGTERM"),
|
||||
width: 6,
|
||||
options: [
|
||||
{ label: "正常终止 (15)", value: "SIGTERM" },
|
||||
{ label: "强制终止 (9)", value: "SIGKILL" },
|
||||
{ label: "中断进程 (2)", value: "SIGINT" },
|
||||
{ label: "退出信号 (3)", value: "SIGQUIT" },
|
||||
{ label: "挂起信号 (1)", value: "SIGHUP" },
|
||||
],
|
||||
defaultValue: "SIGKILL",
|
||||
width: 5,
|
||||
},
|
||||
],
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user