forked from mangelroman/audio2score
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_processing.py
More file actions
128 lines (102 loc) · 6.34 KB
/
new_processing.py
File metadata and controls
128 lines (102 loc) · 6.34 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
import pretty_midi
from pydub import AudioSegment
import audio_data_processing
import os
import re
import pickle
def find_actual_start_time(mid):
start_time = 1e8
for instrument in mid.instruments:
for note in instrument.notes:
if note.start < start_time:
start_time = note.start
return(start_time)
def subset_midi_segment(file_name,actual_start,actual_end):
mid = pretty_midi.PrettyMIDI(os.getcwd()+file_name)
# Adjust times based on actual time start of midi
adjustment = find_actual_start_time(mid)
start_adj = actual_start + adjustment
end_adj = actual_end + adjustment
final_start = 0
final_end = actual_end - actual_start
new_mid = pretty_midi.PrettyMIDI() # Create new midi object
# Extract notes, pitch bends, and control changes and add to new midi object
for instrument in mid.instruments:
# Create new instrument to add to new midi object:
new_instrument = pretty_midi.Instrument(instrument.program, instrument.is_drum, pretty_midi.program_to_instrument_name(instrument.program))
new_mid.instruments.append(new_instrument)
# Add notes
for i in range(len(instrument.notes)): # For each note in the instrument...
note = instrument.notes[i]
validity = (note.start < note.end and note.start < end_adj and note.end > start_adj)
within_range = (note.start >= start_adj) and (note.end <= end_adj) and validity
change_start = (note.start < start_adj) and (note.end <= end_adj) and validity
change_end = (note.start >= start_adj) and (note.end > end_adj) and validity
if within_range: # If it's within the range,
new_note = pretty_midi.Note(note.velocity, note.pitch, note.start - start_adj, note.end - start_adj)
new_mid.instruments[-1].notes.append(new_note) # Add the note
elif change_start: # If the note starts before the range,
new_note = pretty_midi.Note(note.velocity, note.pitch, 0, note.end - start_adj) # Update start time
new_mid.instruments[-1].notes.append(new_note) # Add the note
elif change_end: # If the note ends after the range,
new_note = pretty_midi.Note(note.velocity, note.pitch, note.start - start_adj, final_end) # Update start time
new_mid.instruments[-1].notes.append(new_note) # Add the note
# Add pitch_bends
for i in range(len(instrument.pitch_bends)): # For each note in the instrument...
pitch_bend = instrument.pitch_bends[i]
if (pitch_bend.time > start_adj) and (pitch_bend.time < end_adj): # If it's within the range,
new_pitch_bend = pretty_midi.PitchBend(pitch_bend.pitch,pitch_bend.time - start_adj)
new_mid.instruments[-1].pitch_bends.append(new_pitch_bend) # Add to our new midi
# Add control_changes
for i in range(len(instrument.control_changes)): # For each note in the instrument...
control_change = instrument.control_changes[i]
if (control_change.time > start_adj) and (control_change.time < end_adj): # If it's within the range,
new_control_change = pretty_midi.ControlChange(control_change.number,control_change.value,control_change.time - start_adj)
new_mid.instruments[-1].control_changes.append(new_control_change) # Add to our new midi
# Extract time signature changes
for time_sig in mid.time_signature_changes:
if len(new_mid.time_signature_changes) < 1: # If this is the first time sig, add it
new_time_sig = pretty_midi.TimeSignature(time_sig.numerator,time_sig.denominator,0) # Fix the start time if needed
new_mid.time_signature_changes.append(new_time_sig)
elif time_sig.time <= start_adj: # If this is not the first, and we are still before the start time
new_mid.time_signature_changes.pop() # Remove last item
new_time_sig = pretty_midi.TimeSignature(time_sig.numerator,time_sig.denominator,0) # Fix the start time if needed
new_mid.time_signature_changes.append(new_time_sig)
elif time_sig.time < end_adj:
new_time_sig = pretty_midi.TimeSignature(time_sig.numerator,time_sig.denominator,time_sig.time - start_adj)
new_mid.time_signature_changes.append(new_time_sig)
# Extract key signature changes
for key_sig in mid.key_signature_changes:
if len(new_mid.key_signature_changes) < 1: # If this is the first key sig, add it
new_key_sig = pretty_midi.KeySignature(key_sig.key_number,0) # Fix the start key if needed
new_mid.key_signature_changes.append(new_key_sig)
elif key_sig.time <= start_adj: # If this is not the first, and we are still before the start key
new_mid.key_signature_changes.pop() # Remove last item
new_key_sig = pretty_midi.KeySignature(key_sig.key_number,0) # Fix the start key if needed
new_mid.key_signature_changes.append(new_key_sig)
elif key_sig.time < end_adj:
new_key_sig = pretty_midi.KeySignature(key_sig.key_number,key_sig.time - start_adj)
new_mid.key_signature_changes.append(new_key_sig)
return new_mid
def subset_audio_segment(file_path,actual_start,actual_end):
# Convert secs to milsecs:
actual_start = actual_start * 1000
actual_end = actual_end * 1000 - 1
# Read in audio and trim
sound = AudioSegment.from_file(os.getcwd()+file_path, format="wav")
trimmed_sound = sound[actual_start:actual_end]
new_file_path = re.sub(r"\.wav",r"_temp.wav", file_path)
trimmed_sound.export(os.getcwd()+new_file_path, format="wav")
sound_tensor = audio_data_processing.process_audio_as_spect(new_file_path)
os.remove(os.getcwd()+new_file_path)
return(sound_tensor)
def output_pickle(object, file_path):
with open(os.getcwd()+file_path, 'wb') as outp:
pickle.dump(object, outp, pickle.HIGHEST_PROTOCOL)
def input_pickle(pkl_file_path):
with open(os.getcwd()+pkl_file_path, 'rb') as inp:
object = pickle.load(inp)
return object
def wav_path_to_pkl_path(wav_path):
pickle_file_path = re.sub(r"\.wav",r".pkl", wav_path)
return pickle_file_path