Skip to content
RobotXTeamPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

OpenASR

Current runtime: systemd-managed F8 hotkey + D-Bus ASR service + independent Qt OSD. The GNOME Shell extension is kept in the source tree for reference, but the live workflow disables it because GNOME Shell JS caching and clipboard paths were unreliable.

Install the .deb, then enable the desktop user services:

sudo apt install ./openasr_0.1.0_amd64.deb
openasr-install-user

Use F8 to start/stop recording and Esc to cancel.

Speech2Text for GNOME

系统级、本地离线语音输入方案,专为 Ubuntu 22.04/24.04 GNOME 桌面环境设计。

架构

┌─────────────────────────────────────────────────────────┐
│                    用户操作层                              │
│                  按下 F2 快捷键                            │
└──────────────────────┬──────────────────────────────────┘
                       │
┌──────────────────────▼──────────────────────────────────┐
│              GNOME Shell Extension                        │
│  ┌─────────┐  ┌──────────┐  ┌─────────────────────┐     │
│  │ F2 快捷键│  │ OSD 动画  │  │ D-Bus Proxy 客户端   │     │
│  └─────────┘  └──────────┘  └──────────┬──────────┘     │
└─────────────────────────────────────────┼────────────────┘
                                          │ D-Bus Session Bus
┌─────────────────────────────────────────▼────────────────┐
│              D-Bus Service (org.speech2text.Service)       │
│  ┌──────────────┐  ┌──────────────┐  ┌───────────────┐  │
│  │ State Machine │  │ Audio Record │  │ ASR Provider  │  │
│  │ idle/recording│  │  16kHz PCM   │  │   Registry    │  │
│  │ /transcribing │  │              │  │               │  │
│  └──────────────┘  └──────────────┘  └───────┬───────┘  │
└──────────────────────────────────────────────┼───────────┘
                                               │
┌──────────────────────────────────────────────▼───────────┐
│                ASR Provider Layer                          │
│  ┌──────────────┐ ┌──────────┐ ┌──────────┐ ┌────────┐  │
│  │Faster-Whisper │ │ Whisper  │ │whisper.cpp│ │  Vosk  │  │
│  │  (默认)      │ │(OpenAI)  │ │  (C++)   │ │(轻量级)│  │
│  └──────────────┘ └──────────┘ └──────────┘ └────────┘  │
│  ┌──────────────┐ ┌──────────┐ ┌──────────┐             │
│  │ Moonshine    │ │Parakeet  │ │ SenseVoice│  ...更多    │
│  │  (未来扩展)  │ │(未来扩展) │ │ (未来扩展) │             │
│  └──────────────┘ └──────────┘ └──────────┘             │
└──────────────────────────────────────────────────────────┘
                       │
┌──────────────────────▼──────────────────────────────────┐
│              Text Injection Layer                          │
│  ┌──────────────────────┐  ┌──────────────────────┐     │
│  │ Clutter VirtualDevice │  │ wl-clipboard fallback│     │
│  │  (compositor级注入)   │  │  (剪贴板+Ctrl+V)    │     │
│  └──────────────────────┘  └──────────────────────┘     │
└──────────────────────────────────────────────────────────┘
                       │
┌──────────────────────▼──────────────────────────────────┐
│              当前焦点应用                                   │
│  Chrome / Firefox / VSCode / Telegram / WeChat / ...      │
└──────────────────────────────────────────────────────────┘

目录结构

speech2text-gnome/
├── README.md                          # 本文档
├── gnome-extension/                   # GNOME Shell Extension
│   ├── metadata.json                  # 扩展元数据
│   ├── extension.js                   # 主扩展逻辑 (热键+OSD+D-Bus+注入)
│   ├── stylesheet.css                 # GNOME 原生风格样式
│   ├── prefs.js                       # 偏好设置页面
│   └── schemas/
│       └── *.gschema.xml              # GSettings 配置 schema
├── dbus-service/                      # Python D-Bus 服务
│   ├── service.py                     # 主服务入口
│   ├── audio_recorder.py              # 音频录制 (GStreamer/sounddevice)
│   └── providers/                     # ASR 提供者层
│       ├── __init__.py
│       ├── base.py                    # BaseASRProvider 抽象接口
│       ├── registry.py                # 提供者注册与管理
│       ├── faster_whisper_provider.py # Faster-Whisper 实现
│       ├── whisper_provider.py        # OpenAI Whisper 实现
│       ├── whisper_cpp_provider.py    # whisper.cpp 实现
│       └── vosk_provider.py           # Vosk 实现
├── dbus-interfaces/                   # D-Bus 接口定义
│   ├── org.speech2text.Service.xml    # D-Bus 接口 XML
│   └── org.speech2text.service        # D-Bus 自启动服务文件
├── systemd/
│   └── speech2text.service            # systemd 用户服务
├── schemas/
│   └── org.gnome.shell.extensions.speech2text.gschema.xml
└── scripts/
    ├── install.sh                     # 安装脚本
    └── uninstall.sh                   # 卸载脚本

安装

自动安装

cd ~/speech2text-gnome
bash scripts/install.sh

安装脚本会自动:

  1. 检测系统环境 (Ubuntu 版本、GNOME 版本、GPU、Python)
  2. 安装系统依赖
  3. 安装 Python 依赖 (faster-whisper, sounddevice)
  4. 下载默认语音识别模型
  5. 安装 GNOME Shell 扩展
  6. 配置 D-Bus 服务
  7. 配置 systemd 用户服务
  8. 配置文本注入工具 (ydotool)
  9. 启用扩展和服务

手动安装

如果自动安装失败,可以手动执行:

# 1. 安装系统依赖
sudo apt install python3.10 python3-gi python3-dbus wl-clipboard ydotool libportaudio2

# 2. 安装 Python 依赖
python3.10 -m pip install --user faster-whisper sounddevice

# 3. 将用户加入 input 组 (ydotool 需要)
sudo usermod -aG input $USER
# 然后重新登录

# 4. 安装扩展
cp -r gnome-extension ~/.local/share/gnome-shell/extensions/speech2text@local
glib-compile-schemas ~/.local/share/gnome-shell/extensions/speech2text@local/schemas/

# 5. 启用扩展
gsettings set org.gnome.shell enabled-extensions "$(gsettings get org.gnome.shell enabled-extensions | sed "s/]/, 'speech2text@local']/")"

# 6. 安装并启动服务
cp systemd/speech2text.service ~/.config/systemd/user/
systemctl --user daemon-reload
systemctl --user enable --now speech2text.service

使用方法

基本操作

  1. 按 F2 → 开始录音,屏幕显示"正在录音"OSD
  2. 再次按 F2 → 结束录音,显示"正在识别"OSD
  3. 识别完成 → 文本自动插入当前输入框
  4. 按 F2 (识别完成后) → 开始新的录音

界面说明

状态 OSD 显示 说明
录音中 🔴 正在录音 麦克风图标 + 脉冲动画
识别中 🔍 正在识别 搜索图标 + 淡入淡出
完成 ✅ 识别完成 显示识别文本,2秒后消失
错误 ❌ 识别错误 显示错误信息

配置

配置文件位置

文件 路径 说明
扩展 ~/.local/share/gnome-shell/extensions/speech2text@local/ GNOME 扩展
服务 ~/speech2text-gnome/dbus-service/ Python 服务
模型 ~/.local/share/speech2text/models/ 下载的模型
日志 ~/.local/share/speech2text/logs/ 服务日志
systemd ~/.config/systemd/user/speech2text.service 服务配置
D-Bus ~/.local/share/dbus-1/services/org.speech2text.service D-Bus 配置

切换 ASR 提供者

通过 D-Bus 命令切换:

# 查看可用提供者
gdbus call --session --dest org.speech2text.Service \
  --object-path /org/speech2text/Service \
  --method org.speech2text.Service.ListProviders

# 切换到 OpenAI Whisper
gdbus call --session --dest org.speech2text.Service \
  --object-path /org/speech2text/Service \
  --method org.speech2text.Service.SetProvider "whisper"

# 切换模型
gdbus call --session --dest org.speech2text.Service \
  --object-path /org/speech2text/Service \
  --method org.speech2text.Service.SetModel "medium"

默认提供者

提供者 安装命令 特点
Faster-Whisper pip3 install faster-whisper 默认,CPU 优化,int8 量化
Whisper pip3 install openai-whisper OpenAI 官方,需要 PyTorch
whisper.cpp pip3 install pywhispercpp C++ 实现,轻量
Vosk pip3 install vosk 超轻量,支持多语言

扩展新模型

添加新的 ASR 提供者只需 3 步:

1. 创建提供者文件

在 dbus-service/providers/ 目录下创建新文件,例如 moonshine_provider.py:

from .base import BaseASRProvider, ASRResult

class MoonshineProvider(BaseASRProvider):
    @property
    def name(self) -> str:
        return 'moonshine'

    @property
    def display_name(self) -> str:
        return 'Moonshine'

    # ... 实现其他抽象方法

PROVIDER_CLASS = MoonshineProvider

2. 注册提供者

在 providers/registry.py 的 _PROVIDER_MODULES 中添加:

_PROVIDER_MODULES = {
    'faster-whisper': 'providers.faster_whisper_provider',
    'whisper': 'providers.whisper_provider',
    'moonshine': 'providers.moonshine_provider',  # 新增
}

3. 重启服务

systemctl --user restart speech2text.service

跨平台扩展说明

当前架构已预留跨平台扩展接口:

平台抽象层

platform/
├── __init__.py              # 平台检测
├── linux_gnome.py           # Linux GNOME (当前实现)
├── linux_kde.py             # Linux KDE (未来)
├── windows.py               # Windows (未来)
├── macos.py                 # macOS (未来)
└── base.py                  # 平台接口抽象

需要为新平台实现的接口

  1. 热键注册 - 各平台的全局快捷键 API
  2. OSD 显示 - 各平台的通知/覆盖层 API
  3. 文本注入 - 各平台的键盘模拟 API
  4. 服务通信 - 各平台的 IPC 机制
平台 热键 OSD 文本注入 IPC
Linux GNOME GNOME Shell Extension Clutter/St Clutter VirtualDevice D-Bus
Linux KDE KGlobalAccel KNotification uinput/ydotool D-Bus
Windows RegisterHotKey Toast Notification SendInput Named Pipe
macOS CGEventTap NSUserNotification CGEvent XPC/Unix Socket

服务管理

常用命令

# 查看服务状态
systemctl --user status speech2text.service

# 启动/停止/重启
systemctl --user start speech2text.service
systemctl --user stop speech2text.service
systemctl --user restart speech2text.service

# 查看日志
journalctl --user -u speech2text.service -f

# 查看服务日志
cat ~/.local/share/speech2text/logs/service.log

# 测试 D-Bus 连接
gdbus call --session --dest org.speech2text.Service \
  --object-path /org/speech2text/Service \
  --method org.speech2text.Service.Ping

# 手动触发录音
gdbus call --session --dest org.speech2text.Service \
  --object-path /org/speech2text/Service \
  --method org.speech2text.Service.ToggleRecording

GNOME 扩展管理

# 列出已安装扩展
gnome-extensions list

# 启用/禁用
gnome-extensions enable speech2text@local
gnome-extensions disable speech2text@local

# 查看扩展信息
gnome-extensions info speech2text@local

# 打开扩展偏好设置
gnome-extensions prefs speech2text@local

故障排查

F2 按下无反应

  1. 检查扩展是否启用:

    gnome-extensions list --enabled | grep speech2text
  2. 检查是否有快捷键冲突:

    gsettings list-recursively org.gnome.shell.keybindings | grep F2
  3. 重启 GNOME Shell:

    • X11: 按 Alt+F2,输入 r,回车
    • Wayland: 注销后重新登录

服务无法启动

  1. 查看服务日志:

    journalctl --user -u speech2text.service --no-pager -n 50
  2. 手动启动查看错误:

    PYTHONPATH=~/speech2text-gnome/dbus-service /usr/bin/python3.10 ~/speech2text-gnome/dbus-service/service.py
  3. 检查 Python 依赖:

    python3.10 -c "import faster_whisper; print('OK')"
    python3.10 -c "import dbus; print('OK')"
    python3.10 -c "import gi; print('OK')"

录音没有声音

  1. 检查音频设备:

    arecord -l
    pactl list sources short
  2. 检查麦克风权限:

    # GNOME 隐私设置
    gnome-control-center privacy
  3. 测试录音:

    arecord -d 5 -f S16_LE -r 16000 test.wav
    aplay test.wav

文本无法注入

  1. 检查 ydotool:

    # 检查 daemon
    systemctl --user status ydotoold.service
    
    # 检查 input 组
    groups | grep input
    
    # 测试注入
    echo "test" | wl-copy
    ydotool key 29:1 47:1 47:0 29:0  # Ctrl+V
  2. 如果 ydotool 不工作,扩展回退到 Clutter VirtualDevice,需要检查 GNOME Shell 日志:

    journalctl /usr/bin/gnome-shell -f

识别结果不准确

  1. 确认使用正确的语言模型 (中文建议使用 small 或更大的模型)
  2. 检查录音质量 (安静环境,靠近麦克风)
  3. 尝试更大的模型:
    gdbus call --session --dest org.speech2text.Service \
      --object-path /org/speech2text/Service \
      --method org.speech2text.Service.SetModel "medium"

回滚方法

完全卸载

cd ~/speech2text-gnome
bash scripts/uninstall.sh

手动卸载

# 停止服务
systemctl --user stop speech2text.service
systemctl --user disable speech2text.service

# 删除扩展
rm -rf ~/.local/share/gnome-shell/extensions/speech2text@local

# 删除服务文件
rm -f ~/.config/systemd/user/speech2text.service
rm -f ~/.local/share/dbus-1/services/org.speech2text.service
systemctl --user daemon-reload

# 可选:删除模型和日志
rm -rf ~/.local/share/speech2text

# 可选:移除 input 组
sudo gpasswd -d $USER input

禁用但保留

# 只禁用扩展
gnome-extensions disable speech2text@local

# 只停止服务
systemctl --user stop speech2text.service

系统要求

  • Ubuntu 22.04 LTS 或 24.04 LTS
  • GNOME Shell 42+
  • Python 3.10+ (系统自带)
  • 至少 2GB 可用内存 (用于加载模型)
  • 麦克风 (内置或外接)

性能指标

模型 大小 内存占用 识别速度 (CPU) 准确度
tiny ~75MB ~300MB ~0.3x 实时 ★★☆☆☆
base ~150MB ~500MB ~0.5x 实时 ★★★☆☆
small ~500MB ~1GB ~1x 实时 ★★★★☆
medium ~1.5GB ~2GB ~2x 实时 ★★★★★
large-v3 ~3GB ~4GB ~4x 实时 ★★★★★

识别速度表示处理 1 秒音频需要的时间,1x 表示实时

许可证

MIT License

致谢

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages