File size: 3,049 Bytes
feaeab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'''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 soundfile as sf
import streamlit as st


def audio_waveframe(file_path):
    # Load the audio file
    audio_data, sampling_rate = librosa.load(file_path)
    # Calculate the duration of the audio file
    duration = len(audio_data) / sampling_rate
    # Create a time array for plotting
    time = np.arange(0, duration, 1/sampling_rate)
    # Plot the waveform
    plt.figure(figsize=(30, 10))
    plt.plot(time, audio_data, color='blue')
    plt.title('Audio Waveform')
    plt.xlabel('Time (s)')
    plt.ylabel('Amplitude')
    # plt.savefig('audio_waveframe.png')
    plot = st.pyplot(plt)

    return plot


# def spectrogram(file_path):
#     # Compute the short-time Fourier transform (STFT)
#     n_fft = 500  # Number of FFT points 2048
#     hop_length = 1  # Hop length for STFT 512
#     audio_data, sampling_rate = librosa.load(file_path)
#     stft = librosa.stft(audio_data, n_fft=n_fft, hop_length=hop_length)
#     # Convert the magnitude spectrogram to decibels (log scale)
#     spectrogram = librosa.amplitude_to_db(np.abs(stft))
#     # Plot the spectrogram
#     plt.figure(figsize=(30, 10))
#     # librosa.display.specshow(spectrogram, sr=sampling_rate, hop_length=hop_length, x_axis='time', y_axis='linear')
#     librosa.display.specshow(spectrogram, sr=sampling_rate, hop_length=hop_length)
#     plt.colorbar(format='%+2.0f dB')
#     plt.title('Spectrogram')
#     plt.xlabel('Time (s)')
#     plt.ylabel('Frequency (Hz)')
#     plt.tight_layout()
#     # plt.savefig('spectrogram.png')
#     plot = st.pyplot(plt)

#     return plot


def spectrogram(file_path):
    
    y, sr = librosa.load(file_path)
    # Compute the spectrogram
    D = librosa.stft(y)
    # Convert magnitude spectrogram to decibels
    DB = librosa.amplitude_to_db(abs(D))
    # Plot the spectrogram
    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