Skip to content

IndexError: index 510 is out of bounds in _create_audio when phonemes are truncated to MAX_PHONEME_LENGTH #184

Description

@matteofrassi

Bug description

When input text produces phonemes that are truncated to MAX_PHONEME_LENGTH (510), _create_audio raises an IndexError at line 108:

voice = voice[len(tokens)]

The voice array has shape (510, ...) (valid indices 0–509), but len(tokens) can be exactly 510 after truncation, causing an out-of-bounds access.

Steps to reproduce

from kokoro_onnx import Kokoro

kokoro = Kokoro("kokoro-v1.0.onnx", "voices-v1.0.bin")

# Any text long enough to produce ≥510 phonemes triggers the bug
long_text = "L'intelligenza artificiale sta trasformando radicalmente il modo in cui le aziende operano e competono sul mercato globale. " * 10

samples, sr = kokoro.create(long_text, voice="if_sara", speed=1.0, lang="it")

Error

WARNING  [__init__.py:98] Phonemes are too long, truncating to 510 phonemes
Traceback (most recent call last):
  ...
  File "kokoro_onnx/__init__.py", line 201, in create
    audio_part, _ = self._create_audio(phonemes, voice, speed)
  File "kokoro_onnx/__init__.py", line 108, in _create_audio
    voice = voice[len(tokens)]
IndexError: index 510 is out of bounds for axis 0 with size 510

Root cause

In _create_audio (__init__.py):

  1. Line 101 truncates phonemes to 510 characters: phonemes = phonemes[:MAX_PHONEME_LENGTH]
  2. Line 103 tokenizes → len(tokens) can be exactly 510
  3. Line 104 asserts len(tokens) <= MAX_PHONEME_LENGTH → passes (510 <= 510)
  4. Line 108 indexes voice[len(tokens)] → voice[510] is out of bounds for an array of size 510

Suggested fix

Clamp the index to prevent out-of-bounds access:

- voice = voice[len(tokens)]
+ voice = voice[min(len(tokens), len(voice) - 1)]

Environment

  • kokoro-onnx: 0.5.0
  • Python: 3.12.12
  • OS: macOS (Darwin 25.3.0)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions