Spaces:
Sleeping
Sleeping
| # ================================================================================================= | |
| # https://huggingface.co/spaces/projectlosangeles/MuseCraft-AlgoPOP | |
| # ================================================================================================= | |
| import os | |
| import time as reqtime | |
| import datetime | |
| from pytz import timezone | |
| import gradio as gr | |
| import TMIDIX | |
| from musicpy import algorithms, scale, write | |
| # ================================================================================================= | |
| def Generate_POP(song_length, | |
| song_semitone_key, | |
| song_scale, | |
| output_as_solo_piano | |
| ): | |
| print('=' * 70) | |
| print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
| start_time = reqtime.time() | |
| print('=' * 70) | |
| print('Requested settings:') | |
| print('-' * 70) | |
| print('Song length:', song_length) | |
| print('Song semitone key:', song_semitone_key) | |
| print('Song scale (1) Major / (0) Minor:', song_scale) | |
| print('Output as solo Piano:', output_as_solo_piano) | |
| print('-' * 70) | |
| #================================================================== | |
| print('=' * 70) | |
| print('MuseCraft AlgoPOP Generator') | |
| print('=' * 70) | |
| print('Generating...') | |
| pop_piece = algorithms.write_pop(scale('C', 'Major')) | |
| midi_data = write(pop_piece, save_as_file=False) | |
| midi_data.seek(0) | |
| raw_score = TMIDIX.midi2single_track_ms_score(midi_data.read()) | |
| escore = TMIDIX.advanced_score_processor(raw_score, | |
| return_enhanced_score_notes=True, | |
| apply_sustain=True | |
| )[0] | |
| output_score = TMIDIX.recalculate_score_timings(escore) | |
| if output_as_solo_piano: | |
| output_score = TMIDIX.solo_piano_escore_notes(output_score) | |
| output_midi = 'MuseCraft-AlgoPOP-Composition' | |
| SONG, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(output_score) | |
| detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(SONG, | |
| output_signature = 'MuseCraft AlgoPOP', | |
| output_file_name = output_midi, | |
| track_name='Project Los Angeles', | |
| list_of_MIDI_patches=patches | |
| ) | |
| output_midi = output_midi + '.mid' | |
| #======================================================== | |
| print('Done!') | |
| print('=' * 70) | |
| #======================================================== | |
| print('-' * 70) | |
| print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
| print('-' * 70) | |
| print('Req execution time:', (reqtime.time() - start_time), 'sec') | |
| return output_midi | |
| # ================================================================================================= | |
| if __name__ == "__main__": | |
| PDT = timezone('US/Pacific') | |
| print('=' * 70) | |
| print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) | |
| print('=' * 70) | |
| #=============================================================================== | |
| app = gr.Blocks() | |
| with app: | |
| gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>MuseCraft AlgoPOP</h1>") | |
| gr.Markdown("<h1 style='text-align: left; margin-bottom: 1rem'>Simple algorithmic POP music generator</h1>") | |
| gr.HTML(""" | |
| Check out <a href="https://github.com/MIDIAI/MuseCraft">MuseCraft project</a> on GitHub | |
| <p> | |
| <a href="https://huggingface.co/spaces/projectlosangeles/MuseCraft-AlgoPOP?duplicate=true"> | |
| <img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-md.svg" alt="Duplicate in Hugging Face"> | |
| </a> | |
| </p> | |
| """) | |
| gr.Markdown("## Select generation options") | |
| song_length = gr.Slider(10, 100, value=30, step=1, label="Song length") | |
| song_semitone_key = gr.Slider(0, 11, value=0, step=1, label="Song semitone key") | |
| song_scale = gr.Slider(0, 1, value=1, step=1, label="Song scale as (1) Major or as (0) Minor") | |
| output_as_solo_piano = gr.Checkbox(value=False, label="Output as solo Piano") | |
| run_btn = gr.Button("generate", variant="primary") | |
| gr.Markdown("## Generation results") | |
| output_midi = gr.File(label="Output MIDI file", file_types=[".mid"]) | |
| run_event = run_btn.click(Generate_POP, [ | |
| song_length, | |
| song_semitone_key, | |
| song_scale, | |
| output_as_solo_piano | |
| ], | |
| [output_midi] | |
| ) | |
| app.queue().launch() |