### Generate Music Samples with Pop Music Transformer (Python) Source: https://github.com/yatingmusic/remi/blob/master/README.md This Python script demonstrates how to use the PopMusicTransformer model to generate music. It shows examples of generating music from scratch and generating continuations from a given MIDI prompt. Dependencies include the REMI library and TensorFlow. The function takes parameters like checkpoint path, generation length, temperature, top-k sampling, and output path. ```python from model import PopMusicTransformer import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' def main(): # declare model model = PopMusicTransformer( checkpoint='REMI-tempo-checkpoint', is_training=False) # generate from scratch model.generate( n_target_bar=16, temperature=1.2, topk=5, output_path='./result/from_scratch.midi', prompt=None) # generate continuation model.generate( n_target_bar=16, temperature=1.2, topk=5, output_path='./result/continuation.midi', prompt='./data/evaluation/000.midi') # close model model.close() if __name__ == '__main__': main() ``` -------------------------------- ### Print Tempo Items Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Prints the first 10 'Item' objects representing tempo changes. Each item details the tempo's start tick and its corresponding BPM value. ```python print(*tempo_items[:10], sep='\n') ``` -------------------------------- ### Print Note Items Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Prints the 'Item' objects representing notes read from the MIDI file. Each item shows its name, start time, end time, velocity, and pitch. ```python print(*note_items, sep='\n') ``` -------------------------------- ### Read and Process MIDI Files Source: https://context7.com/yatingmusic/remi/llms.txt Loads MIDI files and extracts structured note and tempo information. The 'utils.read_items' function parses a MIDI file into lists of 'Note' and 'Tempo' items. Notes contain start, end, velocity, and pitch, while tempo items indicate BPM changes. Includes functionality to quantize notes to a specified grid. ```python from utils import read_items, quantize_items, Item import numpy as np # Read MIDI file (assumes single instrument track) note_items, tempo_items = read_items('song.midi') # Each note_item contains: # - name: 'Note' # - start: start tick # - end: end tick # - velocity: MIDI velocity (0-127) # - pitch: MIDI pitch (0-127) print(f"Total notes: {len(note_items)}") print(f"First note: {note_items[0]}") # Output: Item(name=Note, start=0, end=480, velocity=64, pitch=60) # Quantize notes to 120-tick grid (eighth notes at 480 ticks per beat) quantized_notes = quantize_items(note_items, ticks=120) # Tempo items contain tempo changes at each beat for tempo_item in tempo_items[:5]: print(f"Tick {tempo_item.start}: {tempo_item.pitch} BPM") ``` -------------------------------- ### Print Notes from MIDI Object Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Prints all notes from the first instrument in the parsed MIDI object. Each note is represented with its start time, end time, pitch, and velocity. ```python print(*midi_obj.instruments[0].notes, sep='\n') ``` -------------------------------- ### Quantize Note Items using utils Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Applies quantization to the 'Item' objects representing notes using the utils.quantize_items function. This adjusts note start and end times to align with a defined grid, often for rhythmic accuracy. ```python note_items = utils.quantize_items(note_items) ``` -------------------------------- ### Collect MIDI Files and Prepare Training Data (Python) Source: https://context7.com/yatingmusic/remi/llms.txt This snippet demonstrates how to collect all MIDI files from a specified directory using glob and then prepare this data for training a REMI model. It involves extracting events, converting them to word indices, segmenting into fixed-length sequences, and batching. ```python import glob import numpy as np # Assuming 'model' is an initialized REMI model object # model = ... # Collect all MIDI files from directory midi_paths = glob.glob('./my_dataset/**/*.midi', recursive=True) print(f"Found {len(midi_paths)} MIDI files") # Prepare training data # - Extracts events from each MIDI file # - Converts events to word indices # - Segments into sequences of length 512 (x_len) # - Groups into batches with memory segments training_data = model.prepare_data(midi_paths=midi_paths) print(f"Created {len(training_data)} training segments") # Each segment shape: [group_size, 2, x_len] # group_size=5, 2 for (input, target), x_len=512 # Save prepared data for later use np.save('training_data.npy', training_data) # Load and use saved data loaded_data = np.load('training_data.npy') model.finetune( training_data=loaded_data, output_checkpoint_folder='my-model-checkpoint' ) model.close() ``` -------------------------------- ### Import Libraries Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Imports the necessary libraries, miditoolkit for MIDI processing and utils for custom utility functions. ```python import miditoolkit import utils ``` -------------------------------- ### Prepare Custom Training Data for REMI Model Source: https://context7.com/yatingmusic/remi/llms.txt Converts a collection of MIDI files into training-ready data segments for model fine-tuning using the `PopMusicTransformer` in training mode. This involves iterating through multiple MIDI files to prepare them as input for the model's training process. ```python from model import PopMusicTransformer from glob import glob import numpy as np # Initialize model in training mode model = PopMusicTransformer( checkpoint='REMI-tempo-checkpoint', is_training=True ) ``` -------------------------------- ### Generate Music Continuation with PopMusicTransformer Source: https://context7.com/yatingmusic/remi/llms.txt Continues an existing MIDI file by generating additional bars based on the prompt's style and pattern using the PopMusicTransformer model. It utilizes a chord-aware checkpoint and requires TensorFlow 1.14. Takes parameters for the number of bars, temperature, top-k sampling, output path, and the prompt MIDI file. ```python from model import PopMusicTransformer import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Initialize model model = PopMusicTransformer( checkpoint='REMI-tempo-chord-checkpoint', # Use chord-aware checkpoint is_training=False ) # Generate continuation from existing MIDI file model.generate( n_target_bar=16, temperature=1.2, topk=5, output_path='./result/continuation.midi', prompt='./data/evaluation/000.midi' # Path to prompt MIDI file ) model.close() ``` -------------------------------- ### Generate Music Continuation Source: https://context7.com/yatingmusic/remi/llms.txt Continues an existing MIDI file by generating additional bars based on the prompt's style and pattern. This allows for extending existing musical ideas. ```APIDOC ## Generate Music Continuation ### Description Continue an existing MIDI file by generating additional bars based on the prompt's style and pattern. ### Method Not directly an HTTP API, but a Python function call. ### Endpoint `PopMusicTransformer.generate()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from model import PopMusicTransformer import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Initialize model model = PopMusicTransformer( checkpoint='REMI-tempo-chord-checkpoint', # Use chord-aware checkpoint is_training=False ) # Generate continuation from existing MIDI file model.generate( n_target_bar=16, temperature=1.2, topk=5, output_path='./result/continuation.midi', prompt='./data/evaluation/000.midi' # Path to prompt MIDI file ) model.close() ``` ### Response #### Success Response (200) Generates a MIDI file at the specified `output_path`, continuing the provided `prompt` MIDI. #### Response Example (A MIDI file named `continuation.midi` is created.) ``` -------------------------------- ### Generate Music from Scratch Source: https://context7.com/yatingmusic/remi/llms.txt Generates a new piano music composition without any input prompt using the pre-trained PopMusicTransformer model. This is useful for creating entirely novel musical pieces. ```APIDOC ## Generate Music from Scratch ### Description Generate new piano music composition without any input prompt using the pre-trained PopMusicTransformer model. ### Method Not directly an HTTP API, but a Python function call. ### Endpoint `PopMusicTransformer.generate()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from model import PopMusicTransformer import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Initialize model with pre-trained checkpoint model = PopMusicTransformer( checkpoint='REMI-tempo-checkpoint', is_training=False ) # Generate 16 bars of music from scratch model.generate( n_target_bar=16, # Number of bars to generate temperature=1.2, # Controls randomness (higher = more random) topk=5, # Top-k sampling parameter output_path='./result/from_scratch.midi', prompt=None # None = generate from scratch ) model.close() ``` ### Response #### Success Response (200) Generates a MIDI file at the specified `output_path`. #### Response Example (A MIDI file named `from_scratch.midi` is created.) ``` -------------------------------- ### Generate Music from Scratch using PopMusicTransformer Source: https://context7.com/yatingmusic/remi/llms.txt Generates a new piano music composition without an input prompt using the pre-trained PopMusicTransformer model. Requires TensorFlow 1.14 and specifies CUDA device. Outputs a MIDI file and takes parameters for number of bars, temperature, and top-k sampling. ```python from model import PopMusicTransformer import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Initialize model with pre-trained checkpoint model = PopMusicTransformer( checkpoint='REMI-tempo-checkpoint', is_training=False ) # Generate 16 bars of music from scratch model.generate( n_target_bar=16, # Number of bars to generate temperature=1.2, # Controls randomness (higher = more random) topk=5, # Top-k sampling parameter output_path='./result/from_scratch.midi', prompt=None # None = generate from scratch ) model.close() ``` -------------------------------- ### Fine-tune PopMusicTransformer on Custom MIDI Data Source: https://context7.com/yatingmusic/remi/llms.txt Adapts the pre-trained PopMusicTransformer model to a custom musical style by fine-tuning on user-provided MIDI files. It requires TensorFlow 1.14 and loads custom MIDI data using glob. The process involves preparing data, creating an output folder for checkpoints, and then initiating the fine-tuning process which trains for up to 200 epochs. ```python from model import PopMusicTransformer from glob import glob import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Initialize model in training mode model = PopMusicTransformer( checkpoint='REMI-tempo-checkpoint', is_training=True ) # Load your custom MIDI files midi_paths = glob('./my_music_folder/*.midi') # Prepare training data from MIDI files training_data = model.prepare_data(midi_paths=midi_paths) # Create output folder for checkpoints output_checkpoint_folder = 'my-custom-model' if not os.path.exists(output_checkpoint_folder): os.mkdir(output_checkpoint_folder) # Fine-tune the model (trains for up to 200 epochs) model.finetune( training_data=training_data, output_checkpoint_folder=output_checkpoint_folder ) model.close() # Note: After fine-tuning, rename your chosen checkpoint to "model" # and copy dictionary.pkl from the original checkpoint folder ``` -------------------------------- ### BibTeX Citation for Pop Music Transformer Paper Source: https://github.com/yatingmusic/remi/blob/master/README.md This is the BibTeX entry for the 'Pop Music Transformer: Beat-Based Modeling and Generation of Expressive Pop Piano Compositions' paper. It contains author information, title, publication year, publisher, and other relevant details for academic citation. ```bibtex @inproceedings{10.1145/3394171.3413671, author = {Huang, Yu-Siang and Yang, Yi-Hsuan}, title = {Pop Music Transformer: Beat-Based Modeling and Generation of Expressive Pop Piano Compositions}, year = {2020}, isbn = {9781450379885}, publisher = {Association for Computing Machinery}, address = {New York, NY, USA}, url = {https://doi.org/10.1145/3394171.3413671}, doi = {10.1145/3394171.3413671}, pages = {1180–1188}, numpages = {9}, location = {Seattle, WA, USA}, series = {MM '20} } ``` -------------------------------- ### Convert REMI Events to MIDI File Source: https://context7.com/yatingmusic/remi/llms.txt Generates playable MIDI files from REMI event sequences or model output. It supports creating new MIDI files from scratch or appending generated events to an existing MIDI file as a continuation. Requires the 'utils' module and a 'dictionary.pkl' file for event-to-word conversion. ```python from utils import write_midi import pickle # Load dictionary for word-to-event conversion checkpoint = 'REMI-tempo-checkpoint' event2word, word2event = pickle.load(open(f'{checkpoint}/dictionary.pkl', 'rb')) # Example: words from model generation # words is a list of integer indices representing events words = [15, 234, 567, 123, 890, ...] # Model-generated word sequence # Write MIDI from scratch write_midi( words=words, word2event=word2event, output_path='output.midi', prompt_path=None # None = create new MIDI ) # Write MIDI continuation (appends to existing prompt) write_midi( words=words, # Only the newly generated words word2event=word2event, output_path='continuation_output.midi', prompt_path='prompt.midi' # Append to this MIDI file ) ``` -------------------------------- ### Extract Chord Progressions from MIDI using chord_recognition Source: https://context7.com/yatingmusic/remi/llms.txt Automatically detects and extracts chord progressions from MIDI note sequences using chord recognition algorithms. It initializes a MIDIChord model, reads MIDI files, quantizes note items to a grid, and then extracts chord progressions. The output is a list of tuples, each representing a time interval and its corresponding chord name. ```python import chord_recognition from utils import read_items, quantize_items # Initialize chord recognition model chord_detector = chord_recognition.MIDIChord() # Read MIDI file and extract note items note_items, tempo_items = read_items('input.midi') # Quantize notes to align with grid note_items = quantize_items(note_items, ticks=120) # Extract chord progressions # Returns list of [start_tick, end_tick, chord_name] tuples chords = chord_detector.extract(notes=note_items) # Example output format: # [[0, 1920, 'C:maj'], [1920, 3840, 'G:maj/B'], [3840, 5760, 'Am:min']] for start, end, chord in chords: print(f"Time {start}-{end}: {chord}") ``` -------------------------------- ### Temperature Sampling for Music Generation Source: https://context7.com/yatingmusic/remi/llms.txt Controls the randomness and creativity of music generation using temperature-controlled sampling. The `PopMusicTransformer` model's `generate` method accepts a 'temperature' parameter to influence the output. Lower temperatures produce more predictable music, while higher temperatures lead to more diverse and experimental results. ```python import numpy as np def generate_with_different_temperatures(): from model import PopMusicTransformer model = PopMusicTransformer( checkpoint='REMI-tempo-checkpoint', is_training=False ) # Conservative generation (low temperature = more predictable) model.generate( n_target_bar=8, temperature=0.8, # Low temperature topk=3, # Limited choices output_path='./result/conservative.midi', prompt=None ) # Balanced generation model.generate( n_target_bar=8, temperature=1.2, # Medium temperature topk=5, output_path='./result/balanced.midi', prompt=None ) # Creative generation (high temperature = more random) model.generate( n_target_bar=8, temperature=1.8, # High temperature topk=10, # More choices output_path='./result/creative.midi', prompt=None ) model.close() # Temperature controls the softmax distribution: # - Lower (0.5-1.0): More conservative, follows training patterns closely # - Medium (1.0-1.5): Balanced between predictability and creativity # - Higher (1.5-2.0): More experimental, diverse outputs generate_with_different_temperatures() ``` -------------------------------- ### Print Tempo Changes from MIDI Object Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Prints the first 10 tempo changes found in the MIDI file. Each tempo change includes the BPM and the tick at which it occurs. ```python print(*midi_obj.tempo_changes[:10], sep='\n') ``` -------------------------------- ### Read MIDI to Item Objects using utils Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Reads a MIDI file and converts its notes and tempo changes into a list of 'Item' objects using the custom utils.read_items function. This provides a unified format for further processing. ```python note_items, tempo_items = utils.read_items('./data/evaluation/000.midi') ``` -------------------------------- ### Fine-tune Model on Custom Data Source: https://context7.com/yatingmusic/remi/llms.txt Adapts the pre-trained model to your own musical style by fine-tuning it on custom MIDI files. This allows for personalized music generation. ```APIDOC ## Fine-tune Model on Custom Data ### Description Adapt the pre-trained model to your own musical style by fine-tuning on custom MIDI files. ### Method Not directly an HTTP API, but a Python function call. ### Endpoint `PopMusicTransformer.finetune()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from model import PopMusicTransformer from glob import glob import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' # Initialize model in training mode model = PopMusicTransformer( checkpoint='REMI-tempo-checkpoint', is_training=True ) # Load your custom MIDI files midi_paths = glob('./my_music_folder/*.midi') # Prepare training data from MIDI files training_data = model.prepare_data(midi_paths=midi_paths) # Create output folder for checkpoints output_checkpoint_folder = 'my-custom-model' if not os.path.exists(output_checkpoint_folder): os.mkdir(output_checkpoint_folder) # Fine-tune the model (trains for up to 200 epochs) model.finetune( training_data=training_data, output_checkpoint_folder=output_checkpoint_folder ) model.close() # Note: After fine-tuning, rename your chosen checkpoint to "model" # and copy dictionary.pkl from the original checkpoint folder ``` ### Response #### Success Response (200) Saves fine-tuned model checkpoints to the specified `output_checkpoint_folder`. #### Response Example (Checkpoints are saved in the `./my-custom-model/` directory.) ``` -------------------------------- ### Convert MIDI to REMI Events using utils Source: https://context7.com/yatingmusic/remi/llms.txt Transforms MIDI files into REMI event sequences, suitable for model input or analysis. This utility function reads MIDI, quantizes notes, extracts chords and tempo information, and then groups these elements before converting them into REMI event sequences. ```python from utils import read_items, quantize_items, extract_chords, group_items, item2event # Read MIDI file note_items, tempo_items = read_items('input.midi') # Quantize notes to grid note_items = quantize_items(note_items) # Calculate max time max_time = note_items[-1].end # Extract chords (optional, for chord-aware model) chord_items = extract_chords(note_items) # Combine all musical elements items = chord_items + tempo_items + note_items # Group items by bar groups = group_items(items, max_time) # Convert to REMI events events = item2event(groups) ``` -------------------------------- ### Chord Recognition and Analysis in MIDI Source: https://context7.com/yatingmusic/remi/llms.txt Analyzes MIDI files to detect chord progressions and their qualities, assigning a score to each detected chord. It utilizes the `chord_recognition.MIDIChord` class and requires notes in an 'Item' format. The output provides chord symbols (e.g., C:maj, G:dom/B) and their durations in beats. ```python import chord_recognition import miditoolkit import numpy as np # Initialize chord detector detECTOR = chord_recognition.MIDIChord() # Available chord qualities detected: # - maj: Major chords (root, major third) # - min: Minor chords (root, minor third) # - dim: Diminished chords # - aug: Augmented chords # - dom: Dominant seventh chords # Load MIDI file midi_obj = miditoolkit.midi.parser.MidiFile('song.midi') notes = midi_obj.instruments[0].notes # Convert notes to items format from utils import Item note_items = [ Item(name='Note', start=n.start, end=n.end, velocity=n.velocity, pitch=n.pitch) for n in notes ] # Extract chords with scoring chords = detector.extract(notes=note_items) # Process detected chords for start_tick, end_tick, chord_symbol in chords: duration_beats = (end_tick - start_tick) / 480 print(f"{chord_symbol}: {duration_beats:.1f} beats") # Example output: # C:maj: 4.0 beats # F:maj: 2.0 beats # G:dom/B: 2.0 beats (G dominant 7th with B bass) # Am:min: 4.0 beats ``` -------------------------------- ### Group Items by Time using Python Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Combines chord, tempo, and note items into a single list and groups them based on the maximum time. The `group_items` utility is used for this purpose. The grouped items are then printed to the console, with each group separated by a blank line. ```python items = chord_items + tempo_items + note_items max_time = note_items[-1].end groups = utils.group_items(items, max_time) ``` ```python for g in groups: print(*g, sep='\n') print() ``` -------------------------------- ### Extract Chord Progressions from MIDI Source: https://context7.com/yatingmusic/remi/llms.txt Automatically detects and extracts chord progressions from MIDI note sequences using chord recognition algorithms. This is useful for music analysis and understanding harmonic structure. ```APIDOC ## Extract Chord Progressions from MIDI ### Description Automatically detect and extract chord progressions from MIDI note sequences using chord recognition algorithms. ### Method Not directly an HTTP API, but Python function calls. ### Endpoint `chord_recognition.MIDIChord().extract()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import chord_recognition from utils import read_items, quantize_items # Initialize chord recognition model chord_detector = chord_recognition.MIDIChord() # Read MIDI file and extract note items note_items, tempo_items = read_items('input.midi') # Quantize notes to align with grid note_items = quantize_items(note_items, ticks=120) # Extract chord progressions # Returns list of [start_tick, end_tick, chord_name] tuples chords = chord_detector.extract(notes=note_items) # Example output format: # [[0, 1920, 'C:maj'], [1920, 3840, 'G:maj/B'], [3840, 5760, 'Am:min']] for start, end, chord in chords: print(f"Time {start}-{end}: {chord}") ``` ### Response #### Success Response (200) Returns a list of tuples, where each tuple represents a chord progression with start tick, end tick, and chord name. #### Response Example ``` Time 0-1920: C:maj Time 1920-3840: G:maj/B Time 3840-5760: Am:min ``` ``` -------------------------------- ### Parse MIDI File with miditoolkit Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Parses a MIDI file into a MidiFile object using the miditoolkit library. This object contains information about instruments and notes within the MIDI file. ```python midi_obj = miditoolkit.midi.parser.MidiFile('./data/evaluation/000.midi') ``` -------------------------------- ### Convert Items to Events using Python Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Transforms grouped items (chords, tempos, notes) into a structured event format. The `item2event` utility function handles this conversion. The first 30 events of the resulting list are then printed to the console, each on a new line. ```python events = utils.item2event(groups) ``` ```python print(*events[:30], sep='\n') ``` -------------------------------- ### Convert MIDI to REMI Events Source: https://context7.com/yatingmusic/remi/llms.txt Transforms MIDI files into REMI event sequences for model input or analysis. This utility is essential for preparing data for the REMI model. ```APIDOC ## Convert MIDI to REMI Events ### Description Transform MIDI files into REMI event sequences for model input or analysis. ### Method Not directly an HTTP API, but Python function calls. ### Endpoint `utils.item2event()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from utils import read_items, quantize_items, extract_chords, group_items, item2event # Read MIDI file note_items, tempo_items = read_items('input.midi') # Quantize notes to grid note_items = quantize_items(note_items) # Calculate max time max_time = note_items[-1].end # Extract chords (optional, for chord-aware model) chord_items = extract_chords(note_items) # Combine all musical elements items = chord_items + tempo_items + note_items # Group items by bar groups = group_items(items, max_time) # Convert to REMI events events = item2event(groups) ``` ### Response #### Success Response (200) Returns a list of REMI events, representing the musical content of the MIDI file in a format suitable for the REMI model. #### Response Example (A list of REMI event objects/dictionaries.) ``` -------------------------------- ### Extract Chords using Python Source: https://github.com/yatingmusic/remi/blob/master/midi2remi.ipynb Extracts chord information from a list of note items. It utilizes the `extract_chords` utility function. The output is a list of chord items, which are then printed to the console, formatted with newlines. ```python chord_items = utils.extract_chords(note_items) ``` ```python print(*chord_items, sep='\n') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.