File size: 9,840 Bytes
45ee559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This file was created by jhlfrfufyfn for choose speaker from the Belarusian Mozilla Voice corpus\n",
    "#\n",
    "#\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import os\n",
    "import librosa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# unpackage tar gz file cv-corpus-12.0-2022-12-07-be.tar.gz\n",
    "# import tarfile\n",
    "# tar = tarfile.open(\"cv-corpus-12.0-2022-12-07-be.tar.gz\", \"r:gz\")\n",
    "# tar.extractall()\n",
    "# tar.close()\n",
    "\n",
    "corpuspath = '/a/cv-corpus'\n",
    "outputpath = '/storage/filtered_dataset'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# open validated.tsv\n",
    "df = pd.read_csv(corpuspath+'/be/validated.tsv', sep='\\t' ,low_memory=False)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# drop from df columns age, accents\n",
    "df = df.drop(['age', 'accents', 'gender', 'variant', 'locale', 'segment'], axis=1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# count number of recordes with down_votes > 0\n",
    "df[df['down_votes'] > 0].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# count number of recordes with up_votes == 0\n",
    "df[df['up_votes'] == 0].count()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# drop all rows with down_votes > 0 and up_votes == 0\n",
    "df = df[df['down_votes'] == 0]\n",
    "df = df[df['up_votes'] > 0]\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# drop column down_votes and up_votes\n",
    "df = df.drop(['down_votes', 'up_votes'], axis=1)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# sort by count\n",
    "df_sorted = df.groupby('client_id').count().sort_values(by='path', ascending=False)\n",
    "df_sorted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get top 10 speakers\n",
    "top_10_speakers = df_sorted.head(10)\n",
    "top_10_speakers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get for the first speaker ten random paths to audio files\n",
    "def get_speaker_audio_list(speaker_id, n=10):\n",
    "    return df[df['client_id'] == speaker_id].sample(n)['path'].values.tolist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# CHOOSE : which speaker will we use\n",
    "speaker_index = 0\n",
    "speaker_audio_list = get_speaker_audio_list(top_10_speakers.index[speaker_index])\n",
    "print(speaker_audio_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# open audio files from speaker_audio_list and play them\n",
    "# audio files lie in cv-corpus-12.0-2022-12-07/be/clips\n",
    "import IPython.display as ipd\n",
    "for audio in speaker_audio_list:\n",
    "    audio = corpuspath+'/be/clips/' + audio\n",
    "    audio_data = ipd.Audio(audio)\n",
    "    display(audio_data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0 is pretty good\n",
    "# 1 is bad\n",
    "# 2 is partly 0, other are different\n",
    "# 3 is bad\n",
    "# 4 is pretty fast and clear, but not good\n",
    "# 5 is echoing, sometimes mic cracks\n",
    "# 6 is really slow and clear, but accent?\n",
    "# 7 has a lot of intonation, but is pretty clear\n",
    "# 8 is clear and slow, sometimes little mic crack\n",
    "# 9 has background noise, whispering\n",
    "\n",
    "# options: 0, 6, 8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate speech rate in words per minute for each speaker\n",
    "def get_speech_rate(speaker_id):\n",
    "    df_speaker = df[df['client_id'] == speaker_id]\n",
    "    # get 1000 random samples to calculate speech rate\n",
    "    df_speaker = df_speaker.sample(1000)\n",
    "    # get duration of each audio file\n",
    "    df_speaker['duration'] = df_speaker['path'].apply(lambda x: librosa.get_duration(path=corpuspath+'/be/clips/' + x))\n",
    "    # get number of words in each audio file\n",
    "    df_speaker['words'] = df_speaker['sentence'].apply(lambda x: len(x.split()))\n",
    "    # calculate speech rate\n",
    "    df_speaker['speech_rate'] = df_speaker['words'] / df_speaker['duration'] * 60\n",
    "    # return mean speech rate\n",
    "    return df_speaker['speech_rate'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# calculate speech rate for each speaker\n",
    "print(f'Speech rate for speaker {speaker_index}: ', get_speech_rate(top_10_speakers.index[speaker_index]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_average_duration(df_speaker):\n",
    "    # get 1000 random samples to calculate speech rate\n",
    "    df_speaker = df_speaker.sample(1000)\n",
    "    # get duration of each audio file\n",
    "    df_speaker['duration'] = df_speaker['path'].apply(lambda x: librosa.get_duration(path=corpuspath+'/be/clips/' + x))\n",
    "    return df_speaker['duration'].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_speaker = df[df['client_id'] == top_10_speakers.index[speaker_index]]\n",
    "\n",
    "avg_duration = get_average_duration(df_speaker)\n",
    "avg_total_duration = avg_duration * len(df_speaker.index)\n",
    "print(f'Average duration for speaker {speaker_index}: ', avg_duration, \", average total duration(hours): \",(avg_total_duration/60.0/60.0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get df with speaker_index speaker \n",
    "df_speaker = df[df['client_id'] == top_10_speakers.index[speaker_index]]\n",
    "df_speaker = df_speaker.drop(['client_id'], axis=1)\n",
    "\n",
    "# get only x latest hours\n",
    "limit_hours = 30\n",
    "limit_files = round(limit_hours*60*60 / avg_duration)\n",
    "df_speaker = df_speaker.tail(limit_files)\n",
    "\n",
    "df_speaker"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # move all files of that speaker to another folder\n",
    "# # use multiprocessing to speed up\n",
    "# # add progress bar\n",
    "# from tqdm import tqdm\n",
    "# import multiprocessing\n",
    "# from multiprocessing import Pool\n",
    "# import shutil\n",
    "\n",
    "# def move_file(file):\n",
    "#     shutil.move(corpuspath+'/be/clips/' + file, corpuspath+'/be/speaker_0/' + file)\n",
    "\n",
    "# # get list of files to move\n",
    "# files = df_speaker['path'].values.tolist()\n",
    "\n",
    "# # move files\n",
    "# with Pool(multiprocessing.cpu_count()) as p:\n",
    "#     r = list(tqdm(p.imap(move_file, files), total=len(files)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# cleanup output and save text lines to csv\n",
    "if os.path.isdir(outputpath):\n",
    "    for file in os.scandir(outputpath):\n",
    "        os.remove(file.path)\n",
    "else:\n",
    "    os.mkdir(outputpath)\n",
    "\n",
    "df_speaker['path2'] = df_speaker['path'].str.replace('\\.mp3$','.wav', regex=True)\n",
    "df_speaker[['path2','sentence']].to_csv(outputpath+'/df_speaker.csv', sep='|', header=False, index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# make rate=22050 of all mp3 files in speaker_0 folder with multiprocessing and tqdm\n",
    "import multiprocessing\n",
    "from multiprocessing import Pool\n",
    "from tqdm import tqdm\n",
    "from pydub import AudioSegment\n",
    "\n",
    "def convert_mp3_to_wav(file):\n",
    "    sound = AudioSegment.from_mp3(corpuspath+'/be/clips/' + file)\n",
    "    sound = sound.set_frame_rate(22050)\n",
    "    sound.export(outputpath+'/' + file[:-4] + '.wav', format='wav')\n",
    "\n",
    "# get list of files to convert\n",
    "files = df_speaker['path'].values.tolist()\n",
    "\n",
    "# convert files\n",
    "with Pool(multiprocessing.cpu_count()) as p:\n",
    "    r = list(tqdm(p.imap(convert_mp3_to_wav, files), total=len(files)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}