Spaces:
Build error
Build error
Commit ·
95073d8
1
Parent(s): df1554a
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,6 +49,7 @@ with open('dict.pickle', 'rb') as handle:
|
|
| 49 |
import streamlit as st
|
| 50 |
import tempfile
|
| 51 |
import librosa
|
|
|
|
| 52 |
|
| 53 |
from streamlit_webrtc import (
|
| 54 |
AudioProcessorBase,
|
|
@@ -68,11 +69,12 @@ class MicrophoneRecorder(AudioProcessorBase):
|
|
| 68 |
|
| 69 |
def get_audio(self):
|
| 70 |
y = np.concatenate(self.frames)
|
| 71 |
-
return y
|
| 72 |
|
| 73 |
def save_to_file(self, file_path):
|
| 74 |
y = self.get_audio()
|
| 75 |
-
|
|
|
|
| 76 |
|
| 77 |
def on_receive(self, data):
|
| 78 |
if data["type"] == "media":
|
|
@@ -98,21 +100,20 @@ def record_audio():
|
|
| 98 |
),
|
| 99 |
)
|
| 100 |
if webrtc_ctx.audio_receiver and webrtc_ctx.state.playing:
|
| 101 |
-
|
| 102 |
-
|
|
|
|
| 103 |
else:
|
| 104 |
st.write("WebRTC state is not playing")
|
| 105 |
-
return
|
| 106 |
|
| 107 |
st.set_page_config(page_title='Tune Recognition', page_icon=':musical_note:')
|
| 108 |
st.title('Chord Song Recognition')
|
| 109 |
st.markdown('Click the button below to start recording your tune.')
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
rec_file = 'recording.wav'
|
| 115 |
-
song, sr = librosa.load(rec_file)
|
| 116 |
to_match = np.copy(song[0:220500])
|
| 117 |
|
| 118 |
# Create spectrogram image of the song to match
|
|
@@ -125,7 +126,7 @@ if audio is not None:
|
|
| 125 |
# Get the embedding of the song to match
|
| 126 |
to_match_emb = embedding_model.predict(to_match_img)
|
| 127 |
|
| 128 |
-
|
| 129 |
songsdistdict = {}
|
| 130 |
for key, values in songspecdict.items():
|
| 131 |
dist_array = []
|
|
|
|
| 49 |
import streamlit as st
|
| 50 |
import tempfile
|
| 51 |
import librosa
|
| 52 |
+
import numpy as np
|
| 53 |
|
| 54 |
from streamlit_webrtc import (
|
| 55 |
AudioProcessorBase,
|
|
|
|
| 69 |
|
| 70 |
def get_audio(self):
|
| 71 |
y = np.concatenate(self.frames)
|
| 72 |
+
return y.tobytes()
|
| 73 |
|
| 74 |
def save_to_file(self, file_path):
|
| 75 |
y = self.get_audio()
|
| 76 |
+
with open(file_path, "wb") as f:
|
| 77 |
+
f.write(y)
|
| 78 |
|
| 79 |
def on_receive(self, data):
|
| 80 |
if data["type"] == "media":
|
|
|
|
| 100 |
),
|
| 101 |
)
|
| 102 |
if webrtc_ctx.audio_receiver and webrtc_ctx.state.playing:
|
| 103 |
+
recorder.save_to_file('recording.wav')
|
| 104 |
+
st.write("Audio saved!")
|
| 105 |
+
return True
|
| 106 |
else:
|
| 107 |
st.write("WebRTC state is not playing")
|
| 108 |
+
return False
|
| 109 |
|
| 110 |
st.set_page_config(page_title='Tune Recognition', page_icon=':musical_note:')
|
| 111 |
st.title('Chord Song Recognition')
|
| 112 |
st.markdown('Click the button below to start recording your tune.')
|
| 113 |
+
audio_recorded = record_audio()
|
| 114 |
+
|
| 115 |
+
if audio_recorded:
|
| 116 |
+
song, sr = librosa.load('recording.wav')
|
|
|
|
|
|
|
| 117 |
to_match = np.copy(song[0:220500])
|
| 118 |
|
| 119 |
# Create spectrogram image of the song to match
|
|
|
|
| 126 |
# Get the embedding of the song to match
|
| 127 |
to_match_emb = embedding_model.predict(to_match_img)
|
| 128 |
|
| 129 |
+
# Calculate the distances between the song to match and the songs in the database
|
| 130 |
songsdistdict = {}
|
| 131 |
for key, values in songspecdict.items():
|
| 132 |
dist_array = []
|