-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathpreprocess16.py
More file actions
128 lines (67 loc) · 1.91 KB
/
preprocess16.py
File metadata and controls
128 lines (67 loc) · 1.91 KB
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
# coding: utf-8
# ## Alternative Model (Preprocessing)
# You need to run this before you run notebook 4b.
#
# The wavs in your dataset will be converted to 9bit linear and 80-band mels.
# In[1]:
#import matplotlib.pyplot as plt
import math, pickle, os, glob
import numpy as np
import sys
from utils import *
from utils.dsp import *
import os
# In[2]:
bits = 16
notebook_name = 'nb4'
# In[3]:
# Point SEG_PATH to a folder containing your training wavs
# Doesn't matter if it's LJspeech, CMU Arctic etc. it should work fine
SEG_PATH = sys.argv[1]
DATA_PATH = sys.argv[2]
os.makedirs(DATA_PATH, exist_ok=True)
# In[4]:
def get_files(path, extension='.wav') :
filenames = []
for filename in glob.iglob(f'{path}/**/*{extension}', recursive=True):
filenames += [filename]
return filenames
# In[5]:
wav_files = get_files(SEG_PATH)
# In[6]:
def convert_file(path) :
wav = load_wav(path, encode=False)
mel = melspectrogram(wav)
quant = wav * (2**15 - 0.5) - 0.5
return mel.astype(np.float32), quant.astype(np.int16)
# In[33]:
#m, x = convert_file(wav_files[1])
# In[34]:
#plot_spec(m)
# In[35]:
#plot(x)
# In[36]:
#x = 2 * x / (2**bits - 1) - 1
# In[37]:
#librosa.output.write_wav(DATA_PATH + 'test_quant.wav', x, sr=sample_rate)
# In[38]:
QUANT_PATH = DATA_PATH + '/quant/'
MEL_PATH = DATA_PATH + '/mel/'
os.makedirs(QUANT_PATH, exist_ok=True)
os.makedirs(MEL_PATH, exist_ok=True)
# In[ ]:
#wav_files[0].split('/')[-1][:-4]
# In[ ]:
print("")
# This will take a while depending on size of dataset
dataset_ids = []
for i, path in enumerate(wav_files) :
id = path.split('/')[-1][:-4]
dataset_ids += [id]
m, x = convert_file(path)
np.save(f'{MEL_PATH}{id}.npy', m)
np.save(f'{QUANT_PATH}{id}.npy', x)
print("\r%i/%i", (i + 1, len(wav_files)), end='')
# In[ ]:
with open(DATA_PATH + '/dataset_ids.pkl', 'wb') as f:
pickle.dump(dataset_ids, f)