|
'''Copyright 2024 Ashok Kumar
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.'''
|
|
|
|
|
|
import os
|
|
import matplotlib.pyplot as plt
|
|
plt.style.use('dark_background')
|
|
import seaborn as sns
|
|
import numpy as np
|
|
import librosa
|
|
from IPython.display import Audio
|
|
|
|
import streamlit as st
|
|
|
|
|
|
def audio_waveframe(file_path):
|
|
|
|
audio_data, sampling_rate = librosa.load(file_path)
|
|
|
|
duration = len(audio_data) / sampling_rate
|
|
|
|
time = np.arange(0, duration, 1/sampling_rate)
|
|
|
|
plt.figure(figsize=(30, 10))
|
|
plt.plot(time, audio_data, color='blue')
|
|
plt.title('Audio Waveform')
|
|
plt.xlabel('Time (s)')
|
|
plt.ylabel('Amplitude')
|
|
|
|
plot = st.pyplot(plt)
|
|
|
|
return plot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def spectrogram(file_path):
|
|
|
|
y, sr = librosa.load(file_path)
|
|
|
|
D = librosa.stft(y)
|
|
|
|
DB = librosa.amplitude_to_db(abs(D))
|
|
|
|
plt.figure(figsize=(20, 5))
|
|
librosa.display.specshow(DB, sr=sr, x_axis='time', y_axis='hz')
|
|
plt.colorbar(format='%+2.0f dB')
|
|
plt.title('Spectrogram')
|
|
plt.xlabel('Time (s)')
|
|
plt.ylabel('Frequency (Hz)')
|
|
plt.tight_layout()
|
|
plot = st.pyplot(plt)
|
|
|
|
return plot
|
|
|
|
|
|
|
|
def audio_signals(file_path):
|
|
aw = audio_waveframe(file_path)
|
|
spg = spectrogram(file_path)
|
|
|
|
return aw, spg
|
|
|