abby4 commited on
Commit
c0d7ef2
1 Parent(s): 6097d90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -1
app.py CHANGED
@@ -17,7 +17,14 @@ def record_audio(duration=3, sr=44100, channels=1):
17
  print("Recording stopped.")
18
  return audio, sr
19
 
20
- def audio_to_spectrogram(audio, sr):
 
 
 
 
 
 
 
21
  S = librosa.feature.melspectrogram(y=audio[:, 0], sr=sr, n_mels=128, fmax=8000)
22
  S_dB = librosa.power_to_db(S, ref=np.max)
23
  fig, ax = plt.subplots()
 
17
  print("Recording stopped.")
18
  return audio, sr
19
 
20
+ def audio_to_spectrogram(audio_file, sr):
21
+ if audio_file.endswith('.mp3'):
22
+ with tempfile.NamedTemporaryFile(suffix='.wav') as temp_wav:
23
+ audio = AudioSegment.from_mp3(audio_file)
24
+ audio.export(temp_wav.name, format='wav')
25
+ y, sr = librosa.load(temp_wav.name, sr=None)
26
+ else:
27
+ y, sr = librosa.load(audio_file, sr=None)
28
  S = librosa.feature.melspectrogram(y=audio[:, 0], sr=sr, n_mels=128, fmax=8000)
29
  S_dB = librosa.power_to_db(S, ref=np.max)
30
  fig, ax = plt.subplots()