File size: 1,514 Bytes
e717139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import streamlit as st
import torch
from unidecode import unidecode
from musc.model import PretrainedModel
from unidecode import unidecode
import os
import sys
import torch
import json
#from yt_dlp import YoutubeDL
sys.path.append('MUSC_violin')
from MUSC_violin import musc

# Function to transcribe the WAV file and generate the MIDI file
def transcribe_and_generate_midi(wav_file_path, model, batch_size=32, postprocessing='spotify'):
    midi, _, title = model.transcribe_wav(wav_file_path, batch_size=batch_size, postprocessing=postprocessing)

    # Write the MIDI file
    midi_file_name = unidecode(title) + '.mid'
    midi.write(midi_file_name)

    return midi_file_name, title

# Set up the Pretrained Model
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model = PretrainedModel(instrument='violin').to(device)

# Streamlit UI
st.title("Violin to MIDI Converter")

uploaded_file = st.file_uploader("Upload your WAV file", type=["wav"])

if uploaded_file is not None:
    st.write("File Uploaded Successfully!")
    st.audio(uploaded_file, format='audio/wav')

    if st.button("Convert to MIDI"):
        try:
            midi_file_name, title = transcribe_and_generate_midi(uploaded_file, model)
            st.success(f"MIDI file generated successfully: {midi_file_name}")
            st.audio(midi_file_name, format='audio/midi', label='Download MIDI')
        except Exception as e:
            st.error(f"Error: {str(e)}")
else:
    st.info("Please upload a WAV file to convert to MIDI.")