-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
185 lines (174 loc) · 7.18 KB
/
plugin.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from __future__ import annotations
from tuneflow_py import TuneflowPlugin, Song, ParamDescriptor, WidgetType, TrackType, InjectSource, Track, Clip, TuneflowPluginTriggerData, ClipAudioDataInjectData, pitch_to_hz
from typing import Any
import tempfile
import traceback
import tensorflow as tf
from basic_pitch import ICASSP_2022_MODEL_PATH
from basic_pitch.inference import predict
from pretty_midi import PrettyMIDI, Instrument, Note
from math import floor, ceil
basic_pitch_model = tf.saved_model.load(str(ICASSP_2022_MODEL_PATH))
class BasicPitchTranscribe(TuneflowPlugin):
@staticmethod
def provider_id():
return "andantei"
@staticmethod
def plugin_id():
return "basic-pitch"
@staticmethod
def params(song: Song) -> dict[str, ParamDescriptor]:
return {
"clipAudioData": {
"displayName": {
"zh": '音频',
"en": 'Audio',
},
"defaultValue": None,
"widget": {
"type": WidgetType.NoWidget.value,
},
"hidden": True,
"injectFrom": {
"type": InjectSource.ClipAudioData.value,
"options": {
"clips": "selectedAudioClips",
"convert": {
"toFormat": "ogg",
"options": {
"sampleRate": 44100
}
}
}
}
},
"onsetThreshold": {
"displayName": {
"zh": '音符分割粒度',
"en": 'Note Segmentation Granularity',
},
"description": {
"zh": '值越小,越容易检测到新的音符',
"en": 'The smaller the value, the easier the model detects a new note',
},
"defaultValue": 0.5,
"widget": {
"type": WidgetType.Slider.value,
"config": {
"minValue": 0.05,
"maxValue": 0.95,
"step": 0.05,
},
},
},
"frameThresh": {
"displayName": {
"zh": '音符生成粒度',
"en": 'Note Creation Granularity',
},
"description": {
"zh": '值越小,越容易生成音符',
"en": 'The smaller the value, the easier the model creates a note',
},
"defaultValue": 0.3,
"widget": {
"type": WidgetType.Slider.value,
"config": {
"minValue": 0.05,
"maxValue": 0.95,
"step": 0.05,
},
},
},
"minNoteLen": {
"displayName": {
"zh": '最小音符长度',
"en": 'Minimum Note Length',
},
"description": {
"zh": '创建一个音符需要的最小长度(ms)',
"en": 'The minimum length required to create a note, in milliseconds',
},
"defaultValue": 11,
"widget": {
"type": WidgetType.Slider.value,
"config": {
"minValue": 3,
"maxValue": 50,
"step": 1,
},
},
},
"maxPitch": {
"displayName": {
"zh": '音高上限',
"en": 'Maximum Allowed Note Pitch',
},
"defaultValue": 102,
"widget": {
"type": WidgetType.Pitch.value,
"config": {
"minAllowedPitch": 28,
"maxAllowedPitch": 102,
},
},
},
"minPitch": {
"displayName": {
"zh": '音高下限',
"en": 'Minimum Allowed Note Pitch',
},
"defaultValue": 28,
"widget": {
"type": WidgetType.Pitch.value,
"config": {
"minAllowedPitch": 28,
"maxAllowedPitch": 102,
},
},
},
}
@staticmethod
def run(song: Song, params: dict[str, Any]):
trigger: TuneflowPluginTriggerData = params["trigger"]
trigger_entity_id = trigger["entities"][0]
track = song.get_track_by_id(trigger_entity_id["trackId"])
if track is None:
raise Exception("Cannot find track")
clip = track.get_clip_by_id(trigger_entity_id["clipId"])
if clip is None:
raise Exception("Cannot find clip")
target_tempo = song.get_tempo_event_at_tick(clip.get_clip_start_tick()).get_bpm()
clip_audio_data_list: ClipAudioDataInjectData = params["clipAudioData"]
minFreq = floor(pitch_to_hz(params["minPitch"]))
maxFreq = ceil(pitch_to_hz(params["maxPitch"]))
clip_start_time = song.tick_to_seconds(clip.get_clip_start_tick())
tmp_file = tempfile.NamedTemporaryFile(delete=True, suffix=clip_audio_data_list[0]["audioData"]["format"])
tmp_file.write(clip_audio_data_list[0]["audioData"]["data"])
try:
model_output, midi_data, note_events = predict(
tmp_file.name, basic_pitch_model, onset_threshold=params["onsetThreshold"],
frame_threshold=params["frameThresh"],
minimum_note_length=params["minNoteLen"],
minimum_frequency=minFreq, maximum_frequency=maxFreq, midi_tempo=target_tempo)
midi_data: PrettyMIDI = midi_data
new_midi_track = song.create_track(type=TrackType.MIDI_TRACK, index=song.get_track_index(
track_id=track.get_id()),
assign_default_sampler_plugin=True)
new_midi_clip = new_midi_track.create_midi_clip(
clip_start_tick=clip.get_clip_start_tick(),
clip_end_tick=clip.get_clip_end_tick(),
insert_clip=True)
for input_track in midi_data.instruments:
input_track: Instrument = input_track
for note in input_track.notes:
note: Note = note
note_start_time = note.start + clip_start_time
note_end_time = note.end + clip_start_time
print(note, note_start_time, note_end_time)
added_note = new_midi_clip.create_note(pitch=note.pitch.item(), velocity=note.velocity, start_tick=song.seconds_to_tick(
note_start_time), end_tick=song.seconds_to_tick(note_end_time), update_clip_range=False, resolve_clip_conflict=False)
except Exception as e:
print(traceback.format_exc())
finally:
tmp_file.close()