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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# %%
import os
from pathlib import Path
from pprint import pprint

from lhotse.recipes import (
    download_voxceleb1,
    download_voxceleb2,
    hifitts,
    libritts,
    prepare_voxceleb,
)
import pandas as pd

# %%
root_dir = Path("../../datasets_cache")
# root_dir = Path("datasets_cache")

voxceleb1_path = root_dir / "voxceleb1"
voxceleb2_path = root_dir / "voxceleb2"

hifitts_path = root_dir / "hifitts"
libritts_path = root_dir / "librittsr"

num_jobs = os.cpu_count() - 3  # type: ignore

num_jobs, hifitts_path

# %%
# voxceleb1_root = download_voxceleb1(voxceleb1_path)
# voxceleb1_root

# %%
# voxceleb2_root = download_voxceleb2(voxceleb2_path)
# voxceleb2_root

# %%
hifitts_root = hifitts.download_hifitts(hifitts_path)
hifitts_root


# %%
result = hifitts.prepare_hifitts(hifitts_root, num_jobs=num_jobs)
result


# %%
result.keys()

# %%
from lhotse import CutSet, Fbank, FbankConfig, Mfcc, MfccConfig, RecordingSet

cuts_train = CutSet.from_manifests(**result["6670_other_test"])  # type: ignore
cuts_train

# %%
pprint(cuts_train[0])

# %%
from lhotse.cut import Cut

# Filter the CutSet to only include cuts that are no more than the duration limit
duration_limit_min = 2.0
duration_limit_max = 2.5
# Duration limit in seconds
cuts_train = cuts_train.filter(
    lambda cut: isinstance(cut, Cut)
    and cut.duration >= duration_limit_min
    and cut.duration <= duration_limit_max,
)
cuts_train

# %%
cuts_train[0].supervisions[0]

# %%
# filter_length=2048,
# hop_length=512,  # NOTE: 441 ?? https://github.com/jik876/hifi-gan/issues/116#issuecomment-1436999858
# win_length=2048,
# n_mel_channels=128,
# mel_fmin=20,
# mel_fmax=11025,

fbank = Fbank(
    FbankConfig(
        sampling_rate=44100,
        num_filters=128,
    ),
)

cuts_train_fbank = cuts_train.compute_and_store_features(
    extractor=fbank,
    storage_path=hifitts_root / "features",
    num_jobs=1,
)

cuts_train_fbank

# %%
# cuts_train_fbank.to_file(hifitts_root / "cuts_train.json.gz")

# %%
cuts_train_fbank[0].plot_features()

# %%
cuts_train_fbank_item = cuts_train_fbank[0]
cuts_train_fbank_item

# %%
from lhotse.cut import MonoCut

if isinstance(cuts_train_fbank_item, MonoCut):
    print(cuts_train_fbank_item.features)

# %%
cuts_train_fbank_item.plot_audio()

# %%
cuts_train_fbank_item.play_audio()

# %%
from lhotse import CutSet
from lhotse.dataset import (
    SimpleCutSampler,
    UnsupervisedDataset,
    UnsupervisedWaveformDataset,
)
from torch.utils.data import DataLoader, Dataset

dataset = UnsupervisedDataset()
sampler = SimpleCutSampler(cuts_train_fbank, max_duration=300)

dataloader = DataLoader(dataset, sampler=sampler, batch_size=None)

batch = next(iter(dataloader))
batch


# %%
batch["cuts"][0].recording.sources[0].load_audio().shape

# %%
batch["cuts"][0].features

# %%
batch["features"][0].shape

# %%
batch["features"][0]

# %%
# Prepare the LibriTTS dataset
libritts_root = libritts.download_librittsr(
    libritts_path,
    dataset_parts=["train-clean-100"],
)
libritts_root, libritts_path

# %%
prepared_libri = libritts.prepare_librittsr(
    libritts_root / "LibriTTS_R",
    # dataset_parts=["dev-clean"],
    dataset_parts=["train-clean-100"],
    num_jobs=num_jobs,
)

# %%
prepared_libri

# %%
prepared_libri_100 = (
    pd.DataFrame(prepared_libri["train-clean-100"]["supervisions"])
    .groupby("speaker")["duration"]
    .sum()
    .sort_values(ascending=False)
)
prepared_libri_100

# %%
for k in prepared_libri:
    prepared_libri_ = (
        pd.DataFrame(prepared_libri[k]["supervisions"])
        .groupby("speaker")["duration"]
        .sum()
        .sort_values(ascending=False)
    )
    print(prepared_libri_.loc[prepared_libri_ >= 1800])

# %%
from lhotse import CutSet, SupervisionSet

supervisions_libri = SupervisionSet()

supervisions_libri.to_file(libritts_root / "supervisions_libri.json.gz")

# dev-clean
# Series([], Name: duration, dtype: float64)
# dev-other
# Series([], Name: duration, dtype: float64)
# test-clean
# speaker
# 3570    1865.052667
# Name: duration, dtype: float64
# test-other
# Series([], Name: duration, dtype: float64)
# train-clean-100
# speaker
# 40      2096.569333
# 6209    1926.765000
# 7447    1915.213333
# 1088    1900.926000
# Name: duration, dtype: float64
# train-clean-360
# speaker
# 3003    2385.213333
# 2204    2242.730333
# 3307    2086.246500
# 8080    2051.131500
# 5935    1959.650833
# 3922    1938.523500
# 7982    1893.050833
# 3638    1843.324000
# 3032    1812.692000
# Name: duration, dtype: float64
# train-other-500
# speaker
# 215     2385.047833
# 6594    2341.286667
# 3433    2206.806500
# 3867    2118.326167
# 5733    2097.689833
# 7649    2016.925500
# 2834    2008.083000
# 8291    1977.892000
# 483     1964.766000
# 5181    1959.280000
# 8799    1909.690500
# 7839    1888.650500
# 1665    1877.726833
# 8430    1872.845500
# 47      1861.966167
# 2361    1839.646333
# 1132    1838.686333
# 5439    1837.487000
# 3319    1821.083833
# 5445    1808.444667
# 2208    1804.525833
# 8346    1804.405500
# Name: duration, dtype: float64

selected_speakers_man = [
    # train-clean-100
    "40",
    "1088",
    # train-clean-360
    "3307",
    "5935",
    "3032",
    # train-other-500
    "215",
    "6594",
    "3867",
    "5733",
    "8291",
    "5181",
    "8799",
    "2361",
    "1132",
    "5439",
    "3319",
    "8346",
]


# %%
num_speakers_lib_100_over_1900_sec = prepared_libri_100.loc[prepared_libri_100 >= 1900]
num_speakers_lib_100_over_1900_sec

# %%
prepared_libri_360 = libritts.prepare_librittsr(
    libritts_root / "LibriTTS_R",
    # dataset_parts=["dev-clean"],
    dataset_parts=["train-clean-360"],
    num_jobs=num_jobs,
)

# %%
speaker_durations_360 = (
    pd.DataFrame(prepared_libri_360["train-clean-360"]["supervisions"])
    .groupby("speaker")["duration"]
    .sum()
    .sort_values(ascending=False)
)
speaker_durations_360

# %%
# Get the speaker IDs from both dataframes
speaker_ids_100 = prepared_libri_100.index
speaker_ids_360 = speaker_durations_360.index

# Find the intersection of the speaker IDs
common_speaker_ids = speaker_ids_100.intersection(speaker_ids_360)

# No intersection!
common_speaker_ids

# %%
num_speakers_lib_360_over_1900_sec = speaker_durations_360.loc[
    speaker_durations_360 > 1900
].count()
num_speakers_lib_360_over_1900_sec

# %%
from lhotse import CutSet, Fbank, FbankConfig

cuts_train = CutSet.from_manifests(**prepared_libri["train-clean-100"])  # type: ignore
cuts_train

# %%
# You can save the prepared CutSet to a file!
cuts_train.to_file("./libri_selected.json.gz")

cuts_train.to_file(root_dir / "./libri_selected.json.gz")

# %%
from lhotse import CutSet, SupervisionSet

libri_selected = CutSet.from_file(root_dir / "libri.json.gz")
libri_selected

# %%

pprint(libri_selected[0])

print(libri_selected[0].recording.sources[0].source)

# %%
libri_selected[0].play_audio()

# %%
import torchaudio

torchaudio.load(
    "datasets_cache/librittsr/LibriTTS_R/dev-clean/5694/64025/5694_64025_000017_000002.wav",
)

# %%
supervisions_libri = SupervisionSet.from_file(
    root_dir / "supervisions_libri.json.gz",
)
recordings_libri = RecordingSet.from_file(
    root_dir / "recordings_libri.json.gz",
)
supervisions_libri, recordings_libri


# %%
supervisions_libri[0]

# %%
speakers_dur = (
    pd.DataFrame(supervisions_libri)
    .groupby("speaker")["duration"]
    .sum()
    .sort_values(ascending=False)
)

# %%
speakers_dur_1900 = speakers_dur.loc[speakers_dur >= 1900]
speakers_dur_1900

# %%
# selected_1900_ids = set(
#     map(int, speakers_dur_1900.index.to_list()),
# )

selected_1900_ids = set(
    speakers_dur_1900.index.to_list(),
)
selected_1900_ids

# %%
duration_limit_min = 0.5
duration_limit_max = 35.0

libri_selected.filter(
    lambda cut: isinstance(cut, Cut)
    and cut.supervisions[0].speaker in selected_1900_ids
    and cut.duration >= duration_limit_min
    and cut.duration <= duration_limit_max,
)

# %%
libri_selected[0]

# %%
cuts_train_frame = pd.DataFrame(cuts_train)
cuts_train_frame

# %%
cuts_train[0].supervisions[0].speaker

# %%
# duration_limit_min = 2.0
# duration_limit_max = 2.5

cuts_train = cuts_train.filter(
    lambda cut: isinstance(cut, Cut) and cut.supervisions[0].speaker == "5338",
    # and cut.duration >= duration_limit_min
    # and cut.duration <= duration_limit_max,
)

cuts_train

# %%
# cuts_train.map(lambda cut: cut.supervisions[0].speaker)
# %%
cuts_train[0]

# %%
len(cuts_train)

# %%
selected_speakers_libri_ids = [
    # train-clean-100
    40,
    1088,
    # train-clean-360
    3307,
    5935,
    3032,
    # train-other-500
    215,
    6594,
    3867,
    5733,
    8291,
    5181,
    8799,
    2361,
    1132,
    5439,
    3319,
    8346,
]

# The selected speakers from the HiFiTTS dataset
selected_speakers_hi_fi_ids = [
    92,
    6670,
    6671,
    6097,
    8051,
    11614,
    11697,
    9017,
    12787,
    9136,
]

selected_speakers_ids = {
    v: k
    for k, v in enumerate(
        selected_speakers_libri_ids + selected_speakers_hi_fi_ids,
    )
}

selected_speakers_ids[1088]

# %%

selected_speakers_libri_ids = [
    # train-clean-100
    40,
    1088,
    # train-clean-360
    3307,
    5935,
    3032,
    # train-other-500
    215,
    6594,
    3867,
    5733,
    8291,
    5181,
    8799,
    2361,
    1132,
    5439,
    3319,
    8346,
]

# The selected speakers from the HiFiTTS dataset
selected_speakers_hi_fi_ids = [
    "Cori Samuel",  # 92,
    "Phil Benson",  # 6097,
    "Mike Pelton",  # 6670,
    "Tony Oliva",  # 6671,
    "Maria Kasper",  # 8051,
    "John Van Stan",  # 9017,
    "Helen Taylor",  # 9136,
    "Sylviamb",  # 11614,
    "Celine Major",  # 11697,
    "LikeManyWaters",  # 12787,
]

# Map the speaker ids to string and list of selected speaker ids to set
selected_speakers_ids = {
    v: k
    for k, v in enumerate(
        selected_speakers_libri_ids + selected_speakers_hi_fi_ids,
    )
}

selected_speakers_ids, len(selected_speakers_ids)

# %%
import os
import sys

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.dirname(SCRIPT_DIR))

from pathlib import Path

from IPython import display
import torchaudio
from voicefixer import Vocoder

from .hifi_libri_dataset import HifiLibriDataset, HifiLibriItem

vocoder_vf = Vocoder(44100)
dataset = HifiLibriDataset(cache_dir="datasets_cache", cache=True)

item = dataset[0]
wav = vocoder_vf.forward(item.mel.permute((1, 0)).unsqueeze(0))
display.Audio(wav.squeeze(0).cpu().detach().numpy(), rate=44100)
# wav_path = Path(f"results/{item.id}.wav")
# torchaudio.save(str(wav_path), wav, 44100)

# %%