-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_words.py
185 lines (106 loc) · 4.31 KB
/
import_words.py
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
'''
After using get_words.
Imports the wave word chunks from dir as a np arrays, and creates labels, according to numberOfFiles,
stores as 'soundData' variable for pre-processing and feature extraction.
Also has function to shuffle soundData in unison with its label
'''
import numpy as np
from scipy.io.wavfile import read
from matplotlib import pyplot as plt
import scipy.io.wavfile
from pydub import AudioSegment
import os
rateList = []
dataList = []
soundData = []
outFileWav = [] # wav file chunks
outFileMp3 = [] # mp3 file chunks
def getNumberOfFiles(path = '../LL_chunks'):
list = os.listdir(path) # dir is your directory path
number_files = len(list)
# print ("Number of Files found in all_chunks:", number_files-1)
return number_files
def getNumberOfSentences(path = '../LL-sentences'):
list = os.listdir(path) # dir is your directory path
number_files = len(list)
# print ("Number of sentences found in sentences directory:", number_files - 1)
return number_files
def convertToMp3(numberOfFilesHere): # run only after you get all the wav chunks in all_chunks
outFile, outFileMp = nameAll(numberOfFilesHere)
for i in range(numberOfFilesHere):
AudioSegment.from_wav(outFile[i]).export(outFileMp[i], format="mp3")
def importAll(numberOfFilesHere): # not in use
print("assigning names of samples")
outFile, outFileMpthree = nameAll(numberOfFilesHere)
for i in range(numberOfFilesHere-1):
# get names of all the samples in the form of "outfile"
rate, data = scipy.io.wavfile.read(outFile[i])
rateList.append(rate)
dataList.append(rate)
soundData.append(np.absolute(data)) # diode full wave rectification is done here
return soundData
def importAllFromDir(path): # better import function to import them all
list = os.listdir(path)
if '.DS_Store' in list:
list.remove('.DS_Store')
print ("There are {} chunks in directory {}".format(len(list), path))
print ("importing all of them...")
if path == '../LL_chunks':
labels = np.ones((len(list), 1))
else:
labels = np.zeros((len(list), 1))
soundData = []
for i in range(len(list)):
# get names of all the samples in the form of "outfile"
if list[i] == '.DS_Store':
continue
rate, data = scipy.io.wavfile.read(path+'/'+list[i])
rateList.append(rate)
dataList.append(data)
soundData.append(np.absolute(data))
print("imported {}".format(list[i]))
return np.array(soundData), labels
def nameAll(numberOfFilesHere): # gets list of file names for wav and mp3 to export later, not needed
for i in range(numberOfFilesHere-1):
outFileWav.append("../LL_chunks/chunk{0}.wav".format(i+1))
print(outFileWav[i])
for i in range(numberOfFilesHere-1):
outFileMp3.append("../mp3_chunks/chunk{0}.mp3".format(i+1))
print("done naming all samples...")
return outFileWav, outFileMp3
def plotAllMaxFour(soundData, numberOfFilesHere): # for max 4 plots
print("plotting all the samples")
for i in range(numberOfFilesHere-1):
plt.subplot(221+i)
plt.plot(soundData[i])
plt.show()
print("done plotting samples...")
def plotData(soundDataHere, numberOfFilesHere):
print("plotting some the samples")
for i in range(numberOfFilesHere-1):
plt.plot(soundDataHere[i])
plt.show()
print("done plotting samples...")
def plotAll(soundDataHere):
print("plotting all the samples")
for i in range(len(soundDataHere)):
plt.title("chunk{}".format(i+1))
plt.xlabel("Sample")
plt.ylabel("Normalised amplitude")
plt.plot(soundDataHere[i])
plt.show()
print("done plotting samples...")
def shuffle_in_unison_scary(data, labels):
rng_state = np.random.get_state()
np.random.shuffle(data)
np.random.set_state(rng_state)
np.random.shuffle(labels)
if __name__ == '__main__':
soundData, labels = importAllFromDir('../LL_chunks')
plotAll(soundData)
# print getNumberOfSentences('./train-data')
# numberOfFiles = getNumberOfFiles()
# soundData = importAll(numberOfFiles) # function to import all the chunks
# shuffle_in_unison_scary(soundData)
# plotAll(soundData, numberOfFiles)
# convertToMp3()