File size: 4,222 Bytes
d5ee97c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import random\n",
    "import shutil\n",
    "import sys\n",
    "\n",
    "libri_path = \"....../LibriTTS\" # absolute path to TensorFlowTTS.\n",
    "dataset_path = \"....../libritts\" # Change to your paths. This is a output of re-format dataset.\n",
    "subset = \"train-clean-100\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(os.path.join(libri_path, \"SPEAKERS.txt\")) as f:\n",
    "    data = f.readlines()\n",
    "    \n",
    "dataset_info = {}\n",
    "max_speakers = 20  # Max number of speakers to train on\n",
    "min_len = 20  # Min len of speaker narration time\n",
    "max_file_len = 11  # max audio file lenght\n",
    "min_file_len = 2  # min audio file lenght"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "possible_dataset = [i.split(\"|\") for i in data[12:] if i.split(\"|\")[2].strip() == subset and float(i.split(\"|\")[3].strip()) >= min_len]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "ids = [i[0].strip() for i in possible_dataset]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import soundfile as sf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "possible_map = {}\n",
    "subset_path = os.path.join(libri_path, subset)\n",
    "for i in os.listdir(subset_path):\n",
    "    if i in ids:\n",
    "        id_path = os.path.join(subset_path, i)\n",
    "        id_dur = 0\n",
    "        id_included = []\n",
    "        \n",
    "        for k in os.listdir(id_path):\n",
    "            for j in os.listdir(os.path.join(id_path, k)):\n",
    "                if \".wav\" in j:\n",
    "                    f_path = os.path.join(id_path, k, j)\n",
    "                    sf_file =  sf.SoundFile(f_path)\n",
    "                    dur = len(sf_file) / sf_file.samplerate\n",
    "                    if max_file_len < dur or dur < min_file_len:\n",
    "                        continue\n",
    "                    else:\n",
    "                        id_included.append(f_path)\n",
    "                        id_dur += dur\n",
    "        possible_map[i] = {\"dur\": id_dur, \"included\": id_included}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "poss_speakers = {k: v[\"included\"] for k, v in possible_map.items() if v[\"dur\"]/60 >= min_len}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "to_move = list(poss_speakers.keys())\n",
    "random.shuffle(to_move)\n",
    "to_move = to_move[:max_speakers]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "for sp_id, v in poss_speakers.items():\n",
    "    if sp_id in to_move:\n",
    "        for j in v:\n",
    "            f_name = j.split(os.path.sep)[-1]\n",
    "            text_f_name = f_name.split(\".wav\")[0] + \".txt\"\n",
    "            os.makedirs(os.path.join(dataset_path, sp_id), exist_ok=True)\n",
    "            shutil.copy(j, os.path.join(dataset_path, sp_id, f_name))\n",
    "            shutil.copy(j.replace(\".wav\", \".normalized.txt\"), os.path.join(dataset_path, sp_id, text_f_name))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}