> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# API 参考 - VOICEVOX Core for PHP

> VOICEVOX Core for PHP 的所有类、方法、enum 的参考。

## `Onnxruntime`

ONNX Runtime 加载器。是进程级单例，每个进程只有 1 个实例。

### 方法

| 方法                                             | 说明                                                                    |
| ---------------------------------------------- | --------------------------------------------------------------------- |
| `static loadOnce(string $filename = ''): self` | 加载并初始化 ONNX Runtime。第 2 次及以后调用时会忽略参数并返回已有实例。                          |
| `static get(): ?self`                          | 返回已有实例。若尚未初始化则返回 `null`。                                              |
| `supportedDevices(): string`                   | 以 JSON 字符串返回可用设备信息。                                                   |
| `static libVersionedFilename(): string`        | 返回带版本号的 ONNX Runtime 库文件名（例如 `libvoicevox_onnxruntime.1.17.3.dylib`）。 |
| `static libUnversionedFilename(): string`      | 返回不带版本号的 ONNX Runtime 库文件名。                                           |

### 常量

| 常量            | 说明                            |
| ------------- | ----------------------------- |
| `LIB_NAME`    | 库的基础名（`voicevox_onnxruntime`） |
| `LIB_VERSION` | 推荐的 ONNX Runtime 版本           |

***

## `OpenJtalk`

使用 OpenJTalk 的文本分析器。

### 方法

| 方法                                      | 说明                      |
| --------------------------------------- | ----------------------- |
| `__construct(string $openJtalkDictDir)` | 使用 OpenJTalk 词典目录路径初始化。 |
| `analyze(string $text): string`         | 分析日语文本，返回重音短语数组的 JSON。  |
| `useUserDict(UserDict $userDict): void` | 应用用户词典。若词典变更，需要再次调用。    |

***

## `VoiceModelFile`

语音模型文件（`.vvm` 文件）。

### 方法

| 方法                                | 说明                         |
| --------------------------------- | -------------------------- |
| `static open(string $path): self` | 打开 `.vvm` 文件。              |
| `id(): string`                    | 以 hex 字符串（16 字节）返回语音模型 ID。 |
| `createMetasJson(): string`       | 以 JSON 字符串返回说话人元数据。        |
| `close(): void`                   | 关闭文件并释放资源。                 |

***

## `Synthesizer`

文本朗读的主要类。

### 方法

| 方法                                                                                                                               | 说明                                          |
| -------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------- |
| `__construct(Onnxruntime $onnxruntime, OpenJtalk $openJtalk, AccelerationMode $accelerationMode = Auto, int $cpuNumThreads = 0)` | 初始化合成器。                                     |
| `onnxruntime(): Onnxruntime`                                                                                                     | 返回该合成器持有的 `Onnxruntime` 实例。                 |
| `isGpuMode(): bool`                                                                                                              | 返回是否启用了 GPU 模式。                             |
| `metas(): string`                                                                                                                | 以 JSON 字符串返回已加载说话人的元数据。                     |
| `loadVoiceModel(VoiceModelFile $model): void`                                                                                    | 加载语音模型。                                     |
| `unloadVoiceModel(string $voiceModelId): void`                                                                                   | 卸载由 hex ID 指定的语音模型。                         |
| `isLoadedVoiceModel(string $voiceModelId): bool`                                                                                 | 检查语音模型是否已加载。                                |
| `createAudioQuery(string $text, int $styleId): string`                                                                           | 从日语文本生成 AudioQuery JSON。                    |
| `createAudioQueryFromKana(string $kana, int $styleId): string`                                                                   | 从 AquesTalk 风格的假名记法生成 AudioQuery JSON。      |
| `createAccentPhrases(string $text, int $styleId): string`                                                                        | 从日语文本生成重音短语数组 JSON。                         |
| `createAccentPhrasesFromKana(string $kana, int $styleId): string`                                                                | 从假名记法生成重音短语数组 JSON。                         |
| `replaceMoraData(string $accentPhrasesJson, int $styleId): string`                                                               | 返回已更新 mora 音高与音素时长的新重音短语。                   |
| `replacePhonemeLength(string $accentPhrasesJson, int $styleId): string`                                                          | 返回已更新音素时长的新重音短语。                            |
| `replaceMoraPitch(string $accentPhrasesJson, int $styleId): string`                                                              | 返回已更新 mora 音高的新重音短语。                        |
| `synthesis(string $audioQueryJson, int $styleId, bool $enableInterrogativeUpspeak = true): string`                               | 从 AudioQuery JSON 合成音频。返回 WAV 二进制。          |
| `tts(string $text, int $styleId, bool $enableInterrogativeUpspeak = true): string`                                               | 从日语文本一步合成音频。返回 WAV 二进制。                     |
| `ttsFromKana(string $kana, int $styleId, bool $enableInterrogativeUpspeak = true): string`                                       | 从假名记法合成音频。返回 WAV 二进制。                       |
| `createSingFrameAudioQuery(string $scoreJson, int $styleId): string`                                                             | 从乐谱 JSON 生成歌唱合成 query JSON。                 |
| `frameSynthesis(string $frameAudioQueryJson, int $styleId): string`                                                              | 从 frame audio query 合成歌唱音频。返回 WAV 二进制。      |
| `createSingFrameF0(string $scoreJson, string $frameAudioQueryJson, int $styleId): string`                                        | 从乐谱与歌唱 query 以 JSON 的 float 数组返回按帧的 F0（基频）。 |
| `createSingFrameVolume(string $scoreJson, string $frameAudioQueryJson, int $styleId): string`                                    | 从乐谱与歌唱 query 以 JSON 的 float 数组返回按帧的音量。      |

***

## `VoicevoxCore`

VOICEVOX Core 的全局工具函数。

### 方法

| 方法                                                                              | 说明                                                    |
| ------------------------------------------------------------------------------- | ----------------------------------------------------- |
| `static getVersion(): string`                                                   | 以 SemVer 字符串返回 VOICEVOX Core 的版本。                     |
| `static audioQueryCreateFromAccentPhrases(string $accentPhrasesJson): string`   | 从重音短语数组 JSON 生成 AudioQuery JSON。                      |
| `static audioQueryValidate(string $audioQueryJson): void`                       | 校验 `AudioQuery` JSON。不合法时抛出 `VoicevoxException`。      |
| `static accentPhraseValidate(string $accentPhraseJson): void`                   | 校验 `AccentPhrase` JSON。不合法时抛出 `VoicevoxException`。    |
| `static moraValidate(string $moraJson): void`                                   | 校验 `Mora` JSON。不合法时抛出 `VoicevoxException`。            |
| `static scoreValidate(string $scoreJson): void`                                 | 校验 `Score` JSON。不合法时抛出 `VoicevoxException`。           |
| `static noteValidate(string $noteJson): void`                                   | 校验 `Note` JSON。不合法时抛出 `VoicevoxException`。            |
| `static frameAudioQueryValidate(string $frameAudioQueryJson): void`             | 校验 `FrameAudioQuery` JSON。不合法时抛出 `VoicevoxException`。 |
| `static framePhonemeValidate(string $framePhonemeJson): void`                   | 校验 `FramePhoneme` JSON。不合法时抛出 `VoicevoxException`。    |
| `static ensureCompatible(string $scoreJson, string $frameAudioQueryJson): void` | 确认乐谱与歌唱合成 query 的组合是否有效。不合法时抛出 `VoicevoxException`。   |

***

## `UserDict`

用于注册自定义词发音的用户词典。

### 方法

| 方法                                                                                                                                                        | 说明                     |
| --------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------- |
| `__construct()`                                                                                                                                           | 创建空的用户词典。              |
| `load(string $path): void`                                                                                                                                | 从文件加载用户词典。             |
| `save(string $path): void`                                                                                                                                | 将用户词典保存到文件。            |
| `addWord(string $surface, string $pronunciation, int $accentType, UserDictWordType $wordType = CommonNoun, int $priority = 5): string`                    | 添加词。以 hex 字符串返回词 UUID。 |
| `updateWord(string $wordUuid, string $surface, string $pronunciation, int $accentType, UserDictWordType $wordType = CommonNoun, int $priority = 5): void` | 更新由 UUID 指定的已有词。       |
| `removeWord(string $wordUuid): void`                                                                                                                      | 删除由 UUID 指定的词。         |
| `importDict(UserDict $other): void`                                                                                                                       | 从另一个 `UserDict` 导入词。   |
| `toJson(): string`                                                                                                                                        | 以 JSON 字符串返回所有词。       |

***

## Enum：`AccelerationMode`

合成器的硬件加速模式。

| Case   | 值   | 说明           |
| ------ | --- | ------------ |
| `Auto` | `0` | 自动选择可用的最佳模式。 |
| `Cpu`  | `1` | 强制 CPU 模式。   |
| `Gpu`  | `2` | 强制 GPU 模式。   |

***

## Enum：`UserDictWordType`

用户词典条目的词类。

| Case         | 值   | 说明   |
| ------------ | --- | ---- |
| `ProperNoun` | `0` | 专有名词 |
| `CommonNoun` | `1` | 普通名词 |
| `Verb`       | `2` | 动词   |
| `Adjective`  | `3` | 形容词  |
| `Suffix`     | `4` | 后缀   |

***

## `VoicevoxException`

当 VOICEVOX Core 的 C API 调用返回错误码时抛出。异常消息中包含来自库的错误说明。


## Related topics

- [VOICEVOX Core for PHP](/zh/packages/voicevox-core-php/index.md)
- [用法 - VOICEVOX Core for PHP](/zh/packages/voicevox-core-php/usage.md)
- [配置参考 - VOICEVOX for Laravel](/zh/packages/laravel-voicevox/configuration.md)
- [Core Versions](/zh/packages/laravel-voicevox/engine-api/other/core-versions.md)
- [VOICEVOX for Laravel](/zh/packages/laravel-voicevox/index.md)
