Skip to content

Commit 0c2b7a5

Browse files
author
Alex
authored
Version 1.0. Added piano-roll. Limited number of generation attempts.
Adjusted default settings.
1 parent c835ed6 commit 0c2b7a5

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

Examples/markovify_piano.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Original file is located at
77
https://colab.research.google.com/drive/17SGUaA8r6nZyHHH3huOmgcsVUDH6HBWE
88
9-
# Markovify Piano
9+
# Markovify Piano (ver 1.0)
1010
1111
***
1212
@@ -31,6 +31,12 @@
3131
!apt install fluidsynth #Pip does not work for some reason. Only apt works
3232
!pip install midi2audio
3333

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+
3440
#@title Load needed modules
3541
print('Loading needed modules. Please wait...')
3642

@@ -50,6 +56,11 @@
5056
from midi2audio import FluidSynth
5157
from IPython.display import display, Javascript, HTML, Audio
5258

59+
# only for plotting pianoroll
60+
import pretty_midi
61+
import librosa.display
62+
import matplotlib.pyplot as plt
63+
5364
from google.colab import output, drive
5465

5566
print('Creating Dataset dir...')
@@ -227,7 +238,7 @@
227238
"""# Train TXT Markov chain/model"""
228239

229240
#@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}
231242

232243
print('Training Markov chain/model. Please wait...')
233244
markov_text_model = markovify.NewlineText(text, well_formed=False, state_size=markov_chain_state_size)
@@ -263,9 +274,12 @@
263274
#@title Generate Music
264275

265276
#@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}
269283
print_generated_song = False #@param {type:"boolean"}
270284

271285
Output_TXT_String = ''
@@ -281,6 +295,8 @@
281295
Output_TXT_String = ''.join(out)
282296
print('Attempt #', attempt)
283297
attempt += 1
298+
if attempt > 15:
299+
break
284300

285301
print('Generation complete!')
286302
print('=' * 70)
@@ -317,8 +333,21 @@
317333
print('Detailed MIDI stats:')
318334
pprint(detailed_stats)
319335

320-
#@title Listen to the last generated composition
336+
#@title Plot and listen to the last generated composition
321337
#@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+
322351
print('Synthesizing the last output MIDI. Please stand-by... ')
323352
FluidSynth("/usr/share/sounds/sf2/FluidR3_GM.sf2", 16000).midi_to_audio(str(fname + '.mid'), str(fname + '.wav'))
324353
Audio(str(fname + '.wav'), rate=16000)

0 commit comments

Comments
 (0)