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

# Modalità client - Preset - VOICEVOX for Laravel

> Come utilizzare i preset del motore ufficiale in modalità client di VOICEVOX for Laravel per salvare e riutilizzare i parametri di sintesi vocale.

Utilizzando la funzionalità dei preset puoi salvare e riutilizzare combinazioni di parametri di sintesi vocale frequenti sul motore ufficiale VOICEVOX. In modalità client i preset non vengono salvati lato Laravel, ma viene utilizzata l'API dei preset gestita dal motore ufficiale a cui ti connetti.

## Panoramica

I preset in modalità client vengono manipolati tramite la Facade `Voicevox`. Internamente, invia richieste HTTP a `/presets`, `/add_preset`, `/update_preset`, `/delete_preset`, `/audio_query_from_preset` del motore ufficiale VOICEVOX.

I preset del motore ufficiale sono salvati come `presets.yaml` nell'area utente specifica per ciascun sistema operativo. Nella build macOS dell'app VOICEVOX (versione stabile), normalmente vengono salvati nel seguente percorso.

```text theme={null}
~/Library/Application Support/voicevox-engine/presets.yaml
```

Anche se avvii il motore ufficiale con Docker, vengono salvati nell'area utente all'interno del container. Finché continui a utilizzare lo stesso container, i dati vengono mantenuti anche dopo il riavvio. Tuttavia, se crei ogni volta un container temporaneo, ad esempio con `docker run --rm`, i preset vengono persi al termine del container. Per rendere persistenti i preset in modo affidabile, monta l'area utente all'interno del container tramite un volume Docker.

Questa è una gestione separata dallo `storage/voicevox/presets.json` della modalità nativa. Il `config('voicevox.core.presets')` lato Laravel non viene utilizzato in modalità client.

## Prerequisiti

Avvia il motore ufficiale VOICEVOX.

```shell theme={null}
docker pull voicevox/voicevox_engine:cpu-latest
docker run --rm -p '127.0.0.1:50021:50021' voicevox/voicevox_engine:cpu-latest
```

Puoi modificare la destinazione con `VOICEVOX_URL`.

```env theme={null}
VOICEVOX_URL=http://127.0.0.1:50021
```

## Struttura di un preset

I preset scambiati in modalità client sono array allineati allo schema dell'API del motore ufficiale.

```php theme={null}
[
    'id' => 1,                // ID del preset (intero)
    'name' => 'ゆっくり',      // Nome del preset
    'speaker_uuid' => 'uuid', // UUID dello speaker (stringa)
    'style_id' => 1,          // Style ID (intero)
    'speedScale' => 0.8,      // Velocità di parlato
    'pitchScale' => 0.0,      // Pitch
    'intonationScale' => 1.0, // Intonazione
    'volumeScale' => 1.0,     // Volume
    'prePhonemeLength' => 0.1, // Silenzio iniziale
    'postPhonemeLength' => 0.1, // Silenzio finale
]
```

## Utilizzo di base

### Creazione di un preset

Puoi creare un preset con `Voicevox::addPreset()`. Nel motore ufficiale, specificando `0` come `id`, viene assegnato un ID non ancora utilizzato.

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

$id = Voicevox::addPreset([
    'id' => 0,
    'name' => 'ゆっくり丁寧',
    'speaker_uuid' => '7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff',
    'style_id' => 1,
    'speedScale' => 0.8,
    'pitchScale' => 0.0,
    'intonationScale' => 1.2,
    'volumeScale' => 1.0,
    'prePhonemeLength' => 0.1,
    'postPhonemeLength' => 0.1,
]);

echo $id; // Es: 1
```

### Recupero di tutti i preset

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

$presets = Voicevox::presets();

/*
[
    [
        "id" => 1,
        "name" => "ゆっくり丁寧",
        "speaker_uuid" => "7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff",
        "style_id" => 1,
        "speedScale" => 0.8,
        ...
    ],
    ...
]
*/
```

### Aggiornamento di un preset

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

$id = Voicevox::updatePreset([
    'id' => 1,
    'name' => 'ゆっくりはっきり',
    'speaker_uuid' => '7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff',
    'style_id' => 1,
    'speedScale' => 0.7,
    'pitchScale' => 0.0,
    'intonationScale' => 1.5,
    'volumeScale' => 1.0,
    'prePhonemeLength' => 0.1,
    'postPhonemeLength' => 0.1,
]);
```

### Eliminazione di un preset

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

Voicevox::deletePreset(1);
```

## Sintesi vocale con preset

### talkFromPreset()

`Voicevox::talkFromPreset()` utilizza `/audio_query_from_preset` del motore ufficiale per creare una `TalkAudioQuery` con il preset già applicato.

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

$response = Voicevox::talkFromPreset('プリセットを使うのだ', presetId: 1)
    ->generate(id: 1);

$response->storeAs('client', 'preset-talk.wav');
```

### audioQueryFromPreset()

Se vuoi ottenere solo l'array della audio query prima della sintesi, usa `audioQueryFromPreset()`.

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

$audioQuery = Voicevox::audioQueryFromPreset(
    text: 'プリセットのクエリだけ作るのだ',
    presetId: 1,
);

$audioQuery['speedScale'] = 1.1;
```

### Regolazione aggiuntiva con tap()

Anche una `TalkAudioQuery` creata da un preset può essere regolata con `tap()` come una normale `Voicevox::talk()`.

```php theme={null}
use Revolution\Voicevox\Client\TalkAudioQuery;
use Revolution\Voicevox\Voicevox;

$response = Voicevox::talkFromPreset('少し速く読むのだ', presetId: 1)
    ->tap(function (TalkAudioQuery $talk) {
        $talk->audioQuery['speedScale'] = 1.2;
    })
    ->generate(id: 1);
```

## Corrispondenza con l'Engine API

Ogni metodo della Facade `Voicevox` corrisponde all'API del motore ufficiale.

| Laravel                            | API del motore ufficiale                            |
| ---------------------------------- | --------------------------------------------------- |
| `Voicevox::presets()`              | `GET /presets`                                      |
| `Voicevox::addPreset()`            | `POST /add_preset`                                  |
| `Voicevox::updatePreset()`         | `POST /update_preset`                               |
| `Voicevox::deletePreset()`         | `POST /delete_preset`                               |
| `Voicevox::audioQueryFromPreset()` | `POST /audio_query_from_preset`                     |
| `Voicevox::talkFromPreset()`       | `POST /audio_query_from_preset` + `POST /synthesis` |

## Differenze con la modalità nativa

| Elemento                      | Modalità client                                              | Modalità nativa                     |
| ----------------------------- | ------------------------------------------------------------ | ----------------------------------- |
| Modalità d'uso                | Facade `Voicevox`                                            | Helper `preset()` / `talk()`        |
| Formato di salvataggio        | `presets.yaml` del motore ufficiale                          | `presets.json` di Laravel           |
| Esempio su macOS              | `~/Library/Application Support/voicevox-engine/presets.yaml` | `storage/voicevox/presets.json`     |
| Generazione della audio query | `/audio_query_from_preset` del motore ufficiale              | Implementazione nativa lato Laravel |
| Processo del motore           | Necessario                                                   | Non necessario (richiede FFI)       |

## Esempi pratici

### Per narrazione

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

Voicevox::addPreset([
    'id' => 0,
    'name' => 'ナレーション',
    'speaker_uuid' => '7ffcb7ce-00ec-4bdc-82cd-45a8889e43ff',
    'style_id' => 1,
    'speedScale' => 1.0,
    'pitchScale' => -0.05,
    'intonationScale' => 0.8,
    'volumeScale' => 1.0,
    'prePhonemeLength' => 0.15,
    'postPhonemeLength' => 0.15,
]);
```

### Per parlato veloce

```php theme={null}
Voicevox::addPreset([
    'id' => 0,
    'name' => '早口',
    'speaker_uuid' => '388f246b-8c41-4ac1-8e2d-5d79f3ff56d9',
    'style_id' => 3,
    'speedScale' => 1.5,
    'pitchScale' => 0.05,
    'intonationScale' => 1.3,
    'volumeScale' => 1.0,
    'prePhonemeLength' => 0.05,
    'postPhonemeLength' => 0.05,
]);
```

## Dettagli sui parametri

* `speedScale`: 0.5 – 2.0 (velocità di parlato)
* `pitchScale`: -0.15 – 0.15 (pitch)
* `intonationScale`: 0.0 – 2.0 (intonazione)
* `volumeScale`: 0.0 – 2.0 (volume)
* `prePhonemeLength`: 0.0 – 1.5 (silenzio iniziale, in secondi)
* `postPhonemeLength`: 0.0 – 1.5 (silenzio finale, in secondi)

## Avvertenze

<Warning>
  In modalità client viene aggiornato `presets.yaml` lato motore ufficiale. Non viene salvato nella `storage` del progetto Laravel.
</Warning>

* Se crei ogni volta un container temporaneo con `docker run --rm`, i preset vengono persi al termine del container.
* Includi sia `speaker_uuid` che `style_id`.
* Quando esegui `generate(id:)` su una query creata con `talkFromPreset()`, normalmente specifica lo stesso ID dello `style_id` del preset.

## Risoluzione dei problemi

### Impossibile recuperare i preset

1. Verifica che il motore ufficiale VOICEVOX sia in esecuzione.
2. Verifica che `VOICEVOX_URL` punti alla destinazione corretta.
3. Verifica di non aver riavviato un container Docker differente.

### Non trovi il file dei preset

Nella build macOS controlla il seguente percorso.

```text theme={null}
~/Library/Application Support/voicevox-engine/presets.yaml
```

Con Docker vengono salvati nell'area utente (all'interno del container) del motore ufficiale. Se serve persistenza, monta l'area utente all'interno del container su un volume Docker.

### Non conosci lo speaker\_uuid

Puoi ottenere gli UUID dall'elenco degli speaker.

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

$speakers = Voicevox::speakers();

foreach ($speakers as $speaker) {
    echo $speaker['name'].': '.$speaker['speaker_uuid'].PHP_EOL;
}
```


## Related topics

- [Modalità nativa - Preset - VOICEVOX for Laravel](/it/packages/laravel-voicevox/native-presets.md)
- [Modalità client - Dizionario utente - VOICEVOX for Laravel](/it/packages/laravel-voicevox/client-user-dict.md)
- [Modalità client: Song (sintesi vocale cantata) - VOICEVOX for Laravel](/it/packages/laravel-voicevox/client-song.md)
- [Modalità client: Talk (sintesi vocale da testo) - VOICEVOX for Laravel](/it/packages/laravel-voicevox/client-talk.md)
- [Modalità nativa: Song (sintesi vocale canora) - VOICEVOX for Laravel](/it/packages/laravel-voicevox/native-song.md)
