mbrola 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mbrola/__about__.py ADDED
@@ -0,0 +1,4 @@
1
+ # SPDX-FileCopyrightText: 2024-present gongcastro <gongarciacastro@gmail.com>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ __version__ = "0.0.2"
mbrola/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ # SPDX-FileCopyrightText: 2024-present gongcastro <gongarciacastro@gmail.com>
2
+ #
3
+ # SPDX-License-Identifier: MIT
mbrola/mbrola.py ADDED
@@ -0,0 +1,173 @@
1
+ import os
2
+ import subprocess as sp
3
+ import platform
4
+ import shutil
5
+ import functools
6
+
7
+
8
+ class MBROLA:
9
+ """A class for generating MBROLA sounds.
10
+
11
+ An MBROLA class contains the necessary elements to synthesise an audio using MBROLA.
12
+
13
+ Args:
14
+ word (str): label for the mbrola sound.
15
+ phon (list[str]): list of phonemes.
16
+ durations (list[int] | int, optional): phoneme duration in milliseconds. Defaults to 100.
17
+ If an integer is provided, all phonemes in ``phon`` are assumed to be the same length. If a list is provided, each element in the list indicates the duration of each phoneme.
18
+ pitch (list[int] | int, optional): pitch in Hertz (Hz). Defaults to 200.
19
+ If an integer is provided, the pitch contour of each phoneme is assumed to be constant at the indicated value. If a list of integers or strings is provided, each element in the list indicates the value at which the pitch contour of each phoneme is kept constant. If a list of lists (of integers or strings), each value in each element describes the pitch contour for each phoneme.
20
+ onset_silence (int, optional): duration in milliseconds of the silence interval to be inserted at onset. Defaults to 1.
21
+ offset_silence (int, optional): duration in milliseconds of the silence interval to be inserted at offset. Defaults to 1.
22
+
23
+ Attributes:
24
+ word (str): label for the mbrola sound.
25
+ phon (list[str]): list of phonemes.
26
+ durations (list[int] | int, optional): phoneme duration in milliseconds. Defaults to 100.
27
+ If an integer is provided, all phonemes in ``phon`` are assumed to be the same length. If a list is provided, each element in the list indicates the duration of each phoneme.
28
+ pitch (list[int] | int, optional): pitch in Hertz (Hz). Defaults to 200.
29
+ If an integer is provided, the pitch contour of each phoneme is assumed to be constant at the indicated value. If a list of integers or strings is provided, each element in the list indicates the value at which the pitch contour of each phoneme is kept constant. If a list of lists (of integers or strings), each value in each element describes the pitch contour for each phoneme.
30
+ onset_silence (int, optional): duration in milliseconds of the silence interval to be inserted at onset. Defaults to 1.
31
+ offset_silence (int, optional): duration in milliseconds of the silence interval to be inserted at offset. Defaults to 1.
32
+
33
+ Raises:
34
+ ValueError: ``word`` must be a string
35
+ ValueError: ``phon`` must be a list of strings
36
+ ValueError: ``durations`` must be a list of integers or an integer
37
+ ValueError: ``phon`` and ``durations`` must have the same length
38
+ ValueError: ``pitch`` must be a list of integers or an integer
39
+ ValueError: ``phon`` and ``pitch`` must have the same length
40
+ ValueError: ``onset_silence`` must be an integer
41
+ ValueError: ``offset_silence`` must be an integer
42
+ """
43
+
44
+ def __init__(
45
+ self,
46
+ word: str,
47
+ phon: list[str],
48
+ durations: list[int] | int = 100,
49
+ pitch: list[int] | int = 200,
50
+ onset_silence: int = 1,
51
+ offset_silence: int = 1,
52
+ ):
53
+ self.word = word
54
+ self.phon = phon
55
+ self.durations = durations
56
+ self.pitch = pitch
57
+ self.onset_silence = onset_silence
58
+ self.offset_silence = offset_silence
59
+
60
+ nphon = len(self.phon)
61
+
62
+ if isinstance(self.durations, int):
63
+ self.durations = [self.durations] * nphon
64
+ self.durations = list(map(str, self.durations))
65
+ if isinstance(self.pitch, int):
66
+ self.pitch = [[self.pitch, self.pitch]] * nphon
67
+ if isinstance(self.pitch[0], int):
68
+ self.pitch = [list(map(str, [p, p])) for p in self.pitch]
69
+ self.pitch = [list(map(str, p)) for p in self.pitch]
70
+
71
+ validate_mbrola_args(self)
72
+
73
+ self.pho = make_pho(self)
74
+
75
+ def __str__(self):
76
+ return str("\n".join(self.pho))
77
+
78
+ def __repr__(self):
79
+ return str("\n".join(self.pho))
80
+
81
+ def export_pho(self, file: str):
82
+ try:
83
+ with open(f"{file}", "w+") as f:
84
+ f.write("\n".join(self.pho))
85
+ except FileNotFoundError:
86
+ print(f"{file} is not a valid path")
87
+
88
+ def make_sound(
89
+ self,
90
+ file: str,
91
+ voice: str = "it4",
92
+ f0_ratio: float = 1.0,
93
+ dur_ratio: float = 1.0,
94
+ remove_pho: bool = True,
95
+ ):
96
+ with open("tmp.pho", mode="w") as f:
97
+ f.write("\n".join(self.pho))
98
+
99
+ cmd = f"{mbrola_cmd()} -f {f0_ratio} -t {dur_ratio} /usr/share/mbrola/{voice}/{voice} tmp.pho {file}"
100
+
101
+ try:
102
+ sp.check_output(cmd)
103
+ except sp.CalledProcessError as e:
104
+ print(f"Error when making sound for {file}")
105
+ f.close()
106
+ if remove_pho:
107
+ os.remove("tmp.pho")
108
+ return None
109
+
110
+
111
+ def validate_mbrola_args(self) -> None:
112
+ nphon = len(self.phon)
113
+ if isinstance(self.durations, list) and len(self.durations) != nphon:
114
+ raise ValueError("`phon` and `durations` must have the same length")
115
+ if isinstance(self.pitch, list):
116
+ if len(self.pitch) != nphon:
117
+ raise ValueError("`phon` and `pitch` must have the same length")
118
+ if self.onset_silence <= 0:
119
+ raise ValueError("`onset_silence` must be a positive integer")
120
+ if self.offset_silence <= 0:
121
+ raise ValueError("`offset_silence` must be a positive integer")
122
+ return None
123
+
124
+
125
+ def make_pho(self) -> list[str]:
126
+ pho = [f"; {self.word}", f"_ {self.onset_silence}"]
127
+ for ph, d, p in zip(self.phon, self.durations, self.pitch):
128
+ p_seq = " ".join(p)
129
+ pho.append(" ".join([ph, d, p_seq]))
130
+ pho.append(f"_ {self.offset_silence}")
131
+ return pho
132
+
133
+
134
+ @functools.cache
135
+ def mbrola_cmd():
136
+ """
137
+ Get MBROLA command for system command line.
138
+ """
139
+ try:
140
+ if is_wsl() or os.name == "posix":
141
+ return "mbrola"
142
+ if os.name == "nt":
143
+ if wsl_available():
144
+ return "wsl mbrola"
145
+ else:
146
+ raise Exception(
147
+ f"MBROLA only available on {platform.system()} using the Windows Subsystem for Linux (WSL). Please, follow the instructions in the WSL site: https://learn.microsoft.com/en-us/windows/wsl/install."
148
+ )
149
+ except:
150
+ raise Exception(f"MBROLA not available for {platform.system()}")
151
+
152
+
153
+ @functools.cache
154
+ def is_wsl(version: str = platform.uname().release) -> int:
155
+ """
156
+ Returns ```True`` if Python is running in WSL, otherwise ```False``
157
+ """
158
+ return version.endswith("microsoft-standard-WSL2")
159
+
160
+
161
+ @functools.cache
162
+ def wsl_available() -> int:
163
+ """
164
+ Returns ```True`` if Windows Subsystem for Linux (WLS) is available from Windows, otherwise ```False``
165
+ """
166
+ if os.name != "nt" or not shutil.which("wsl"):
167
+ return False
168
+ try:
169
+ return is_wsl(
170
+ sp.check_output(["wsl", "uname", "-r"], text=True, timeout=15).strip()
171
+ )
172
+ except sp.SubprocessError:
173
+ return False
@@ -0,0 +1,45 @@
1
+ Metadata-Version: 2.3
2
+ Name: mbrola
3
+ Version: 0.0.2
4
+ Summary: A Python front-end for the MBROLA speech synthesizer
5
+ Project-URL: Documentation, https://github.com/gongcastro/pymbrola#readme
6
+ Project-URL: Issues, https://github.com/gongcastro/pymbrola/issues
7
+ Project-URL: Source, https://github.com/gongcastro/pymbrola
8
+ Author-email: gongcastro <gongarciacastro@gmail.com>
9
+ License: MIT
10
+ Classifier: Development Status :: 4 - Beta
11
+ Classifier: Programming Language :: Python
12
+ Classifier: Programming Language :: Python :: 3.8
13
+ Classifier: Programming Language :: Python :: 3.9
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: Implementation :: CPython
18
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
19
+ Requires-Python: >=3.8
20
+ Description-Content-Type: text/markdown
21
+
22
+ # pymbrola
23
+
24
+ [![PyPI - Version](https://img.shields.io/pypi/v/pymbrola.svg)](https://pypi.org/project/pymbrola)
25
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pymbrola.svg)](https://pypi.org/project/pymbrola)
26
+
27
+ -----
28
+
29
+
30
+ ## Table of Contents
31
+
32
+ - [Installation](#installation)
33
+ - [License](#license)
34
+
35
+ ## Installation
36
+
37
+ MBROLA is currently available only on Linux-based systems like Ubuntu, or on Windows via the [Windows Susbsystem for Linux (WSL)](https://learn.microsoft.com/en-us/windows/wsl/install). Install MBROLA in your machine following the instructions in the [MBROLA repository](https://github.com/numediart/MBROLA). If you are using WSL, install MBROLA in WSL. After this, you should be ready to install **pymbrola** using pip.
38
+
39
+ ```console
40
+ pip install pymbrola
41
+ ```
42
+
43
+ ## License
44
+
45
+ `pymbrola` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,7 @@
1
+ mbrola/__about__.py,sha256=_XpvlvzO5FLDZ3mwkwfahtLEH_WN_Zowg5zZgEOj4MY,133
2
+ mbrola/__init__.py,sha256=L32MnyqXJX8wZRZI6Vmi4oQUShS2uJvvzo1GSd5M00k,111
3
+ mbrola/mbrola.py,sha256=viHZeOyF7C_LnWWDUK9fhZudq03Ub1ubORBOf0CvnD0,7094
4
+ mbrola-0.0.2.dist-info/METADATA,sha256=d51QQbzOvbZDPm9v2w-plsI3dSCEDojxpzzZbIryGic,1823
5
+ mbrola-0.0.2.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
6
+ mbrola-0.0.2.dist-info/licenses/LICENSE.txt,sha256=WYUTGonvq9i_KrEDq4rP7Dy5byx11u5BZAawspudlMo,1103
7
+ mbrola-0.0.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.26.3
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present gongcastro <gongarciacastro@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.