forked from k2kobayashi/sprocket
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitialize.py
More file actions
executable file
·182 lines (154 loc) · 6.31 KB
/
Copy pathinitialize.py
File metadata and controls
executable file
·182 lines (154 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
"""An example script to initialize audio lists and speaker configurations.
Usage: initialize.py [-h] [-1] [-2] [-3] SOURCE TARGET SAMPLING_RATE
Options:
-h, --help Show the help
-1, --step1 Execute step1 (Generation of initial list files)
-2, --step2 Execute step2 (Generation of configure files)
-3, --step3 Execute step3 (Estimation of F0 ranges)
SOURCE The name of speaker
whose voice you would like to convert from
TARGET The name of speaker whose voice you would like to convert to
SAMPLING_RATE The sampling rate of WAV files of voices
"""
import os
import shutil
import sys
from pathlib import Path
from docopt import docopt
sys.path.append(os.path.join(os.path.dirname(__file__), "src")) # isort:skip
from src import initialize_speaker # isort:skip # pylint: disable=C0413
def create_configure(dest, base, exist_ok=True):
"""Creates a configuration file based on a template file.
This does not overwrite the existing one.
Parameters
----------
dest : str or path-like
The path of the configuration file you are creating.
base : str or path-like
The path of the template configure file.
exist_ok : bool
If `False`, this function throws
`IOError` (Python 2.7) or `FileExistsError` (Python 3 or later)
when `dest` is already created.
Raises
------
FileExistsError
If `exist_ok` is `False` and `dest` is already exists.
"""
if os.path.exists(str(dest)): # Wrapping in str is for Python 3.5
message = "The configuration file {} already exists.".format(dest)
if exist_ok:
print(message)
else:
raise FileExistsError(message)
else:
print("Generate {}".format(dest), file=sys.stderr)
shutil.copy(str(base), str(dest))
def create_list(dest, wav_dir, exist_ok=True):
"""Create an audio list file based on a template.
This does not overwrite the existing one.
Parameters
----------
dest : str or path-like
The path of the list file you are creating.
wav_dir : str or path-like
The path of the directory of audio files.
The name of directory must be that of speaker.
exist_ok : bool
If `False`, this function throws
`IOError` (Python 2.7) or `FileExistsError` (Python 3 or later)
when `dest` is already created.
Raises
------
IOError (Python 2.7) or FileExistsError (Python 3 or later)
If `exist_ok` is `False` and `dest` is already exists.
You can catch both of them by:
>>> except IOError:
Notes
-----
List example of the speaker `SPEAKER`::
SPEAKER/001
SPEAKER/002
SPEAKER/003
when there is a audio directory of him/her named `SPEAKER` that contains:
* 001.wav
* 002.wav
* 003.wav
* other_file.txt (ignored)
Note that the delimiter `/` turns to `\\` in Windows.
"""
if os.path.exists(str(dest)):
message = "The list file {} already exists.".format(dest)
if exist_ok:
print(message)
else:
raise FileExistsError(message)
else:
print("Generate {}".format(dest))
speaker_label = os.path.basename(str(wav_dir))
lines = (os.path.join(str(speaker_label), os.path.splitext(str(wav_file_name))[0])
for wav_file_name in os.listdir(str(wav_dir))
if os.path.splitext(str(wav_file_name))[1] == ".wav")
with open(str(dest), "w") as file_handler:
for line in sorted(lines):
print(line, file=file_handler)
USES = ("train", "eval")
LIST_SUFFIXES = {
use: "_" + use + ".list" for use in USES}
EXAMPLE_ROOT_DIR = Path(__file__).parent
CONF_DIR = EXAMPLE_ROOT_DIR / "conf"
DATA_DIR = EXAMPLE_ROOT_DIR / "data"
LIST_DIR = EXAMPLE_ROOT_DIR / "list"
WAV_DIR = DATA_DIR / "wav"
if __name__ == "__main__":
args = docopt(__doc__) # pylint: disable=invalid-name
LABELS = {label: args[label.upper()] for label in ("source", "target")}
SOURCE_TARGET_PAIR = LABELS["source"] + "-" + LABELS["target"]
PAIR_DIR = DATA_DIR / "pair" / SOURCE_TARGET_PAIR
LIST_FILES = {
speaker_part: {
use: LIST_DIR / (speaker_label + LIST_SUFFIXES[use])
for use in USES}
for speaker_part, speaker_label in LABELS.items()}
SPEAKER_CONF_FILES = {
part: CONF_DIR / "speaker" / (label + ".yml")
for part, label in LABELS.items()}
PAIR_CONF_FILE = CONF_DIR / "pair" / (SOURCE_TARGET_PAIR + ".yml")
SAMPLING_RATE = args["SAMPLING_RATE"]
# The first False is dummy for alignment
# between indexes of `args_execute_steps` and arguments
# pylint: disable=invalid-name
execute_steps = [False] \
+ [args["--step{}".format(step_index)] for step_index in range(1, 4)]
# Execute all steps if no options on steps are given
# Keep index #0 False in case you create a hotbed for bugs.
if not any(execute_steps):
raise("Please specify steps with options")
if execute_steps[1]:
print("### 1. create initial list files ###")
# create list files for both the speakers
for use in USES:
for part, speaker in LABELS.items():
create_list(LIST_FILES[part][use], WAV_DIR / speaker)
print("# Please modify train and eval list files, if you want. #")
if execute_steps[2]:
print("### 2. create configure files ###")
# create speaker-dependent configure file
for part, speaker in LABELS.items():
create_configure(
SPEAKER_CONF_FILES[part],
CONF_DIR / "default" / "speaker_default_{}.yml".format(
SAMPLING_RATE))
# create pair-dependent configure file
create_configure(PAIR_CONF_FILE, os.path.join(
str(CONF_DIR), "default", "pair_default.yml"))
if execute_steps[3]:
print("### 3. create figures to define parameters ###")
# get F0 range in each speaker
for part, speaker in LABELS.items():
initialize_speaker.main(
speaker, str(LIST_FILES[part]["train"]),
str(WAV_DIR), str(CONF_DIR / "figure"))
print("# Please modify f0 range and power threshold"
" in speaker-dependent YAML files #")