6
6
Original file is located at
7
7
https://colab.research.google.com/drive/17SGUaA8r6nZyHHH3huOmgcsVUDH6HBWE
8
8
9
- # Markovify Piano
9
+ # Markovify Piano (ver 1.0)
10
10
11
11
***
12
12
31
31
!apt install fluidsynth #Pip does not work for some reason. Only apt works
32
32
!pip install midi2audio
33
33
34
+ # packages below are for plotting pianoroll only
35
+ # they are not needed for anything else
36
+ !pip install pretty_midi
37
+ !pip install librosa
38
+ !pip install matplotlib
39
+
34
40
#@title Load needed modules
35
41
print ('Loading needed modules. Please wait...' )
36
42
50
56
from midi2audio import FluidSynth
51
57
from IPython .display import display , Javascript , HTML , Audio
52
58
59
+ # only for plotting pianoroll
60
+ import pretty_midi
61
+ import librosa .display
62
+ import matplotlib .pyplot as plt
63
+
53
64
from google .colab import output , drive
54
65
55
66
print ('Creating Dataset dir...' )
227
238
"""# Train TXT Markov chain/model"""
228
239
229
240
#@title Train Markov-chain/model
230
- markov_chain_state_size = 4 #@param {type:"slider", min:1, max:10, step:1}
241
+ markov_chain_state_size = 5 #@param {type:"slider", min:1, max:10, step:1}
231
242
232
243
print ('Training Markov chain/model. Please wait...' )
233
244
markov_text_model = markovify .NewlineText (text , well_formed = False , state_size = markov_chain_state_size )
263
274
#@title Generate Music
264
275
265
276
#@markdown HINT: Each note = 3-5 characters depending on the MIDI processing settings above
266
- minimum_number_of_characters_to_generate = 5000 #@param {type:"slider", min:100, max:10000, step:100}
267
- number_of_cycles_to_try_to_generate_desired_result = 5000 #@param {type:"slider", min:10, max:10000, step:10}
268
- minimum_notes_to_generate = 210 #@param {type:"slider", min:10, max:1000, step:10}
277
+
278
+ #@markdown NOTE: If nothing is being generated after 10+ attempts, try again with different model state # and generation settings
279
+
280
+ minimum_number_of_characters_to_generate = 600 #@param {type:"slider", min:100, max:1500, step:100}
281
+ number_of_cycles_to_try_to_generate_desired_result = 9000 #@param {type:"slider", min:10, max:10000, step:10}
282
+ minimum_notes_to_generate = 100 #@param {type:"slider", min:10, max:1000, step:10}
269
283
print_generated_song = False #@param {type:"boolean"}
270
284
271
285
Output_TXT_String = ''
281
295
Output_TXT_String = '' .join (out )
282
296
print ('Attempt #' , attempt )
283
297
attempt += 1
298
+ if attempt > 15 :
299
+ break
284
300
285
301
print ('Generation complete!' )
286
302
print ('=' * 70 )
317
333
print ('Detailed MIDI stats:' )
318
334
pprint (detailed_stats )
319
335
320
- #@title Listen to the last generated composition
336
+ #@title Plot and listen to the last generated composition
321
337
#@markdown NOTE: May be very slow with the long compositions
338
+ fn = os .path .basename (fname + '.mid' )
339
+ fn1 = fn .split ('.' )[0 ]
340
+ print ('Playing and plotting composition...' )
341
+
342
+ pm = pretty_midi .PrettyMIDI (fname + '.mid' )
343
+
344
+ # Retrieve piano roll of the MIDI file
345
+ piano_roll = pm .get_piano_roll ()
346
+
347
+ plt .figure (figsize = (14 , 5 ))
348
+ librosa .display .specshow (piano_roll , x_axis = 'time' , y_axis = 'cqt_note' , sr = 64000 , cmap = plt .cm .hot )
349
+ plt .title ('Composition: ' + fn1 )
350
+
322
351
print ('Synthesizing the last output MIDI. Please stand-by... ' )
323
352
FluidSynth ("/usr/share/sounds/sf2/FluidR3_GM.sf2" , 16000 ).midi_to_audio (str (fname + '.mid' ), str (fname + '.wav' ))
324
353
Audio (str (fname + '.wav' ), rate = 16000 )
0 commit comments