# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + id="cXM3RFspNBRb"
# adapted from https://colab.research.google.com/notebooks/magenta/hello_magenta/hello_magenta.ipynb﻿

# + id="lsFoCLATNBRh" outputId="195cde38-a10b-4a89-9e36-f1cf392e9be2" colab={"base_uri": "https://localhost:8080/", "height": 700}
# !apt-get update -qq && apt-get install -qq libfluidsynth1 fluid-soundfont-gm build-essential libasound2-dev libjack-dev
# !pip install -qU pyfluidsynth pretty_midi

# !pip install -qU magenta

# + id="N0NmZPB-NBRj"
# only needed for Colab environment:
import ctypes.util
orig_ctypes_util_find_library = ctypes.util.find_library
def proxy_find_library(lib):
    if lib == 'fluidsynth':
        return 'libfluidsynth.so.1'
    else:
        return orig_ctypes_util_find_library(lib)
ctypes.util.find_library = proxy_find_library

# + id="NXeSOiqENBRm"
from note_seq.protobuf import music_pb2
import note_seq

twinkle_twinkle = music_pb2.NoteSequence()
twinkle_twinkle.notes.add(pitch=60, start_time=0.0, end_time=0.5, velocity=80)
twinkle_twinkle.notes.add(pitch=60, start_time=0.5, end_time=1.0, velocity=80)
twinkle_twinkle.notes.add(pitch=67, start_time=1.0, end_time=1.5, velocity=80)
twinkle_twinkle.notes.add(pitch=67, start_time=1.5, end_time=2.0, velocity=80)
twinkle_twinkle.notes.add(pitch=69, start_time=2.0, end_time=2.5, velocity=80)
twinkle_twinkle.notes.add(pitch=69, start_time=2.5, end_time=3.0, velocity=80)
twinkle_twinkle.notes.add(pitch=67, start_time=3.0, end_time=4.0, velocity=80)
twinkle_twinkle.notes.add(pitch=65, start_time=4.0, end_time=4.5, velocity=80)
twinkle_twinkle.notes.add(pitch=65, start_time=4.5, end_time=5.0, velocity=80)
twinkle_twinkle.notes.add(pitch=64, start_time=5.0, end_time=5.5, velocity=80)
twinkle_twinkle.notes.add(pitch=64, start_time=5.5, end_time=6.0, velocity=80)
twinkle_twinkle.notes.add(pitch=62, start_time=6.0, end_time=6.5, velocity=80)
twinkle_twinkle.notes.add(pitch=62, start_time=6.5, end_time=7.0, velocity=80)
twinkle_twinkle.notes.add(pitch=60, start_time=7.0, end_time=8.0, velocity=80) 
twinkle_twinkle.total_time = 8

twinkle_twinkle.tempos.add(qpm=60);

# + id="D_7VU4IXNBRp" outputId="4d4f25c9-b215-4e97-b78d-067f322b4aca" colab={"base_uri": "https://localhost:8080/", "height": 275}
note_seq.plot_sequence(twinkle_twinkle)
note_seq.play_sequence(twinkle_twinkle,synth=note_seq.fluidsynth)

# + id="aIrH4uy9NBRr" outputId="356896f5-22a8-42f8-e606-5428506249c1" colab={"base_uri": "https://localhost:8080/", "height": 68}
import os
from magenta.models.melody_rnn import melody_rnn_sequence_generator
from magenta.models.shared import sequence_generator_bundle
from note_seq.protobuf import generator_pb2
from note_seq.protobuf import music_pb2

basedir = '/content'  # set this to your gdrive location if you like

note_seq.notebook_utils.download_bundle('attention_rnn.mag', basedir)
bundle = sequence_generator_bundle.read_bundle_file(
    os.path.join(basedir, 'attention_rnn.mag')
)
generator_map = melody_rnn_sequence_generator.get_generator_map()
melody_rnn = generator_map['attention_rnn'](checkpoint=None, bundle=bundle)
melody_rnn.initialize()


# + id="NkOJYYItNBRu" outputId="ec390335-7408-45f7-8e0c-8796c461668e" colab={"base_uri": "https://localhost:8080/", "height": 34}
def get_options(input_sequence, num_steps=128, temperature=1.0):
    last_end_time = (max(n.end_time for n in input_sequence.notes)
                      if input_sequence.notes else 0)
    qpm = input_sequence.tempos[0].qpm 
    seconds_per_step = 60.0 / qpm / melody_rnn.steps_per_quarter
    total_seconds = num_steps * seconds_per_step

    generator_options = generator_pb2.GeneratorOptions()
    generator_options.args['temperature'].float_value = temperature
    generate_section = generator_options.generate_sections.add(
      start_time=last_end_time + seconds_per_step,
      end_time=total_seconds)
    return generator_options

sequence = melody_rnn.generate(twinkle_twinkle, get_options(twinkle_twinkle))

# + id="m6qhxa7pNBRx" outputId="c790625c-a151-4fd8-99ee-ed545b1a8b67" colab={"base_uri": "https://localhost:8080/", "height": 275}
note_seq.plot_sequence(sequence)
note_seq.play_sequence(sequence, synth=note_seq.fluidsynth)

# + id="_2AQlwVRNBR2"
note_seq.sequence_proto_to_midi_file(sequence, 'twinkle_continued.midi')

# + id="3gGD4l90NBR4" outputId="edcf4314-81e9-420c-fd2c-e955b1116ced" colab={"base_uri": "https://localhost:8080/", "height": 17}
from google.colab import files
files.download('twinkle_continued.midi')

# + id="rSJ1fDmpOuHX"


StarcoderdataJupyterScriptsDedupFiltered