Spaces:
Runtime error
Runtime error
| #write a gradio app that will turn humming into a musical note file that can be used as an input to a midi generator | |
| import gradio as gr | |
| import numpy as np | |
| import librosa as lr | |
| import mido | |
| from io import BytesIO | |
| def humming_to_midi(humming_input): | |
| # Load the humming audio file | |
| if isinstance(humming_input, str): | |
| humming_input = BytesIO(humming_input.encode()) | |
| wav, sr = lr.load(humming_input, sr=44100) | |
| # Extract the pitch of the audio | |
| pitches, magnitudes = lr.piptrack(wav, sr=sr) | |
| # Convert pitches to MIDI note numbers | |
| midi_notes = lr.hz_to_midi(pitches) | |
| # Create a MIDI file using the extracted notes | |
| midi_file = mido.MidiFile() | |
| track = mido.MidiTrack() | |
| midi_file.tracks.append(track) | |
| # Set tempo to 120 beats per minute | |
| track.append(mido.MetaMessage('set_tempo', tempo=mido.bpm2tempo(120))) | |
| # Convert note numbers to MIDI events and add them to the MIDI track | |
| for midi_note in midi_notes: | |
| note_on = mido.Message('note_on', note=int(round(midi_note)), velocity=64, time=0) | |
| note_off = mido.Message('note_off', note=int(round(midi_note)), velocity=0, time=100) | |
| track.append(note_on) | |
| track.append(note_off) | |
| # Save the MIDI file | |
| output_file = 'humming.midi' | |
| midi_file.save(output_file) | |
| return output_file | |
| # Create an input interface for the microphone recording | |
| microphone_input = gr.inputs.Audio(source="microphone", label="Record your humming", type="filepath") | |
| # Create the Gradio interface | |
| gr.Interface(humming_to_midi, microphone_input, "label", description="description").launch(debug=True) |