Skip to content

Commit bb6ff57

Browse files
authored
Patch sonata to support newer model configs (#3)
Add nl-NL and pt-BR voices, with necessary patches
1 parent 25ba09a commit bb6ff57

4 files changed

Lines changed: 276 additions & 15 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Support for current piper voice configs in sonata-piper
2+
#
3+
# Voice configs published on huggingface for the retrained piper voices
4+
# (piper_version >= 1.5) declare a `vowel_clusters` field and carry
5+
# multi-codepoint keys ("aɪ", "oʊ", ...) in phoneme_id_map for the
6+
# diphthong tokens those models were trained with. sonata-piper
7+
# previously:
8+
#
9+
# 1. required the long-gone `phoneme_map` field, so raw configs failed
10+
# to deserialize at all, and
11+
# 2. looked up phoneme ids per UTF-8 codepoint, and omitted the pad
12+
# id after BOS, diverging from every piper implementation.
13+
#
14+
# This patch, against the sonata revision pinned by speech-provider-
15+
# piper's Cargo.lock (eeejay/sonata 45ba1a7), makes ModelConfig parse
16+
# raw configs (phoneme_map now optional) and tokenizes phonemes the
17+
# same way piper does: split into codepoints, then merge adjacent
18+
# codepoints matching a declared vowel cluster, so the diphthong's own
19+
# id is used; also emit the pad id after BOS. The result is
20+
# byte-identical phoneme id sequences to piper 1.8.0 for the voices
21+
# tested (en_US-amy-medium, pt_BR-faber-medium, nl_NL-pim-medium).
22+
diff --git a/sonata/models/piper/src/lib.rs b/sonata/models/piper/src/lib.rs
23+
index 7595ffc..c5c20cc 100644
24+
--- a/sonata/models/piper/src/lib.rs
25+
+++ b/sonata/models/piper/src/lib.rs
26+
@@ -17,9 +17,45 @@ use std::sync::{Arc, RwLock};
27+
28+
const MIN_CHUNK_SIZE: isize = 44;
29+
const MAX_CHUNK_SIZE: usize = 1024;
30+
-const BOS: char = '^';
31+
-const EOS: char = '$';
32+
-const PAD: char = '_';
33+
+const BOS: &str = "^";
34+
+const EOS: &str = "$";
35+
+const PAD: &str = "_";
36+
+
37+
+// Tokenize an espeak IPA string the same way piper does: split it into
38+
+// UTF-8 codepoints, then merge adjacent codepoints that form one of the
39+
+// vowel clusters declared in the voice config (e.g. "a"+"ɪ" -> "aɪ") so
40+
+// that the diphthong's own id is used. Retrained piper voices expect
41+
+// these merged tokens; older voices declare no clusters and are looked
42+
+// up per codepoint as before.
43+
+fn split_and_merge_vowel_clusters(phonemes: &str, clusters: &[Vec<String>]) -> Vec<String> {
44+
+ let chars: Vec<char> = phonemes.chars().collect();
45+
+ let max_len = clusters.iter().map(Vec::len).max().unwrap_or(0);
46+
+ let cluster_strs: Vec<String> = clusters.iter().map(|c| c.join("")).collect();
47+
+ let mut tokens: Vec<String> = Vec::with_capacity(chars.len());
48+
+ let mut i = 0;
49+
+ while i < chars.len() {
50+
+ let mut matched: Option<(usize, String)> = None;
51+
+ let hi = max_len.min(chars.len() - i);
52+
+ for n in (2..=hi).rev() {
53+
+ let candidate: String = chars[i..i + n].iter().collect();
54+
+ if cluster_strs.iter().any(|s| *s == candidate) {
55+
+ matched = Some((n, candidate));
56+
+ break;
57+
+ }
58+
+ }
59+
+ match matched {
60+
+ Some((n, token)) => {
61+
+ tokens.push(token);
62+
+ i += n;
63+
+ }
64+
+ None => {
65+
+ tokens.push(chars[i].to_string());
66+
+ i += 1;
67+
+ }
68+
+ }
69+
+ }
70+
+ tokens
71+
+}
72+
73+
#[inline(always)]
74+
fn reversed_mapping<K, V>(input: &HashMap<K, V>) -> HashMap<V, K>
75+
@@ -152,9 +188,11 @@ pub struct ModelConfig {
76+
inference: InferenceConfig,
77+
#[allow(dead_code)]
78+
num_symbols: u32,
79+
+ #[serde(default)]
80+
#[allow(dead_code)]
81+
phoneme_map: HashMap<i64, char>,
82+
- phoneme_id_map: HashMap<char, Vec<i64>>,
83+
+ phoneme_id_map: HashMap<String, Vec<i64>>,
84+
+ vowel_clusters: Option<Vec<Vec<String>>>,
85+
}
86+
87+
#[derive(Debug, Clone, Default)]
88+
@@ -172,9 +210,9 @@ trait VitsModelCommons {
89+
fn get_tashkeel_engine(&self) -> Option<&libtashkeel_base::DynamicInferenceEngine>;
90+
fn get_meta_ids(&self) -> (i64, i64, i64) {
91+
let config = self.get_config();
92+
- let pad_id = *config.phoneme_id_map.get(&PAD).unwrap().first().unwrap();
93+
- let bos_id = *config.phoneme_id_map.get(&BOS).unwrap().first().unwrap();
94+
- let eos_id = *config.phoneme_id_map.get(&EOS).unwrap().first().unwrap();
95+
+ let pad_id = *config.phoneme_id_map.get(PAD).unwrap().first().unwrap();
96+
+ let bos_id = *config.phoneme_id_map.get(BOS).unwrap().first().unwrap();
97+
+ let eos_id = *config.phoneme_id_map.get(EOS).unwrap().first().unwrap();
98+
(pad_id, bos_id, eos_id)
99+
}
100+
fn language(&self) -> Option<String> {
101+
@@ -239,8 +277,13 @@ trait VitsModelCommons {
102+
let config = self.get_config();
103+
let mut phoneme_ids: Vec<i64> = Vec::with_capacity((phonemes.len() + 1) * 2);
104+
phoneme_ids.push(bos_id);
105+
- for phoneme in phonemes.chars() {
106+
- if let Some(id) = config.phoneme_id_map.get(&phoneme) {
107+
+ phoneme_ids.push(pad_id);
108+
+ let tokens = split_and_merge_vowel_clusters(
109+
+ phonemes,
110+
+ config.vowel_clusters.as_deref().unwrap_or(&[]),
111+
+ );
112+
+ for phoneme in tokens {
113+
+ if let Some(id) = config.phoneme_id_map.get(phoneme.as_str()) {
114+
phoneme_ids.push(*id.first().unwrap());
115+
phoneme_ids.push(pad_id);
116+
}

‎speech-provider-piper/snapcraft.yaml‎

Lines changed: 107 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ apps:
2323
daemon-scope: user
2424
activates-on: [speech-provider]
2525
environment:
26-
ESPEAK_DATA_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/espeak-ng-data
26+
# espeak-ng appends "espeak-ng-data" to this path; the matching data
27+
# snapshot is staged by the espeak-ng-data part
28+
ESPEAK_DATA_PATH: $SNAP/usr/share/piper
2729
LD_LIBRARY_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/pulseaudio:$LD_LIBRARY_PATH
2830
plugs:
2931
- audio-playback
@@ -49,6 +51,28 @@ parts:
4951
- python3-tap
5052
- gi-docgen
5153

54+
# espeak-ng data matching the piper voices: the retrained voices on
55+
# huggingface were trained against the newer phoneme inventory shipped
56+
# by piper1-gpl (e.g. pt-BR "ɐ" where the distro's espeak-ng-data
57+
# package still produces "æ"), so phonemization must use the same data
58+
# snapshot as the reference piper. Staged under share/piper because the
59+
# distro espeak-ng-data (pulled in via libespeak-ng1) occupies the
60+
# default location; the app's ESPEAK_DATA_PATH points at its parent.
61+
espeak-ng-data:
62+
plugin: nil
63+
build-packages:
64+
- ca-certificates
65+
- curl
66+
- unzip
67+
override-build: |
68+
curl -L --fail --retry 3 -o "$CRAFT_PART_SRC/piper-tts.whl" \
69+
"https://github.com/OHF-Voice/piper1-gpl/releases/download/v1.8.0/piper_tts-1.8.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl"
70+
unzip -q "$CRAFT_PART_SRC/piper-tts.whl" 'piper/espeak-ng-data/*' \
71+
-d "$CRAFT_PART_SRC/piper-tts"
72+
mkdir -p "$CRAFT_PART_INSTALL/usr/share/piper"
73+
mv "$CRAFT_PART_SRC/piper-tts/piper/espeak-ng-data" \
74+
"$CRAFT_PART_INSTALL/usr/share/piper/espeak-ng-data"
75+
5276
speech-provider-piper:
5377
plugin: meson
5478
source: https://github.com/project-spiel/speech-provider-piper.git
@@ -59,11 +83,13 @@ parts:
5983
- rustup
6084
build-packages:
6185
- cmake
86+
- jq
6287
- libclang-dev
6388
- libespeak-ng-dev
89+
- patch
6490
stage-packages:
65-
# Piper requires espeak-ng-data for phoneme generation
66-
- espeak-ng-data
91+
# Runtime espeak-ng library; the espeak-ng-data snapshot matching the
92+
# retrained voices comes from the espeak-ng-data part above.
6793
- libespeak-ng1
6894
- libpulse0
6995
build-environment:
@@ -81,6 +107,22 @@ parts:
81107
rustup default "$RUSTUP_TOOLCHAIN"
82108
rustc --version
83109
cargo --version
110+
# Patch the sonata revision pinned by Cargo.lock before building:
111+
# current piper voice configs need sonata-piper changes that are
112+
# not in that pin (see patches/ for details and the verification
113+
# against piper's own phoneme id output).
114+
cargo fetch --manifest-path "$CRAFT_PART_SRC/Cargo.toml" --locked
115+
sonata_checkout_dir="$(cargo metadata --format-version 1 --locked \
116+
--manifest-path "$CRAFT_PART_SRC/Cargo.toml" \
117+
| jq -r '.packages[] | select(.name == "sonata-piper") | .manifest_path
118+
| rtrimstr("/Cargo.toml") | rtrimstr("/sonata/models/piper")')"
119+
# cargo reuses a previously fetched checkout, so skip the patch when
120+
# it is already applied to it
121+
if ! grep -q "vowel_clusters" \
122+
"$sonata_checkout_dir/sonata/models/piper/src/lib.rs"; then
123+
patch -d "$sonata_checkout_dir" -p1 \
124+
< "${CRAFT_PROJECT_DIR:-$SNAPCRAFT_PROJECT_DIR}/patches/sonata-piper-vowel-clusters.patch"
125+
fi
84126
craftctl default
85127
86128
voices-cs-cz-jirka-medium:
@@ -413,6 +455,28 @@ parts:
413455
meta/hooks/install: (component/voices-it-it-paola-medium)/meta/hooks/install
414456
meta/hooks/post-refresh: (component/voices-it-it-paola-medium)/meta/hooks/post-refresh
415457

458+
voices-nl-nl-pim-medium:
459+
plugin: dump
460+
source: ./component-base
461+
build-packages:
462+
- curl
463+
- ca-certificates
464+
override-build: |
465+
craftctl default
466+
set -eu
467+
mkdir -p "$CRAFT_PART_INSTALL/nl_NL-pim-medium"
468+
curl -L --fail --retry 3 \
469+
-o "$CRAFT_PART_INSTALL/nl_NL-pim-medium/nl_NL-pim-medium.onnx" \
470+
"https://huggingface.co/rhasspy/piper-voices/resolve/main/nl/nl_NL/pim/medium/nl_NL-pim-medium.onnx"
471+
curl -L --fail --retry 3 \
472+
-o "$CRAFT_PART_INSTALL/nl_NL-pim-medium/nl_NL-pim-medium.onnx.json" \
473+
"https://huggingface.co/rhasspy/piper-voices/resolve/main/nl/nl_NL/pim/medium/nl_NL-pim-medium.onnx.json"
474+
organize:
475+
nl_NL-pim-medium/nl_NL-pim-medium.onnx: (component/voices-nl-nl-pim-medium)/nl_NL-pim-medium.onnx
476+
nl_NL-pim-medium/nl_NL-pim-medium.onnx.json: (component/voices-nl-nl-pim-medium)/nl_NL-pim-medium.onnx.json
477+
meta/hooks/install: (component/voices-nl-nl-pim-medium)/meta/hooks/install
478+
meta/hooks/post-refresh: (component/voices-nl-nl-pim-medium)/meta/hooks/post-refresh
479+
416480
voices-no-no-talesyntese-medium:
417481
plugin: dump
418482
source: ./component-base
@@ -457,6 +521,28 @@ parts:
457521
meta/hooks/install: (component/voices-pl-pl-gosia-medium)/meta/hooks/install
458522
meta/hooks/post-refresh: (component/voices-pl-pl-gosia-medium)/meta/hooks/post-refresh
459523

524+
voices-pt-br-faber-medium:
525+
plugin: dump
526+
source: ./component-base
527+
build-packages:
528+
- curl
529+
- ca-certificates
530+
override-build: |
531+
craftctl default
532+
set -eu
533+
mkdir -p "$CRAFT_PART_INSTALL/pt_BR-faber-medium"
534+
curl -L --fail --retry 3 \
535+
-o "$CRAFT_PART_INSTALL/pt_BR-faber-medium/pt_BR-faber-medium.onnx" \
536+
"https://huggingface.co/rhasspy/piper-voices/resolve/main/pt/pt_BR/faber/medium/pt_BR-faber-medium.onnx"
537+
curl -L --fail --retry 3 \
538+
-o "$CRAFT_PART_INSTALL/pt_BR-faber-medium/pt_BR-faber-medium.onnx.json" \
539+
"https://huggingface.co/rhasspy/piper-voices/resolve/main/pt/pt_BR/faber/medium/pt_BR-faber-medium.onnx.json"
540+
organize:
541+
pt_BR-faber-medium/pt_BR-faber-medium.onnx: (component/voices-pt-br-faber-medium)/pt_BR-faber-medium.onnx
542+
pt_BR-faber-medium/pt_BR-faber-medium.onnx.json: (component/voices-pt-br-faber-medium)/pt_BR-faber-medium.onnx.json
543+
meta/hooks/install: (component/voices-pt-br-faber-medium)/meta/hooks/install
544+
meta/hooks/post-refresh: (component/voices-pt-br-faber-medium)/meta/hooks/post-refresh
545+
460546
voices-ro-ro-mihai-medium:
461547
plugin: dump
462548
source: ./component-base
@@ -681,6 +767,15 @@ components:
681767
install: {}
682768
post-refresh: {}
683769

770+
voices-nl-nl-pim-medium:
771+
type: standard
772+
summary: nl-NL pim voice for Piper
773+
description: nl-NL pim voice for Piper
774+
version: '0.1'
775+
hooks:
776+
install: {}
777+
post-refresh: {}
778+
684779
voices-no-no-talesyntese-medium:
685780
type: standard
686781
summary: no-NO talesyntese voice for Piper
@@ -699,6 +794,15 @@ components:
699794
install: {}
700795
post-refresh: {}
701796

797+
voices-pt-br-faber-medium:
798+
type: standard
799+
summary: pt-BR faber voice for Piper
800+
description: pt-BR faber voice for Piper
801+
version: '0.1'
802+
hooks:
803+
install: {}
804+
post-refresh: {}
805+
702806
voices-ro-ro-mihai-medium:
703807
type: standard
704808
summary: ro-RO mihai voice for Piper

‎speech-provider-piper/snapcraft.yaml.in‎

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ apps:
2020
daemon-scope: user
2121
activates-on: [speech-provider]
2222
environment:
23-
ESPEAK_DATA_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/espeak-ng-data
23+
# espeak-ng appends "espeak-ng-data" to this path; the matching data
24+
# snapshot is staged by the espeak-ng-data part
25+
ESPEAK_DATA_PATH: $SNAP/usr/share/piper
2426
LD_LIBRARY_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/pulseaudio:$LD_LIBRARY_PATH
2527
plugs:
2628
- audio-playback
@@ -46,6 +48,28 @@ parts:
4648
- python3-tap
4749
- gi-docgen
4850

51+
# espeak-ng data matching the piper voices: the retrained voices on
52+
# huggingface were trained against the newer phoneme inventory shipped
53+
# by piper1-gpl (e.g. pt-BR "ɐ" where the distro's espeak-ng-data
54+
# package still produces "æ"), so phonemization must use the same data
55+
# snapshot as the reference piper. Staged under share/piper because the
56+
# distro espeak-ng-data (pulled in via libespeak-ng1) occupies the
57+
# default location; the app's ESPEAK_DATA_PATH points at its parent.
58+
espeak-ng-data:
59+
plugin: nil
60+
build-packages:
61+
- ca-certificates
62+
- curl
63+
- unzip
64+
override-build: |
65+
curl -L --fail --retry 3 -o "$CRAFT_PART_SRC/piper-tts.whl" \
66+
"https://github.com/OHF-Voice/piper1-gpl/releases/download/v1.8.0/piper_tts-1.8.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_28_x86_64.whl"
67+
unzip -q "$CRAFT_PART_SRC/piper-tts.whl" 'piper/espeak-ng-data/*' \
68+
-d "$CRAFT_PART_SRC/piper-tts"
69+
mkdir -p "$CRAFT_PART_INSTALL/usr/share/piper"
70+
mv "$CRAFT_PART_SRC/piper-tts/piper/espeak-ng-data" \
71+
"$CRAFT_PART_INSTALL/usr/share/piper/espeak-ng-data"
72+
4973
speech-provider-piper:
5074
plugin: meson
5175
source: https://github.com/project-spiel/speech-provider-piper.git
@@ -56,11 +80,13 @@ parts:
5680
- rustup
5781
build-packages:
5882
- cmake
83+
- jq
5984
- libclang-dev
6085
- libespeak-ng-dev
86+
- patch
6187
stage-packages:
62-
# Piper requires espeak-ng-data for phoneme generation
63-
- espeak-ng-data
88+
# Runtime espeak-ng library; the espeak-ng-data snapshot matching the
89+
# retrained voices comes from the espeak-ng-data part above.
6490
- libespeak-ng1
6591
- libpulse0
6692
build-environment:
@@ -78,4 +104,20 @@ parts:
78104
rustup default "$RUSTUP_TOOLCHAIN"
79105
rustc --version
80106
cargo --version
107+
# Patch the sonata revision pinned by Cargo.lock before building:
108+
# current piper voice configs need sonata-piper changes that are
109+
# not in that pin (see patches/ for details and the verification
110+
# against piper's own phoneme id output).
111+
cargo fetch --manifest-path "$CRAFT_PART_SRC/Cargo.toml" --locked
112+
sonata_checkout_dir="$(cargo metadata --format-version 1 --locked \
113+
--manifest-path "$CRAFT_PART_SRC/Cargo.toml" \
114+
| jq -r '.packages[] | select(.name == "sonata-piper") | .manifest_path
115+
| rtrimstr("/Cargo.toml") | rtrimstr("/sonata/models/piper")')"
116+
# cargo reuses a previously fetched checkout, so skip the patch when
117+
# it is already applied to it
118+
if ! grep -q "vowel_clusters" \
119+
"$sonata_checkout_dir/sonata/models/piper/src/lib.rs"; then
120+
patch -d "$sonata_checkout_dir" -p1 \
121+
< "${CRAFT_PROJECT_DIR:-$SNAPCRAFT_PROJECT_DIR}/patches/sonata-piper-vowel-clusters.patch"
122+
fi
81123
craftctl default

‎speech-provider-piper/voices.yaml‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ it-IT:
6565
# test_phrase: 다람쥐 헌 쳇바퀴에 타고파
6666
# voices:
6767
# - kss-medium
68-
#nl-NL:
69-
# test_phrase: Pa's wijze lynx bezag vroom het fikse aquaduct.
70-
# voices:
71-
# - pim-medium
68+
nl-NL:
69+
test_phrase: Pa's wijze lynx bezag vroom het fikse aquaduct.
70+
voices:
71+
- pim-medium
7272
no-NO:
7373
test_phrase: Vår sære Zulu fra badeøya spilte jazz og quickstep i min taxi.
7474
voices:
@@ -77,11 +77,10 @@ pl-PL:
7777
test_phrase: Pchnąć w tę łódź jeża lub ośm skrzyń fig.
7878
voices:
7979
- gosia-medium
80-
#FIXME does not work
81-
#pt-BR:
82-
# test_phrase: Um pequeno jabuti xereta viu dez cegonhas felizes.
83-
# voices:
84-
# - faber-medium
80+
pt-BR:
81+
test_phrase: Um pequeno jabuti xereta viu dez cegonhas felizes.
82+
voices:
83+
- faber-medium
8584
ro-RO:
8685
test_phrase: Muzicologă în bej vând whisky și tequila, preț fix.
8786
voices:

0 commit comments

Comments
 (0)