Skip to content

Commit

Permalink
adhered to flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
gituser12981u2 committed May 17, 2024
1 parent 29f40e0 commit 5c17ce5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
29 changes: 20 additions & 9 deletions audio_visualizer/standalone_visualizers/horizontal.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,33 @@
# Get the audio data and apply the FFT
data = np.frombuffer(stream.read(CHUNK), dtype=np.int16)
fft = np.abs(np.fft.fft(data * np.hamming(CHUNK)).real)[:CHUNK // 2]

alpha = 0.2
smoothed_fft = alpha * smoothed_fft + (1 - alpha) * fft # Apply smoothing
max_fft = np.max(smoothed_fft) if np.max(smoothed_fft) > 0 else 1 # Normalize

smoothed_fft = (alpha * smoothed_fft
+ (1 - alpha) * fft) # Apply smoothing
max_fft = (np.max(smoothed_fft)
if np.max(smoothed_fft) > 0 else 1) # Normalize

# Calculate the height of each bar according to the FFT results
log_scale_index = np.logspace(0, np.log10(len(smoothed_fft)), num=bar_count, endpoint=True, base=10).astype(int) - 1
log_scale_index = np.unique(np.clip(log_scale_index, 0, len(smoothed_fft)-1)) # Ensure unique indices and within bounds
log_scale_index = np.logspace(0, np.log10(len(smoothed_fft)),
num=bar_count, endpoint=True,
base=10).astype(int) - 1
log_scale_index = np.unique(np.clip(log_scale_index, 0,
# unique indices within bounds
len(smoothed_fft)-1))

for i in range(len(log_scale_index) - 1):
bar_values = smoothed_fft[log_scale_index[i]:log_scale_index[i+1]]
bar_height = int(np.mean(bar_values) / max_fft * max_bar_height) if bar_values.size > 0 else 0
bar_height = (
int(np.mean(bar_values) / max_fft * max_bar_height)
if bar_values.size > 0 else 0
)

bar_heights[i] = bar_height

# Clear the frame buffer
frame_buffer = "\n".join("".join("█" if bar_heights[col] >= row else " " for col in range(bar_count - 1)) for row in range(max_bar_height - 1, -1, -1))
frame_buffer = "\n".join("".join("█" if bar_heights[col] >= row else " " # noqa: E501
for col in range(bar_count - 1)) for row in range(max_bar_height - 1, -1, -1)) # noqa: E501

# Clear the terminal and print the entire frame at once
os.system('cls' if os.name == 'nt' else 'clear')
Expand All @@ -59,4 +70,4 @@
finally:
stream.stop_stream()
stream.close()
audio.terminate()
audio.terminate()
23 changes: 16 additions & 7 deletions audio_visualizer/standalone_visualizers/vertical.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,26 @@
windowed_data = data * window
fft = np.abs(np.fft.fft(windowed_data).real)
fft = fft[:int(len(fft)/2)] # keep only the first half

# Smoothing factor
alpha = 0.2
smoothed_fft = alpha * smoothed_fft + (1 - alpha) * fft

max_fft = np.max(smoothed_fft) if np.max(smoothed_fft) > 0 else 1 # Avoid division by zero

max_fft = (
np.max(smoothed_fft) if np.max(smoothed_fft)
> 0 else 1 # Avoid division by zero
)

# Clear the screen
os.system('cls' if os.name == 'nt' else 'clear')

# Visualization logic
bar_count = 75
indices = np.logspace(0, np.log10(len(smoothed_fft)), num=bar_count + 1, endpoint=True, base=10).astype(int) - 1
indices = np.unique(np.clip(indices, 0, len(smoothed_fft)-1)) # Ensure unique indices and within bounds
indices = np.logspace(0, np.log10(len(smoothed_fft)),
num=bar_count + 1, endpoint=True,
base=10).astype(int) - 1
# Ensure unique indices within bounds
indices = np.unique(np.clip(indices, 0, len(smoothed_fft)-1))

for i in range(len(indices)-1):
bar_values = smoothed_fft[indices[i]:indices[i+1]]
Expand All @@ -53,8 +59,11 @@
bar_value = 0

# Calculate the number of characters to print
num_chars = int(np.sqrt(bar_value / max_fft) * 50) if not np.isnan(bar_value) else 0
print('█' * num_chars) # Normalize and apply sqrt to enhance visibility
num_chars = (int(np.sqrt(bar_value / max_fft) * 50)
# Normalize and apply sqrt to enhance visibility
if not np.isnan(bar_value) else 0
)
print('█' * num_chars)

time.sleep(0.07) # control frame rate
finally:
Expand Down
4 changes: 2 additions & 2 deletions audio_visualizer/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def visualize_vertical(self):
indices = np.logspace(0, np.log10(len(self.smoothed_fft)),
num=self.bar_count + 1, endpoint=True,
base=10).astype(int) - 1
# Ensure unique indices and within bounds
# Ensure unique indices within bounds
indices = np.unique(np.clip(indices, 0, len(self.smoothed_fft)-1))

for i in range(len(indices) - 1):
Expand Down Expand Up @@ -149,7 +149,7 @@ def visualize_horizontal(self):
num=bar_count, endpoint=True,
base=10).astype(int) - 1
log_scale_index = np.unique(np.clip(log_scale_index, 0,
# unique indices in bounds
# unique indices within bounds
len(self.smoothed_fft)-1))

for i in range(len(log_scale_index) - 1):
Expand Down

0 comments on commit 5c17ce5

Please sign in to comment.