↓跳过正文
  1. 文字/

用 Obsidian + Gemini + Azure TTS 实现笔记翻译、朗读自动化

·1719 字·4 分钟
效率 Obsidian QuickAdd Gemini Azure TTS 英语学习
Kyo
作者
Kyo
0.5x~2x
目录

李笑来介绍了一种英语学习的方法:

  1. 先用母语写下差不多 100~150 字,记录并整理自己想说的话;随着任务的推进,逐步增加篇幅;
  2. 使用 AI 辅助工具将其转换成英文表达;
  3. 使用 AI 辅助工具将英文文字转换成语音文件;
  4. 跟读、朗读、直至完整背诵……

核心在于内容是“自己写的”,并通过高强度重复(背诵)来塑造语音和记忆。而如果你正好每天有用 Obsidian 做笔记/日记的习惯,那么可以借助 QuickAdd 插件、Gemini API、Azure TTS,直接在 Obsidian 里很方便地实现。

效果演示
#

Obsidian-1
Obsidian-2

准备工作:获取 API Key
#

1. 获取 Google Gemini API Key (用于翻译)
#

  • 费用:截至2025年12月底,推荐使用的模型是 gemini-3-flash-preview,每天少于 20 次调用是免费的,超出的费用也不贵。
  • 步骤:
    1. 访问 Google AI Studio。
    2. 登录 Google 账号。
    3. 点击 Create API key。
    4. 复制 API key 备用。

2. 获取 Microsoft Azure TTS Key (用于语音)
#

  • 费用:每月 50 万字符免费额度(F0 层级)。
  • 步骤:
    1. 注册并登录 Azure 门户,即使有微软账号,也需要单独注册 Azure,过程中需要绑定 Visa 或 Master Card。
    2. 搜索并创建资源:Speech services (语音服务)。点击 Create (创建)。
    3. 关键设置:
      • Subscription (订阅): 选择 “Azure subscription 1” (默认)。
      • Resource Group: 点击 “Create new”,随便起个名字比如 ObsidianTTS。
      • Region (区域):推荐选 East Asia (东亚)。记住你选的区域。
      • Name: 随便起名,如 MyEnglishTrainer2025。
      • Pricing tier (价格层):选择 Free F0。
    4. 资源创建完成后,点击 “Go to resource” (转到资源),点击左侧 Keys and Endpoint。
    5. 复制 KEY 1 和 Region (如 eastasia)。

Azure-1


插件配置:QuickAdd
#

1. 安装与设置
#

  1. 在 Obsidian 社区插件市场搜索并安装 QuickAdd。
  2. 进入 QuickAdd 设置页面,开启 Disable AI & Online features 选项。

2. 创建脚本文件
#

在你的电脑文件管理器中(不要在 Obsidian 内直接新建),进入 Obsidian 库的任意文件夹(如 Scripts),新建一个文本文件并重命名为 english_trainer.js。

复制以下代码到文件中,修改其中的 Key :

/* 
  文件名: english_trainer.js
  功能: Gemini 翻译 -> Azure TTS (支持语速调节) -> 插入笔记
*/

module.exports = async (params) => {
    const { app } = params;
    
    // ================== 核心配置区 (请修改这里) ==================
    
    // 1. Gemini Key (翻译)
    const GEMINI_API_KEY = "AIzaSyXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // 替换你的 Key

    // 2. Azure Key (语音)
    const AZURE_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // 替换你的 Key1
    const AZURE_REGION = "eastus"; // 替换你的区域 (如 eastus, japaneast)
    
    // 3. 语音设置
    // 推荐: en-US-AndrewNeural (男), en-US-AvaNeural (女)
    const AZURE_VOICE = "en-US-AndrewNeural"; 
    
    // 语速控制: "-15%" (推荐跟读), "0%" (正常), "-30%" (慢速)
    const SPEECH_RATE = "-15%"; 
    
    // ===========================================================

    const GEMINI_MODEL = "gemini-3-flash-preview"; 
    const editor = app.workspace.activeLeaf.view.editor;
    const selection = editor.getSelection();

    if (!selection) { new Notice("❌ 请先选中一段中文文本!"); return; }
    if (AZURE_KEY.includes("xxxx")) { new Notice("❌ 请先配置脚本中的 API Key"); return; }

    new Notice("✨ 正在翻译 (Gemini)...");

    try {
        // --- 1. Gemini 翻译 ---
        const geminiUrl = `https://generativelanguage.googleapis.com/v1beta/models/${GEMINI_MODEL}:generateContent?key=${GEMINI_API_KEY}`;
        const geminiResponse = await requestUrl({
            url: geminiUrl, method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                contents: [{ parts: [{ text: `Translate into natural spoken English, as if it were something a real person would say in daily conversation, prioritize natural phrasing over literal translation (only output English): "${selection}"` }] }]
            })
        });

        const translatedText = geminiResponse.json.candidates?.[0]?.content?.parts?.[0]?.text?.trim();
        if (!translatedText) throw new Error("翻译结果为空");

        // --- 2. Azure TTS 生成 ---
        new Notice(`🎙️ 生成语音 (语速 ${SPEECH_RATE})...`);

        const ssml = `
            <speak version='1.0' xml:lang='en-US'>
                <voice xml:lang='en-US' xml:gender='Male' name='${AZURE_VOICE}'>
                    <prosody rate='${SPEECH_RATE}'>
                        ${translatedText}
                    </prosody>
                </voice>
            </speak>`;

        const azureUrl = `https://${AZURE_REGION}.tts.speech.microsoft.com/cognitiveservices/v1`;
        
        const audioResponse = await requestUrl({
            url: azureUrl,
            method: 'POST',
            headers: {
                'Ocp-Apim-Subscription-Key': AZURE_KEY,
                'Content-Type': 'application/ssml+xml',
                'X-Microsoft-OutputFormat': 'audio-16khz-128kbitrate-mono-mp3',
                'User-Agent': 'ObsidianQuickAdd'
            },
            body: ssml
        });

        if (audioResponse.status !== 200) throw new Error(`Azure Error: ${audioResponse.status}`);

        // --- 3. 保存与输出 ---
        const arrayBuffer = audioResponse.arrayBuffer;
        const AUDIO_FOLDER = "English_Audio"; 
        
        if (!app.vault.getAbstractFileByPath(AUDIO_FOLDER)) await app.vault.createFolder(AUDIO_FOLDER);
        const fileName = `${AUDIO_FOLDER}/Speech_${moment().format("YYYYMMDD_HHmmss")}.mp3`;
        await app.vault.createBinary(fileName, arrayBuffer);

        const output = `

//选中原文后等待生成并覆盖原文的意思。如果选中后光标定位到别的行,那就可以删掉这行,不必覆盖。
${selection}  

🇺🇸 英文  
${translatedText}  

> [!TIP] 🎧 语音训练 (Azure)
> ![[${fileName}]]
`;
        editor.replaceSelection(output);
        new Notice("✅ 完成!");

    } catch (error) {
        console.error(error);
        new Notice(`❌ 失败: ${error.message}`);
    }
};  

如果你想生成两种版本的翻译,可以替换为下面的提示词:


                contents: [{ parts: [{ text: `Translate the user input into natural, everyday conversational English that a real native speaker would actually say. Prioritize idiomatic phrasing, rhythm, and flow over literal translation, and freely uses common spoken structures, contractions, and phrasal verbs where appropriate. For each input, it provides two version: 1. A concise, natural version suitable for casual daily conversation. 2. A slightly expanded version that sounds fluent and relaxed, as if spoken aloud. The output should be English only, translated texts only. User input: "${selection}"` }] }]

3. 配置 Macro
#

回到 QuickAdd 设置界面。

在 Name 填入一个名字,如 English Trainer,Type 选择 Macro,点击 Add Choice。

Obsidian-3

点击新增条目右侧的 齿轮 (Configure) 图标,在 User Scripts 列表中找到 english_trainer.js,点击 Add。

Obsidian-4

退出配置页,点击条目右侧的 闪电 (Lightning) 图标,激活命令。

Obsidian-5

使用方法
#

用鼠标选中 Obsidian 日记中的段落,按下 Cmd + P (Windows 为 Ctrl + P),模糊检索定位到 English Trainer 并回车。

Obsidian-6

等待几秒,你的原文下方就会出现地道英文翻译 + 慢速跟读音频。

Obsidian-2

你可以戴上耳机,开始跟读、背诵。也可以把音频保存到手机上,随时随地练习。

相关文章

梦中梦与虚拟机
·1742 字·4 分钟
随想
当我们说「撑住」的时候最好真的过去撑一下
·1535 字·4 分钟
随想
坂本龙一漫谈
·613 字·2 分钟
随想