szili2011 commited on
Commit
719e8ed
·
verified ·
1 Parent(s): 13c2156

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import librosa
3
+ import random
4
+ import numpy as np
5
+ from midiutil import MIDIFile
6
+
7
+ # Load audio and process it
8
+ def audio_to_chromatic_scale(audio_file):
9
+ y, sr = librosa.load(audio_file)
10
+
11
+ # Define chromatic scale range (MIDI notes 60-71 for one octave)
12
+ midi_notes = list(range(60, 72))
13
+
14
+ # Randomly cut audio into segments corresponding to chromatic notes
15
+ chromatic_audio = {}
16
+ segment_length = len(y) // len(midi_notes)
17
+
18
+ for i, note in enumerate(midi_notes):
19
+ start = random.randint(0, len(y) - segment_length)
20
+ chromatic_audio[note] = y[start:start + segment_length]
21
+
22
+ # Create a MIDI file with the audio segments mapped to MIDI keys
23
+ midi = MIDIFile(1)
24
+ midi.addTempo(0, 0, 120)
25
+
26
+ for note, segment in chromatic_audio.items():
27
+ midi.addNote(0, 0, note, 0, 1, 100)
28
+
29
+ # Save MIDI file
30
+ with open("output_chromatic_scale.mid", "wb") as output_file:
31
+ midi.writeFile(output_file)
32
+
33
+ return "output_chromatic_scale.mid"
34
+
35
+ # Build Gradio UI
36
+ interface = gr.Interface(
37
+ fn=audio_to_chromatic_scale,
38
+ inputs=gr.inputs.Audio(type="file"),
39
+ outputs=gr.outputs.File(label="Download MIDI File")
40
+ )
41
+
42
+ interface.launch()