Skip to content

Commit 5e44db4

Browse files
committed
Add default speaker id
1 parent 49fab2f commit 5e44db4

6 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/piper/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class PiperConfig:
5858
The final cluster phoneme must be present in the id map.
5959
"""
6060

61+
default_speaker_id: int = 0
62+
"""Id of the default speaker for multi-speaker voices."""
63+
6164
@staticmethod
6265
def from_dict(config: dict[str, Any]) -> "PiperConfig":
6366
"""Load configuration from a dictionary."""
@@ -84,6 +87,8 @@ def from_dict(config: dict[str, Any]) -> "PiperConfig":
8487
vowel_clusters=(
8588
{tuple(vc) for vc in vowel_clusters} if vowel_clusters else None
8689
),
90+
#
91+
default_speaker_id=config.get("default_speaker_id", 0),
8792
)
8893

8994
def to_dict(self) -> dict[str, Any]:
@@ -106,6 +111,7 @@ def to_dict(self) -> dict[str, Any]:
106111
"phoneme_id_map": self.phoneme_id_map,
107112
"speaker_id_map": self.speaker_id_map,
108113
"hop_length": self.hop_length,
114+
"default_speaker_id": self.default_speaker_id,
109115
}
110116

111117
if self.piper_version:

src/piper/http_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ def app_synthesize() -> bytes:
262262
speaker,
263263
voice.config.speaker_id_map.keys(),
264264
)
265-
speaker_id = args.speaker or 0
265+
speaker_id = args.speaker or voice.config.default_speaker_id
266266

267267
if (speaker_id is not None) and (speaker_id > voice.config.num_speakers):
268268
speaker_id = 0

src/piper/phonemize_espeak.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
import re
44
import unicodedata
5+
from collections.abc import Sequence
56
from pathlib import Path
6-
from typing import Dict, List, Optional, Set, Tuple, Union
7+
from typing import List, Optional, Set, Tuple, Union
78

89
_DIR = Path(__file__).parent
910
ESPEAK_DATA_DIR = _DIR / "espeak-ng-data"
1011

11-
from collections.abc import Sequence
12-
1312

1413
class EspeakPhonemizer:
1514
"""Phonemizer that uses espeak-ng."""

src/piper/voice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def phoneme_ids_to_audio(
504504

505505
if (self.config.num_speakers > 1) and (speaker_id is None):
506506
# Default speaker
507-
speaker_id = 0
507+
speaker_id = self.config.default_speaker_id
508508

509509
if speaker_id is not None:
510510
sid = np.array([speaker_id], dtype=np.int64)

tests/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
EN_US_VOWEL_CLUSTERS = {
1+
from typing import Set, Tuple
2+
3+
EN_US_VOWEL_CLUSTERS: Set[Tuple[str, ...]] = {
24
("a", "ɪ"),
35
("a", "ʊ"),
46
("ɔ", "ɪ"),

tests/test_piper.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from piper import PiperVoice
1414
from piper.const import BOS, EOS
1515
from piper.phonemize_espeak import EspeakPhonemizer
16+
from piper.train.vits.dataset import VitsDataModule
1617

1718
from . import EN_US_VOWEL_CLUSTERS
1819

@@ -555,9 +556,6 @@ def test_training_phonemize_matches_inference_with_vowel_clusters() -> None:
555556
"""
556557
import json
557558

558-
from piper.phonemize_espeak import EspeakPhonemizer
559-
from piper.train.vits.dataset import VitsDataModule
560-
561559
data_module = VitsDataModule(
562560
csv_path="dataset.csv",
563561
cache_dir="cache",

0 commit comments

Comments
 (0)