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

# Using VOICEVOX Core in Muse

> How to use VOICEVOX Core for PHP in the Muse Linux VM to synthesize speech from Japanese text.

This guide shows how to use [`revolution/voicevox-core`](https://github.com/invokable/voicevox-core-php) in the Muse Linux VM to turn Japanese text received in a chat into an audio file.

> [!NOTE]
>
> VOICEVOX Core supports Japanese speech synthesis only. The input text in this guide must be Japanese, so the examples keep their Japanese text as is.

> [!WARNING]
>
> `ext-ffi` is often disabled in web server environments, and this package is designed to run from the CLI. The Muse VM can also be recreated, so keep the files you need under your home directory.

## Tested environment

This procedure was tested with the following combination.

| Item | Version / environment |
| - | - |
| VOICEVOX Core | 0.17.1 |
| `revolution/voicevox-core` | 0.17.1 |
| Voice model | `0.vvm` from `voicevox_vvm` 0.17.0 |
| ONNX Runtime | `voicevox_onnxruntime` 1.23.2 (Linux x64, CPU) |
| PHP | 8.3.6 (Ubuntu 24.04 package) |
| Muse | Linux VM (x86\_64) |

## Prepare a PHP build for FFI

The default PHP in Muse is built with `static-php-cli`, so it may not include `ext-ffi`. Instead of replacing the default PHP, extract a separate PHP 8.3 for FFI into your home directory.

```bash theme={null}
mkdir -p ~/workspace/tools/php83-ffi/debs
cd ~/workspace/tools/php83-ffi/debs

BASE=http://azure.archive.ubuntu.com/ubuntu/pool/main/p/php8.3
VER=8.3.6-0ubuntu0.24.04.14
for p in php8.3-cli php8.3-common php8.3-opcache php8.3-readline \
         php8.3-mbstring php8.3-xml php8.3-curl; do
  curl -fsSL -O "$BASE/${p}_${VER}_amd64.deb"
done

cd ~/workspace/tools/php83-ffi
for d in debs/*.deb; do dpkg-deb -x "$d" root; done
```

Save the following wrapper as `~/workspace/tools/bin/php83-ffi`.

```sh theme={null}
#!/bin/sh
ROOT="$HOME/workspace/tools/php83-ffi/root"
EXT="$ROOT/usr/lib/php/20230831"
exec "$ROOT/usr/bin/php8.3" -n \
  -d extension_dir="$EXT" \
  -d ffi.enable=true \
  -d extension=ffi.so \
  -d extension=phar.so \
  -d extension=mbstring.so \
  -d extension=curl.so \
  -d extension=dom.so \
  -d extension=simplexml.so \
  -d extension=xml.so \
  -d extension=xmlreader.so \
  -d extension=xmlwriter.so \
  -d extension=tokenizer.so \
  -d extension=ctype.so \
  -d extension=fileinfo.so \
  -d extension=iconv.so \
  -d extension=posix.so \
  -d extension=sockets.so \
  "$@"
```

Make it executable and confirm that FFI is enabled.

```bash theme={null}
chmod +x ~/workspace/tools/bin/php83-ffi
~/workspace/tools/bin/php83-ffi -v
~/workspace/tools/bin/php83-ffi -r 'var_dump(class_exists("FFI"));'
```

You are ready when `class_exists("FFI")` returns `true`.

## Place the VOICEVOX Core files

The official downloader may not get through the Muse proxy, so download the parts the downloader would fetch one by one and place them in the same directory layout.

```bash theme={null}
mkdir -p ~/workspace/voicevox-test/voicevox_core/{c_api/lib,onnxruntime/lib,models/vvms,dict}
cd ~/workspace/voicevox-test
```

| Part | Source | Destination |
| - | - | - |
| C API | [`VOICEVOX/voicevox_core` 0.17.1](https://github.com/VOICEVOX/voicevox_core/releases/tag/0.17.1) | `voicevox_core/c_api/lib/` |
| ONNX Runtime | [`VOICEVOX/onnxruntime-builder` 1.23.2](https://github.com/VOICEVOX/onnxruntime-builder/releases/tag/voicevox_onnxruntime-1.23.2) | `voicevox_core/onnxruntime/lib/` |
| OpenJTalk dictionary | [`r9y9/open_jtalk` v1.11.1](https://github.com/r9y9/open_jtalk/releases/tag/v1.11.1) | `voicevox_core/dict/` |
| Voice model | [`VOICEVOX/voicevox_vvm` 0.17.0](https://github.com/VOICEVOX/voicevox_vvm/releases/tag/0.17.0) | `voicevox_core/models/vvms/` |

### C API and ONNX Runtime

```bash theme={null}
curl -fsSL -o /tmp/core.zip \
  https://github.com/VOICEVOX/voicevox_core/releases/download/0.17.1/voicevox_core-linux-x64-0.17.1.zip
unzip -p /tmp/core.zip '*/lib/libvoicevox_core.so' \
  > voicevox_core/c_api/lib/libvoicevox_core.so

curl -fsSL -o /tmp/onnx.tgz \
  https://github.com/VOICEVOX/onnxruntime-builder/releases/download/voicevox_onnxruntime-1.23.2/voicevox_onnxruntime-linux-x64-1.23.2.tgz
tar -xzf /tmp/onnx.tgz --no-same-owner -C /tmp
cp -r /tmp/voicevox_onnxruntime-linux-x64-1.23.2/lib/. \
  voicevox_core/onnxruntime/lib/
```

### OpenJTalk dictionary and voice model

```bash theme={null}
curl -fsSL -o /tmp/dict.tar.gz \
  https://github.com/r9y9/open_jtalk/releases/download/v1.11.1/open_jtalk_dic_utf_8-1.11.tar.gz
tar -xzf /tmp/dict.tar.gz --no-same-owner -C voicevox_core/dict

curl -fsSL -o voicevox_core/models/vvms/0.vvm \
  https://github.com/VOICEVOX/voicevox_vvm/releases/download/0.17.0/0.vvm
```

Pass `--no-same-owner` to `tar`, because Muse does not allow changing file ownership.

The examples on this page use Zundamon's style ID `3` (Normal), which is included in `0.vvm`. VVM file numbers do not map one to one to speakers, so check the style ID to use in the VVM metadata.

## Install the package

Run Composer with the FFI PHP as well.

```bash theme={null}
cd ~/workspace/voicevox-test
~/workspace/tools/bin/php83-ffi /path/to/composer.phar \
  require revolution/voicevox-core:0.17.1
```

## Synthesize speech from Japanese text

Create `zundamon.php`. To make it easy to run from a Muse conversation, it takes the input text as the first argument and the output file as the second. Without arguments, it uses the sample text and `zundamon.wav`.

```php theme={null}
<?php

declare(strict_types=1);

require __DIR__.'/vendor/autoload.php';

use Revolution\Voicevox\Core\Enums\AccelerationMode;
use Revolution\Voicevox\Core\Onnxruntime;
use Revolution\Voicevox\Core\OpenJtalk;
use Revolution\Voicevox\Core\Synthesizer;
use Revolution\Voicevox\Core\VoiceModelFile;

$voicevoxCoreDir = __DIR__.'/voicevox_core';
$onnxruntimeFilename = $voicevoxCoreDir.'/onnxruntime/lib/'.Onnxruntime::libRecommendedVersionedFilename();
$dictDir = $voicevoxCoreDir.'/dict/open_jtalk_dic_utf_8-1.11';
$vvmPath = $voicevoxCoreDir.'/models/vvms/0.vvm';

$text = $argv[1] ?? 'こんにちは、ずんだもんなのだ。';
$styleId = 3;
$outPath = $argv[2] ?? __DIR__.'/zundamon.wav';

$onnxruntime = Onnxruntime::loadOnce($onnxruntimeFilename);
$openJtalk = new OpenJtalk($dictDir);
$synthesizer = new Synthesizer($onnxruntime, $openJtalk, AccelerationMode::Auto);

$model = VoiceModelFile::open($vvmPath);
$synthesizer->loadVoiceModel($model);

// Native モードでは英単語のカタカナ変換がないため、日本語の読みを入力する
$audioQuery = $synthesizer->createAudioQuery($text, $styleId);
$wav = $synthesizer->synthesis($audioQuery, $styleId);

file_put_contents($outPath, $wav);
echo 'Wrote '.$outPath.PHP_EOL;
```

Set environment variables so the VOICEVOX Core libraries can be found at runtime.

```bash theme={null}
cd ~/workspace/voicevox-test
export VOICEVOX_CORE_LIB_PATH="$PWD/voicevox_core/c_api/lib/libvoicevox_core.so"
export LD_LIBRARY_PATH="$PWD/voicevox_core/c_api/lib:$PWD/voicevox_core/onnxruntime/lib"
```

To run it without arguments, use this command.

```bash theme={null}
~/workspace/tools/bin/php83-ffi zundamon.php
```

To change the text and output path from a Muse conversation, pass them as arguments.

```bash theme={null}
~/workspace/tools/bin/php83-ffi zundamon.php \
  'ミューズの環境で、ピーエイチピーから音声合成できたのだ。' \
  "$PWD/output.wav"
```

For other APIs, such as frame-by-frame PCM generation with `render()`, see [Usage](https://kawax.biz/en/packages/voicevox-core-php/usage.md) and the [API Reference](https://kawax.biz/en/packages/voicevox-core-php/api.md).

## Results and notes

On a shared-CPU Muse VM, the example above generated a WAV of about 7.67 seconds (24 kHz / 16 bit / mono). Processing took about 9 seconds.

* The Muse VM can be recreated, so keep PHP and the libraries in a persistent location such as `~/workspace/`.
* This package is CLI only. To run it from a web server, plan the `ext-ffi` settings and the PHP binary setup separately.
* The terms of use of VOICEVOX and each voice library apply to the generated audio. When you use Zundamon, you must credit it as `VOICEVOX:ずんだもん`.


## Related topics

- [Usage - VOICEVOX Core for PHP](https://kawax.biz/en/packages/voicevox-core-php/usage.md)
- [API Reference - VOICEVOX Core for PHP](https://kawax.biz/en/packages/voicevox-core-php/api.md)
- [Installation and setup - VOICEVOX for Laravel](https://kawax.biz/en/packages/laravel-voicevox/installation.md)
- [VOICEVOX Core for PHP](https://kawax.biz/en/packages/voicevox-core-php/index.md)
- [Create query for speech synthesis using preset](https://kawax.biz/en/packages/laravel-voicevox/engine-api/query-creation/create-query-for-speech-synthesis-using-preset.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.