跳转到内容

模块开发

制作一个增益模块,从接口声明、打包和试用到配置 GitHub 搜索。

下面从本体源码中的 examples/modules/gain-process 制作一个「我的音量」模块。它读取音频、乘以设置的增益,再把同一时间线上的音频交回处理图。做完可以通过本地 ZIP 安装,接进麦克风或文件线路。

文件 这个例子用它做什么
obliviousmic.module.json 声明唯一模块 ID、版本、兼容范围、运行时、输入与输出及处理能力
settings.schema.json 声明 gain 默认 1、范围 0~2、可热更新。也声明需要重新准备线路的 block
actions.schema.json 这个例子用 {"actions":{}}。声纹登记、资产导入等额外操作可在这里声明
runtime.py 实现 Gain。prepare 接收已验证的初始设置,process 处理当前 PortBundle,update_parameters 接收运行时的增益修改
LICENSE 随示例代码分发的 Apache-2.0 许可证

示例声明 schema_version: 1、package_type: "module",并为 framework 和 Module API 各填一个起止范围。framework.min 和 api.min 包含在范围内,max_exclusive 为上限。platforms/architectures 可在可移植模块中填 ["any"]。示例的 runtime.kind 为 process,entrypoint 为 runtime:Gain,处理发生在 Python 工作进程里。

两个音频端口分别位于 ports.inputs 和 ports.outputs,都用 ID audio,声明 16/48 kHz、1/2 声道和交错 float32。准备耗时的计算、模型和临时资源放在 prepare。在 process 中读取输入的数据包,用 packet.with_samples(...) 生成处理后的包并返回,保留时间信息。模块有内部缓存时,通过 report_latency 声明延迟、flush 在正常结束时排尾、reset 清理旧时间线状态,并用 stop/dispose 回收资源。

需要直接在 PythonAdapter 中运行、接 C ABI 或增加控制/文本端口时,对照源码内的 docs/spec/MODULE_API_V1.md、docs/spec/FOREIGN_RUNTIME_V1.md、docs/spec/TEXT_STREAM_V1.md 与 src/obliviousmic/schemas/obliviousmic-module-v1.json。先让最小线路通过,再增加模型或服务。配置密钥字段时按 Schema 标记 secret。模型初始化可另加 setup.json 与 resources.json,在 README 说明取得方式与许可。

  1. 在 本体源码根目录打开 PowerShell。下面的命令复制样例到本地应用数据下的新目录,填入示例 ID 和名称。发布自己的包前,把 ID、作者和仓库地址改成你自己的值。org.obliviousmic. 和 core. 由框架使用。$omModuleRepository 本地试做时可留空,打算公开发布时应在压缩前填写实际的 GitHub 仓库主页。

    终端窗口
    $omModuleRepository = $null
    $omModuleSource = (Resolve-Path -LiteralPath '.\examples\modules\gain-process').Path
    $omModuleWork = Join-Path $env:LOCALAPPDATA 'ObliviousMicExamples\gain-1.0.0'
    if (Test-Path -LiteralPath $omModuleWork) { throw '示例目录已存在,请换一个目录名。' }
    $omModuleDir = Join-Path $omModuleWork 'package'
    New-Item -ItemType Directory -Path $omModuleDir | Out-Null
    $omModuleFiles = 'obliviousmic.module.json', 'runtime.py',
    'settings.schema.json', 'actions.schema.json', 'LICENSE'
    foreach ($omName in $omModuleFiles) {
    Copy-Item -LiteralPath (Join-Path $omModuleSource $omName) -Destination $omModuleDir
    }
    $omManifestPath = Join-Path $omModuleDir 'obliviousmic.module.json'
    $omManifest = Get-Content -LiteralPath $omManifestPath -Encoding UTF8 -Raw | ConvertFrom-Json
    $omManifest.module_id = 'com.example.my_gain'
    $omManifest.name = 'My Gain'
    $omManifest.author = 'Your name'
    $omManifest.repository = $omModuleRepository
    $omManifest | Add-Member -NotePropertyName display_names -NotePropertyValue @{
    'zh-CN' = '我的音量'
    en = 'My Gain'
    }
    $omManifest | Add-Member -NotePropertyName installation -NotePropertyValue @{ direct = $true }
    $omManifestJson = $omManifest | ConvertTo-Json -Depth 30
    [IO.File]::WriteAllText($omManifestPath, $omManifestJson, [Text.UTF8Encoding]::new($false))
    $omModuleZip = Join-Path $omModuleWork 'my-gain-1.0.0.obmic.zip'
    $omModulePaths = $omModuleFiles | ForEach-Object { Join-Path $omModuleDir $_ }
    Compress-Archive -LiteralPath $omModulePaths -DestinationPath $omModuleZip
    Get-Item -LiteralPath $omModuleZip
  2. 打开 ZIP 核对根目录就能看到 obliviousmic.module.json、runtime.py 和 Schema 文件。提交包时,压缩的是这些文件。用「安装本地模块包」导入,等任务完成后在「已安装模块」找「我的音量」或 My Gain。

  3. 复制直连图,添加该节点。先用耳机路线,连「麦克风输入 → 我的音量 → 音频设备输出」。给模块参数表设置 gain = 0.5,保存、验证并应用。启动会话,确认比 gain = 1 安静。需要测试热更新时,在运行中保存并更新实时参数,然后比较输出电平。

  4. 再复制一张「音频文件输入 → 我的音量 → 音频文件输出」的离线图。用同一段测试 WAV 分别以 1 和 0.5 导出,回听并比较电平。确认实时与离线都能用,再写面向用户的 README。README 至少包含安装依赖、端口如何接、参数默认值和能听出差异的测试办法。

  1. 在你管理的公开仓库维护模块。给仓库写与你的用途相符的名称和简介。在仓库首页 About 旁的设置进入 Topics,添加 obliviousmic-module 并保存。直接搜索能用仓库名称和简介中的词。

  2. 在 GitHub 仓库的 Releases → Draft a new release 中选一个对应版本的 tag,例如 v1.0.0,上传上一步测试过的 .obmic.zip 后发布。公开包内的 repository 填真实的 GitHub 仓库 URL。下次更新递增 module_version 再发布新附件。

  3. 给仓库默认分支根目录准备一份外部分发声明,同名为 obliviousmic.module.json。它以 ZIP 根目录的声明为底稿,额外增加 distribution。该段包含仓库、Release 和附件的数字 ID、tag、附件下载地址、字节数和本地最终 ZIP 的 SHA-256。先上传最终 ZIP,才能取得实际 ID。

  4. 可以沿用上一节窗口里的 $omManifestPath 和 $omModuleZip,在附件已经公开可下载后运行以下命令。将 OWNER/REPO 和 tag 换成自己的。脚本会把外部声明保存到包目录外,便于将这份文件提交到默认分支根目录。

    终端窗口
    $omRepo = 'OWNER/REPO'
    $omReleaseTag = 'v1.0.0'
    $omZip = Get-Item -LiteralPath $omModuleZip -ErrorAction Stop
    $omRepoApi = "https://api.github.com/repos/$omRepo"
    $omGithubHeaders = @{ Accept = 'application/vnd.github+json' }
    $omRepoInfo = Invoke-RestMethod -Uri $omRepoApi -Headers $omGithubHeaders
    $omRelease = Invoke-RestMethod -Uri "$omRepoApi/releases/tags/$omReleaseTag" -Headers $omGithubHeaders
    $omAssets = @($omRelease.assets | Where-Object name -eq $omZip.Name)
    if ($omAssets.Count -ne 1 -or $omAssets[0].size -ne $omZip.Length) {
    throw '请确认 Release 的附件名和字节数与本地 ZIP 一致。'
    }
    $omDigest = (Get-FileHash -LiteralPath $omZip.FullName -Algorithm SHA256).Hash.ToLowerInvariant()
    if ($omAssets[0].digest -and $omAssets[0].digest -ne "sha256:$omDigest") {
    throw '线上附件的 SHA-256 与本地文件不一致。'
    }
    $omExternal = Get-Content -LiteralPath $omManifestPath -Encoding UTF8 -Raw | ConvertFrom-Json
    if ($omExternal.repository -ne $omRepoInfo.html_url) {
    throw '请更新包内仓库声明,重打包并发布对应的 ZIP。'
    }
    $omExternal | Add-Member -NotePropertyName distribution -NotePropertyValue @{
    repository_id = $omRepoInfo.id
    release_id = $omRelease.id
    asset_id = $omAssets[0].id
    release = $omRelease.tag_name
    url = $omAssets[0].browser_download_url
    bytes = $omZip.Length
    sha256 = $omDigest
    }
    $omExternalPath = Join-Path $omZip.DirectoryName (Split-Path -Leaf $omManifestPath)
    $omExternalJson = $omExternal | ConvertTo-Json -Depth 30
    [IO.File]::WriteAllText($omExternalPath, $omExternalJson, [Text.UTF8Encoding]::new($false))
    Get-Item -LiteralPath $omExternalPath
  5. 提交外部分发声明之后,从另一台机器或独立数据目录在「安装新模块」搜仓库名,核对发布者、版本和安装按钮,然后做一次下载与图接入测试。首页热门列表按目录刷新。找不到新条目时,先检查公开仓库的 Topic、默认分支根目录声明、Release 是否已发布和附件是否可访问。 GitHub Topics 操作 · GitHub 发布操作

除外部的 distribution 字段外,两份声明须完全一致。更新包先打好最终 ZIP,再刷新对应声明。示例模块只需要 base 环境,因此用了 installation.direct: true。如果你的包还需 pip、原生库或模型准备,填 false,把可执行步骤写进 README。发布文件只放代码、声明及有授权的资源。录音、声纹、烧录结果和密钥应留在本机数据里。