Safetensors
llama
dx2102 commited on
Commit
8a05cd6
·
verified ·
1 Parent(s): e7b25f8

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +57 -0
README.md CHANGED
@@ -117,3 +117,60 @@ def postprocess(txt, path):
117
 
118
  postprocess(txt, './result.mid')
119
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
  postprocess(txt, './result.mid')
119
  ```
120
+
121
+
122
+
123
+ Similarlt, to convert a midi file to the text representation:
124
+
125
+ ```
126
+
127
+ def preprocess(path):
128
+ # turn the midi into a custom format and write it to ./example/output.txt
129
+ # midi files may be broken
130
+ try:
131
+ score = symusic.Score(path, ttype='Second')
132
+ except Exception as e:
133
+ print('Ignored midi loading error:', e)
134
+ return ''
135
+
136
+ # prolong notes to the end of the current pedal
137
+ score = score.copy()
138
+ for track in score.tracks:
139
+ notes = track.notes
140
+ pedals = track.pedals
141
+ track.pedals = []
142
+ j = 0
143
+ for i, note in enumerate(notes):
144
+ while j < len(pedals) and pedals[j].time + pedals[j].duration < note.time:
145
+ j += 1
146
+ if j < len(pedals) and pedals[j].time <= note.time <= pedals[j].time + pedals[j].duration:
147
+ # adjust the duration
148
+ note.duration = max(
149
+ note.duration,
150
+ pedals[j].time + pedals[j].duration - note.time,
151
+ )
152
+
153
+ notes = []
154
+ for track in score.tracks:
155
+ instrument = str(track.program) # program id. `instrument` is always a string.
156
+ if track.is_drum:
157
+ instrument = 'drum'
158
+ for note in track.notes:
159
+ notes.append((note.time, note.duration, note.pitch, note.velocity, instrument))
160
+ # dedup
161
+ notes = list({
162
+ (time, duration, pitch): (time, duration, pitch, velocity, instrument)
163
+ for time, duration, pitch, velocity, instrument in notes
164
+ }.values())
165
+ # merge channels, sort by start time. If notes start at the same time, the higher pitch comes first.
166
+ notes.sort(key=lambda x: (x[0], -x[2]))
167
+ # Translate start time to the delta time format:
168
+ # ie. 'pitch duration wait', in milliseconds.
169
+ notes1 = []
170
+
171
+ txt = []
172
+ for note in notes1:
173
+ txt.append(' '.join(map(str, note)))
174
+ txt = '\n'.join(txt)
175
+ return txt
176
+ ```