tianhk's picture
Update app.py
d56142e
raw
history blame
2.41 kB
import gradio as gr
import os
# from pathlib import Path
from midi_ddsp.midi_ddsp_synthesize import load_pretrained_model
from midi_ddsp.utils.midi_synthesis_utils import synthesize_mono_midi
from midi_ddsp.data_handling.instrument_name_utils import INST_NAME_TO_ID_DICT, INST_NAME_LIST
os.system('midi_ddsp_download_model_weights')
synthesis_generator, expression_generator = load_pretrained_model()
def midi_ddsp_synth(instrument, midi_file):
'''
Paras:
midi_file: the directory of the midi file
instrument: instrument type, choose from 'violin', 'viola', 'cello', 'double bass',
'flute', 'oboe', 'clarinet', 'saxophone',
'bassoon', 'trumpet', 'horn', 'trombone', 'tuba'
Returns:
fs: the sampling rate
x: the audio data
'''
if not (os.path.isfile(midi_file) and midi_file[-4:] == '.mid'):
raise FileNotFoundError('Error: not a valid midi file')
if instrument not in INST_NAME_LIST:
raise ValueError('Error: not an available instrument type')
instrument_id = INST_NAME_TO_ID_DICT[instrument]
sample_rate = 16000
midi_audio, midi_control_params, midi_synth_params, conditioning_df = synthesize_mono_midi(synthesis_generator, expression_generator, midi_file, instrument_id, output_dir=None, pitch_offset=0, speed_rate=1)
x = midi_audio[0].numpy()
return sample_rate, x
# def inference(audio):
# os.system("midi_ddsp_synthesize --midi_path "+audio.name)
# return Path(audio.name).stem+"/0_violin.wav"
# title = "Midi-DDSP"
# description = "Gradio demo for MIDI-DDSP: Detailed Control of Musical Performance via Hierarchical Modeling. To use it, simply upload your midi file, or click one of the examples to load them. Read more at the links below."
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2112.09312' target='_blank'>MIDI-DDSP: Detailed Control of Musical Performance via Hierarchical Modeling</a> | <a href='https://github.com/magenta/midi-ddsp' target='_blank'>Github Repo</a></p>"
gr.Interface(
fn = midi_ddsp_synth,
inputs = [
gr.inputs.Textbox(
lines=1, placeholder=None, default='', label='instrument name'),
gr.File(type="file", label="Input")
],
outputs = "audio",
article = article,
).launch(debug=True)