-
Notifications
You must be signed in to change notification settings - Fork 29
/
los_angeles_music_composer_edition.py
699 lines (503 loc) · 19.9 KB
/
los_angeles_music_composer_edition.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# -*- coding: utf-8 -*-
"""Los_Angeles_Music_Composer_Edition.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/asigalov61/Los-Angeles-Music-Composer/blob/main/Los_Angeles_Music_Composer_Edition.ipynb
# Los Angeles Music Composer Edition (ver. 4.0)
***
Powered by tegridy-tools: https://github.com/asigalov61/tegridy-tools
***
WARNING: This complete implementation is a functioning model of the Artificial Intelligence. Please excercise great humility, care, and respect. https://www.nscai.gov/
***
#### Project Los Angeles
#### Tegridy Code 2023
***
# (GPU CHECK)
"""
#@title NVIDIA GPU check
!nvidia-smi
"""# (SETUP ENVIRONMENT)"""
#@title Install dependencies
!git clone --depth 1 https://github.com/asigalov61/Los-Angeles-Music-Composer
!pip install torch
!pip install einops
!pip install torch-summary
!pip install tqdm
!pip install matplotlib
!apt install fluidsynth #Pip does not work for some reason. Only apt works
!pip install midi2audio
# Commented out IPython magic to ensure Python compatibility.
#@title Import modules
print('=' * 70)
print('Loading core Los Angeles Music Composer modules...')
import os
import pickle
import random
import secrets
import statistics
from time import time
import tqdm
print('=' * 70)
print('Loading main Los Angeles Music Composer modules...')
import torch
# %cd /content/Los-Angeles-Music-Composer
import TMIDIX
from lwa_transformer import *
# %cd /content/
print('=' * 70)
print('Loading aux Los Angeles Music Composer modeules...')
import matplotlib.pyplot as plt
from torchsummary import summary
from sklearn import metrics
from midi2audio import FluidSynth
from IPython.display import Audio, display
from google.colab import files
print('=' * 70)
print('Done!')
print('Enjoy! :)')
print('=' * 70)
"""# (LOAD MODEL)"""
# Commented out IPython magic to ensure Python compatibility.
#@title Unzip Pre-Trained Los Angeles Music Composer Model
print('=' * 70)
# %cd /content/Los-Angeles-Music-Composer/Model
print('=' * 70)
print('Unzipping pre-trained Los Angeles Music Composer model...Please wait...')
!cat /content/Los-Angeles-Music-Composer/Model/Los_Angeles_Music_Composer_Trained_Model.zip* > /content/Los-Angeles-Music-Composer/Model/Los_Angeles_Music_Composer_Trained_Model.zip
print('=' * 70)
!unzip -j /content/Los-Angeles-Music-Composer/Model/Los_Angeles_Music_Composer_Trained_Model.zip
print('=' * 70)
print('Done! Enjoy! :)')
print('=' * 70)
# %cd /content/
print('=' * 70)
#@title Load Los Angeles Music Composer Model
full_path_to_model_checkpoint = "/content/Los-Angeles-Music-Composer/Model/Los_Angeles_Music_Composer_Model_88835_steps_0.643_loss.pth" #@param {type:"string"}
#@markdown Model precision option
model_precision = "bfloat16" # @param ["bfloat16", "float16", "float32"]
#@markdown bfloat16 == Third precision/triple speed (if supported, otherwise the model will default to float16)
#@markdown float16 == Half precision/double speed
#@markdown float32 == Full precision/normal speed
plot_tokens_embeddings = False # @param {type:"boolean"}
print('=' * 70)
print('Loading Los Angeles Music Composer Pre-Trained Model...')
print('Please wait...')
print('=' * 70)
print('Instantiating model...')
torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul
torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn
device_type = 'cuda'
if model_precision == 'bfloat16' and torch.cuda.is_bf16_supported():
dtype = 'bfloat16'
else:
dtype = 'float16'
if model_precision == 'float16':
dtype = 'float16'
if model_precision == 'float32':
dtype = 'float32'
ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype]
ctx = torch.amp.autocast(device_type=device_type, dtype=ptdtype)
SEQ_LEN = 4096
# instantiate the model
model = LocalTransformer(
num_tokens = 2831,
dim = 1024,
depth = 36,
causal = True,
local_attn_window_size = 512,
max_seq_len = SEQ_LEN
)
model = torch.nn.DataParallel(model)
model.cuda()
print('=' * 70)
print('Loading model checkpoint...')
model.load_state_dict(torch.load(full_path_to_model_checkpoint))
print('=' * 70)
model.eval()
print('Done!')
print('=' * 70)
print('Model will use', dtype, 'precision...')
print('=' * 70)
# Model stats
print('Model summary...')
summary(model)
# Plot Token Embeddings
if plot_tokens_embeddings:
tok_emb = model.module.token_emb.weight.detach().cpu().tolist()
cos_sim = metrics.pairwise_distances(
tok_emb, metric='cosine'
)
plt.figure(figsize=(7, 7))
plt.imshow(cos_sim, cmap="inferno", interpolation="nearest")
im_ratio = cos_sim.shape[0] / cos_sim.shape[1]
plt.colorbar(fraction=0.046 * im_ratio, pad=0.04)
plt.xlabel("Position")
plt.ylabel("Position")
plt.tight_layout()
plt.plot()
plt.savefig("/content/Los-Angeles-Music-Composer-Tokens-Embeddings-Plot.png", bbox_inches="tight")
"""# (LOAD SEED MIDI)"""
#@title Load Seed MIDI
#@markdown Press play button to to upload your own seed MIDI or to load one of the provided sample seed MIDIs from the dropdown list below
select_seed_MIDI = "Upload your own custom MIDI" #@param ["Upload your own custom MIDI", "Los-Angeles-Music-Composer-Piano-Seed-1", "Los-Angeles-Music-Composer-Piano-Seed-2", "Los-Angeles-Music-Composer-Piano-Seed-3", "Los-Angeles-Music-Composer-Piano-Seed-4", "Los-Angeles-Music-Composer-Piano-Seed-5", "Los-Angeles-Music-Composer-MI-Seed-1", "Los-Angeles-Music-Composer-MI-Seed-2", "Los-Angeles-Music-Composer-MI-Seed-3", "Los-Angeles-Music-Composer-MI-Seed-4", "Los-Angeles-Music-Composer-MI-Seed-5"]
number_of_prime_tokens = 300 #@param {type:"slider", min:126, max:3000, step:3}
render_MIDI_to_audio = False # @param {type:"boolean"}
print('=' * 70)
print('Los Angeles Music Composer Seed MIDI Loader')
print('=' * 70)
f = ''
if select_seed_MIDI != "Upload your own custom MIDI":
print('Loading seed MIDI...')
f = '/content/Los-Angeles-Music-Composer/Seeds/'+select_seed_MIDI+'.mid'
score = TMIDIX.midi2single_track_ms_score(open(f, 'rb').read(), recalculate_channels=False)
else:
print('Upload your own custom MIDI...')
print('=' * 70)
uploaded_MIDI = files.upload()
if list(uploaded_MIDI.keys()):
score = TMIDIX.midi2single_track_ms_score(list(uploaded_MIDI.values())[0], recalculate_channels=False)
f = list(uploaded_MIDI.keys())[0]
if f != '':
print('=' * 70)
print('File:', f)
print('=' * 70)
#=======================================================
# START PROCESSING
# INSTRUMENTS CONVERSION CYCLE
events_matrix = []
melody_chords_f = []
melody_chords_f1 = []
itrack = 1
patches = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
patch_map = [
[0, 1, 2, 3, 4, 5, 6, 7], # Piano
[24, 25, 26, 27, 28, 29, 30], # Guitar
[32, 33, 34, 35, 36, 37, 38, 39], # Bass
[40, 41], # Violin
[42, 43], # Cello
[46], # Harp
[56, 57, 58, 59, 60], # Trumpet
[64, 65, 66, 67, 68, 69, 70, 71], # Sax
[72, 73, 74, 75, 76, 77, 78], # Flute
[-1], # Drums
[52, 53], # Choir
[16, 17, 18, 19, 20] # Organ
]
while itrack < len(score):
for event in score[itrack]:
if event[0] == 'note' or event[0] == 'patch_change':
events_matrix.append(event)
itrack += 1
events_matrix.sort(key=lambda x: x[1])
events_matrix1 = []
for event in events_matrix:
if event[0] == 'patch_change':
patches[event[2]] = event[3]
if event[0] == 'note':
event.extend([patches[event[3]]])
once = False
for p in patch_map:
if event[6] in p and event[3] != 9: # Except the drums
event[3] = patch_map.index(p)
once = True
if not once and event[3] != 9: # Except the drums
event[3] = 15 # All other instruments/patches channel
event[5] = max(80, event[5])
if event[3] < 12: # We won't write chans 12-16 for now...
events_matrix1.append(event)
#=======================================================
# PRE-PROCESSING
# checking number of instruments in a composition
instruments_list_without_drums = list(set([y[3] for y in events_matrix1 if y[3] != 9]))
if len(events_matrix1) > 0 and len(instruments_list_without_drums) > 0:
# recalculating timings
for e in events_matrix1:
e[1] = int(e[1] / 10) # Max 1 seconds for start-times
e[2] = int(e[2] / 20) # Max 2 seconds for durations
# Sorting by pitch, then by start-time
events_matrix1.sort(key=lambda x: x[4], reverse=True)
events_matrix1.sort(key=lambda x: x[1])
#=======================================================
# FINAL PRE-PROCESSING
melody_chords = []
pe = events_matrix1[0]
for e in events_matrix1:
if e[1] >= 0 and e[2] >= 0:
# Cliping all values...
tim = max(0, min(127, e[1]-pe[1]))
dur = max(1, min(127, e[2]))
cha = max(0, min(11, e[3]))
ptc = max(1, min(127, e[4]))
vel = max(8, min(127, e[5]))
velocity = round(vel / 15)
# Writing final note
melody_chords.append([tim, dur, cha, ptc, velocity])
pe = e
instruments_list = list(set([y[2] for y in melody_chords]))
num_instr = len(instruments_list)
#=======================================================
# FINAL PROCESSING
#=======================================================
# Break between compositions / Intro seq
if 9 in instruments_list:
drums_present = 2818 # Yes
else:
drums_present = 2817 # No
melody_chords_f.extend([2816, drums_present, 2819+(num_instr-1)])
#=======================================================
# Composition control seq
intro_mode_time = statistics.mode([0] + [y[0] for y in melody_chords if y[2] != 9 and y[0] != 0])
intro_mode_dur = statistics.mode([y[1] for y in melody_chords if y[2] != 9])
intro_mode_pitch = statistics.mode([y[3] for y in melody_chords if y[2] != 9])
intro_mode_velocity = statistics.mode([y[4] for y in melody_chords if y[2] != 9])
# Instrument value 12 is reserved for composition control seq
intro_dur_vel = (intro_mode_dur * 8) + (intro_mode_velocity-1)
intro_cha_ptc = (12 * 128) + intro_mode_pitch
melody_chords_f.extend([intro_mode_time, intro_dur_vel+128, intro_cha_ptc+1152])
# TOTAL DICTIONARY SIZE 2831
#=======================================================
# MAIN PROCESSING CYCLE
#=======================================================
for m in melody_chords:
# WRITING EACH NOTE HERE
dur_vel = (m[1] * 8) + (m[4]-1)
cha_ptc = (m[2] * 128) + m[3]
melody_chords_f.extend([m[0], dur_vel+128, cha_ptc+1152])
melody_chords_f1.append([m[0], dur_vel+128, cha_ptc+1152])
melody_chords_f1 = melody_chords_f1[:(number_of_prime_tokens // 3)]
melody_chords_f = melody_chords_f[:number_of_prime_tokens]
#=======================================================
song = melody_chords_f
song_f = []
tim = 0
dur = 0
vel = 0
pitch = 0
channel = 0
son = []
song1 = []
for s in song:
if s >= 128 and s < (12*128)+1152:
son.append(s)
else:
if len(son) == 3:
song1.append(son)
son = []
son.append(s)
for ss in song1:
tim += ss[0] * 10
dur = ((ss[1]-128) // 8) * 20
vel = (((ss[1]-128) % 8)+1) * 15
channel = (ss[2]-1152) // 128
pitch = (ss[2]-1152) % 128
song_f.append(['note', tim, dur, channel, pitch, vel ])
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
output_signature = 'Los Angeles Music Composer',
output_file_name = '/content/Los-Angeles-Music-Composer-Seed-Composition',
track_name='Project Los Angeles',
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0]
)
#=======================================================
print('=' * 70)
print('Composition stats:')
print('Composition has', len(melody_chords_f1), 'notes')
print('Composition has', len(melody_chords_f), 'tokens')
print('=' * 70)
print('Displaying resulting composition...')
print('=' * 70)
fname = '/content/Los-Angeles-Music-Composer-Seed-Composition'
x = []
y =[]
c = []
colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver']
block_lines = [(song_f[-1][1] / 1000)]
block_tokens = [min(len(melody_chords_f), number_of_prime_tokens)]
for s in song_f:
x.append(s[1] / 1000)
y.append(s[4])
c.append(colors[s[3]])
if render_MIDI_to_audio:
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
display(Audio(str(fname + '.wav'), rate=16000))
plt.figure(figsize=(14,5))
ax=plt.axes(title=fname)
ax.set_facecolor('black')
plt.scatter(x,y, c=c)
plt.xlabel("Time")
plt.ylabel("Pitch")
plt.show()
else:
print('=' * 70)
"""# (COMPOSITION LOOP)
## Run the cells below in a loop to generate endless continuation
"""
#@title Standard Continuation Generator
number_of_tokens_to_generate = 120 # @param {type:"slider", min:33, max:1023, step:3}
number_of_batches_to_generate = 4 #@param {type:"slider", min:1, max:16, step:1}
preview_length_in_tokens = 120 # @param {type:"slider", min:33, max:240, step:3}
number_of_memory_tokens = 4095 #@param {type:"slider", min:402, max:4095, step:3}
temperature = 1 #@param {type:"slider", min:0.1, max:1, step:0.1}
render_MIDI_to_audio = True # @param {type:"boolean"}
print('=' * 70)
print('Los Angeles Music Composer Standard Continuation Model Generator')
print('=' * 70)
preview = melody_chords_f[-preview_length_in_tokens:]
inp = [melody_chords_f[-number_of_memory_tokens:]] * number_of_batches_to_generate
inp = torch.LongTensor(inp).cuda()
with ctx:
out = model.module.generate(inp,
number_of_tokens_to_generate,
temperature=temperature,
return_prime=False,
verbose=True)
out0 = out.tolist()
print('=' * 70)
print('Done!')
#======================================================================
print('=' * 70)
print('Rendering results...')
for i in range(number_of_batches_to_generate):
print('=' * 70)
print('Batch #', i)
print('=' * 70)
out1 = out0[i]
print('Sample INTs', out1[:12])
print('=' * 70)
if len(out) != 0:
song = preview + out1
song_f = []
tim = 0
dur = 0
vel = 0
pitch = 0
channel = 0
son = []
song1 = []
for s in song:
if s >= 128 and s < (12*128)+1152:
son.append(s)
else:
if len(son) == 3:
song1.append(son)
son = []
son.append(s)
for ss in song1:
tim += ss[0] * 10
dur = ((ss[1]-128) // 8) * 20
vel = (((ss[1]-128) % 8)+1) * 15
channel = (ss[2]-1152) // 128
pitch = (ss[2]-1152) % 128
song_f.append(['note', tim, dur, channel, pitch, vel ])
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
output_signature = 'Los Angeles Music Composer',
output_file_name = '/content/Los-Angeles-Music-Composer-Music-Composition_'+str(i),
track_name='Project Los Angeles',
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0]
)
print('=' * 70)
print('Displaying resulting composition...')
print('=' * 70)
fname = '/content/Los-Angeles-Music-Composer-Music-Composition_'+str(i)
x = []
y =[]
c = []
colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver']
for s in song_f:
x.append(s[1] / 1000)
y.append(s[4])
c.append(colors[s[3]])
if render_MIDI_to_audio:
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
display(Audio(str(fname + '.wav'), rate=16000))
plt.figure(figsize=(14,5))
ax=plt.axes(title=fname)
ax.set_facecolor('black')
plt.scatter(x,y, c=c)
pbl = song_f[(int(preview_length_in_tokens / 3))][1] / 1000
ax.axvline(x=pbl, c='w')
plt.xlabel("Time")
plt.ylabel("Pitch")
plt.show()
#@title Choose one generated block to add to the composition
block_action = "add_last_generated_block" #@param ["add_last_generated_block", "remove_last_added_block"]
add_block_with_batch_number = 0 #@param {type:"slider", min:0, max:15, step:1}
render_MIDI_to_audio = False # @param {type:"boolean"}
print('=' * 70)
if block_action == 'add_last_generated_block':
melody_chords_f.extend(out0[min(len(out0)-1, add_block_with_batch_number)])
print('Block added!')
else:
if len(block_tokens) > 1:
melody_chords_f = melody_chords_f[:(len(melody_chords_f)-block_tokens[-1])]
print('Block removed!')
else:
print('Nothing to remove!!!')
print('=' * 70)
print('Composition now has', (len(melody_chords_f) // 4), 'notes')
print('Composition now has', len(melody_chords_f), 'tokens')
print('=' * 70)
print('Sample INTs', out1[:12])
print('=' * 70)
if len(melody_chords_f) != 0:
song = melody_chords_f
song_f = []
tim = 0
dur = 0
vel = 0
pitch = 0
channel = 0
son = []
song1 = []
for s in song:
if s >= 128 and s < (12*128)+1152:
son.append(s)
else:
if len(son) == 3:
song1.append(son)
son = []
son.append(s)
for ss in song1:
tim += ss[0] * 10
dur = ((ss[1]-128) // 8) * 20
vel = (((ss[1]-128) % 8)+1) * 15
channel = (ss[2]-1152) // 128
pitch = (ss[2]-1152) % 128
song_f.append(['note', tim, dur, channel, pitch, vel ])
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
output_signature = 'Los Angeles Music Composer',
output_file_name = '/content/Los-Angeles-Music-Composer-Music-Composition',
track_name='Project Los Angeles',
list_of_MIDI_patches=[0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0]
)
print('=' * 70)
print('Displaying resulting composition...')
print('=' * 70)
fname = '/content/Los-Angeles-Music-Composer-Music-Composition'
x = []
y =[]
c = []
colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'pink', 'orange', 'purple', 'gray', 'white', 'gold', 'silver']
if block_action == 'add_last_generated_block':
block_lines.append((song_f[-1][1] / 1000))
block_tokens.append(len(out0[min(len(out0)-1, add_block_with_batch_number)]))
else:
if len(block_tokens) > 1:
block_lines.pop()
block_tokens.pop()
for s in song_f:
x.append(s[1] / 1000)
y.append(s[4])
c.append(colors[s[3]])
if render_MIDI_to_audio:
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
display(Audio(str(fname + '.wav'), rate=16000))
plt.figure(figsize=(14,5))
ax=plt.axes(title=fname)
ax.set_facecolor('black')
plt.scatter(x,y, c=c)
for bl in block_lines:
ax.axvline(x=bl, c='w')
plt.xlabel("Time")
plt.ylabel("Pitch")
plt.show()
"""# Congrats! You did it! :)"""