-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiditimework.py
More file actions
162 lines (130 loc) · 5.53 KB
/
miditimework.py
File metadata and controls
162 lines (130 loc) · 5.53 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
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
from mido import MidiFile, second2tick
import os
import sox
import shutil
from sox import file_info
import math
import csv
temp = ["Audio Path", "Midi Path", "CSV Path", "Length in Seconds", "Length in Ticks", "Number of Samples", "Ticks per Second", "Samples per Second", "Samples per Tick"]
#include in sheet:
#audio file name ***********[0] (r'filename')
#midi file name ************[1] (r'filename')
#csv file name *************[2] (r'filename')
#length of audio in sec ****[3] (mid.length)
#length of midi in ticks ***[4] (math.ceil(second2tick(mid.length, mid.ticks_per_beat, 500000)))
#number of samples *********[5] (file_info.num_samples(file))
#ticks per sec *************[6] (ticks/sec)
#samples per sec ***********[7] (samples/sec)
#samples per tick **********[8] (samples/ticks)
audioloc = r'C:\Users\victo\Documents\Research\Datasets\maestro-v2.0.0\AudioOnly'
midiloc = r'C:\Users\victo\Documents\Research\Datasets\maestro-v2.0.0\MidiOnly'
csvloc = r'C:\Users\victo\Documents\Research\Datasets\maestro-v2.0.0\CSV'
def allinfo(directory, temp):
n = 0
with open(r'C:\Users\victo\Documents\Research\Datasets\maestro-v2.0.0\masterInfo.csv', 'w+', newline='') as result_file:
wr = csv.writer(result_file, dialect='excel')
wr.writerow(temp)
for subdir, dirs, files in os.walk(directory):
for filename in files:
if filename.endswith("_cutandresample.wav"):
audiopath = subdir + os.sep + filename
audioname = filename
temp[0] = audioname
midipath = audiopath
midipath = midipath.replace("AudioOnly", "MidiOnly")
midipath = midipath.replace("_cutandresample.wav", ".midi")
midiname = filename
midiname = midiname.replace("_cutandresample.wav", ".midi")
temp[1] = midiname
csvpath = audiopath
csvpath = csvpath.replace("AudioOnly", "CSV")
csvpath = csvpath.replace("_cutandresample.wav", ".csv")
csvname = filename
csvname = csvname.replace("_cutandresample.wav", ".csv")
temp[2] = csvname
mid = MidiFile(midipath)
leng = mid.length
temp[3] = leng
tic = math.ceil(second2tick(mid.length, mid.ticks_per_beat, 500000))
temp[4] = tic
samp = file_info.num_samples(audiopath)
temp[5] = samp
temp[6] = tic/leng
temp[7] = samp/leng
temp[8] = samp/tic
with open(r'C:\Users\victo\Documents\Research\Datasets\maestro-v2.0.0\masterInfo.csv', 'a', newline='') as result_file:
wr = csv.writer(result_file, dialect='excel')
wr.writerow(temp)
n+=1
print("Created line", n)
pass
def writeinfotocsv(directory):
f = open(r'C:\Users\victo\Documents\Research\Datasets\maestro-v2.0.0\timemath.csv', 'w')
for subdir, dirs, files in os.walk(directory):
for filename in files:
filepath = subdir + os.sep + filename
if filepath.endswith(".midi"):
mid = MidiFile(filepath)
temp[0] = str(filepath)
temp[1] = " Length: "+str(mid.length)
for i in range(len(temp)):
f.write(temp[i])
f.write("\n")
pass
def copyaudiotonewfolder(directory, destination):
n = 0
for subdir, dirs, files in os.walk(directory):
for filename in files:
filepath = subdir + os.sep + filename
if filepath.endswith(".wav"):
shutil.copy(filepath, destination)
n+=1
print("Audio", n)
pass
def copymiditonewfolder(directory, destination):
n = 0
for subdir, dirs, files in os.walk(directory):
for filename in files:
filepath = subdir + os.sep + filename
if filepath.endswith(".midi"):
shutil.copy(filepath, destination)
n+=1
print("Midi",n)
pass
def cutaudio(audio_path):
n = 0
for subdir, dirs, files in os.walk(audio_path):
for filename in files:
filepath = subdir + os.sep + filename
if filepath.endswith(".wav"):
midiversion = filepath
midiversion = midiversion.replace('.wav', '.midi')
midiversion = midiversion.replace('AudioOnly', 'MidiOnly')
mid = MidiFile(midiversion)
newfile = filepath
newfile = newfile.replace('.wav', '_cut.wav')
tfm = sox.Transformer()
tfm.trim(0,mid.length)
tfm.build(filepath, newfile)
n+=1
print("Cut", n)
pass
def resampleaudio(audio_path):
n = 0
for subdir, dirs, files in os.walk(audio_path):
for filename in files:
filepath = subdir + os.sep + filename
if filepath.endswith("_cut.wav"):
tfn = sox.Transformer()
tfn.rate(16000)
remix_dictionary = {1:[1,2]}
tfn.remix(remix_dictionary)
tfn.build(filepath, filepath.replace('cut.wav', 'cutandresample.wav'))
print("Resample ", n)
pass
#writeinfotocsv(direc)
#copyaudiotonewfolder(direc, dest)
#copymiditonewfolder(direc, mididest)
#cutaudio(dest)
#resampleaudio(dest)
allinfo(audioloc, temp)