Skip to content

Commit

Permalink
adding barplot visualizer (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Khushalsarode authored Aug 10, 2024
1 parent 513f34f commit a04f05c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
85 changes: 85 additions & 0 deletions barplot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import numpy as np
import sounddevice as sd
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.widgets import Button

# Constants
CHUNK = 1024
RATE = 22050 # Sample rate
N_BARS = 50 # Number of bars in the visualization

# Initialize plot
fig, ax = plt.subplots(figsize=(10, 6))
bars = ax.bar(np.arange(N_BARS), np.zeros(N_BARS), color='blue')
ax.set_ylim(0, 1)
ax.set_xlim(0, N_BARS)
ax.set_title('Real-time Audio Bar Visualization')
ax.set_xlabel('Frequency Bins')
ax.set_ylabel('Amplitude')

# Global variables
is_paused = False
color_maps = ['blue', 'green', 'red', 'purple', 'orange']
color_map_index = 0
audio_data = np.zeros(CHUNK)

# Function to update the plot
def update(frame):
global audio_data, is_paused, bars, color_maps, color_map_index
if not is_paused:
# Compute the FFT of the audio data
fft_data = np.abs(np.fft.fft(audio_data)[:N_BARS])
# Normalize the data
fft_data = fft_data / np.max(fft_data)
# Update the heights and colors of the bars
for bar, height in zip(bars, fft_data):
bar.set_height(height)
bar.set_color(color_maps[color_map_index])
return bars

# Callback function for audio input stream
def audio_callback(indata, frames, time, status):
global audio_data
if status:
print(status)
audio_data = indata[:, 0]

# Function to toggle pause/resume
def toggle_pause(event):
global is_paused
is_paused = not is_paused
if is_paused:
ax.set_title('Real-time Audio Bar Visualization (Paused)')
else:
ax.set_title('Real-time Audio Bar Visualization')

# Function to change color
def change_color(event):
global color_map_index
color_map_index = (color_map_index + 1) % len(color_maps)
update(None) # Force an update to apply the new color

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

# Add change color button
ax_color = plt.axes([0.61, 0.01, 0.15, 0.075])
btn_color = Button(ax_color, 'Change Color')
btn_color.on_clicked(change_color)

# Start the audio input stream
stream = sd.InputStream(callback=audio_callback, channels=1, samplerate=RATE, blocksize=CHUNK)
stream.start()

# Start the animation
ani = animation.FuncAnimation(fig, update, blit=False, interval=50)

# Show plot
plt.show()

# Stop the stream on exit
stream.stop()
stream.close()
10 changes: 7 additions & 3 deletions mainLanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def build(self):
"Depth-Perspective Visualizer",
"Chroma-Feature Visualizer"
"Lissajous-Curves Visualizer",
"Voxel-Grid Visualizer"
"Voxel-Grid Visualizer",
"Chromagram Visualizer",
"BarPlot Histogram Visualizer"
]
for visualizer in visualizers:
button = Button(
Expand Down Expand Up @@ -148,9 +150,11 @@ 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"
"Chroma-Feature Visualizer": "Chroma-Feature-Visualizer.py",
"Lissajous-Curves Visualizer": "Lissajous-Curves-Visualizer.py",
"Voxel-Grid Visualizer": "Voxel-Grid-Visualizer.py"
"Voxel-Grid Visualizer": "Voxel-Grid-Visualizer.py",
"Chromagram Visualizer": "Chromagram.py",
"BarPlot Histogram Visualizer": "barplot.py"
}

script_name = instance.text
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ PySimpleGUI
sounddevice
soundfile
librosa
kivy
pygame
PyOpenGL

0 comments on commit a04f05c

Please sign in to comment.