> For the complete documentation index, see [llms.txt](https://tgiann.gitbook.io/tgiann/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tgiann.gitbook.io/tgiann/scripts/tgiann-npc-guide/voice-generator.md). Agents can also search and read this documentation through its [MCP server](https://tgiann.gitbook.io/tgiann/~gitbook/mcp).

# Voice Generator

The `generator/` folder contains a small tool that turns every NPC line and story from your configs into audio files, using Microsoft's free neural voices (edge-tts). You run it once on your own computer after editing the texts; the resulting files are shipped with the resource and played in game. No API key, no account, no paid service.

## How the NPC voice works

Each NPC has a `voice` block in `configs/config.lua`:

```lua
voice = {
    mode = "files",          -- "files" | "browser" | "off"
    folder = "sounds/elder", -- where the audio files of this NPC are
    fallbackToBrowser = true,
},
```

| mode      | What happens                                                                                                                                                                                            |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `files`   | Plays `sounds/<folder>/<lang>-<id>.ogg` (or `.mp3`) for every line and story. This is what the generator produces.                                                                                      |
| `browser` | No files. The NUI speaks the text with the browser's built-in text to speech, using the `tts` block (`lang`, `rate`, `pitch`, `voiceHint`). Quality depends on the voices installed on the player's PC. |
| `off`     | Silent, text only.                                                                                                                                                                                      |

With `fallbackToBrowser = true` a line whose file is missing is spoken with browser TTS instead of staying silent. This is handy while you are still writing texts: the NPC keeps talking even before you regenerate the files.

The browser voice follows the player's language: `tts.lang` (and `tts.voiceHint`) can be a language table such as `{ tr = "tr-TR", en = "en-US" }`, and the entry matching the player's language is used. A plain string uses the same voice for everyone.

{% hint style="info" %}
The `/npcguidevoices` admin command prints the browser voices installed on your client to the F8 console. Put a part of a voice name (for example `Google` or `Microsoft`) into `tts.voiceHint` to prefer it.
{% endhint %}

## File naming

One file per line / story, per language:

```
sounds/<npc id>/<lang>-<id>.ogg
```

* `<npc id>`: the `id` of the NPC in `configs/config.lua` (`elder`, `outlaw`). The folder itself comes from `voice.folder`.
* `<lang>`: the language key of the text (`tr`, `en`).
* `<id>`: the `id` of the line (`greet_1`, `bye_2`, `think_1`, `wait_1`, `cooldown`, `nothing`) or the `id` of the story from `configs/configTips.lua`.

Examples: `sounds/elder/tr-greet_1.ogg`, `sounds/outlaw/en-queue.ogg`.

That is why every line and story in the configs carries an `id`. The id is the file name, so keep it unique inside the NPC and do not rename it once the files are generated. If you add your own files by hand (recorded voice, another TTS service) just follow the same naming and skip the generator.

## Requirements

* **Python 3.9 or newer** on the computer that runs the generator. Download it from [python.org](https://www.python.org/downloads/) and tick **Add python to PATH** during the setup.
* **Internet connection**, the voices are synthesized by Microsoft's servers.
* **ffmpeg** on PATH (optional but recommended). edge-tts only returns mp3; ffmpeg converts it to ogg/opus which is roughly half the size. Without ffmpeg the generator falls back to mp3 on its own. See [Installing ffmpeg](#installing-ffmpeg) below.

The first run creates a virtual environment in `generator/.venv` and installs `edge-tts` and `lupa` (a Lua runtime used to read your config files) from `requirements.txt`. Nothing is installed system wide.

## Installing ffmpeg

The generator only needs the `ffmpeg` command to be reachable from a terminal. Pick the way that fits your system, then **close and reopen** the terminal (or run `generate.bat` again from a fresh window) so the new PATH is picked up.

### Windows

**Option 1: winget** (Windows 10 / 11, no download needed). Open PowerShell or Command Prompt and run:

```
winget install Gyan.FFmpeg
```

**Option 2: Chocolatey**, if you already use it:

```
choco install ffmpeg
```

**Option 3: manual**

1. Download the latest `ffmpeg-release-essentials.zip` from [gyan.dev/ffmpeg/builds](https://www.gyan.dev/ffmpeg/builds/).
2. Extract it somewhere permanent, for example `C:\ffmpeg`. Inside you get a `bin` folder with `ffmpeg.exe`.
3. Add that `bin` folder to PATH: press the Windows key, type **environment variables**, open **Edit the system environment variables** → **Environment Variables**. Under **User variables** select **Path** → **Edit** → **New** and paste `C:\ffmpeg\bin`. Confirm every dialog with OK.

### macOS

```
brew install ffmpeg
```

Homebrew is at [brew.sh](https://brew.sh/) if you do not have it yet.

### Linux

```
sudo apt install ffmpeg        # Debian / Ubuntu
sudo dnf install ffmpeg        # Fedora
sudo pacman -S ffmpeg          # Arch
```

### Check that it works

Open a **new** terminal and run:

```
ffmpeg -version
```

If the version banner prints, the generator will find it and produce ogg files. If you get "not recognized" / "command not found", the `bin` folder is not on PATH or the terminal was opened before you changed it.

{% hint style="info" %}
ffmpeg is only needed on the machine that runs the generator, never on the game server. The server just streams the finished ogg / mp3 files.
{% endhint %}

## Running it

Open the `generator/` folder and double click `generate.bat`, or run it from a terminal to pass options:

```
generate.bat                     generates every file that is missing
generate.bat --force             regenerates everything (after changing a text or a voice)
generate.bat --clean             also deletes files whose id no longer exists in the config
generate.bat --force --clean     full clean rebuild
generate.bat --npc outlaw        only this NPC
generate.bat --lang tr           only this language
generate.bat --format mp3        keep the raw mp3 instead of converting to ogg
generate.bat --bitrate 32k       ogg bitrate (default 24k)
list-voices.bat                  prints the Turkish and English voices you can use in voices.json
```

Options can be combined, for example `generate.bat --force --npc elder --lang en`.

The generator reads `configs/config.lua` and `configs/configTips.lua` directly, so there is nothing to copy or export. It walks every NPC, collects `greetings`, `waitGreetings`, `goodbyes`, `thinking`, `cooldown`, `nothingLeft` and every story, and writes one file per language that has a text. Files that already exist are skipped unless `--force` is given. A `%s` placeholder (the cooldown line) is spoken as a short pause.

At the end you get a summary like `Done: 42 generated, 0 already existed, 0 removed, 42 total.`

## voices.json

`generator/voices.json` picks the voice, speed and tone per NPC and per language:

```json
{
  "_help": "Voice per NPC id and language. rate/pitch/volume are edge-tts offsets (e.g. -15%, -20Hz). Run list-voices.bat to see the available names.",
  "format": "ogg",
  "bitrate": "24k",
  "elder": {
    "tr": { "voice": "tr-TR-AhmetNeural", "rate": "-18%", "pitch": "-25Hz" },
    "en": { "voice": "en-GB-RyanNeural", "rate": "-18%", "pitch": "-25Hz" }
  },
  "outlaw": {
    "tr": { "voice": "tr-TR-AhmetNeural", "rate": "-5%", "pitch": "-40Hz" },
    "en": { "voice": "en-US-GuyNeural", "rate": "-5%", "pitch": "-40Hz" }
  },
  "default": {
    "tr": { "voice": "tr-TR-AhmetNeural" },
    "en": { "voice": "en-US-GuyNeural" }
  }
}
```

* The top level keys are NPC ids. An NPC without its own block uses `default`.
* Inside an NPC, each key is a language. **Only the languages listed here are generated** for that NPC. If your config has a `de` text but `voices.json` has no `de` entry, no German file is created.
* `voice`: an edge-tts voice name. Run `list-voices.bat` to see the Turkish and English ones; every other locale works too, the list is just filtered for readability.
* `rate`: speaking speed offset, for example `-18%` slower or `+10%` faster.
* `pitch`: tone offset in Hz, for example `-40Hz` for a deeper voice.
* `volume`: optional, for example `+10%`.
* `format` (`ogg` / `mp3`) and `bitrate` are the defaults; the command line flags override them.

If you want to add a third language, add its key to every NPC (or to `default`) and add the matching texts to the configs.

## Typical workflow

1. Edit the lines in `configs/config.lua` and the stories in `configs/configTips.lua` so they describe your server.
2. Pick voices in `voices.json` (optional).
3. Run `generate.bat --force --clean`. This rebuilds every file and removes audio for ids you deleted.
4. Restart the resource. The files are streamed through `fxmanifest.lua` (`sounds/**/*.ogg` and `sounds/**/*.mp3` are already listed), so nothing else to configure.

When you only added a new story, plain `generate.bat` is enough: existing files are kept and only the new ids are created.

## Troubleshooting

* **"Python 3 was not found"**: install Python and make sure it is on PATH, then run the bat again.
* **"Missing packages"**: run `generate.bat` instead of `generate.py`, the bat installs the requirements.
* **"ffmpeg was not found, falling back to mp3"**: not an error. Install ffmpeg (see above) for smaller files or keep mp3; the script plays both. If you just installed it, open a new terminal first.
* **"ffmpeg conversion failed"**: your ffmpeg build has no opus encoder. Run with `--format mp3`.
* **A line stays silent in game**: check that the file name matches `<lang>-<id>` exactly and that the NPC's `voice.folder` points at that folder. With `fallbackToBrowser = true` the browser voice steps in for missing files, so a silent line usually means the browser has no voice for `tts.lang` either.
* **Changed a text but the voice is old**: files are skipped when they already exist. Run with `--force`.
