Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,70 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import streamlit.components.v1 as components
|
| 3 |
-
import rtmidi
|
| 4 |
import json
|
| 5 |
from pathlib import Path
|
| 6 |
import time
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def get_piano_html():
|
| 25 |
"""Return the HTML content for the piano keyboard"""
|
|
@@ -128,6 +173,10 @@ def main():
|
|
| 128 |
st.title("MIDI Piano Keyboard")
|
| 129 |
st.write("Click keys or use your computer keyboard (A-K and W-U for white and black keys)")
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
# Initialize MIDI output
|
| 132 |
midi_out = create_midi_output()
|
| 133 |
|
|
@@ -146,6 +195,14 @@ def main():
|
|
| 146 |
st.session_state.midi_events = []
|
| 147 |
|
| 148 |
def handle_midi_event(event):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
if event['type'] == 'noteOn':
|
| 150 |
midi_out.send_message([0x90, event['note'], event['velocity']])
|
| 151 |
midi_message_placeholder.write(f"Note On: {event['note']}")
|
|
@@ -172,15 +229,6 @@ def main():
|
|
| 172 |
""",
|
| 173 |
height=0
|
| 174 |
)
|
| 175 |
-
|
| 176 |
-
# Add a loop to continuously check for new MIDI events
|
| 177 |
-
while True:
|
| 178 |
-
time.sleep(0.1) # Small delay to prevent excessive CPU usage
|
| 179 |
-
|
| 180 |
-
# Process any pending MIDI events
|
| 181 |
-
for event in st.session_state.midi_events:
|
| 182 |
-
handle_midi_event(event)
|
| 183 |
-
st.session_state.midi_events = []
|
| 184 |
|
| 185 |
if __name__ == "__main__":
|
| 186 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import streamlit.components.v1 as components
|
|
|
|
| 3 |
import json
|
| 4 |
from pathlib import Path
|
| 5 |
import time
|
| 6 |
+
import subprocess
|
| 7 |
+
import sys
|
| 8 |
|
| 9 |
+
def check_midi_prerequisites():
|
| 10 |
+
"""Check and report MIDI system status"""
|
| 11 |
+
issues = []
|
| 12 |
+
solutions = []
|
| 13 |
|
| 14 |
+
try:
|
| 15 |
+
import rtmidi
|
| 16 |
+
except ImportError:
|
| 17 |
+
issues.append("python-rtmidi is not installed")
|
| 18 |
+
solutions.append("pip install python-rtmidi")
|
| 19 |
+
except OSError as e:
|
| 20 |
+
issues.append(f"MIDI system error: {str(e)}")
|
| 21 |
+
solutions.extend([
|
| 22 |
+
"sudo apt-get install libasound2-dev",
|
| 23 |
+
"sudo apt-get install alsa-utils",
|
| 24 |
+
"sudo modprobe snd-seq-midi"
|
| 25 |
+
])
|
| 26 |
|
| 27 |
+
if issues:
|
| 28 |
+
st.error("MIDI System Issues Detected")
|
| 29 |
+
st.write("Problems found:")
|
| 30 |
+
for issue in issues:
|
| 31 |
+
st.write(f"- {issue}")
|
| 32 |
+
|
| 33 |
+
st.write("Try these solutions:")
|
| 34 |
+
for solution in solutions:
|
| 35 |
+
st.code(solution)
|
| 36 |
+
|
| 37 |
+
st.write("After installing prerequisites, restart the application.")
|
| 38 |
+
return False
|
| 39 |
+
return True
|
| 40 |
+
|
| 41 |
+
def create_midi_output():
|
| 42 |
+
"""Initialize MIDI output with fallback options"""
|
| 43 |
+
try:
|
| 44 |
+
import rtmidi
|
| 45 |
+
midi_out = rtmidi.MidiOut()
|
| 46 |
+
available_ports = midi_out.get_ports()
|
| 47 |
+
|
| 48 |
+
if available_ports:
|
| 49 |
+
st.sidebar.write("Available MIDI ports:", available_ports)
|
| 50 |
+
selected_port = st.sidebar.selectbox(
|
| 51 |
+
"Select MIDI Output",
|
| 52 |
+
range(len(available_ports)),
|
| 53 |
+
format_func=lambda x: available_ports[x]
|
| 54 |
+
)
|
| 55 |
+
midi_out.open_port(selected_port)
|
| 56 |
+
else:
|
| 57 |
+
st.sidebar.warning("No hardware MIDI ports found. Creating virtual port.")
|
| 58 |
+
try:
|
| 59 |
+
midi_out.open_virtual_port("Virtual MIDI Output")
|
| 60 |
+
except rtmidi.SystemError:
|
| 61 |
+
st.sidebar.error("Could not create virtual MIDI port. Running in simulation mode.")
|
| 62 |
+
return None
|
| 63 |
+
return midi_out
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.sidebar.error(f"MIDI system unavailable: {str(e)}")
|
| 66 |
+
st.sidebar.info("Running in simulation mode (no MIDI output)")
|
| 67 |
+
return None
|
| 68 |
|
| 69 |
def get_piano_html():
|
| 70 |
"""Return the HTML content for the piano keyboard"""
|
|
|
|
| 173 |
st.title("MIDI Piano Keyboard")
|
| 174 |
st.write("Click keys or use your computer keyboard (A-K and W-U for white and black keys)")
|
| 175 |
|
| 176 |
+
# Check MIDI prerequisites
|
| 177 |
+
if not check_midi_prerequisites():
|
| 178 |
+
return
|
| 179 |
+
|
| 180 |
# Initialize MIDI output
|
| 181 |
midi_out = create_midi_output()
|
| 182 |
|
|
|
|
| 195 |
st.session_state.midi_events = []
|
| 196 |
|
| 197 |
def handle_midi_event(event):
|
| 198 |
+
if midi_out is None:
|
| 199 |
+
# Simulation mode - just display the events
|
| 200 |
+
if event['type'] == 'noteOn':
|
| 201 |
+
midi_message_placeholder.write(f"Simulated Note On: {event['note']} (No MIDI Output)")
|
| 202 |
+
else:
|
| 203 |
+
midi_message_placeholder.write(f"Simulated Note Off: {event['note']} (No MIDI Output)")
|
| 204 |
+
return
|
| 205 |
+
|
| 206 |
if event['type'] == 'noteOn':
|
| 207 |
midi_out.send_message([0x90, event['note'], event['velocity']])
|
| 208 |
midi_message_placeholder.write(f"Note On: {event['note']}")
|
|
|
|
| 229 |
""",
|
| 230 |
height=0
|
| 231 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
if __name__ == "__main__":
|
| 234 |
main()
|