hayaton0005 commited on
Commit
9bdb002
·
verified ·
1 Parent(s): d4b96ca

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +29 -25
  2. requirements.txt +1 -2
app.py CHANGED
@@ -1,32 +1,36 @@
1
  import gradio as gr
2
  import os
 
 
 
3
  from infer import infer_midi_from_wav
4
 
5
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
6
 
7
- # 推論関数(MIDI生成のみ)
8
- def transcribe(audio_path):
9
- midi_path = infer_midi_from_wav(audio_path)
10
- return midi_path
11
-
12
- # HTML再生用(replay.htmlを読み込む)
13
- replay_html = open(os.path.join(BASE_DIR, "replay.html")).read()
14
-
15
- # Gradio インターフェース
16
- with gr.Blocks() as demo:
17
- gr.Markdown("## 鼻歌からのMIDI変換デモ")
18
- gr.Markdown("録音した音声をMIDIに変換し、ブラウザで再生できます。")
19
-
20
- with gr.Row():
21
- audio_input = gr.Audio(type="filepath", label="マイク録音")
22
- midi_output = gr.File(label="変換されたMIDI")
23
 
24
- run_btn = gr.Button("▶️ 変換")
25
-
26
- # MIDI変換ボタンを押すとMIDIファイルを出力
27
- run_btn.click(fn=transcribe, inputs=audio_input, outputs=midi_output)
28
-
29
- # HTMLによるMIDI再生UI
30
- gr.HTML(replay_html)
31
-
32
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import os
3
+ import soundfile as sf
4
+ import numpy as np
5
+ import pretty_midi
6
  from infer import infer_midi_from_wav
7
 
8
  BASE_DIR = os.path.dirname(os.path.abspath(__file__))
9
 
10
+ # MIDI → WAV(サイン波ベースの簡易合成器)
11
+ def convert_midi_to_wav_sine(midi_path):
12
+ pm = pretty_midi.PrettyMIDI(midi_path)
13
+ waveform = pm.synthesize() # デフォルトは fs=22050Hz
14
+ wav_path = os.path.join(BASE_DIR, "synth_output.wav")
15
+ sf.write(wav_path, waveform, samplerate=22050)
16
+ return wav_path
 
 
 
 
 
 
 
 
 
17
 
18
+ # 推論関数(録音 MIDI → WAV → 再生用)
19
+ def transcribe_and_play(audio_path):
20
+ midi_path = infer_midi_from_wav(audio_path)
21
+ wav_output_path = convert_midi_to_wav_sine(midi_path)
22
+ return wav_output_path, midi_path
23
+
24
+ # Gradio UI
25
+ interface = gr.Interface(
26
+ fn=transcribe_and_play,
27
+ inputs=gr.Audio(type="filepath", label="マイク録音"),
28
+ outputs=[
29
+ gr.Audio(label="ピアノ音で再生"),
30
+ gr.File(label="MIDIダウンロード")
31
+ ],
32
+ title="鼻歌からのMIDI変換デモ",
33
+ description="録音した音声をMIDIに変換し、自動で再生します。"
34
+ )
35
+
36
+ interface.launch()
requirements.txt CHANGED
@@ -17,8 +17,7 @@ tqdm
17
  librosa
18
  soundfile
19
  gradio
20
- fluidsynth
21
-
22
 
23
 
24
  # MIDI・音楽処理
 
17
  librosa
18
  soundfile
19
  gradio
20
+ scipy
 
21
 
22
 
23
  # MIDI・音楽処理