forked from Agerrr/Automated_Music_Transcription
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonset_frames_split.py
68 lines (56 loc) · 2.32 KB
/
onset_frames_split.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
import sys
import wave
import os
class OnsetFrameSplitter(object):
"""
A class for splitting a file into onset frames.
"""
def __init__(self, music_file, output_directory):
self.music_file = music_file
self.output_directory = output_directory
self.verbose = False
def onset_frames_split(self):
"""
Splits a music file into onset frames.
"""
print 'Just about to execute object frames split function'
onsets_output_file = "onsets.txt"
#OD_METHOD = 'mkl'
# Executing aubioonset command to get the onsets.
os.system('aubioonset -i ' + self.music_file + ' --onset complex > ' + onsets_output_file)
onsets = [float(x) for x in open(onsets_output_file).read().splitlines()]
if self.verbose:
print 'onsets: '
for o in onsets:
print o
print 'Executed aubioonset function to split the file into onsets'
# Reading in the music wave and getting parameters.
input_music_wave = wave.open(self.music_file, "rb")
nframes = input_music_wave.getnframes()
params = input_music_wave.getparams()
framerate = input_music_wave.getframerate()
duration = nframes / float(framerate)
if self.verbose:
print "nframes: %d" % (nframes,)
print "frame rate: %d " % (framerate,)
print "duration: %f seconds" % (duration,)
onsets.append(duration)
onsets[0] = 0.0
if not os.path.exists(self.output_directory):
os.makedirs(self.output_directory)
print 'Just about to split the file into onset frames'
# Splitting the music file into onset frames.
for i in range(len(onsets) - 1):
frame = int(framerate * (onsets[i + 1] - onsets[i]))
sound = input_music_wave.readframes(frame)
music_wave = wave.open(self.output_directory + "/note%d.wav" % (i, ), "wb")
music_wave.setparams(params)
music_wave.setnframes(frame)
music_wave.writeframes(sound)
music_wave.close()
print 'Split the file into onset frames'
if __name__ == '__main__':
music_file = sys.argv[1]
directory = 'frames'
splitter = OnsetFrameSplitter(music_file, directory)
splitter.onset_frames_split()