apptor-tiny-tts German v1 (step 82044, Vocos 60k)
Browse files- .gitattributes +2 -0
- LICENSE +202 -0
- README.md +94 -0
- apptor-tiny-tts-de.json +94 -0
- apptor-tiny-tts-de.onnx +3 -0
- apptor-tiny-tts-vocos-de.pt +3 -0
- samples/demo-without-vocos.wav +3 -0
- samples/demo.wav +3 -0
- speak.py +66 -0
- tinyvoca/__init__.py +3 -0
- tinyvoca/normalize_de.py +65 -0
- tinyvoca/text.py +281 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
samples/demo-without-vocos.wav filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
samples/demo.wav filter=lfs diff=lfs merge=lfs -text
|
LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
Apache License
|
| 3 |
+
Version 2.0, January 2004
|
| 4 |
+
http://www.apache.org/licenses/
|
| 5 |
+
|
| 6 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
| 7 |
+
|
| 8 |
+
1. Definitions.
|
| 9 |
+
|
| 10 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
| 11 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
| 12 |
+
|
| 13 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
| 14 |
+
the copyright owner that is granting the License.
|
| 15 |
+
|
| 16 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
| 17 |
+
other entities that control, are controlled by, or are under common
|
| 18 |
+
control with that entity. For the purposes of this definition,
|
| 19 |
+
"control" means (i) the power, direct or indirect, to cause the
|
| 20 |
+
direction or management of such entity, whether by contract or
|
| 21 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
| 22 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
| 23 |
+
|
| 24 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
| 25 |
+
exercising permissions granted by this License.
|
| 26 |
+
|
| 27 |
+
"Source" form shall mean the preferred form for making modifications,
|
| 28 |
+
including but not limited to software source code, documentation
|
| 29 |
+
source, and configuration files.
|
| 30 |
+
|
| 31 |
+
"Object" form shall mean any form resulting from mechanical
|
| 32 |
+
transformation or translation of a Source form, including but
|
| 33 |
+
not limited to compiled object code, generated documentation,
|
| 34 |
+
and conversions to other media types.
|
| 35 |
+
|
| 36 |
+
"Work" shall mean the work of authorship, whether in Source or
|
| 37 |
+
Object form, made available under the License, as indicated by a
|
| 38 |
+
copyright notice that is included in or attached to the work
|
| 39 |
+
(an example is provided in the Appendix below).
|
| 40 |
+
|
| 41 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
| 42 |
+
form, that is based on (or derived from) the Work and for which the
|
| 43 |
+
editorial revisions, annotations, elaborations, or other modifications
|
| 44 |
+
represent, as a whole, an original work of authorship. For the purposes
|
| 45 |
+
of this License, Derivative Works shall not include works that remain
|
| 46 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
| 47 |
+
the Work and Derivative Works thereof.
|
| 48 |
+
|
| 49 |
+
"Contribution" shall mean any work of authorship, including
|
| 50 |
+
the original version of the Work and any modifications or additions
|
| 51 |
+
to that Work or Derivative Works thereof, that is intentionally
|
| 52 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
| 53 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
| 54 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
| 55 |
+
means any form of electronic, verbal, or written communication sent
|
| 56 |
+
to the Licensor or its representatives, including but not limited to
|
| 57 |
+
communication on electronic mailing lists, source code control systems,
|
| 58 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
| 59 |
+
Licensor for the purpose of discussing and improving the Work, but
|
| 60 |
+
excluding communication that is conspicuously marked or otherwise
|
| 61 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
| 62 |
+
|
| 63 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
| 64 |
+
on behalf of whom a Contribution has been received by Licensor and
|
| 65 |
+
subsequently incorporated within the Work.
|
| 66 |
+
|
| 67 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
| 68 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 69 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 70 |
+
copyright license to reproduce, prepare Derivative Works of,
|
| 71 |
+
publicly display, publicly perform, sublicense, and distribute the
|
| 72 |
+
Work and such Derivative Works in Source or Object form.
|
| 73 |
+
|
| 74 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
| 75 |
+
this License, each Contributor hereby grants to You a perpetual,
|
| 76 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
| 77 |
+
(except as stated in this section) patent license to make, have made,
|
| 78 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
| 79 |
+
where such license applies only to those patent claims licensable
|
| 80 |
+
by such Contributor that are necessarily infringed by their
|
| 81 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
| 82 |
+
with the Work to which such Contribution(s) was submitted. If You
|
| 83 |
+
institute patent litigation against any entity (including a
|
| 84 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
| 85 |
+
or a Contribution incorporated within the Work constitutes direct
|
| 86 |
+
or contributory patent infringement, then any patent licenses
|
| 87 |
+
granted to You under this License for that Work shall terminate
|
| 88 |
+
as of the date such litigation is filed.
|
| 89 |
+
|
| 90 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
| 91 |
+
Work or Derivative Works thereof in any medium, with or without
|
| 92 |
+
modifications, and in Source or Object form, provided that You
|
| 93 |
+
meet the following conditions:
|
| 94 |
+
|
| 95 |
+
(a) You must give any other recipients of the Work or
|
| 96 |
+
Derivative Works a copy of this License; and
|
| 97 |
+
|
| 98 |
+
(b) You must cause any modified files to carry prominent notices
|
| 99 |
+
stating that You changed the files; and
|
| 100 |
+
|
| 101 |
+
(c) You must retain, in the Source form of any Derivative Works
|
| 102 |
+
that You distribute, all copyright, patent, trademark, and
|
| 103 |
+
attribution notices from the Source form of the Work,
|
| 104 |
+
excluding those notices that do not pertain to any part of
|
| 105 |
+
the Derivative Works; and
|
| 106 |
+
|
| 107 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
| 108 |
+
distribution, then any Derivative Works that You distribute must
|
| 109 |
+
include a readable copy of the attribution notices contained
|
| 110 |
+
within such NOTICE file, excluding those notices that do not
|
| 111 |
+
pertain to any part of the Derivative Works, in at least one
|
| 112 |
+
of the following places: within a NOTICE text file distributed
|
| 113 |
+
as part of the Derivative Works; within the Source form or
|
| 114 |
+
documentation, if provided along with the Derivative Works; or,
|
| 115 |
+
within a display generated by the Derivative Works, if and
|
| 116 |
+
wherever such third-party notices normally appear. The contents
|
| 117 |
+
of the NOTICE file are for informational purposes only and
|
| 118 |
+
do not modify the License. You may add Your own attribution
|
| 119 |
+
notices within Derivative Works that You distribute, alongside
|
| 120 |
+
or as an addendum to the NOTICE text from the Work, provided
|
| 121 |
+
that such additional attribution notices cannot be construed
|
| 122 |
+
as modifying the License.
|
| 123 |
+
|
| 124 |
+
You may add Your own copyright statement to Your modifications and
|
| 125 |
+
may provide additional or different license terms and conditions
|
| 126 |
+
for use, reproduction, or distribution of Your modifications, or
|
| 127 |
+
for any such Derivative Works as a whole, provided Your use,
|
| 128 |
+
reproduction, and distribution of the Work otherwise complies with
|
| 129 |
+
the conditions stated in this License.
|
| 130 |
+
|
| 131 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
| 132 |
+
any Contribution intentionally submitted for inclusion in the Work
|
| 133 |
+
by You to the Licensor shall be under the terms and conditions of
|
| 134 |
+
this License, without any additional terms or conditions.
|
| 135 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
| 136 |
+
the terms of any separate license agreement you may have executed
|
| 137 |
+
with Licensor regarding such Contributions.
|
| 138 |
+
|
| 139 |
+
6. Trademarks. This License does not grant permission to use the trade
|
| 140 |
+
names, trademarks, service marks, or product names of the Licensor,
|
| 141 |
+
except as required for reasonable and customary use in describing the
|
| 142 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
| 143 |
+
|
| 144 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
| 145 |
+
agreed to in writing, Licensor provides the Work (and each
|
| 146 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
| 147 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
| 148 |
+
implied, including, without limitation, any warranties or conditions
|
| 149 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
| 150 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
| 151 |
+
appropriateness of using or redistributing the Work and assume any
|
| 152 |
+
risks associated with Your exercise of permissions under this License.
|
| 153 |
+
|
| 154 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
| 155 |
+
whether in tort (including negligence), contract, or otherwise,
|
| 156 |
+
unless required by applicable law (such as deliberate and grossly
|
| 157 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
| 158 |
+
liable to You for damages, including any direct, indirect, special,
|
| 159 |
+
incidental, or consequential damages of any character arising as a
|
| 160 |
+
result of this License or out of the use or inability to use the
|
| 161 |
+
Work (including but not limited to damages for loss of goodwill,
|
| 162 |
+
work stoppage, computer failure or malfunction, or any and all
|
| 163 |
+
other commercial damages or losses), even if such Contributor
|
| 164 |
+
has been advised of the possibility of such damages.
|
| 165 |
+
|
| 166 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
| 167 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
| 168 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
| 169 |
+
or other liability obligations and/or rights consistent with this
|
| 170 |
+
License. However, in accepting such obligations, You may act only
|
| 171 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
| 172 |
+
of any other Contributor, and only if You agree to indemnify,
|
| 173 |
+
defend, and hold each Contributor harmless for any liability
|
| 174 |
+
incurred by, or claims asserted against, such Contributor by reason
|
| 175 |
+
of your accepting any such warranty or additional liability.
|
| 176 |
+
|
| 177 |
+
END OF TERMS AND CONDITIONS
|
| 178 |
+
|
| 179 |
+
APPENDIX: How to apply the Apache License to your work.
|
| 180 |
+
|
| 181 |
+
To apply the Apache License to your work, attach the following
|
| 182 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
| 183 |
+
replaced with your own identifying information. (Don't include
|
| 184 |
+
the brackets!) The text should be enclosed in the appropriate
|
| 185 |
+
comment syntax for the file format. We also recommend that a
|
| 186 |
+
file or class name and description of purpose be included on the
|
| 187 |
+
same "printed page" as the copyright notice for easier
|
| 188 |
+
identification within third-party archives.
|
| 189 |
+
|
| 190 |
+
Copyright [yyyy] [name of copyright owner]
|
| 191 |
+
|
| 192 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
| 193 |
+
you may not use this file except in compliance with the License.
|
| 194 |
+
You may obtain a copy of the License at
|
| 195 |
+
|
| 196 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
| 197 |
+
|
| 198 |
+
Unless required by applicable law or agreed to in writing, software
|
| 199 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
| 200 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 201 |
+
See the License for the specific language governing permissions and
|
| 202 |
+
limitations under the License.
|
README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- de
|
| 5 |
+
pipeline_tag: text-to-speech
|
| 6 |
+
library_name: onnx
|
| 7 |
+
tags:
|
| 8 |
+
- tts
|
| 9 |
+
- text-to-speech
|
| 10 |
+
- german
|
| 11 |
+
- onnx
|
| 12 |
+
- cpu
|
| 13 |
+
- tiny-voca
|
| 14 |
+
- vocos
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# apptor-tiny-tts (German)
|
| 18 |
+
|
| 19 |
+
A tiny German text-to-speech model that runs on the CPU: **4.5M parameters, one 21 MB ONNX file**, about 40x faster
|
| 20 |
+
than real time on four CPU threads. Two voices: `F2` (female) and `M2` (male), 24 kHz.
|
| 21 |
+
|
| 22 |
+
It is built on **[tiny-voca](https://github.com/n4ze3m/tiny-voca) by n4ze3m**: same architecture and training
|
| 23 |
+
recipe, retrained from scratch for German. This is **not an official tiny-voca release**. Thanks to n4ze3m for the
|
| 24 |
+
model and the idea.
|
| 25 |
+
|
| 26 |
+
An optional second stage, a [Vocos](https://github.com/gemelo-ai/vocos) vocoder fine-tuned on the two voices
|
| 27 |
+
(13.5M parameters, 54 MB), re-synthesises the waveform and removes most of the buzzy sound of the tiny built-in
|
| 28 |
+
generator. It is still real time on the CPU (~20x).
|
| 29 |
+
|
| 30 |
+
Listen: `samples/demo.wav` (with Vocos) and `samples/demo-without-vocos.wav`.
|
| 31 |
+
|
| 32 |
+
## Files
|
| 33 |
+
|
| 34 |
+
| file | what | license |
|
| 35 |
+
|---|---|---|
|
| 36 |
+
| `apptor-tiny-tts-de.onnx` | the TTS model (text tokens -> 24 kHz audio) | Apache-2.0 |
|
| 37 |
+
| `apptor-tiny-tts-de.json` | sample rate, voices, phoneme symbols, limits | Apache-2.0 |
|
| 38 |
+
| `apptor-tiny-tts-vocos-de.pt` | optional: Vocos state dict, fine-tuned from `charactr/vocos-mel-24khz` | MIT |
|
| 39 |
+
| `tinyvoca/` | German text front end (eSpeak NG phonemes, number normalisation), from tiny-voca | Apache-2.0 |
|
| 40 |
+
| `speak.py` | minimal example | Apache-2.0 |
|
| 41 |
+
|
| 42 |
+
## Use
|
| 43 |
+
|
| 44 |
+
Needs the `espeak-ng` program (`apt install espeak-ng`; GPL-3.0, called as a separate process) and
|
| 45 |
+
`pip install onnxruntime numpy soundfile num2words`.
|
| 46 |
+
|
| 47 |
+
```bash
|
| 48 |
+
python speak.py "Am 3. Mai um 18:30 Uhr kostet das Ticket 12,50 Euro." --voice M2 --out test.wav
|
| 49 |
+
# with the Vocos second stage (pip install torch vocos):
|
| 50 |
+
python speak.py "Hallo, wie geht es dir?" --voice F2 --vocos --out test.wav
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
ONNX inputs: `tokens` and `stress` (int64 [1, N], from `tinyvoca.text.phonemize`), `voice` (int64 [1], index into
|
| 54 |
+
`voices`), `speed`, `pitch_shift`, `pitch_range` (float32 [1]; 1.0 / 0.0 / 1.0 for normal). Outputs: `audio`
|
| 55 |
+
(float32 [1, samples]) and `durations`. At most 512 phoneme tokens per call; split longer text into sentences.
|
| 56 |
+
|
| 57 |
+
It is also a voice of [flash-voice](https://github.com/ViktorsGitUser/flash-voice) (`services/tinytts_server.py`,
|
| 58 |
+
same HTTP API as the other voices there), as an Apache-2.0 alternative to the German Phonon model, which is
|
| 59 |
+
CC BY-NC 4.0 (non-commercial only).
|
| 60 |
+
|
| 61 |
+
## Training
|
| 62 |
+
|
| 63 |
+
- **Data:** about 12 hours of synthetic German speech, ~15,000 utterances, made with
|
| 64 |
+
[Qwen3-TTS](https://github.com/QwenLM/Qwen3-TTS) 1.7B Base (Apache-2.0) with two of its built-in voices. Texts:
|
| 65 |
+
Mozilla Common Voice German sentences (CC0). Utterances were screened with Whisper.
|
| 66 |
+
- **Front end:** eSpeak NG `de`, 67 phoneme symbols (the English inventory of tiny-voca replaced by a German one),
|
| 67 |
+
German number/date/time normalisation.
|
| 68 |
+
- **Model:** tiny-voca recipe, 82k steps on one RTX 3090 (about 10 hours).
|
| 69 |
+
- **Vocoder:** Vocos fine-tuned for about 60k steps on an RTX 5060 Ti: input is tiny-voca's own teacher-forced
|
| 70 |
+
waveform, target the recording.
|
| 71 |
+
- Data, training and tuning took about 12.5 hours on one Sunday.
|
| 72 |
+
|
| 73 |
+
**Evaluation** (Whisper large-v3-turbo, word error rate): 5.0% on 16 unseen sentences (F2 4.6%, M2 5.5%).
|
| 74 |
+
|
| 75 |
+
## Limitations
|
| 76 |
+
|
| 77 |
+
- German only. English words are read with German pronunciation.
|
| 78 |
+
- The sound is clear but not studio quality; without Vocos it is noticeably buzzy.
|
| 79 |
+
- A word-final long "a" before a comma is sometimes drawn out ("Anna, ...").
|
| 80 |
+
- The voices are synthetic (Qwen3-TTS built-in voices), not clones of real people.
|
| 81 |
+
|
| 82 |
+
## Deutsch
|
| 83 |
+
|
| 84 |
+
Ein winziges deutsches TTS-Modell für die CPU (4,5M Parameter, 21 MB ONNX), auf Basis von tiny-voca von n4ze3m
|
| 85 |
+
nachtrainiert, mit optionalem Vocos-Vocoder. Gewichte und Code unter Apache 2.0, also auch kommerziell nutzbar,
|
| 86 |
+
der Vocoder unter MIT. Keine offizielle tiny-voca-Version.
|
| 87 |
+
|
| 88 |
+
## Credits
|
| 89 |
+
|
| 90 |
+
- [tiny-voca](https://github.com/n4ze3m/tiny-voca) by n4ze3m: architecture, training recipe, text front end (Apache-2.0)
|
| 91 |
+
- [Vocos](https://github.com/gemelo-ai/vocos), `charactr/vocos-mel-24khz` (MIT)
|
| 92 |
+
- [Qwen3-TTS](https://github.com/QwenLM/Qwen3-TTS) (Apache-2.0): training speech
|
| 93 |
+
- [Mozilla Common Voice](https://commonvoice.mozilla.org) German (CC0): texts
|
| 94 |
+
- [eSpeak NG](https://github.com/espeak-ng/espeak-ng) (GPL-3.0, not included), num2words (LGPL-2.1)
|
apptor-tiny-tts-de.json
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"sample_rate": 24000,
|
| 3 |
+
"hop": 256,
|
| 4 |
+
"voices": [
|
| 5 |
+
"F2",
|
| 6 |
+
"M2"
|
| 7 |
+
],
|
| 8 |
+
"symbols": [
|
| 9 |
+
"<pad>",
|
| 10 |
+
"<bos>",
|
| 11 |
+
"<eos>",
|
| 12 |
+
"<sp>",
|
| 13 |
+
"<unk>",
|
| 14 |
+
",",
|
| 15 |
+
".",
|
| 16 |
+
"?",
|
| 17 |
+
"!",
|
| 18 |
+
";",
|
| 19 |
+
":",
|
| 20 |
+
"\"",
|
| 21 |
+
"'",
|
| 22 |
+
"(",
|
| 23 |
+
")",
|
| 24 |
+
"—",
|
| 25 |
+
"…",
|
| 26 |
+
"p",
|
| 27 |
+
"b",
|
| 28 |
+
"t",
|
| 29 |
+
"d",
|
| 30 |
+
"k",
|
| 31 |
+
"ɡ",
|
| 32 |
+
"f",
|
| 33 |
+
"v",
|
| 34 |
+
"s",
|
| 35 |
+
"z",
|
| 36 |
+
"ʃ",
|
| 37 |
+
"ʒ",
|
| 38 |
+
"ç",
|
| 39 |
+
"x",
|
| 40 |
+
"h",
|
| 41 |
+
"m",
|
| 42 |
+
"n",
|
| 43 |
+
"ŋ",
|
| 44 |
+
"l",
|
| 45 |
+
"ɾ",
|
| 46 |
+
"r",
|
| 47 |
+
"j",
|
| 48 |
+
"ts",
|
| 49 |
+
"pf",
|
| 50 |
+
"tʃ",
|
| 51 |
+
"dʒ",
|
| 52 |
+
"ə",
|
| 53 |
+
"ɐ",
|
| 54 |
+
"ɜ",
|
| 55 |
+
"ɪ",
|
| 56 |
+
"i",
|
| 57 |
+
"iː",
|
| 58 |
+
"ɛ",
|
| 59 |
+
"ɛː",
|
| 60 |
+
"eː",
|
| 61 |
+
"a",
|
| 62 |
+
"ɑ",
|
| 63 |
+
"ɑː",
|
| 64 |
+
"ɔ",
|
| 65 |
+
"oː",
|
| 66 |
+
"ʊ",
|
| 67 |
+
"uː",
|
| 68 |
+
"y",
|
| 69 |
+
"yː",
|
| 70 |
+
"œ",
|
| 71 |
+
"øː",
|
| 72 |
+
"aɪ",
|
| 73 |
+
"aʊ",
|
| 74 |
+
"ɔø",
|
| 75 |
+
"ʊɐ"
|
| 76 |
+
],
|
| 77 |
+
"max_tokens": 512,
|
| 78 |
+
"max_frames": 3000,
|
| 79 |
+
"step": 82044,
|
| 80 |
+
"frontend": "espeak",
|
| 81 |
+
"frontend_fingerprint": "ddefc3ebfe4beeca2af195dc920c207110b07bd5067f225c575a3cdeef02d73f",
|
| 82 |
+
"inputs": [
|
| 83 |
+
"tokens",
|
| 84 |
+
"stress",
|
| 85 |
+
"voice",
|
| 86 |
+
"speed",
|
| 87 |
+
"pitch_shift",
|
| 88 |
+
"pitch_range"
|
| 89 |
+
],
|
| 90 |
+
"outputs": [
|
| 91 |
+
"audio",
|
| 92 |
+
"durations"
|
| 93 |
+
]
|
| 94 |
+
}
|
apptor-tiny-tts-de.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:594d7536b0138957167bd2a6c9e1ca0301f726e981cab14296799444abdb5e46
|
| 3 |
+
size 21379442
|
apptor-tiny-tts-vocos-de.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5457b535929b1a110eaa49febfb9c383d4fbe948259bee23b33a9db265366679
|
| 3 |
+
size 54369987
|
samples/demo-without-vocos.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f0f50db9201ff42e40662889ddd1c5e306de3f4690c21a6dcfe6252ca356087c
|
| 3 |
+
size 2148268
|
samples/demo.wav
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c1cf2afa84ae2fd413aca5b44571d47b4cdf92ed570b70cf4dd8efeca8bce0ac
|
| 3 |
+
size 2148268
|
speak.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""apptor-tiny-tts: German text -> WAV on the CPU.
|
| 2 |
+
|
| 3 |
+
python speak.py "Hallo, wie geht es dir?" --voice F2 --out hallo.wav [--vocos] [--speed 1.0]
|
| 4 |
+
Needs espeak-ng, onnxruntime, numpy, soundfile, num2words; --vocos also torch and vocos.
|
| 5 |
+
"""
|
| 6 |
+
import argparse
|
| 7 |
+
import json
|
| 8 |
+
import re
|
| 9 |
+
from pathlib import Path
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import onnxruntime as ort
|
| 13 |
+
import soundfile as sf
|
| 14 |
+
|
| 15 |
+
from tinyvoca.text import phonemize
|
| 16 |
+
|
| 17 |
+
HERE = Path(__file__).resolve().parent
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def main():
|
| 21 |
+
ap = argparse.ArgumentParser()
|
| 22 |
+
ap.add_argument("text")
|
| 23 |
+
ap.add_argument("--voice", default="F2", choices=["F2", "M2"])
|
| 24 |
+
ap.add_argument("--out", default="out.wav")
|
| 25 |
+
ap.add_argument("--speed", type=float, default=1.0)
|
| 26 |
+
ap.add_argument("--vocos", action="store_true", help="second stage: fine-tuned Vocos (cleaner sound)")
|
| 27 |
+
ap.add_argument("--threads", type=int, default=4)
|
| 28 |
+
a = ap.parse_args()
|
| 29 |
+
|
| 30 |
+
meta = json.loads((HERE / "apptor-tiny-tts-de.json").read_text(encoding="utf-8"))
|
| 31 |
+
opts = ort.SessionOptions()
|
| 32 |
+
opts.intra_op_num_threads = a.threads
|
| 33 |
+
session = ort.InferenceSession(str(HERE / "apptor-tiny-tts-de.onnx"), opts, providers=["CPUExecutionProvider"])
|
| 34 |
+
voice = meta["voices"].index(a.voice)
|
| 35 |
+
|
| 36 |
+
pieces = []
|
| 37 |
+
for sentence in re.split(r"(?<=[.!?…])\s+", a.text.strip()): # one sentence per call (<= 512 tokens)
|
| 38 |
+
if not sentence:
|
| 39 |
+
continue
|
| 40 |
+
p = phonemize(sentence)
|
| 41 |
+
audio, _ = session.run(None, {
|
| 42 |
+
"tokens": np.array([p.ids], np.int64), "stress": np.array([p.stress], np.int64),
|
| 43 |
+
"voice": np.array([voice], np.int64), "speed": np.array([a.speed], np.float32),
|
| 44 |
+
"pitch_shift": np.array([0.0], np.float32), "pitch_range": np.array([1.0], np.float32)})
|
| 45 |
+
pieces += [audio[0].astype(np.float32), np.zeros(int(0.12 * meta["sample_rate"]), np.float32)]
|
| 46 |
+
audio = np.concatenate(pieces)
|
| 47 |
+
|
| 48 |
+
if a.vocos:
|
| 49 |
+
import torch
|
| 50 |
+
from vocos import Vocos
|
| 51 |
+
torch.set_num_threads(a.threads)
|
| 52 |
+
voc = Vocos.from_pretrained("charactr/vocos-mel-24khz").eval()
|
| 53 |
+
voc.load_state_dict(torch.load(HERE / "apptor-tiny-tts-vocos-de.pt", map_location="cpu", weights_only=True))
|
| 54 |
+
with torch.inference_mode():
|
| 55 |
+
x = torch.from_numpy(audio)[None]
|
| 56 |
+
audio = voc.decode(voc.feature_extractor(x))[0, :len(audio)].numpy()
|
| 57 |
+
|
| 58 |
+
peak = float(np.abs(audio).max())
|
| 59 |
+
if peak > 0.95:
|
| 60 |
+
audio = audio * (0.95 / peak)
|
| 61 |
+
sf.write(a.out, audio, meta["sample_rate"])
|
| 62 |
+
print(f"wrote {a.out} ({len(audio) / meta['sample_rate']:.1f} s)")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
main()
|
tinyvoca/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Text front end of tiny-voca (https://github.com/n4ze3m/tiny-voca, Apache 2.0), German fork:
|
| 2 |
+
text.py (eSpeak NG "de", German phone inventory) and normalize_de.py, copied unchanged from the German training
|
| 3 |
+
code so the server makes exactly the tokens the model was trained on. Needs the espeak-ng program and num2words."""
|
tinyvoca/normalize_de.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""German text normalization before eSpeak: spell out what a speaker reads aloud
|
| 2 |
+
differently from eSpeak's defaults (abbreviations, clock times, money, ordinals
|
| 3 |
+
before months, percent, years). Used for annotation, training and inference alike.
|
| 4 |
+
"""
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
from num2words import num2words as _n2w
|
| 9 |
+
except ImportError: # eSpeak still reads plain numbers; only the special forms need num2words
|
| 10 |
+
_n2w = None
|
| 11 |
+
|
| 12 |
+
MONTHS = ("Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember")
|
| 13 |
+
ABBREVIATIONS = {
|
| 14 |
+
"z.B.": "zum Beispiel", "z. B.": "zum Beispiel", "d.h.": "das heißt", "d. h.": "das heißt", "u.a.": "unter anderem",
|
| 15 |
+
"usw.": "und so weiter", "bzw.": "beziehungsweise", "ca.": "circa", "vgl.": "vergleiche", "evtl.": "eventuell",
|
| 16 |
+
"ggf.": "gegebenenfalls", "inkl.": "inklusive", "Nr.": "Nummer", "Dr.": "Doktor", "Prof.": "Professor",
|
| 17 |
+
"Str.": "Straße", "Hr.": "Herr", "Fr.": "Frau", "St.": "Sankt", "etc.": "et cetera", "Mio.": "Millionen",
|
| 18 |
+
"Mrd.": "Milliarden", "km/h": "Kilometer pro Stunde", "€": " Euro", "%": " Prozent",
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def _num(value, to="cardinal"):
|
| 23 |
+
if _n2w is None:
|
| 24 |
+
return str(value)
|
| 25 |
+
return _n2w(int(value), lang="de", to=to)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _ordinal(match):
|
| 29 |
+
word = _num(match[2], "ordinal") # "dritte"
|
| 30 |
+
prefix = match[1] or ""
|
| 31 |
+
# After a preposition/article in the dative ("am", "vom", "zum", "bis zum") the ending is -n.
|
| 32 |
+
if prefix.strip().lower() in ("am", "vom", "zum", "im", "ab dem", "bis zum", "seit dem", "dem", "den"):
|
| 33 |
+
word += "n"
|
| 34 |
+
return f"{prefix}{word} {match[3]}"
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _time(match):
|
| 38 |
+
hours, minutes = int(match[1]), int(match[2])
|
| 39 |
+
text = f"{_num(hours)} Uhr"
|
| 40 |
+
if hours == 1:
|
| 41 |
+
text = "ein Uhr"
|
| 42 |
+
return text + (f" {_num(minutes)}" if minutes else "")
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def _money(match):
|
| 46 |
+
euros, cents = int(match[1]), int(match[2])
|
| 47 |
+
text = f"{_num(euros)} Euro"
|
| 48 |
+
return text + (f" {_num(cents)}" if cents else "")
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def normalize(text):
|
| 52 |
+
for short, long in ABBREVIATIONS.items():
|
| 53 |
+
text = text.replace(short, long)
|
| 54 |
+
text = re.sub(r"\b(\d{1,2}):(\d{2})\s*Uhr\b", _time, text)
|
| 55 |
+
text = re.sub(r"\b(\d{1,2}):(\d{2})\b", _time, text)
|
| 56 |
+
text = re.sub(r"\b(\d+),(\d{2})\s*Euro\b", _money, text)
|
| 57 |
+
text = re.sub(r"\b((?:[Aa]m|[Vv]om|[Zz]um|[Bb]is zum|[Ss]eit dem|[Aa]b dem) )?(\d{1,2})\. (" + "|".join(MONTHS) + r")\b", _ordinal, text)
|
| 58 |
+
# Years after "im Jahr", or bare four-digit numbers 1100-1999 in running text, are read as years.
|
| 59 |
+
text = re.sub(r"\b(1[1-9]\d\d)\b", lambda m: _num(m[1], "year"), text)
|
| 60 |
+
text = re.sub(r"(\d+),(\d+)", lambda m: f"{_num(m[1])} Komma {' '.join(_num(d) for d in m[2])}", text)
|
| 61 |
+
text = re.sub(r"(?<![\w-])-(\d+)", lambda m: "minus " + m[1], text)
|
| 62 |
+
if _n2w is not None:
|
| 63 |
+
text = re.sub(r"\d+", lambda m: _num(m[0]), text)
|
| 64 |
+
text = re.sub(r"\bein(hundert|tausend)", r"\1", text)
|
| 65 |
+
return re.sub(r"\s+", " ", text).strip()
|
tinyvoca/text.py
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""German front end (fork of the English tiny-voca front end): text -> phoneme tokens with a stress feature.
|
| 2 |
+
|
| 3 |
+
The same code runs during annotation, training-set preparation and inference,
|
| 4 |
+
so tokens can never drift between them. Two programs can produce the phonemes:
|
| 5 |
+
|
| 6 |
+
* ``espeak`` (front-end version 3, the default): eSpeak NG, as described below.
|
| 7 |
+
The current checkpoints and annotations were made with it.
|
| 8 |
+
* ``own`` (front-end version 4): the separate ``dizione`` package, a dictionary
|
| 9 |
+
built from CMUdict, a small model for unknown words and rules. No eSpeak, so
|
| 10 |
+
the whole system can be shipped under a permissive licence. Same 76 symbols.
|
| 11 |
+
|
| 12 |
+
Choose with ``set_frontend("own")`` or ``TINY_VOCA_FRONTEND=own``. Everything
|
| 13 |
+
below about tokens holds for both; the rest of this note is the eSpeak path.
|
| 14 |
+
|
| 15 |
+
* eSpeak NG (``en-us``) is the only pronunciation source. It is an external
|
| 16 |
+
rule-based program: no neural G2P and no downloaded weights.
|
| 17 |
+
* A token is one eSpeak phoneme exactly as eSpeak segments it (``--sep``), for
|
| 18 |
+
example ``t``, ``aɪ``, ``ɑːɹ`` or ``n̩``. This is also the label inventory of
|
| 19 |
+
the phoneme recogniser used for offline forced alignment, so one aligned
|
| 20 |
+
label corresponds to exactly one token.
|
| 21 |
+
* Lexical stress is a parallel feature (0 none, 1 primary, 2 secondary), not a
|
| 22 |
+
token, so it never has to be given audio frames.
|
| 23 |
+
* Punctuation, word boundaries and the utterance edges are explicit tokens that
|
| 24 |
+
may last zero frames. They carry pauses when the recording has them.
|
| 25 |
+
|
| 26 |
+
Versions 1 and 2 encoded IPA as UTF-8 bytes and piped text to eSpeak without a
|
| 27 |
+
final newline; eSpeak NG 1.51 then drops the last character of the input, so
|
| 28 |
+
the final phonemes of every clause were wrong. Version 3 terminates the input
|
| 29 |
+
and has a regression test for it.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
from dataclasses import dataclass
|
| 33 |
+
import hashlib
|
| 34 |
+
import json
|
| 35 |
+
import os
|
| 36 |
+
import re
|
| 37 |
+
import shutil
|
| 38 |
+
import subprocess
|
| 39 |
+
import unicodedata
|
| 40 |
+
|
| 41 |
+
FRONTEND_VERSION = 103 # German fork: eSpeak NG "de", German phone inventory
|
| 42 |
+
ESPEAK_VOICE = "de"
|
| 43 |
+
SEPARATOR = "_"
|
| 44 |
+
FRONTENDS = ("espeak", "own")
|
| 45 |
+
FRONTEND_VARIABLE = "TINY_VOCA_FRONTEND"
|
| 46 |
+
_frontend = os.environ.get(FRONTEND_VARIABLE, "espeak")
|
| 47 |
+
if _frontend not in FRONTENDS:
|
| 48 |
+
raise ValueError(f"{FRONTEND_VARIABLE} must be one of {FRONTENDS}, not {_frontend!r}")
|
| 49 |
+
|
| 50 |
+
PAD, BOS, EOS, SP, UNK = "<pad>", "<bos>", "<eos>", "<sp>", "<unk>"
|
| 51 |
+
SPECIALS = (PAD, BOS, EOS, SP, UNK)
|
| 52 |
+
PUNCTUATION = (",", ".", "?", "!", ";", ":", '"', "'", "(", ")", "—", "…")
|
| 53 |
+
# German inventory: every phone is a label of the forced-alignment recogniser, except
|
| 54 |
+
# "ʊɐ", which eSpeak NG prints (and the recogniser was trained on) as "??".
|
| 55 |
+
CONSONANTS = ("p", "b", "t", "d", "k", "ɡ", "f", "v", "s", "z", "ʃ", "ʒ", "ç", "x", "h",
|
| 56 |
+
"m", "n", "ŋ", "l", "ɾ", "r", "j", "ts", "pf", "tʃ", "dʒ")
|
| 57 |
+
VOWELS = ("ə", "ɐ", "ɜ", "ɪ", "i", "iː", "ɛ", "ɛː", "eː", "a", "ɑ", "ɑː", "ɔ", "oː", "ʊ", "uː",
|
| 58 |
+
"y", "yː", "œ", "øː", "aɪ", "aʊ", "ɔø", "ʊɐ")
|
| 59 |
+
PHONES = CONSONANTS + VOWELS
|
| 60 |
+
SYMBOLS = SPECIALS + PUNCTUATION + PHONES
|
| 61 |
+
SYMBOL_TO_ID = {symbol: index for index, symbol in enumerate(SYMBOLS)}
|
| 62 |
+
PHONE_SET = frozenset(PHONES)
|
| 63 |
+
PHONE_IDS = frozenset(SYMBOL_TO_ID[p] for p in PHONES) | {SYMBOL_TO_ID[UNK]}
|
| 64 |
+
VOICELESS = frozenset(("p", "t", "k", "f", "s", "ʃ", "ç", "x", "h", "ts", "pf", "tʃ"))
|
| 65 |
+
VOWEL_SET = frozenset(VOWELS)
|
| 66 |
+
STRESS_MARKS = {"ˈ": 1, "ˌ": 2}
|
| 67 |
+
|
| 68 |
+
# eSpeak phonemes outside the inventory (English/French loan words, rare allophones).
|
| 69 |
+
FALLBACK = {
|
| 70 |
+
"??": ("ʊɐ",), "ɹ": ("ɾ",), "ʁ": ("ɾ",), "w": ("v",), "ð": ("d",), "θ": ("s",), "ʔ": (),
|
| 71 |
+
"e": ("eː",), "o": ("oː",), "u": ("uː",), "ø": ("øː",), "ɒ": ("ɔ",), "ʌ": ("a",), "æ": ("ɛ",),
|
| 72 |
+
"ɜː": ("øː",), "ɔː": ("oː",), "ɪː": ("iː",), "aː": ("ɑː",), "eɪ": ("eː",), "ɛɪ": ("eː",),
|
| 73 |
+
"əʊ": ("oː",), "oʊ": ("oː",), "eə": ("ɛː",), "ɪə": ("iː", "ɐ"), "ʊə": ("uː", "ɐ"), "aɪə": ("aɪ", "ɐ"),
|
| 74 |
+
"ɔɪ": ("ɔø",), "ɔʏ": ("ɔø",), "əl": ("ə", "l"), "l̩": ("ə", "l"), "n̩": ("ə", "n"), "m̩": ("ə", "m"),
|
| 75 |
+
"ɑ̃": ("ɑ", "ŋ"), "ɔ̃": ("ɔ", "ŋ"), "ɛ̃": ("ɛ", "ŋ"), "œ̃": ("œ", "ŋ"), "ʏ": ("y",), "ɚ": ("ɐ",),
|
| 76 |
+
"ɲ": ("n", "j"), "ɬ": ("l",), "ɫ": ("l",), "dz": ("d", "z"), "β": ("v",), "ɣ": ("ɡ",), "ʎ": ("l", "j"),
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
_QUOTES = str.maketrans({"‘": "'", "’": "'", "‚": "'", "‛": "'", "`": "'", "´": "'",
|
| 80 |
+
"“": '"', "”": '"', "„": '"', "‟": '"', "«": '"', "»": '"',
|
| 81 |
+
"–": "—", "―": "—", "[": "(", "{": "(", "]": ")", "}": ")"})
|
| 82 |
+
_PUNCTUATION_CHARS = frozenset(PUNCTUATION)
|
| 83 |
+
ABBREVIATIONS = {"dr", "prof", "nr", "str", "bzw", "ca", "usw", "vgl", "evtl", "ggf", "inkl", "hr", "fr", "st", "z", "b", "d", "h", "u", "a", "s", "o"}
|
| 84 |
+
_LANGUAGE_TAG = re.compile(r"\((?:_?[a-z0-9]_?){2,3}(?:-(?:_?[a-z0-9]_?)+)*\)")
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@dataclass(frozen=True)
|
| 88 |
+
class Phonemized:
|
| 89 |
+
symbols: tuple
|
| 90 |
+
ids: tuple
|
| 91 |
+
stress: tuple
|
| 92 |
+
unknown: tuple # eSpeak phonemes that had no inventory mapping
|
| 93 |
+
|
| 94 |
+
def __len__(self):
|
| 95 |
+
return len(self.ids)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def frontend():
|
| 99 |
+
"""Name of the program that produces the phonemes: ``"espeak"`` or ``"own"``."""
|
| 100 |
+
return _frontend
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def set_frontend(name):
|
| 104 |
+
"""Select the phonemizer for this process. Tokens of the two front ends
|
| 105 |
+
must not be mixed: annotations and checkpoints record which one made them."""
|
| 106 |
+
global _frontend
|
| 107 |
+
if name not in FRONTENDS:
|
| 108 |
+
raise ValueError(f"front end must be one of {FRONTENDS}, not {name!r}")
|
| 109 |
+
_frontend = name
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def _dizione():
|
| 113 |
+
try:
|
| 114 |
+
import dizione
|
| 115 |
+
except ImportError as error:
|
| 116 |
+
raise RuntimeError('the "own" front end needs the dizione package: pip install dizione') from error
|
| 117 |
+
if tuple(dizione.symbols.SYMBOLS) != SYMBOLS:
|
| 118 |
+
raise RuntimeError("dizione's symbol inventory differs from this model's")
|
| 119 |
+
return dizione
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def frontend_identity():
|
| 123 |
+
if _frontend == "own":
|
| 124 |
+
phonemizer = _dizione()
|
| 125 |
+
return {"frontend_version": phonemizer.VERSION, "phonemizer": "dizione", "symbols": list(SYMBOLS)}
|
| 126 |
+
return {"frontend_version": FRONTEND_VERSION, "voice": ESPEAK_VOICE, "separator": SEPARATOR,
|
| 127 |
+
"symbols": list(SYMBOLS), "fallback": {key: list(value) for key, value in sorted(FALLBACK.items())}}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def frontend_runtime():
|
| 131 |
+
"""What an annotation run records about the exact program behind its
|
| 132 |
+
tokens: the eSpeak version, or the fingerprint of the phonemizer build."""
|
| 133 |
+
if _frontend == "own":
|
| 134 |
+
return {"phonemizer": _dizione().identity()["fingerprint"]}
|
| 135 |
+
return {"espeak_version": espeak_version()}
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def frontend_fingerprint():
|
| 139 |
+
return hashlib.sha256(json.dumps(frontend_identity(), sort_keys=True, ensure_ascii=False).encode()).hexdigest()
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def normalize_text(text):
|
| 143 |
+
text = unicodedata.normalize("NFKC", text).translate(_QUOTES)
|
| 144 |
+
text = re.sub(r"\.{3,}", "…", text)
|
| 145 |
+
text = re.sub(r"-{2,}|\s-\s", "—", text)
|
| 146 |
+
return re.sub(r"\s+", " ", text).strip()
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def chunks(text):
|
| 150 |
+
"""Yield ``(is_punctuation, piece)``. Decimals, clock times, digit groups,
|
| 151 |
+
contractions, title abbreviations and initialisms stay inside the text piece
|
| 152 |
+
for eSpeak; every other punctuation mark becomes its own token."""
|
| 153 |
+
start = 0
|
| 154 |
+
for i, char in enumerate(text):
|
| 155 |
+
if char not in _PUNCTUATION_CHARS:
|
| 156 |
+
continue
|
| 157 |
+
previous = text[i-1] if i else ""
|
| 158 |
+
following = text[i+1] if i+1 < len(text) else ""
|
| 159 |
+
if char == "'" and previous.isalnum() and following.isalnum():
|
| 160 |
+
continue
|
| 161 |
+
if char in ".,:" and previous.isdigit() and following.isdigit():
|
| 162 |
+
continue
|
| 163 |
+
if char == "." and previous.isdigit() and following == " " and i+2 < len(text) and text[i+2].isalnum():
|
| 164 |
+
continue
|
| 165 |
+
if char == ".":
|
| 166 |
+
word = re.search(r"([A-Za-z]+)$", text[:i])
|
| 167 |
+
if word and word[1].lower() in ABBREVIATIONS:
|
| 168 |
+
continue
|
| 169 |
+
if previous.isalpha() and following.isalpha():
|
| 170 |
+
continue
|
| 171 |
+
if text[start:i].strip():
|
| 172 |
+
yield False, text[start:i].strip()
|
| 173 |
+
yield True, char
|
| 174 |
+
start = i+1
|
| 175 |
+
if text[start:].strip():
|
| 176 |
+
yield False, text[start:].strip()
|
| 177 |
+
|
| 178 |
+
|
| 179 |
+
def espeak_executable():
|
| 180 |
+
executable = shutil.which("espeak-ng")
|
| 181 |
+
if executable is None:
|
| 182 |
+
raise RuntimeError("Install espeak-ng on the cloud machine before preprocessing/inference")
|
| 183 |
+
return executable
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def espeak_version():
|
| 187 |
+
return subprocess.run([espeak_executable(), "--version"], capture_output=True, text=True, check=True, timeout=10).stdout.strip()
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def espeak_words(piece):
|
| 191 |
+
"""Phonemize one text piece. Returns words as lists of ``(phone, stress)``.
|
| 192 |
+
|
| 193 |
+
The input must end with a newline: eSpeak NG 1.51 silently drops the last
|
| 194 |
+
character of unterminated standard input.
|
| 195 |
+
"""
|
| 196 |
+
result = subprocess.run(
|
| 197 |
+
[espeak_executable(), "-q", "--ipa", "--sep=" + SEPARATOR, "-v", ESPEAK_VOICE, "--stdin"],
|
| 198 |
+
input=piece.rstrip("\n") + "\n", text=True, capture_output=True, check=True, timeout=30)
|
| 199 |
+
return parse_espeak(result.stdout)
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def parse_espeak(output):
|
| 203 |
+
words = []
|
| 204 |
+
for word in _LANGUAGE_TAG.sub("", output).split():
|
| 205 |
+
units = []
|
| 206 |
+
for unit in word.split(SEPARATOR):
|
| 207 |
+
stress = 0
|
| 208 |
+
for mark, level in STRESS_MARKS.items():
|
| 209 |
+
if mark in unit:
|
| 210 |
+
stress = stress or level
|
| 211 |
+
unit = unit.replace(mark, "")
|
| 212 |
+
unit = "".join(c for c in unit if unicodedata.category(c) != "Cf" and c not in "()")
|
| 213 |
+
if unit:
|
| 214 |
+
units.append((unit, stress))
|
| 215 |
+
if units:
|
| 216 |
+
words.append(units)
|
| 217 |
+
return words
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def canonical(phone):
|
| 221 |
+
"""Map an eSpeak phoneme to inventory phones; ``None`` when unknown."""
|
| 222 |
+
if phone in PHONE_SET:
|
| 223 |
+
return (phone,)
|
| 224 |
+
if phone in FALLBACK:
|
| 225 |
+
return FALLBACK[phone]
|
| 226 |
+
stripped = "".join(c for c in unicodedata.normalize("NFD", phone) if not unicodedata.combining(c))
|
| 227 |
+
for candidate in (stripped, stripped.replace("ː", ""), stripped + "ː"):
|
| 228 |
+
if candidate != phone and candidate in PHONE_SET:
|
| 229 |
+
return (candidate,)
|
| 230 |
+
if candidate != phone and candidate in FALLBACK:
|
| 231 |
+
return FALLBACK[candidate]
|
| 232 |
+
return None
|
| 233 |
+
|
| 234 |
+
|
| 235 |
+
def tokenize(pieces):
|
| 236 |
+
"""Build the token sequence from ``(is_punctuation, value)`` pieces where a
|
| 237 |
+
text piece's value is the parsed eSpeak word list. Pure function (testable
|
| 238 |
+
without eSpeak)."""
|
| 239 |
+
symbols, stress, unknown = [BOS], [0], []
|
| 240 |
+
for punctuation, value in pieces:
|
| 241 |
+
if punctuation:
|
| 242 |
+
if symbols[-1] != value: # collapse repeats such as "!!"
|
| 243 |
+
symbols.append(value)
|
| 244 |
+
stress.append(0)
|
| 245 |
+
continue
|
| 246 |
+
for word in value:
|
| 247 |
+
if symbols[-1] in PHONE_SET or symbols[-1] == UNK:
|
| 248 |
+
symbols.append(SP)
|
| 249 |
+
stress.append(0)
|
| 250 |
+
for phone, level in word:
|
| 251 |
+
mapped = canonical(phone)
|
| 252 |
+
if mapped is None:
|
| 253 |
+
unknown.append(phone)
|
| 254 |
+
mapped = (UNK,)
|
| 255 |
+
# A fallback may expand to several phones: stress goes to its first vowel.
|
| 256 |
+
carrier = next((i for i, item in enumerate(mapped) if item in VOWEL_SET), 0)
|
| 257 |
+
for index, item in enumerate(mapped):
|
| 258 |
+
symbols.append(item)
|
| 259 |
+
stress.append(level if index == carrier else 0)
|
| 260 |
+
symbols.append(EOS)
|
| 261 |
+
stress.append(0)
|
| 262 |
+
if not any(s in PHONE_SET or s == UNK for s in symbols):
|
| 263 |
+
raise ValueError("the text has nothing to pronounce")
|
| 264 |
+
return Phonemized(tuple(symbols), tuple(SYMBOL_TO_ID[s] for s in symbols), tuple(stress), tuple(unknown))
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
def phonemize(text):
|
| 268 |
+
if not isinstance(text, str) or not text.strip():
|
| 269 |
+
raise ValueError("text must not be empty")
|
| 270 |
+
if _frontend == "own":
|
| 271 |
+
return tokenize(_dizione().pieces(text))
|
| 272 |
+
pieces = []
|
| 273 |
+
from .normalize_de import normalize as normalize_german
|
| 274 |
+
for punctuation, piece in chunks(normalize_text(normalize_german(normalize_text(text)))):
|
| 275 |
+
pieces.append((True, piece) if punctuation else (False, espeak_words(piece)))
|
| 276 |
+
return tokenize(pieces)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def is_phone(token_ids):
|
| 280 |
+
"""Boolean list/tensor helper: tokens that must be realised as audio."""
|
| 281 |
+
return [int(i) in PHONE_IDS for i in token_ids]
|