File size: 5,161 Bytes
64db264
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "347ace04",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# VCTK Corpus Path\n",
    "__CORPUSPATH__ = os.path.expanduser(\"~/data/VCTK-Corpus\") \n",
    "\n",
    "# output path\n",
    "__OUTPATH__ = \"./Data\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ce9eb2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from scipy.io import wavfile\n",
    "from pydub import AudioSegment\n",
    "\n",
    "from pydub import AudioSegment\n",
    "from pydub.silence import split_on_silence\n",
    "import os\n",
    "\n",
    "def split(sound):\n",
    "    dBFS = sound.dBFS\n",
    "    chunks = split_on_silence(sound,\n",
    "        min_silence_len = 100,\n",
    "        silence_thresh = dBFS-16,\n",
    "        keep_silence = 100\n",
    "    )\n",
    "    return chunks\n",
    "\n",
    "def combine(_src):\n",
    "    audio = AudioSegment.empty()\n",
    "    for i,filename in enumerate(os.listdir(_src)):\n",
    "        if filename.endswith('.wav'):\n",
    "            filename = os.path.join(_src, filename)\n",
    "            audio += AudioSegment.from_wav(filename)\n",
    "    return audio\n",
    "\n",
    "def save_chunks(chunks, directory):\n",
    "    if not os.path.exists(directory):\n",
    "        os.makedirs(directory)\n",
    "    counter = 0\n",
    "\n",
    "    target_length = 5 * 1000\n",
    "    output_chunks = [chunks[0]]\n",
    "    for chunk in chunks[1:]:\n",
    "        if len(output_chunks[-1]) < target_length:\n",
    "            output_chunks[-1] += chunk\n",
    "        else:\n",
    "            # if the last output chunk is longer than the target length,\n",
    "            # we can start a new one\n",
    "            output_chunks.append(chunk)\n",
    "\n",
    "    for chunk in output_chunks:\n",
    "        chunk = chunk.set_frame_rate(24000)\n",
    "        chunk = chunk.set_channels(1)\n",
    "        counter = counter + 1\n",
    "        chunk.export(os.path.join(directory, str(counter) + '.wav'), format=\"wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "769a7f62",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Source: http://speech.ee.ntu.edu.tw/~jjery2243542/resource/model/is18/en_speaker_used.txt\n",
    "# Source: https://github.com/jjery2243542/voice_conversion\n",
    "\n",
    "speakers = [225,228,229,230,231,233,236,239,240,244,226,227,232,243,254,256,258,259,270,273]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9302fb6a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# downsample to 24 kHz\n",
    "\n",
    "for p in speakers:\n",
    "    directory = __OUTPATH__ + '/p' + str(p)\n",
    "    if not os.path.exists(directory):\n",
    "        audio = combine(__CORPUSPATH__ + '/wav48/p' + str(p))\n",
    "        chunks = split(audio)\n",
    "        save_chunks(chunks, directory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b0ca022",
   "metadata": {},
   "outputs": [],
   "source": [
    "# get all speakers\n",
    "\n",
    "data_list = []\n",
    "for path, subdirs, files in os.walk(__OUTPATH__):\n",
    "    for name in files:\n",
    "        if name.endswith(\".wav\"):\n",
    "            speaker = int(path.split('/')[-1].replace('p', ''))\n",
    "            if speaker in speakers:\n",
    "                data_list.append({\"Path\": os.path.join(path, name), \"Speaker\": int(speakers.index(speaker)) + 1})\n",
    "                \n",
    "import pandas as pd\n",
    "\n",
    "data_list = pd.DataFrame(data_list)\n",
    "data_list = data_list.sample(frac=1)\n",
    "\n",
    "import random\n",
    "\n",
    "split_idx = round(len(data_list) * 0.1)\n",
    "\n",
    "test_data = data_list[:split_idx]\n",
    "train_data = data_list[split_idx:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88df2a45",
   "metadata": {},
   "outputs": [],
   "source": [
    "# write to file \n",
    "\n",
    "file_str = \"\"\n",
    "for index, k in train_data.iterrows():\n",
    "    file_str += k['Path'] + \"|\" +str(k['Speaker'] - 1)+ '\\n'\n",
    "text_file = open(__OUTPATH__ + \"/train_list.txt\", \"w\")\n",
    "text_file.write(file_str)\n",
    "text_file.close()\n",
    "\n",
    "file_str = \"\"\n",
    "for index, k in test_data.iterrows():\n",
    "    file_str += k['Path'] + \"|\" + str(k['Speaker'] - 1) + '\\n'\n",
    "text_file = open(__OUTPATH__ + \"/val_list.txt\", \"w\")\n",
    "text_file.write(file_str)\n",
    "text_file.close()"
   ]
  }
 ],
 "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.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}