-
Notifications
You must be signed in to change notification settings - Fork 3
/
music-generation-with-magenta-using-machine-learning-in-arts.html
1093 lines (1016 loc) · 36 KB
/
music-generation-with-magenta-using-machine-learning-in-arts.html
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Music Generation with Magenta: Using Machine Learning in Arts</title>
<link rel="stylesheet"
href="../common/bower_components/reveal.js/css/reveal.css">
<link rel="stylesheet"
href="../common/bower_components/reveal.js/css/theme/white.css">
<link rel="stylesheet"
href="../common/css/magenta-theme.css">
<script
src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js?skin=desert"></script>
<style>
.slide-first footer,
.slide-last footer {
background-color: #f5af33;
}
.slide-first-vertical {
background-color: white;
}
.slide-first-vertical h1,
.slide-first-vertical h2,
.slide-first-vertical h3,
.slide-first-vertical h4,
.slide-first-vertical h5,
.slide-first-vertical h6 {
color: black;
}
</style>
</head>
<body>
<footer id="footer" class="hide">
<img class="logo"
src="../common/img/magenta/magenta-logo-01.png"
alt="Magenta logo">
<div class="title">
<div>Music Generation with Magenta</div>
<div class="subtitle">Using Machine Learning in Arts</div>
</div>
<div class="title right">
<div>Alexandre DuBreuil @dubreuia</div>
<div class="subtitle">alexandredubreuil.com</div>
</div>
<img class="devoxx"
src="../common/img/devoxx-logo-transparent.png"
alt="Devoxx logo">
</footer>
<div class="reveal">
<div class="slides">
<section>
<section data-background="../common/img/devoxx-background-03.png"
data-background-size="contain"
data-background-position="left">
<img width="250px"
class="logo figure no_border"
src="../common/img/magenta/magenta-logo-02.png"
alt="Magenta logo">
<h1 class="align-left">
Music Generation with <strong style="color:#4c1130ff">Magenta</strong>
</h1>
<h2 class="align-right">Using Machine Learning in Arts</h2>
<h3 class="align-right">Alexandre DuBreuil</h3>
<h4 class="align-right">@dubreuia</h4>
</section>
<section>
<h3>Alexandre DuBreuil</h3>
<p>
Software engineer, sound designer, conference speaker and open source
maintainer.
</p>
<p>@dubreuia</p>
</section>
</section>
<section>
<section>
<h3>Generative Music</h3>
<p>(in 10 minutes)</p>
</section>
<section>
<p>
"Generative art is an artwork partially or completely
created by an autonomous system."
</p>
</section>
<section data-background="resources/ableton-live-02.jpg"
class="background">
<h4>
Make music without being a musician
</h4>
<p>
Maybe you don't know how to <strong>improvise</strong>, maybe you
need help to <strong>compose</strong>.
</p>
<p>
<span class="figure-caption"><a
href="https://media.pitbullaudio.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/s/u/suite_3_1.jpeg">https://media.pitbullaudio.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/s/u/suite_3_1.jpeg</a></span>
</p>
</section>
<section data-background="resources/silly-01.jpg"
class="background">
<h4>
<a href="https://www.youtube.com/watch?v=DkiFjzQgJtg">Monica
Dinculescu - Why you should build silly things</a>
</h4>
<p>
It is okay to make art without taking ourselves seriously. Generative
music is a good way of doing that, because those kind of systems can
be <strong>interacted with</strong> easily.
</p>
<p>
<span class="figure-caption"><a
href="https://www.corelogic.com.au/sites/default/files/2018-08/1200px-800px--tworobots.jpg">https://www.corelogic.com.au/sites/default/files/2018-08/1200px-800px--tworobots.jpg</a></span>
</p>
</section>
<section data-background="resources/system-01.jpg"
class="background">
<h4>
Helping people build generative systems
</h4>
<p>
To make generative art, you need <strong>autonomous systems</strong>
that makes it possible. As software developers, this is our role
to provide that, whether it is for an art exhibit, a generative
radio, or a fun website.
</p>
<p>
<span class="figure-caption"><a
href="https://i.pinimg.com/originals/07/3e/86/073e862a13e4bacc7589f0d4eff4b873.jpg">https://i.pinimg.com/originals/07/3e/86/073e862a13e4bacc7589f0d4eff4b873.jpg</a></span>
</p>
</section>
<section data-background="resources/brian-eno.jpg"
class="background">
<h4>
"The weird and the strange is good" - Brian Eno
</h4>
<p>
Generative systems makes tons of mistakes (also humans), <strong>but
mistakes are good</strong>.
</p>
<p>
<span class="figure-caption"><a
href="https://i.ytimg.com/vi/Dwo-tvmEKhk/maxresdefault.jpg">https://i.ytimg.com/vi/Dwo-tvmEKhk/maxresdefault.jpg</a></span>
</p>
</section>
<section>
<h4>Machine Learning</h4>
<p>
Hand crafting the rules of a painting or the rules of a music style
might be a hard task. That's why Machine Learning is so interesting
in arts: it can learn complex functions.
</p>
</section>
<section>
<h4>Representation: MIDI</h4>
<p>
MIDI is a musical representation analogous to <strong>sheet
music</strong>, where note has a pitch, velocity, and time.
</p>
<p>
Working with MIDI shows the underlying <strong>structure of the
music</strong>, but doesn't define the actual sound, you'll need to
use instruments (numeric or analogic).
</p>
<img width="90%"
src="../common/img/magenta/midi.png"
alt="MIDI diagram"/>
</section>
<section>
<h4>Representation: audio</h4>
<p>
Working with audio is harder because you have to handle 16000
samples per seconds (at least) and keep track of the general
structure. Generating audio is more direct than MIDI.
</p>
<p>
<img width="50%"
src="../common/img/magenta/spectrogram.png"
alt="Audio diagram"/>
</p>
<p>
<span class="figure-caption"><a
href="https://upload.wikimedia.org/wikipedia/commons/c/c5/Spectrogram-19thC.png">https://upload.wikimedia.org/wikipedia/commons/c/c5/Spectrogram-19thC.png</a></span>
</p>
</section>
<section class="align-left">
<h4>Music generation with RNNs (MIDI)</h4>
<p>
<img width="250"
class="figure"
src="../common/img/magenta/rnn.png"
alt="RNN diagram"/>
Recurrent Neural Networks (RNNs) solves two important properties for
music generation: they <strong>operate on sequences for the inputs
and outputs</strong> and they <strong>can remember past
events</strong>.
</p>
<p>
<span class="figure-caption"><a
href="https://www.asimovinstitute.org/wp-content/uploads/2016/09/rnn.png">www.asimovinstitute.org/wp-content/uploads/2016/09/rnn.png</a></span>
</p>
</section>
<section class="align-left">
<h4>Long-term structure with LSTMs (MIDI)</h4>
<p>
<img width="250px"
class="figure"
src="../common/img/magenta/lstm.png"
alt="LSTM diagram"/>
Most RNN uses Long Short-Term Memory (LSTM) cells, since by
themselves, RNNs are hard to train because of the problems of
vanishing and exploding gradient, making long-term dependencies
hard to learn.
</p>
<p>
By using <strong>input, output and forget gates</strong> in
the cell, LSTMs can learn mechanisms to keep or forget information
as they go.
</p>
<p>
<span class="figure-caption"><a
href="https://www.asimovinstitute.org/wp-content/uploads/2016/09/lstm.png">https://www.asimovinstitute.org/wp-content/uploads/2016/09/lstm.png</a></span>
</p>
</section>
<section class="align-left">
<h4>Latent space interpolation with VAEs (MIDI)</h4>
<p>
<img width="250px"
class="figure"
src="../common/img/magenta/vae.png"
alt="VAE diagram"/>
Variational Autoencoders (VAEs) are a pair of networks where an
encoder reduces the input to a lower dimensionality (<strong>latent
space</strong>), from which a decoder tries to reproduce the
input.
</p>
<p>
The latent space is continuous and follows a probability
distribution, meaning it is possible to sample from it.
VAEs are inherently generative models: they can
<strong>sample</strong> and <strong>interpolate</strong>
(smoothly move in the latent space) between two points.
</p>
<p>
<span class="figure-caption"><a
href="https://www.asimovinstitute.org/wp-content/uploads/2016/09/vae.png">https://www.asimovinstitute.org/wp-content/uploads/2016/09/vae.png</a></span>
</p>
</section>
<section class="align-left">
<h4>Audio generation with WaveNet Autoencoders (audio)</h4>
<p>
<img width="500px"
class="figure"
src="../common/img/magenta/wavenet.png"
alt="Wavenet diagram"/>
WaveNet is a convolutional neural network (CNN) taking raw signal
as an input and synthesizing output audio sample by sample.
</p>
<p>
The WaveNet Autoencoder present in Magenta is a Wavenet-style AE
network capable of learning its own temporal embedding, resulting
in a latent space from which is it possible to <strong>sample
</strong> and <strong>mix</strong> elements.
</p>
<p>
<span class="figure-caption"><a
href="https://magenta.tensorflow.org/assets/nsynth_05_18_17/encoder-decoder.png">magenta.tensorflow.org/assets/nsynth_05_18_17/encoder-decoder.png</a></span>
</p>
</section>
<section>
<h4>What's in Magenta?</h4>
<table class="smaller">
<thead>
<tr>
<th>Model</th>
<th>Network</th>
<th>Repr.</th>
<th>Encoding</th>
</tr>
</thead>
<tbody>
<tr>
<td>DrumsRNN</td>
<td>LSTM</td>
<td>MIDI</td>
<td>polyphonic-ish</td>
</tr>
<tr>
<td>MelodyRNN</td>
<td>LSTM</td>
<td>MIDI</td>
<td>monophonic</td>
</tr>
<tr>
<td>PolyphonyRNN</td>
<td>LSTM</td>
<td>MIDI</td>
<td>polyphonic</td>
</tr>
<tr>
<td>PerformanceRNN</td>
<td>LSTM</td>
<td>MIDI</td>
<td>polyphonic, groove</td>
</tr>
<tr>
<td>MusicVAE</td>
<td>VAE</td>
<td>MIDI</td>
<td>multiple</td>
</tr>
<tr>
<td>NSynth</td>
<td>Wavenet AE</td>
<td>Audio</td>
<td>-</td>
</tr>
<tr>
<td>GANSynth</td>
<td>GAN</td>
<td>Audio</td>
<td>-</td>
</tr>
</tbody>
</table>
</section>
</section>
<section>
<section>
<h3>Live code: Generate a track</h3>
<p>(in 20 minutes)</p>
</section>
<section data-background="resources/cat-on-a-keyboard-in-space.jpg"
class="background">
<h4>
STEP 1: make everything sound like a cat.
</h4>
<p>
We'll use NSynth to mix cat sounds with other sounds.
</p>
<p>
<span class="figure-caption"><a
href="https://images6.alphacoders.com/475/475224.jpg">https://images6.alphacoders.com/475/475224.jpg</a></span>
</p>
</section>
<section>
<h4>STEP 1: The sounds</h4>
<div class="sound-clip">
<img src="../common/img/magenta/rainbowgram-bass-01.png"
class="no_border"
alt="Audio diagram bass"/>
<audio controls>
<source src="code/sounds/83249__zgump__bass-0205__crop.wav"
type="audio/wav"/>
</audio>
</div>
<div class="sound-clip">
<img src="../common/img/magenta/rainbowgram-metal-01.png"
class="no_border"
alt="Audio diagram metal"/>
<audio controls>
<source
src="code/sounds/160045__jorickhoofd__metal-hit-with-metal-bar-resonance__crop.wav"
type="audio/wav"/>
</audio>
</div>
<div class="sound-clip">
<img src="../common/img/magenta/rainbowgram-cat-01.png"
class="no_border"
alt="Audio diagram cat"/>
<audio controls>
<source src="code/sounds/412017__skymary__cat-meow-short__crop.wav"
type="audio/wav"/>
</audio>
</div>
<div class="sound-clip">
<img src="../common/img/magenta/rainbowgram-flute-01.png"
class="no_border"
alt="Audio diagram flute"/>
<audio controls>
<source src="code/sounds/427567__maria-mannone__flute__crop.wav"
type="audio/wav"/>
</audio>
</div>
</section>
<section>
<h4>STEP 1: Entry point</h4>
<p>
See "code/nsynth.py" and method <code>app</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def app(unused_argv):
encoding1, encoding2 = encode([FLAGS.wav1, FLAGS.wav2],
sample_length=FLAGS.sample_length,
sample_rate=FLAGS.sample_rate,
checkpoint=FLAGS.checkpoint)
encoding_mix = mix(encoding1, encoding2)
synthesize(encoding_mix, checkpoint=FLAGS.checkpoint)
</code>
</pre>
</div>
</section>
<section>
<h4>STEP 1: Encode</h4>
<p>
See "code/nsynth.py" and method <code>encode</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def encode(paths: List[str],
sample_length: int = 16000,
sample_rate: int = 16000,
checkpoint: str = "checkpoints/wavenet-ckpt/model.ckpt-200000") \
-> np.ndarray:
audios = []
for path in paths:
audio = utils.load_audio(path,
sample_length=sample_length,
sr=sample_rate)
audios.append(audio)
audios = np.array(audios)
encodings = fastgen.encode(audios, checkpoint, sample_length)
return encodings
</code>
</pre>
</div>
</section>
<section>
<h4>STEP 1: Mix</h4>
<p>
See "code/nsynth.py" and method <code>mix</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def mix(encoding1: np.ndarray,
encoding2: np.ndarray) \
-> np.ndarray:
encoding_mix = (encoding1 + encoding2) / 2.0
return encoding_mix
</code>
</pre>
</div>
</section>
<section>
<h4>STEP 1: Synthesize</h4>
<p>
See "code/nsynth.py" and method <code>synthesize</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def synthesize(encoding_mix: np.ndarray,
checkpoint: str = "checkpoints/wavenet-ckpt/model.ckpt-200000"):
os.makedirs(os.path.join("output", "synth"), exist_ok=True)
date_and_time = time.strftime("%Y-%m-%d_%H%M%S")
output = os.path.join("output", "synth", f"{date_and_time}.wav")
encoding_mix = np.array([encoding_mix])
fastgen.synthesize(encoding_mix,
checkpoint_path=checkpoint,
save_paths=[output])
</code>
</pre>
</div>
</section>
<section>
<h4>STEP 1: GANSynth</h4>
<p>
As shown, the NSynth instrument is nice, but really slow for the
audio synthesis. You should use <strong>GANSynth</strong>.
</p>
</section>
<section>
<h4>STEP 1: The results</h4>
<div>
<div class="sound-clip smaller">
<img src="../common/img/magenta/83249_412017_rainbowgram_trim.png"
class="no_border"
alt="Audio diagram bass cat"/>
<span>Bass + Cat</span>
<audio controls>
<source src="code/sounds/83249_412017_bass_cat.wav"
type="audio/wav"/>
</audio>
</div>
<div class="sound-clip smaller">
<img src="../common/img/magenta/83249_427567_rainbowgram_trim.png"
class="no_border"
alt="Audio diagram bass flute"/>
<span>Bass + Flute</span>
<audio controls>
<source src="code/sounds/83249_427567_bass_flute.wav"
type="audio/wav"/>
</audio>
</div>
<div class="sound-clip smaller">
<img src="../common/img/magenta/83249_160045_rainbowgram_trim.png"
class="no_border"
alt="Audio diagram bass metal"/>
<span>Bass + Metal</span>
<audio controls>
<source src="code/sounds/83249_160045_bass_metal.wav"
type="audio/wav"/>
</audio>
</div>
</div>
<div>
<div class="sound-clip smaller">
<img src="../common/img/magenta/160045_83249_rainbowgram_trim.png"
class="no_border"
alt="Audio diagram metal bass"/>
<span>Metal + Bass</span>
<audio controls>
<source src="code/sounds/160045_83249_metal_bass.wav"
type="audio/wav"/>
</audio>
</div>
<div class="sound-clip smaller">
<img src="../common/img/magenta/160045_412017_rainbowgram_trim.png"
class="no_border"
alt="Audio diagram metal cat"/>
<span>Metal + Cat</span>
<audio controls>
<source src="code/sounds/160045_412017_metal_cat.wav"
type="audio/wav"/>
</audio>
</div>
<div class="sound-clip smaller">
<img src="../common/img/magenta/160045_427567_rainbowgram_trim.png"
class="no_border"
alt="Audio diagram metal flute"/>
<span>Metal + Flute</span>
<audio controls>
<source src="code/sounds/160045_427567_metal_flute.wav"
type="audio/wav"/>
</audio>
</div>
</div>
</section>
<section data-background="resources/cats.jpg"
class="background">
<h4>
STEP 2: sequence the cats
</h4>
<p>
We'll use DrumsRNN and MelodyRNN to generate MIDI to play the
samples.
</p>
<p>
<span class="figure-caption"><a
href="http://wallpaperstock.net/cats-line-up_wallpapers_13044_1920x1200.jpg">http://wallpaperstock.net/cats-line-up_wallpapers_13044_1920x1200.jpg</a></span>
</p>
</section>
<section>
<h4>STEP 2: Reset</h4>
<p>
See "code/sequences.py" and method <code>reset</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def reset(loop_start_time: float,
loop_end_time: float,
seconds_per_loop: float):
sequence = music_pb2.NoteSequence()
sequence = loop(sequence,
loop_start_time,
loop_end_time,
seconds_per_loop)
return sequence
</code>
</pre>
</div>
</section>
<section>
<h4>STEP 2: Loop</h4>
<p>
See "code/sequences.py" and method <code>loop</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def loop(sequence: NoteSequence,
loop_start_time: float,
loop_end_time: float,
seconds_per_loop: float):
sequence = ss.trim_note_sequence(sequence,
loop_start_time,
loop_end_time)
sequence = ss.shift_sequence_times(sequence,
seconds_per_loop)
return sequence
</code>
</pre>
</div>
</section>
<section>
<h4>STEP 2: Generate</h4>
<p>
See "code/sequences.py" and method <code>generate</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def generate(sequence: NoteSequence,
name: str,
bundle_filename: str,
config_name: str,
generation_start_time: float,
generation_end_time: float):
generator_options = generator_pb2.GeneratorOptions()
generator_options.args['temperature'].float_value = 1
generator_options.generate_sections.add(
start_time=generation_start_time,
end_time=generation_end_time)
sequence_generator = get_sequence_generator(name,
bundle_filename,
config_name)
sequence = sequence_generator.generate(sequence,
generator_options)
sequence = ss.trim_note_sequence(sequence,
generation_start_time,
generation_end_time)
return sequence
</code>
</pre>
</div>
</section>
<section>
<h4>STEP 2: Sequence generator</h4>
<p>
See "code/sequences.py" and method
<code>get_sequence_generator</code> in this repo.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-python" data-trim data-noescape>
def get_sequence_generator(name: str,
bundle_filename: str,
config_name: str):
if name == "drums":
generator = drums_rnn_sequence_generator
elif name == "melody":
generator = melody_rnn_sequence_generator
else:
raise Exception(f"Unknown sequence generator {name}")
mm.notebook_utils.download_bundle(bundle_filename, "bundles")
bundle = mm.sequence_generator_bundle.read_bundle_file(
os.path.join("bundles", bundle_filename))
generator_map = generator.get_generator_map()
sequence_generator = generator_map[config_name](
checkpoint=None, bundle=bundle)
sequence_generator.initialize()
return sequence_generator
</code>
</pre>
</div>
</section>
<section>
<h4>Wrapping up</h4>
<p>
This generative music demo helped us improvise and compose around an
idea: a track composed of a percussion, a melody, and a cat
😺. We could <strong>interact</strong> with the system
and it helped us improvise around a theme.
</p>
</section>
<section>
<h4>Can we do better?</h4>
<p>
Was it perfect? No, we had little happy accidents.
</p>
<div class="fragment">
<img width="25%"
src="../common/img/magenta/bob-ross.jpg"
alt="Bob Ross image"/>
<div>
<span class="figure-caption"><a
href="https://2.bp.blogspot.com/_s5_5vgBh1Zo/TJVmnBJW-bI/AAAAAAAAAHc/Mcgu8_el_84/s1600/Bob_Ross.jpg">https://2.bp.blogspot.com/_s5_5vgBh1Zo/TJVmnBJW-bI/AAAAAAAAAHc/Mcgu8_el_84/s1600/Bob_Ross.jpg</a></span>
</div>
</div>
<p>
Maybe we could have had more control over the sequences. Maybe we
wanted to improvise around a <strong>musical style</strong>,
or a <strong>specific structure</strong>.
</p>
</section>
</section>
<section>
<section>
<h3>Training</h3>
<p>(in 5 minutes)</p>
</section>
<section>
<h4>Why?</h4>
<p>
The pre-trained models in Magenta are good, but if for example
you want to generate a <strong>specific style</strong>, generate a
<strong>specific time signature</strong> (3/4 for example), or a
<strong>specific instrument</strong> (cello for example) you'll
need to train your own.
</p>
</section>
<section>
<h4>Datasets: LAKHS (MIDI)</h4>
<p>
A good place to start is the LAKHS dataset, a 180,000 MIDI files
dataset, (partially) matched with the Million Song Dataset
(metadata like artist, release, genre).
</p>
<p>
<span class="figure-caption"><a
href="https://colinraffel.com/projects/lmd/">https://colinraffel.com/projects/lmd/</a></span>
</p>
<p>
<span class="figure-caption"><a
href="http://millionsongdataset.com/">http://millionsongdataset.com/</a></span>
</p>
</section>
<section>
<h4>Datasets: NSynth (audio)</h4>
<p>
A large-scale and high-quality dataset of annotated musical notes.
Training audio requires lots of resources, but can be achieved using
GANSynth.
</p>
<p>
<span class="figure-caption"><a
href="https://magenta.tensorflow.org/datasets/nsynth">https://magenta.tensorflow.org/datasets/nsynth</a></span>
</p>
</section>
<section>
<h4>Building the dataset</h4>
<p>
From MIDI, ABCNotation, MusicXML files to <code>NoteSequence</code>.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-bash smaller" data-trim data-noescape>
convert_dir_to_note_sequences \
--input_dir="/path/to/dataset/jazz_midi/drums/v1" \
--output_file="/tmp/notesequences.tfrecord" \
--recursive
</code>
</pre>
</div>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-bash smaller" data-trim data-noescape>
...
Converted MIDI file /path/to/dataset/jazz_midi/drums/v1/TRVUCSW12903CF536D.mid.
Converted MIDI file /path/to/dataset/jazz_midi/drums/v1/TRWSJLM128F92DF651.mid.
...
</code>
</pre>
</div>
</section>
<section>
<h4>Create SequenceExamples</h4>
<p>
The sequence examples are fed into the model, it contains a sequence
of inputs and a sequence of labels that represents the drum track.
Those are split to eval and training sets.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-bash smaller" data-trim data-noescape>
drums_rnn_create_dataset \
--config="drum_kit" \
--input="/tmp/notesequences.tfrecord" \
--output_dir="/tmp/drums_rnn/sequence_examples" \
--eval_ratio=0.10
</code>
</pre>
</div>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-bash smaller" data-trim data-noescape>
...
DAGPipeline_DrumsExtractor_training_drum_track_lengths_in_bars:
[8,10): 1
[10,20): 8
[20,30): 8
[30,40): 29
[40,50): 1
[50,100): 2
...
</code>
</pre>
</div>
</section>
<section>
<h4>Train and evaluate the model</h4>
<p>
Launch the training, using a specific configuration and
hyperparameters.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-bash smaller" data-trim data-noescape>
drums_rnn_train \
--config="drum_kit" \
--run_dir="/tmp/drums_rnn/logdir/run1" \
--sequence_example_file="/tmp/drums_rnn/sequence_examples/training_drum_tracks.tfrecord" \
--hparams="batch_size=64,rnn_layer_sizes=[64,64]" \
--num_training_steps=20000
</code>
</pre>
</div>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-bash smaller" data-trim data-noescape>
...
Saving checkpoints for 0 into /tmp/drums_rnn/logdir/run1/train/model.ckpt.
Accuracy = 0.013341025, Global Step = 1, Loss = 6.2323294, Perplexity = 508.9396
Accuracy = 0.43837976, Global Step = 11, Loss = 5.1239195, Perplexity = 167.99252 (50.009 sec)
global_step/sec: 0.199963
Saving checkpoints for 12 into /tmp/drums_rnn/logdir/run1/train/model.ckpt.
...
</code>
</pre>
</div>
</section>
<section>
<h4>Tensorboard</h4>
<p>
You can launch TensorBoard and go to http://localhost:6006 to view
the TensorBoard dashboard.
</p>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-bash smaller" data-trim data-noescape>
tensorboard --logdir="/tmp/drums_rnn/logdir"
</code>
</pre>
</div>
</section>
</section>
<section>
<section>
<h3>Interaction with the outside world</h3>
<p>(in 5 minutes)</p>
</section>
<section>
<h4>Python to everything using MIDI</h4>
<p>
Magenta can send MIDI, which is understood by basically everything
that makes sound: <strong>DAWs</strong> (like Ableton Live),
<strong>software synthesizers</strong> (like fluidsynth),
<strong>hardware synthesizers</strong> (though USB or MIDI cable),
etc.
</p>
</section>
<section>
<h4>Magenta in the browser with Magenta.js</h4>
<p>
You can use Magenta and most of its models in the browser, using
Magenta.js (which in turns uses Tensorflow.js).
</p>
<p>
Amazing project to share generative music applications. Harder
to use the audio and MIDI in other softwares like DAWs.
</p>
</section>
<section>
<h4>Melody Mixer</h4>
<p>
<img
width="75%"
src="../common/img/magenta/melody-mixer.png"
alt="Melody mixer"/>
</p>
<p>
<span class="figure-caption"><a
href="https://experiments.withgoogle.com/ai/melody-mixer/view/">https://experiments.withgoogle.com/ai/melody-mixer/view/</a></span>
</p>
</section>
<section>
<h4>Neural Drum Machine</h4>
<p>
<img
width="75%"
src="../common/img/magenta/neural-drum-machine.png"
alt="Neural Drum Machine"/>
</p>
<p>
<span class="figure-caption"><a
href="https://codepen.io/teropa/full/JLjXGK">https://codepen.io/teropa/full/JLjXGK</a></span>
</p>
</section>
<section>
<h4>GANHarp</h4>
<p>
<img
width="75%"
src="../common/img/magenta/ganharp.png"
alt="GANHarp"/>
</p>
<p>
<span class="figure-caption"><a
href="https://ganharp.ctpt.co">https://ganharp.ctpt.co</a></span>
</p>
</section>
<section>
<h4>Easy peasy</h4>
<div class="code-wrapper">
<pre class="prettyprint">
<code class="code lang-html" data-trim data-noescape>
<html>
<head>
<!-- Load @magenta/music -->
<script src="https://cdn.jsdelivr.net/npm/@magenta/<mark
class="fragment highlight-current-blue">music@^1.0.0</mark>">
</script>
<script>
// Instantiate model by loading desired config.
const model = new mm.<mark class="fragment highlight-current-blue">MusicVAE</mark>(
'https://storage.googleapis.com/magentadata/' +
'js/checkpoints/music_vae/<mark class="fragment highlight-current-blue">trio_4bar</mark>');
const player = new mm.Player();
// Samples from MusicVAE and play the sample
function play() {
player.resumeContext();
model.<mark class="fragment highlight-current-blue">sample(1)</mark>
.then((samples) => player.start(samples[0], 80));
}
</script>
</head>
<body><button onclick="play()"><h1>Play Trio</h1></button></body>
</html>
</code>
</pre>
</div>