mrfakename commited on
Commit
84ae87e
·
verified ·
1 Parent(s): 189a6bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -0
app.py CHANGED
@@ -8,6 +8,17 @@ from glob import glob
8
  import librosa
9
  from midi2audio import FluidSynth
10
  fs = FluidSynth()
 
 
 
 
 
 
 
 
 
 
 
11
  def gen(piano_only, length):
12
  midi = ''
13
  for item in musicgen(piano_only=piano_only, length=length):
@@ -20,6 +31,7 @@ def gen(piano_only, length):
20
  with tempfile.NamedTemporaryFile(suffix='.midi', delete=False) as tmp, tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as aud:
21
  mid.save(tmp.name)
22
  fs.midi_to_audio(tmp.name, aud.name)
 
23
  yield midi, tmp.name, aud.name
24
  with gr.Blocks() as demo:
25
  gr.Markdown("# RWKV 4 Music (MIDI)\n\nThis demo uses the RWKV 4 MIDI model available [here](https://huggingface.co/BlinkDL/rwkv-4-music/blob/main/RWKV-4-MIDI-560M-v1-20230717-ctx4096.pth). Details may be found [here](https://huggingface.co/BlinkDL/rwkv-4-music). The music generation code may be found [here](https://github.com/BlinkDL/ChatRWKV/tree/main/music). The MIDI Tokenizer may be found [here](https://github.com/briansemrau/MIDI-LLM-tokenizer).\n\nNot sure how to play MIDI files? I recommend using the open source [VLC Media Player](https://www.videolan.org/vlc/) with can play MIDI files using FluidSynth.")
 
8
  import librosa
9
  from midi2audio import FluidSynth
10
  fs = FluidSynth()
11
+ def trim_silence(filename, threshold=0.1, duration=0.5):
12
+ y, sr = librosa.load(filename, sr=None)
13
+ silent_frames = librosa.effects.split(y, top_db=threshold)
14
+ total_duration = librosa.get_duration(y=y, sr=sr)
15
+ duration_samples = int(duration * sr)
16
+ trimmed_audio = []
17
+ for start, end in silent_frames:
18
+ if end - start > duration_samples:
19
+ trimmed_audio.extend(y[start:end])
20
+ trimmed_audio = np.array(trimmed_audio)
21
+ return trimmed_audio, sr
22
  def gen(piano_only, length):
23
  midi = ''
24
  for item in musicgen(piano_only=piano_only, length=length):
 
31
  with tempfile.NamedTemporaryFile(suffix='.midi', delete=False) as tmp, tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as aud:
32
  mid.save(tmp.name)
33
  fs.midi_to_audio(tmp.name, aud.name)
34
+ trim_silence(aud.name)
35
  yield midi, tmp.name, aud.name
36
  with gr.Blocks() as demo:
37
  gr.Markdown("# RWKV 4 Music (MIDI)\n\nThis demo uses the RWKV 4 MIDI model available [here](https://huggingface.co/BlinkDL/rwkv-4-music/blob/main/RWKV-4-MIDI-560M-v1-20230717-ctx4096.pth). Details may be found [here](https://huggingface.co/BlinkDL/rwkv-4-music). The music generation code may be found [here](https://github.com/BlinkDL/ChatRWKV/tree/main/music). The MIDI Tokenizer may be found [here](https://github.com/briansemrau/MIDI-LLM-tokenizer).\n\nNot sure how to play MIDI files? I recommend using the open source [VLC Media Player](https://www.videolan.org/vlc/) with can play MIDI files using FluidSynth.")