<jupyter_start><jupyter_text><jupyter_code>!git clone https://github.com/bearpelican/musicautobot.git
import os
os.chdir('musicautobot')
!apt install musescore fluidsynth
!cp /usr/share/sounds/sf2/FluidR3_GM.sf2 ./font.sf2
!pip install torch fastai music21 pebble fluidsynth midi2audio
from musicautobot.numpy_encode import *
from musicautobot.utils.file_processing import process_all, process_file
from musicautobot.config import *
from musicautobot.music_transformer import *
from musicautobot.utils.setup_musescore import setup_musescore
setup_musescore()
from midi2audio import FluidSynth
from IPython.display import Audio
# Colab cannot play music directly from music21 - must convert to .wav first
def play_wav(stream):
    out_midi = stream.write('midi')
    out_wav = str(Path(out_midi).with_suffix('.wav'))
    FluidSynth("font.sf2").midi_to_audio(out_midi, out_wav)
    return Audio(out_wav)<jupyter_output><empty_output><jupyter_text>Generate Music with Pretrained Model  Load Pretrained<jupyter_code># Location of your midi files
midi_path =  Path('data/midi/examples')

# Location of saved datset
data_path = Path('data/numpy')
# Data
data = MusicDataBunch.empty(data_path)
vocab = data.vocab

# For Saved Data:
# data = load_data(data_path, 'musicitem_data_save.pkl')
# Pretrained Model
# Download pretrained model if you haven't already
pretrained_url = 'https://ashaw-midi-web-server.s3-us-west-2.amazonaws.com/pretrained/MusicTransformerKeyC.pth'
# pretrained_url = 'https://ashaw-midi-web-server.s3-us-west-2.amazonaws.com/pretrained/MusicTransformer.pth'

pretrained_path = data_path/'pretrained'/Path(pretrained_url).name
pretrained_path.parent.mkdir(parents=True, exist_ok=True)
download_url(pretrained_url, dest=pretrained_path)
# Learner
learn = music_model_learner(data, pretrained_path=pretrained_path)<jupyter_output><empty_output><jupyter_text>Prediction  Choose existing midi file as a starting point<jupyter_code>midi_files = get_files(midi_path, recurse=True, extensions='.mid'); midi_files[:4]
idx = 1
f = midi_files[idx]; f<jupyter_output><empty_output><jupyter_text>NextWord/AutocompleteTrim the song to only a few notes. Model will use these notes a seed and continue the idea<jupyter_code>cutoff_beat = 10

item = MusicItem.from_file(f, data.vocab)
seed_item = item.trim_to_beat(cutoff_beat)
seed_item.show()
# seed_item.play()
play_wav(seed_item.stream)<jupyter_output><empty_output><jupyter_text>Use seed to predict next sequence<jupyter_code>pred, full = learn.predict(seed_item, n_words=400, temperatures=(1.1,0.4), min_bars=12, top_k=24, top_p=0.7)
pred.show()
play_wav(pred.stream)
full_song = seed_item.append(pred); full_song.show()
play_wav(full_song.stream)<jupyter_output><empty_output><jupyter_text>Add More Randomness to prediction<jupyter_code>note_temp = 1.4 # Determines amount of variation in note pitches
dur_temp = 0.8 # Amount of randomness in rhythm
top_k = 30
pred, full = learn.predict(seed_item, n_words=400, temperatures=(note_temp, dur_temp), min_bars=12, top_k=top_k, top_p=0.7)
pred.show()
play_wav(pred.stream)<jupyter_output><empty_output><jupyter_text>Pop Music Theory According to hooktheory, the most popular chord progression is I-V-vi-IV  https://www.hooktheory.com/theorytab/common-chord-progressions<jupyter_code># Let's create a partial progression I-V-vi
p = music21.stream.Part()
p.append(music21.chord.Chord('C4 E4 G4', type='half')) # I
p.append(music21.chord.Chord('G3 B3 D4', type='half')) # V
p.append(music21.chord.Chord('A3 C4 E4', type='half')) # vi
s = music21.stream.Score([p])
chord_item = MusicItem.from_stream(s, vocab)
chord_item.show()
temperaturs = (0.5,0.5) # Let's lower the note randomness for this test
pred, full = learn.predict(chord_item, n_words=10, temperatures=(0.5,0.5))
full.show()
# Predicted chords - IV
play_wav(pred.stream)
# Full sequence
chord_item.append(pred).show()<jupyter_output><empty_output><jupyter_text>Looks like it predicted the most popular progression!  Predict without a starting sequence<jupyter_code>empty_item = MusicItem.empty(vocab)
pred, full = learn.predict(empty_item, n_words=200)
pred.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup