tianhk commited on
Commit
d56142e
·
1 Parent(s): 6507e86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -8
app.py CHANGED
@@ -1,12 +1,37 @@
1
  import gradio as gr
2
  import os
3
- from pathlib import Path
 
 
 
4
 
5
- os.system("midi_ddsp_download_model_weights")
 
6
 
7
- def inference(audio):
8
- os.system("midi_ddsp_synthesize --midi_path "+audio.name)
9
- return Path(audio.name).stem+"/0_violin.wav"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  # title = "Midi-DDSP"
12
  # 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."
@@ -14,8 +39,12 @@ def inference(audio):
14
  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>"
15
 
16
  gr.Interface(
17
- fn = inference,
18
- inputs = gr.File(type="file", label="Input"),
 
 
 
 
19
  outputs = "audio",
20
  article = article,
21
- ).launch()
 
1
  import gradio as gr
2
  import os
3
+ # from pathlib import Path
4
+ from midi_ddsp.midi_ddsp_synthesize import load_pretrained_model
5
+ from midi_ddsp.utils.midi_synthesis_utils import synthesize_mono_midi
6
+ from midi_ddsp.data_handling.instrument_name_utils import INST_NAME_TO_ID_DICT, INST_NAME_LIST
7
 
8
+ os.system('midi_ddsp_download_model_weights')
9
+ synthesis_generator, expression_generator = load_pretrained_model()
10
 
11
+ def midi_ddsp_synth(instrument, midi_file):
12
+ '''
13
+ Paras:
14
+ midi_file: the directory of the midi file
15
+ instrument: instrument type, choose from 'violin', 'viola', 'cello', 'double bass',
16
+ 'flute', 'oboe', 'clarinet', 'saxophone',
17
+ 'bassoon', 'trumpet', 'horn', 'trombone', 'tuba'
18
+ Returns:
19
+ fs: the sampling rate
20
+ x: the audio data
21
+ '''
22
+ if not (os.path.isfile(midi_file) and midi_file[-4:] == '.mid'):
23
+ raise FileNotFoundError('Error: not a valid midi file')
24
+ if instrument not in INST_NAME_LIST:
25
+ raise ValueError('Error: not an available instrument type')
26
+ instrument_id = INST_NAME_TO_ID_DICT[instrument]
27
+ sample_rate = 16000
28
+ 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)
29
+ x = midi_audio[0].numpy()
30
+ return sample_rate, x
31
+
32
+ # def inference(audio):
33
+ # os.system("midi_ddsp_synthesize --midi_path "+audio.name)
34
+ # return Path(audio.name).stem+"/0_violin.wav"
35
 
36
  # title = "Midi-DDSP"
37
  # 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."
 
39
  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>"
40
 
41
  gr.Interface(
42
+ fn = midi_ddsp_synth,
43
+ inputs = [
44
+ gr.inputs.Textbox(
45
+ lines=1, placeholder=None, default='', label='instrument name'),
46
+ gr.File(type="file", label="Input")
47
+ ],
48
  outputs = "audio",
49
  article = article,
50
+ ).launch(debug=True)