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

# Utilizzo - VOICEVOX Core for PHP

> Uso di base di VOICEVOX Core for PHP. Esempi di codice per la sintesi vocale (TTS).

## Sintesi vocale di base

Il seguente `talk.php` è un esempio che sintetizza audio dal testo e lo scrive in un file WAV.

<Warning>
  PHP FFI è normalmente disabilitato negli ambienti server Web (come FPM). Esegui questo script **da CLI**, ad esempio con `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;

// Modifica il percorso in base a dove hai installato 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';

// Testo da sintetizzare e ID di stile
$text    = 'この音声は、ボイスボックスを使用して、出力されています。';
$styleId = 0;
$outPath = './output.wav';

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

// Carica il modello vocale
$model = VoiceModelFile::open($vvmPath);
$synthesizer->loadVoiceModel($model);

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

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

Esecuzione:

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

## Sintesi in un solo passo (metodo `tts`)

Se vuoi evitare i due passaggi `createAudioQuery` + `synthesis`, puoi usare il metodo `tts()`:

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

## Sintesi da notazione katakana

È possibile sintetizzare anche partendo da una notazione katakana in stile AquesTalk:

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

## Usare il dizionario utente

Per registrare la lettura di parole personalizzate, usa `UserDict`:

```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,
);

// Salva il dizionario
$userDict->save('./mydict.json');

// Applica il dizionario a OpenJTalk
$openJtalk->useUserDict($userDict);
```

## Regolare l'AudioQuery

Regolando il JSON ottenuto con `createAudioQuery` prima di sintetizzare, puoi controllare finemente intonazione, ritmo, ecc.:

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

// Decodifica il JSON e regolalo
$audioQuery = json_decode($audioQueryJson, true);
$audioQuery['speedScale'] = 1.2;   // velocità 1,2x
$audioQuery['pitchScale'] = 0.05;  // pitch leggermente più alto

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

## Ottenere le frasi accentate

Puoi ottenere le informazioni sulle frasi accentate del testo per verificarle o modificarle:

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

// Sovrascrivi pitch delle mora e durata dei fonemi con un altro stile
$updatedJson = $synthesizer->replaceMoraData($accentPhrasesJson, $styleId);
```

## Usare la modalità GPU

Negli ambienti in cui è disponibile una GPU, specificare `AccelerationMode::Gpu` velocizza l'esecuzione:

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

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

// Verifica se la modalità GPU è attiva
if ($synthesizer->isGpuMode()) {
    echo 'In esecuzione in modalità GPU' . PHP_EOL;
}
```

## Usare più modelli vocali

Caricando più file `.vvm` e cambiando l'ID di stile puoi utilizzare più voci di personaggi diversi:

```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);

// Verifica le informazioni sugli speaker inclusi nel modello
$metas = json_decode($synthesizer->metas(), true);

// Scarica il modello (quando non serve più)
$synthesizer->unloadVoiceModel($model0->id());
```


## Related topics

- [VOICEVOX Core for PHP](/it/packages/voicevox-core-php/index.md)
- [Riferimento API - VOICEVOX Core for PHP](/it/packages/voicevox-core-php/api.md)
- [PHP FFI](/it/advanced/ffi.md)
- [VOICEVOX for Laravel](/it/packages/laravel-voicevox/index.md)
- [Installazione e configurazione - VOICEVOX for Laravel](/it/packages/laravel-voicevox/installation.md)
