File size: 4,259 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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import numpy as np\n",
    "import soundfile as sf\n",
    "from pathlib import Path\n",
    "from shutil import copyfile\n",
    "from tqdm import tqdm\n",
    "\n",
    "input_dataset_path = \"[your_local_path]/synpaflex-corpus/v0.1/\"\n",
    "reorganized_dataset_path = \"../synpaflex/\"\n",
    "\n",
    "maximal_duration = 12 # maximal audio file duration in seconds\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wav_dir = os.path.join(reorganized_dataset_path, \"wavs/\")\n",
    "os.makedirs(wav_dir, exist_ok=True)\n",
    "data = []\n",
    "total_duration = 0\n",
    "\n",
    "# Precomputing walk_count for tqdm\n",
    "walk_count = 0\n",
    "for subdir, dirs, files in os.walk(input_dataset_path):\n",
    "    walk_count += 1\n",
    "\n",
    "# walk through dataset\n",
    "for subdir, dirs, files in tqdm(os.walk(input_dataset_path), total=walk_count, bar_format='Data Reorganization : {l_bar}{bar}|'):\n",
    "    for filename in files:\n",
    "        filepath = os.path.join(subdir, filename)\n",
    "\n",
    "        # read wav files\n",
    "        if filepath.endswith(\".wav\"):\n",
    "            try:\n",
    "                wav, sr = sf.read(filepath)\n",
    "                duration = len(wav) / sr\n",
    "                \n",
    "                # Only keep files with shorter durations than maximal_duration\n",
    "                if duration <= maximal_duration:\n",
    "                    total_duration += duration\n",
    "                    path = Path(filepath)\n",
    "                    current_path = Path(path.parent.absolute())\n",
    "                    \n",
    "                    # find corresponding text file\n",
    "                    txt_file_path = os.path.join(current_path, \"txt\", filename.replace('.wav','.txt'))\n",
    "                    if not os.path.exists(txt_file_path):\n",
    "                        parent_path = Path(current_path.parent.absolute())\n",
    "                        txt_file_path = os.path.join(parent_path, \"txt\", filename.replace('.wav', '.txt'))\n",
    "                        if not os.path.exists(txt_file_path):\n",
    "                            break\n",
    "                    norm_text_file_path = txt_file_path.replace(\".txt\", \"_norm.txt\")\n",
    "                    text = open(txt_file_path, \"r\").read()\n",
    "                    if os.path.exists(norm_text_file_path):\n",
    "                        norm_text = open(norm_text_file_path, 'r').read()\n",
    "                    else : \n",
    "                        norm_text = text\n",
    "                    \n",
    "                    # ignore file if text contains digits, otherwise copy wav file and keep metadata to memory \n",
    "                    if not any(chr.isdigit() for chr in text):\n",
    "                        data_line = filename.replace(\".wav\", \"\") + '|' + text + '|' + norm_text\n",
    "                        data.append(data_line)\n",
    "                        copyfile(filepath, os.path.join(wav_dir, filename))\n",
    "\n",
    "            except RuntimeError:\n",
    "                print(filepath + \" not recognized and ignored.\")  \n",
    "\n",
    "# save metadata\n",
    "with open(os.path.join(reorganized_dataset_path, \"synpaflex.txt\"), 'w') as f:\n",
    "    for item in data:\n",
    "        f.write(\"%s\\n\" % item)\n",
    "\n",
    "# display reorganized dataset total duration\n",
    "duration_hours = total_duration / 3600\n",
    "print(\"total duration = \" + str(f\"{duration_hours:.2f}\") + \" hours\")"
   ]
  }
 ],
 "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.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}