diff --git a/audio_visualizer/standalone_visualizers/horizontal.py b/audio_visualizer/standalone_visualizers/horizontal.py index ca0f17d..a2b3134 100644 --- a/audio_visualizer/standalone_visualizers/horizontal.py +++ b/audio_visualizer/standalone_visualizers/horizontal.py @@ -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') @@ -59,4 +70,4 @@ finally: stream.stop_stream() stream.close() - audio.terminate() \ No newline at end of file + audio.terminate() diff --git a/audio_visualizer/standalone_visualizers/vertical.py b/audio_visualizer/standalone_visualizers/vertical.py index f5ad104..27b0250 100644 --- a/audio_visualizer/standalone_visualizers/vertical.py +++ b/audio_visualizer/standalone_visualizers/vertical.py @@ -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]] @@ -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: diff --git a/audio_visualizer/visualizer.py b/audio_visualizer/visualizer.py index f5790b6..1663c3e 100644 --- a/audio_visualizer/visualizer.py +++ b/audio_visualizer/visualizer.py @@ -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): @@ -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):