Skip to content

Commit

Permalink
added Chroma-Feature-Visualizer (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinxsh authored Jul 23, 2024
2 parents 03dadbc + 735d2e5 commit afd7365
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
71 changes: 71 additions & 0 deletions Chroma-Feature-Visualizer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import numpy as np
import librosa
import librosa.display
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import pyaudio
import threading

FRAME_SIZE = 2048
HOP_SIZE = 512
SR = 22050
BUFFER_SIZE = 10

p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paFloat32, channels=1, rate=SR, input=True, frames_per_buffer=HOP_SIZE)

# Initialize the plot
fig, ax = plt.subplots()
chroma_data = np.zeros((12, BUFFER_SIZE))
img = ax.imshow(chroma_data, aspect='auto', cmap='inferno', origin='lower')
fig.colorbar(img, ax=ax)
ax.set_title('Chroma Feature Visualizer')
ax.set_xlabel('Time')
ax.set_ylabel('Pitch Class')

is_paused = False

# Function to update the plot
def update(frame):
global chroma_data, is_paused
if not is_paused:
data = np.frombuffer(stream.read(HOP_SIZE), dtype=np.float32)
chroma = librosa.feature.chroma_stft(y=data, sr=SR, n_fft=FRAME_SIZE, hop_length=HOP_SIZE)
chroma_db = librosa.amplitude_to_db(chroma, ref=np.max)
chroma_data = np.roll(chroma_data, -1, axis=1)
chroma_data[:, -1] = np.mean(chroma_db, axis=1)
img.set_data(chroma_data)
return [img]

# Function to toggle pause/resume
def toggle_pause(event):
global is_paused
is_paused = not is_paused

# Adding a button to pause/resume
ax_pause = plt.axes([0.81, 0.01, 0.1, 0.075])
btn_pause = plt.Button(ax_pause, 'Pause/Resume')
btn_pause.on_clicked(toggle_pause)

# Function to adjust color map
def change_colormap(event):
current_cmap = img.get_cmap().name
new_cmap = 'inferno' if current_cmap == 'viridis' else 'viridis'
img.set_cmap(new_cmap)
fig.canvas.draw()

# Adding a button to change color map
ax_colormap = plt.axes([0.61, 0.01, 0.15, 0.075])
btn_colormap = plt.Button(ax_colormap, 'Change Color Map')
btn_colormap.on_clicked(change_colormap)

# Create an animation
ani = FuncAnimation(fig, update, frames=BUFFER_SIZE, blit=True, interval=50)

# Start the plot
plt.show()

# Close the stream when done
stream.stop_stream()
stream.close()
p.terminate()
2 changes: 2 additions & 0 deletions mainLanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def build(self):
"Spectrogram",
"Intensity vs Frequency and Time",
"Depth-Perspective Visualizer",
"Chroma-Feature Visualizer"
"Lissajous-Curves Visualizer"
]
for visualizer in visualizers:
Expand Down Expand Up @@ -146,6 +147,7 @@ def launch_visualizer(self, instance):
"Spectrogram": "Spectrogram.py",
"Intensity vs Frequency and Time": "Intensity-vs-Frequency-and-Time.py",
"Depth-Perspective Visualizer": "Depth-Perspective-Visualizer.py",
"Chroma-Feature Visualizer": "Chroma-Feature-Visualizer.py"
"Lissajous-Curves Visualizer": "Lissajous-Curves-Visualizer.py"
}

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ PyAudio
PySimpleGUI
sounddevice
soundfile
librosa

0 comments on commit afd7365

Please sign in to comment.