-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
69 lines (42 loc) · 1.58 KB
/
helpers.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
import csv, librosa, vocal_separation
import os.path
BASE_DIR = 'song_downloader/'
MUSIC_DIR = BASE_DIR + 'songs/wav/'
MUSIC_DIR_CLEAN = BASE_DIR + 'songs/wav/clean/'
MTURK_DIR = BASE_DIR + 'mturk_files/'
def get_song_list(batch_name):
song_timings = list()
with open(MTURK_DIR + batch_name) as file:
reader = csv.reader(file, delimiter=',')
next(reader)
for row in reader:
song_name = row[0].replace('mp3', 'wav')
song_location = MUSIC_DIR + song_name
timings_str = row[1].split('\n')
timings = list()
for timing in timings_str:
start = timing.split(',')[0]
minute_seconds = start.split(':')
minute = int(minute_seconds[0])
seconds = int(minute_seconds[1])
total_start = minute * 60 + seconds
timings.append(total_start)
song_timings.append({'name': song_name, 'timings': timings})
return song_timings
def match_sizes(f_words, non_f_words):
num = f_words.shape[0]
mid = non_f_words.shape[0] // 2
non_f_words_sample = non_f_words[mid:mid+num]
return non_f_words_sample
def generate_foreground_audio():
song_list = get_song_list('batch_main.csv')
for song in song_list:
clean_file_name = '{}clean/{}'.format(MUSIC_DIR, song['name'])
if not os.path.isfile(clean_file_name):
print('working on: {}'.format(song['name']))
file_name = MUSIC_DIR + song['name']
data, sr = librosa.load(file_name, sr=None, mono=True)
foreground_data = vocal_separation.separate_vocals(data, sr)
librosa.output.write_wav(clean_file_name, foreground_data, sr)
generate_foreground_audio()
# print(get_song_list('batch_8.csv', True))