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

# Gebruik - VOICEVOX Core for PHP

> Basisgebruik van VOICEVOX Core for PHP. Codevoorbeelden voor tekst-naar-spraak (TTS) en spraaksynthese.

## Basale spraaksynthese

Het onderstaande `talk.php` is een voorbeeld dat spraak synthetiseert uit tekst en wegschrijft naar een WAV-bestand.

<Warning>
  PHP FFI is in webserveromgevingen (zoals FPM) doorgaans uitgeschakeld. Voer dit script uit **vanuit de CLI**, bijvoorbeeld met `php talk.php`.
</Warning>

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

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;

// Pas de paden aan aan de installatielocatie van voicevox_core
$voicevoxCoreDir = getenv('HOME') . '/.local/voicevox_core';
$onnxruntimeFilename = $voicevoxCoreDir . '/onnxruntime/lib/' . Onnxruntime::libVersionedFilename();
$dictDir = $voicevoxCoreDir . '/dict/open_jtalk_dic_utf_8-1.11';
$vvmPath  = $voicevoxCoreDir . '/models/vvms/0.vvm';

// De tekst en het stijl-ID voor de synthese
$text    = 'この音声は、ボイスボックスを使用して、出力されています。';
$styleId = 0;
$outPath = './output.wav';

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

// Laad het spraakmodel
$model = VoiceModelFile::open($vvmPath);
$synthesizer->loadVoiceModel($model);

// Spraaksynthese
$audioQuery = $synthesizer->createAudioQuery($text, $styleId);
$wav        = $synthesizer->synthesis($audioQuery, $styleId);

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

Uitvoeren:

```bash theme={null}
php talk.php
```

## Spraaksynthese in één stap (de `tts`-methode)

Als je de twee stappen `createAudioQuery` + `synthesis` wilt overslaan, kun je de `tts()`-methode gebruiken:

```php theme={null}
$wav = $synthesizer->tts($text, $styleId);
file_put_contents('./output.wav', $wav);
```

## Spraaksynthese met katakana-notatie

Je kunt ook synthetiseren vanuit katakana-notatie in AquesTalk-stijl:

```php theme={null}
// Met ttsFromKana
$wav = $synthesizer->ttsFromKana("コノオンセイワ'、ボイスボックスオ'/シヨーシテ'、シュツリョクサレテイマ'ス。", $styleId);
file_put_contents('./output.wav', $wav);
```

## Een gebruikerswoordenboek gebruiken

Gebruik `UserDict` om de uitspraak van eigen woorden te registreren:

```php theme={null}
use Revolution\Voicevox\Core\UserDict;
use Revolution\Voicevox\Core\Enums\UserDictWordType;

$userDict = new UserDict();
$uuid = $userDict->addWord(
    surface: 'ボイボ',
    pronunciation: 'ボイボ',
    accentType: 0,
    wordType: UserDictWordType::ProperNoun,
    priority: 10,
);

// Sla het woordenboek op
$userDict->save('./mydict.json');

// Pas het woordenboek toe op OpenJTalk
$openJtalk->useUserDict($userDict);
```

## De AudioQuery aanpassen

Door de JSON die je met `createAudioQuery` hebt opgehaald aan te passen vóór de synthese, kun je intonatie, tempo en dergelijke nauwkeurig regelen:

```php theme={null}
$audioQueryJson = $synthesizer->createAudioQuery($text, $styleId);

// Decodeer de JSON en pas aan
$audioQuery = json_decode($audioQueryJson, true);
$audioQuery['speedScale'] = 1.2;   // Snelheid × 1,2
$audioQuery['pitchScale'] = 0.05;  // Toonhoogte iets omhoog

$wav = $synthesizer->synthesis(json_encode($audioQuery), $styleId);
file_put_contents('./output.wav', $wav);
```

## Accentfrases ophalen

Je kunt de accentfrase-informatie van tekst ophalen om te bekijken en te bewerken:

```php theme={null}
$accentPhrasesJson = $synthesizer->createAccentPhrases($text, $styleId);
$accentPhrases = json_decode($accentPhrasesJson, true);

// Overschrijf de mora-toonhoogte en foneemlengte met een andere stijl
$updatedJson = $synthesizer->replaceMoraData($accentPhrasesJson, $styleId);
```

## De GPU-modus gebruiken

In een omgeving met een beschikbare GPU kun je versnellen door `AccelerationMode::Gpu` op te geven:

```php theme={null}
use Revolution\Voicevox\Core\Enums\AccelerationMode;

$synthesizer = new Synthesizer($onnxruntime, $openJtalk, AccelerationMode::Gpu);

// Controleer of de GPU-modus actief is
if ($synthesizer->isGpuMode()) {
    echo 'Draait in GPU-modus' . PHP_EOL;
}
```

## Meerdere spraakmodellen gebruiken

Door meerdere `.vvm`-bestanden te laden en te wisselen van stijl-ID kun je meerdere karakterstemmen naast elkaar gebruiken:

```php theme={null}
$model0 = VoiceModelFile::open($voicevoxCoreDir . '/models/vvms/0.vvm');
$model1 = VoiceModelFile::open($voicevoxCoreDir . '/models/vvms/1.vvm');

$synthesizer->loadVoiceModel($model0);
$synthesizer->loadVoiceModel($model1);

// Bekijk de informatie van de sprekers in de modellen
$metas = json_decode($synthesizer->metas(), true);

// Ontlaad een model (wanneer het niet meer nodig is)
$synthesizer->unloadVoiceModel($model0->id());
```


## Related topics

- [VOICEVOX Core for PHP](/nl/packages/voicevox-core-php/index.md)
- [API-referentie - VOICEVOX Core for PHP](/nl/packages/voicevox-core-php/api.md)
- [PHP FFI](/nl/advanced/ffi.md)
- [VOICEVOX for Laravel](/nl/packages/laravel-voicevox/index.md)
- [Installatie en configuratie - VOICEVOX for Laravel](/nl/packages/laravel-voicevox/installation.md)
