-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractMelody.py
More file actions
executable file
·78 lines (51 loc) · 1.85 KB
/
Copy pathExtractMelody.py
File metadata and controls
executable file
·78 lines (51 loc) · 1.85 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
#! /usr/bin/env python
import sys
import wave, struct, math
from aubio import source, notes
def ExtractMelody(filename, outputfilename):
def midi2Hz(d):
f = 2**((d-69)/12)
f *= 440
return f
downsample = 1
samplerate = 44100 // downsample
win_s = 512 // downsample # fft size
hop_s = 256 // downsample # hop size
s = source(filename, samplerate, hop_s)
samplerate = s.samplerate
wavef = wave.open(outputfilename,'w')
wavef.setnchannels(1) # mono
wavef.setsampwidth(2)
wavef.setframerate(samplerate)
notes_o = notes("default", win_s, hop_s, samplerate)
notes_o.set_silence(-40)
print("%8s" % "time","[ start","vel","last ]")
samenotecounter = 1
lastnote = 0
lastvelocity = 0
volumeunit = 32767 / 127
# total number of frames read
total_frames = 0
while True:
samples, read = s()
new_note = notes_o(samples)
if (new_note[0] != 0):
for i in range(int(hop_s)*samenotecounter):
value = int(volumeunit*lastvelocity*math.sin(2*lastnote*math.pi*float(i)/float(samplerate)))
data = struct.pack('<h', value)
wavef.writeframesraw( data )
samenotecounter = 1
lastnote = midi2Hz(new_note[0])
lastvelocity = new_note[1]
print("%.6f" % (total_frames/float(samplerate)), new_note[0], new_note[1])
else:
samenotecounter += 1
total_frames += read
if read < hop_s:
for i in range(int(hop_s)*samenotecounter):
value = int(volumeunit*lastvelocity*math.sin(2*lastnote*math.pi*float(i)/float(samplerate)))
value = value % 32766
data = struct.pack('<h', value)
wavef.writeframesraw( data )
break
wavef.close()