-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio to Graph Converter.py
49 lines (37 loc) · 1.16 KB
/
Audio to Graph Converter.py
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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import librosa
# Define parameters
N_FFT = 2048 # Number of FFT points
FPS = 30 # Frames per second
WIN_SIZE = int(N_FFT * FPS / librosa.get_default_sr()) # Window size in seconds
# Create figure and axes
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Initialize audio stream
stream = librosa.stream('input.wav', block_length=WIN_SIZE, hop_length=WIN_SIZE)
# Initialize plot data
x = np.arange(0, N_FFT)
y = np.arange(0, WIN_SIZE)
X, Y = np.meshgrid(x, y)
Z = np.zeros((WIN_SIZE, N_FFT))
# Create 3D surface plot
surf = ax.plot_surface(X, Y, Z, cmap='coolwarm', vmin=-1, vmax=1)
# Define update function
def update(frame):
# Read audio data
data = next(stream)[0]
# Compute STFT
stft = np.abs(librosa.stft(data, n_fft=N_FFT))
# Normalize and transpose data
stft /= np.max(stft)
stft = np.transpose(stft)
# Update surface plot data
surf.set_array(stft.ravel())
surf.changed()
return surf,
# Create animation
ani = animation.FuncAnimation(fig, update, interval=1000/FPS, blit=True)
# Show plot
plt.show()