-
Notifications
You must be signed in to change notification settings - Fork 2
/
preproc.py
executable file
·164 lines (87 loc) · 4.02 KB
/
preproc.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 4 11:20:44 2020
@author: Jake Drysdale
"""
# =============================================================================
# preprocessing
# =============================================================================
import numpy as np
import os
import librosa
import random
from natsort import natsorted
import tensorflow as tf
class Preproc(object):
def __init__(self, size=16384, sr=44100, shuffle=False, filetype='.wav'):
#arguments
self._size = size
self._sr = sr
self._shuffle = shuffle
self._filetype = filetype
def load_audio(self, path):
folders = natsorted([x for x in os.listdir(path) if not x.startswith('.')])
flns=[]
for i in range(len(folders)):
flns.append([x for x in natsorted(os.listdir(path+'/'+folders[i])) if x.endswith(self._filetype)])
audio=[]
for i in range(len(flns)):
audio.append([librosa.load(path+'/'+folders[i]+'/'+x, sr=self._sr)[0][:self._size] for x in flns[i]])
return audio, flns
def pad_audio(self, audio):
padded_audio=[]
for i in range(len(audio)):
if len(audio[i]) < self._size:
pad_size = self._size-len(audio[i])
pad = np.zeros(pad_size)
padded = np.append(audio[i],pad)
padded_audio.append(padded)
else:
padded_audio.append(audio[i])
return padded_audio
def fade_audio(self, audio, fade_r=0.2, fade_l=0.001):
faded_audio=[]
for i in range(len(audio)):
fade_r_len = int(len(audio[i])*fade_r)
fade_l_len = int(len(audio[i])*fade_l)
xfade_r = np.hstack((np.ones(len(audio[i])-fade_r_len), np.linspace(1,0,fade_r_len)))
xfade_l = np.hstack((np.linspace(0,1,fade_l_len), np.ones(len(audio[i])-fade_l_len)))
fade=audio[i]*xfade_r
faded_audio.append(fade*xfade_l)
return faded_audio
def shuffle_audio(self, audio):
audio = random.sample(audio, len(audio))
return audio
def save_audio(self, audio, in_path, flns):#
# if not os.path.exists(out_path+'/'+name):
# os.mkdir(out_path+'/'+name)
os.mkdir(in_path+'/preproc')
for i in range(len(audio)):
for j in range(len(audio[i])):
reshape = np.expand_dims(audio[i][j], axis=1)
pcm = tf.audio.encode_wav(reshape, self._sr)
tf.io.write_file(in_path+'/preproc/'+str(i)+'_drum_'+str(j)+self._filetype,
pcm)
def preproc(self, in_path):
audio, flns = self.load_audio(in_path)
audio = [self.pad_audio(x) for x in audio]
audio = [self.fade_audio(x) for x in audio]
# audio = self.normalise_all(audio)
if self._shuffle:
audio = self.shuffle_audio(audio)
self.save_audio(audio, in_path, flns)
return audio
# in_dir = '/home/jake/Documents/data/dafx2020_data/in'
# # out_dir = '/home/jake/Documents/data/delete_plz/out'
# preproc = Preproc(size=4096)
# audio = preproc.preproc(in_dir)
# in_dir = '/home/jake/Documents/data/drum_generator_dataset/sliced/slices'
# out_dir = '/home/jake/Documents/data/drum_generator_dataset/preprocess_4_gan'
# def process_all(in_dir, out_dir):
# sub_dirs = os.listdir(in_dir)
# for i in range(len(sub_dirs)):
# preproc.preproc(in_dir+'/'+sub_dirs[i], out_dir)
# in_dir = '/home/jake/Documents/data/DAFX_GAN_data/raw'
# out_dir = '/home/jake/Documents/data/DAFX_GAN_data/preprocessed'
# process_all(in_dir, out_dir)