Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions speech-provider-piper/patches/sonata-piper-vowel-clusters.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Support for current piper voice configs in sonata-piper
#
# Voice configs published on huggingface for the retrained piper voices
# (piper_version >= 1.5) declare a `vowel_clusters` field and carry
# multi-codepoint keys ("aɪ", "oʊ", ...) in phoneme_id_map for the
# diphthong tokens those models were trained with. sonata-piper
# previously:
#
# 1. required the long-gone `phoneme_map` field, so raw configs failed
# to deserialize at all, and
# 2. looked up phoneme ids per UTF-8 codepoint, and omitted the pad
# id after BOS, diverging from every piper implementation.
#
# This patch, against the sonata revision pinned by speech-provider-
# piper's Cargo.lock (eeejay/sonata 45ba1a7), makes ModelConfig parse
# raw configs (phoneme_map now optional) and tokenizes phonemes the
# same way piper does: split into codepoints, then merge adjacent
# codepoints matching a declared vowel cluster, so the diphthong's own
# id is used; also emit the pad id after BOS. The result is
# byte-identical phoneme id sequences to piper 1.8.0 for the voices
# tested (en_US-amy-medium, pt_BR-faber-medium, nl_NL-pim-medium).
diff --git a/sonata/models/piper/src/lib.rs b/sonata/models/piper/src/lib.rs
index 7595ffc..c5c20cc 100644
--- a/sonata/models/piper/src/lib.rs
+++ b/sonata/models/piper/src/lib.rs
@@ -17,9 +17,45 @@ use std::sync::{Arc, RwLock};

const MIN_CHUNK_SIZE: isize = 44;
const MAX_CHUNK_SIZE: usize = 1024;
-const BOS: char = '^';
-const EOS: char = '$';
-const PAD: char = '_';
+const BOS: &str = "^";
+const EOS: &str = "$";
+const PAD: &str = "_";
+
+// Tokenize an espeak IPA string the same way piper does: split it into
+// UTF-8 codepoints, then merge adjacent codepoints that form one of the
+// vowel clusters declared in the voice config (e.g. "a"+"ɪ" -> "aɪ") so
+// that the diphthong's own id is used. Retrained piper voices expect
+// these merged tokens; older voices declare no clusters and are looked
+// up per codepoint as before.
+fn split_and_merge_vowel_clusters(phonemes: &str, clusters: &[Vec<String>]) -> Vec<String> {
+ let chars: Vec<char> = phonemes.chars().collect();
+ let max_len = clusters.iter().map(Vec::len).max().unwrap_or(0);
+ let cluster_strs: Vec<String> = clusters.iter().map(|c| c.join("")).collect();
+ let mut tokens: Vec<String> = Vec::with_capacity(chars.len());
+ let mut i = 0;
+ while i < chars.len() {
+ let mut matched: Option<(usize, String)> = None;
+ let hi = max_len.min(chars.len() - i);
+ for n in (2..=hi).rev() {
+ let candidate: String = chars[i..i + n].iter().collect();
+ if cluster_strs.iter().any(|s| *s == candidate) {
+ matched = Some((n, candidate));
+ break;
+ }
+ }
+ match matched {
+ Some((n, token)) => {
+ tokens.push(token);
+ i += n;
+ }
+ None => {
+ tokens.push(chars[i].to_string());
+ i += 1;
+ }
+ }
+ }
+ tokens
+}

#[inline(always)]
fn reversed_mapping<K, V>(input: &HashMap<K, V>) -> HashMap<V, K>
@@ -152,9 +188,11 @@ pub struct ModelConfig {
inference: InferenceConfig,
#[allow(dead_code)]
num_symbols: u32,
+ #[serde(default)]
#[allow(dead_code)]
phoneme_map: HashMap<i64, char>,
- phoneme_id_map: HashMap<char, Vec<i64>>,
+ phoneme_id_map: HashMap<String, Vec<i64>>,
+ vowel_clusters: Option<Vec<Vec<String>>>,
}

#[derive(Debug, Clone, Default)]
@@ -172,9 +210,9 @@ trait VitsModelCommons {
fn get_tashkeel_engine(&self) -> Option<&libtashkeel_base::DynamicInferenceEngine>;
fn get_meta_ids(&self) -> (i64, i64, i64) {
let config = self.get_config();
- let pad_id = *config.phoneme_id_map.get(&PAD).unwrap().first().unwrap();
- let bos_id = *config.phoneme_id_map.get(&BOS).unwrap().first().unwrap();
- let eos_id = *config.phoneme_id_map.get(&EOS).unwrap().first().unwrap();
+ let pad_id = *config.phoneme_id_map.get(PAD).unwrap().first().unwrap();
+ let bos_id = *config.phoneme_id_map.get(BOS).unwrap().first().unwrap();
+ let eos_id = *config.phoneme_id_map.get(EOS).unwrap().first().unwrap();
(pad_id, bos_id, eos_id)
}
fn language(&self) -> Option<String> {
@@ -239,8 +277,13 @@ trait VitsModelCommons {
let config = self.get_config();
let mut phoneme_ids: Vec<i64> = Vec::with_capacity((phonemes.len() + 1) * 2);
phoneme_ids.push(bos_id);
- for phoneme in phonemes.chars() {
- if let Some(id) = config.phoneme_id_map.get(&phoneme) {
+ phoneme_ids.push(pad_id);
+ let tokens = split_and_merge_vowel_clusters(
+ phonemes,
+ config.vowel_clusters.as_deref().unwrap_or(&[]),
+ );
+ for phoneme in tokens {
+ if let Some(id) = config.phoneme_id_map.get(phoneme.as_str()) {
phoneme_ids.push(*id.first().unwrap());
phoneme_ids.push(pad_id);
}
110 changes: 107 additions & 3 deletions speech-provider-piper/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ apps:
daemon-scope: user
activates-on: [speech-provider]
environment:
ESPEAK_DATA_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/espeak-ng-data
# espeak-ng appends "espeak-ng-data" to this path; the matching data
# snapshot is staged by the espeak-ng-data part
ESPEAK_DATA_PATH: $SNAP/usr/share/piper
LD_LIBRARY_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/pulseaudio:$LD_LIBRARY_PATH
plugs:
- audio-playback
Expand All @@ -49,6 +51,28 @@ parts:
- python3-tap
- gi-docgen

# espeak-ng data matching the piper voices: the retrained voices on
# huggingface were trained against the newer phoneme inventory shipped
# by piper1-gpl (e.g. pt-BR "ɐ" where the distro's espeak-ng-data
# package still produces "æ"), so phonemization must use the same data
# snapshot as the reference piper. Staged under share/piper because the
# distro espeak-ng-data (pulled in via libespeak-ng1) occupies the
# default location; the app's ESPEAK_DATA_PATH points at its parent.
espeak-ng-data:
plugin: nil
build-packages:
- ca-certificates
- curl
- unzip
override-build: |
curl -L --fail --retry 3 -o "$CRAFT_PART_SRC/piper-tts.whl" \
"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"
unzip -q "$CRAFT_PART_SRC/piper-tts.whl" 'piper/espeak-ng-data/*' \
-d "$CRAFT_PART_SRC/piper-tts"
mkdir -p "$CRAFT_PART_INSTALL/usr/share/piper"
mv "$CRAFT_PART_SRC/piper-tts/piper/espeak-ng-data" \
"$CRAFT_PART_INSTALL/usr/share/piper/espeak-ng-data"

speech-provider-piper:
plugin: meson
source: https://github.com/project-spiel/speech-provider-piper.git
Expand All @@ -59,11 +83,13 @@ parts:
- rustup
build-packages:
- cmake
- jq
- libclang-dev
- libespeak-ng-dev
- patch
stage-packages:
# Piper requires espeak-ng-data for phoneme generation
- espeak-ng-data
# Runtime espeak-ng library; the espeak-ng-data snapshot matching the
# retrained voices comes from the espeak-ng-data part above.
- libespeak-ng1
- libpulse0
build-environment:
Expand All @@ -81,6 +107,22 @@ parts:
rustup default "$RUSTUP_TOOLCHAIN"
rustc --version
cargo --version
# Patch the sonata revision pinned by Cargo.lock before building:
# current piper voice configs need sonata-piper changes that are
# not in that pin (see patches/ for details and the verification
# against piper's own phoneme id output).
cargo fetch --manifest-path "$CRAFT_PART_SRC/Cargo.toml" --locked
sonata_checkout_dir="$(cargo metadata --format-version 1 --locked \
--manifest-path "$CRAFT_PART_SRC/Cargo.toml" \
| jq -r '.packages[] | select(.name == "sonata-piper") | .manifest_path
| rtrimstr("/Cargo.toml") | rtrimstr("/sonata/models/piper")')"
# cargo reuses a previously fetched checkout, so skip the patch when
# it is already applied to it
if ! grep -q "vowel_clusters" \
"$sonata_checkout_dir/sonata/models/piper/src/lib.rs"; then
patch -d "$sonata_checkout_dir" -p1 \
< "${CRAFT_PROJECT_DIR:-$SNAPCRAFT_PROJECT_DIR}/patches/sonata-piper-vowel-clusters.patch"
fi
craftctl default

voices-cs-cz-jirka-medium:
Expand Down Expand Up @@ -413,6 +455,28 @@ parts:
meta/hooks/install: (component/voices-it-it-paola-medium)/meta/hooks/install
meta/hooks/post-refresh: (component/voices-it-it-paola-medium)/meta/hooks/post-refresh

voices-nl-nl-pim-medium:
plugin: dump
source: ./component-base
build-packages:
- curl
- ca-certificates
override-build: |
craftctl default
set -eu
mkdir -p "$CRAFT_PART_INSTALL/nl_NL-pim-medium"
curl -L --fail --retry 3 \
-o "$CRAFT_PART_INSTALL/nl_NL-pim-medium/nl_NL-pim-medium.onnx" \
"https://huggingface.co/rhasspy/piper-voices/resolve/main/nl/nl_NL/pim/medium/nl_NL-pim-medium.onnx"
curl -L --fail --retry 3 \
-o "$CRAFT_PART_INSTALL/nl_NL-pim-medium/nl_NL-pim-medium.onnx.json" \
"https://huggingface.co/rhasspy/piper-voices/resolve/main/nl/nl_NL/pim/medium/nl_NL-pim-medium.onnx.json"
organize:
nl_NL-pim-medium/nl_NL-pim-medium.onnx: (component/voices-nl-nl-pim-medium)/nl_NL-pim-medium.onnx
nl_NL-pim-medium/nl_NL-pim-medium.onnx.json: (component/voices-nl-nl-pim-medium)/nl_NL-pim-medium.onnx.json
meta/hooks/install: (component/voices-nl-nl-pim-medium)/meta/hooks/install
meta/hooks/post-refresh: (component/voices-nl-nl-pim-medium)/meta/hooks/post-refresh

voices-no-no-talesyntese-medium:
plugin: dump
source: ./component-base
Expand Down Expand Up @@ -457,6 +521,28 @@ parts:
meta/hooks/install: (component/voices-pl-pl-gosia-medium)/meta/hooks/install
meta/hooks/post-refresh: (component/voices-pl-pl-gosia-medium)/meta/hooks/post-refresh

voices-pt-br-faber-medium:
plugin: dump
source: ./component-base
build-packages:
- curl
- ca-certificates
override-build: |
craftctl default
set -eu
mkdir -p "$CRAFT_PART_INSTALL/pt_BR-faber-medium"
curl -L --fail --retry 3 \
-o "$CRAFT_PART_INSTALL/pt_BR-faber-medium/pt_BR-faber-medium.onnx" \
"https://huggingface.co/rhasspy/piper-voices/resolve/main/pt/pt_BR/faber/medium/pt_BR-faber-medium.onnx"
curl -L --fail --retry 3 \
-o "$CRAFT_PART_INSTALL/pt_BR-faber-medium/pt_BR-faber-medium.onnx.json" \
"https://huggingface.co/rhasspy/piper-voices/resolve/main/pt/pt_BR/faber/medium/pt_BR-faber-medium.onnx.json"
organize:
pt_BR-faber-medium/pt_BR-faber-medium.onnx: (component/voices-pt-br-faber-medium)/pt_BR-faber-medium.onnx
pt_BR-faber-medium/pt_BR-faber-medium.onnx.json: (component/voices-pt-br-faber-medium)/pt_BR-faber-medium.onnx.json
meta/hooks/install: (component/voices-pt-br-faber-medium)/meta/hooks/install
meta/hooks/post-refresh: (component/voices-pt-br-faber-medium)/meta/hooks/post-refresh

voices-ro-ro-mihai-medium:
plugin: dump
source: ./component-base
Expand Down Expand Up @@ -681,6 +767,15 @@ components:
install: {}
post-refresh: {}

voices-nl-nl-pim-medium:
type: standard
summary: nl-NL pim voice for Piper
description: nl-NL pim voice for Piper
version: '0.1'
hooks:
install: {}
post-refresh: {}

voices-no-no-talesyntese-medium:
type: standard
summary: no-NO talesyntese voice for Piper
Expand All @@ -699,6 +794,15 @@ components:
install: {}
post-refresh: {}

voices-pt-br-faber-medium:
type: standard
summary: pt-BR faber voice for Piper
description: pt-BR faber voice for Piper
version: '0.1'
hooks:
install: {}
post-refresh: {}

voices-ro-ro-mihai-medium:
type: standard
summary: ro-RO mihai voice for Piper
Expand Down
48 changes: 45 additions & 3 deletions speech-provider-piper/snapcraft.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ apps:
daemon-scope: user
activates-on: [speech-provider]
environment:
ESPEAK_DATA_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/espeak-ng-data
# espeak-ng appends "espeak-ng-data" to this path; the matching data
# snapshot is staged by the espeak-ng-data part
ESPEAK_DATA_PATH: $SNAP/usr/share/piper
LD_LIBRARY_PATH: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET/pulseaudio:$LD_LIBRARY_PATH
plugs:
- audio-playback
Expand All @@ -46,6 +48,28 @@ parts:
- python3-tap
- gi-docgen

# espeak-ng data matching the piper voices: the retrained voices on
# huggingface were trained against the newer phoneme inventory shipped
# by piper1-gpl (e.g. pt-BR "ɐ" where the distro's espeak-ng-data
# package still produces "æ"), so phonemization must use the same data
# snapshot as the reference piper. Staged under share/piper because the
# distro espeak-ng-data (pulled in via libespeak-ng1) occupies the
# default location; the app's ESPEAK_DATA_PATH points at its parent.
espeak-ng-data:
plugin: nil
build-packages:
- ca-certificates
- curl
- unzip
override-build: |
curl -L --fail --retry 3 -o "$CRAFT_PART_SRC/piper-tts.whl" \
"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"
unzip -q "$CRAFT_PART_SRC/piper-tts.whl" 'piper/espeak-ng-data/*' \
-d "$CRAFT_PART_SRC/piper-tts"
mkdir -p "$CRAFT_PART_INSTALL/usr/share/piper"
mv "$CRAFT_PART_SRC/piper-tts/piper/espeak-ng-data" \
"$CRAFT_PART_INSTALL/usr/share/piper/espeak-ng-data"

speech-provider-piper:
plugin: meson
source: https://github.com/project-spiel/speech-provider-piper.git
Expand All @@ -56,11 +80,13 @@ parts:
- rustup
build-packages:
- cmake
- jq
- libclang-dev
- libespeak-ng-dev
- patch
stage-packages:
# Piper requires espeak-ng-data for phoneme generation
- espeak-ng-data
# Runtime espeak-ng library; the espeak-ng-data snapshot matching the
# retrained voices comes from the espeak-ng-data part above.
- libespeak-ng1
- libpulse0
build-environment:
Expand All @@ -78,4 +104,20 @@ parts:
rustup default "$RUSTUP_TOOLCHAIN"
rustc --version
cargo --version
# Patch the sonata revision pinned by Cargo.lock before building:
# current piper voice configs need sonata-piper changes that are
# not in that pin (see patches/ for details and the verification
# against piper's own phoneme id output).
cargo fetch --manifest-path "$CRAFT_PART_SRC/Cargo.toml" --locked
sonata_checkout_dir="$(cargo metadata --format-version 1 --locked \
--manifest-path "$CRAFT_PART_SRC/Cargo.toml" \
| jq -r '.packages[] | select(.name == "sonata-piper") | .manifest_path
| rtrimstr("/Cargo.toml") | rtrimstr("/sonata/models/piper")')"
# cargo reuses a previously fetched checkout, so skip the patch when
# it is already applied to it
if ! grep -q "vowel_clusters" \
"$sonata_checkout_dir/sonata/models/piper/src/lib.rs"; then
patch -d "$sonata_checkout_dir" -p1 \
< "${CRAFT_PROJECT_DIR:-$SNAPCRAFT_PROJECT_DIR}/patches/sonata-piper-vowel-clusters.patch"
fi
craftctl default
17 changes: 8 additions & 9 deletions speech-provider-piper/voices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ it-IT:
# test_phrase: 다람쥐 헌 쳇바퀴에 타고파
# voices:
# - kss-medium
#nl-NL:
# test_phrase: Pa's wijze lynx bezag vroom het fikse aquaduct.
# voices:
# - pim-medium
nl-NL:
test_phrase: Pa's wijze lynx bezag vroom het fikse aquaduct.
voices:
- pim-medium
no-NO:
test_phrase: Vår sære Zulu fra badeøya spilte jazz og quickstep i min taxi.
voices:
Expand All @@ -77,11 +77,10 @@ pl-PL:
test_phrase: Pchnąć w tę łódź jeża lub ośm skrzyń fig.
voices:
- gosia-medium
#FIXME does not work
#pt-BR:
# test_phrase: Um pequeno jabuti xereta viu dez cegonhas felizes.
# voices:
# - faber-medium
pt-BR:
test_phrase: Um pequeno jabuti xereta viu dez cegonhas felizes.
voices:
- faber-medium
ro-RO:
test_phrase: Muzicologă în bej vând whisky și tequila, preț fix.
voices:
Expand Down
Loading