|
| 1 | +""" |
| 2 | +horizontal_left_to_right_visualizer.py |
| 3 | +
|
| 4 | +This module visualizes audio data in a horizontal bar chart. |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import os |
| 9 | +import time |
| 10 | + |
| 11 | +# NOTE rows are the width and cols are the height for os.get_terminal_size |
| 12 | + |
| 13 | + |
| 14 | +def visualize_horizontal_left_to_right( |
| 15 | + stream, chunk, rate, alpha, bar_count, window, smoothed_fft): |
| 16 | + # Initialize smoothed FFT with zeros |
| 17 | + smoothed_fft = np.zeros(chunk // 2 + 1) |
| 18 | + |
| 19 | + while True: |
| 20 | + data = stream.read_data() |
| 21 | + if data is None: |
| 22 | + continue |
| 23 | + |
| 24 | + data = np.frombuffer(data, dtype=np.int16) |
| 25 | + data = data.reshape(-1, 2).mean(axis=1) # Average the two channels |
| 26 | + |
| 27 | + # Apply window function if needed |
| 28 | + windowed_data = data * window |
| 29 | + fft_data = np.abs(np.fft.rfft(windowed_data)) |
| 30 | + |
| 31 | + # Apply exponential moving average for smoothing |
| 32 | + smoothed_fft = alpha * smoothed_fft + (1 - alpha) * fft_data |
| 33 | + |
| 34 | + cols, rows = os.get_terminal_size() |
| 35 | + max_fft = np.max(smoothed_fft, initial=1) # Avoid division by zero |
| 36 | + scaled_fft = np.int16((smoothed_fft / max_fft) * cols) |
| 37 | + |
| 38 | + frame_buffer = [' ' * cols for _ in range(rows)] |
| 39 | + for row in range(min(rows, len(scaled_fft))): |
| 40 | + bar_width = scaled_fft[row] |
| 41 | + frame_buffer[row] = '█' * bar_width + ' ' * (cols - bar_width) |
| 42 | + |
| 43 | + os.system('cls' if os.name == 'nt' else 'clear') |
| 44 | + print('\n'.join(frame_buffer), end='', flush=True) |
| 45 | + |
| 46 | + time.sleep(0.1) # control frame rate |
| 47 | + |
| 48 | + |
| 49 | +# def visualize_horizontal( |
| 50 | +# stream, chunk, rate, alpha, bar_count, window, smoothed_fft): |
| 51 | +# """ |
| 52 | +# Visualizes audio data in a vertical bar chart from bottom to top. |
| 53 | +# """ |
| 54 | +# rows, cols = os.get_terminal_size() # Get terminal size dynamically |
| 55 | +# bar_count = rows # Assume bar_count should use all available rows |
| 56 | + |
| 57 | +# while True: |
| 58 | +# data = stream.read_data() |
| 59 | +# if data is None: |
| 60 | +# break |
| 61 | +# data = np.frombuffer(data, dtype=np.int16) |
| 62 | +# data = data.reshape(-1, 2).mean(axis=1) # Average two channels |
| 63 | + |
| 64 | +# windowed_data = data * window |
| 65 | +# fft = np.abs(np.fft.fft(windowed_data).real) |
| 66 | +# [:len(windowed_data) // 2] |
| 67 | + |
| 68 | +# smoothed_fft = alpha * smoothed_fft + (1 - alpha) * fft |
| 69 | +# max_fft = max(np.max(smoothed_fft), 1) # Normalize the max value |
| 70 | + |
| 71 | +# indices = np.logspace(0, np.log10(len(smoothed_fft)), |
| 72 | +# num=bar_count + 1, |
| 73 | +# endpoint=True, base=10).astype(int) |
| 74 | +# indices = np.unique(np.clip(indices, 0, len(smoothed_fft) - 1)) |
| 75 | + |
| 76 | +# current_frame = {} |
| 77 | +# for i in range(len(indices) - 1): |
| 78 | +# bar_values = smoothed_fft[indices[i]:indices[i + 1]] |
| 79 | +# bar_value = np.average(bar_values, weights=np.linspace( |
| 80 | +# 1, 0.1, num=len(bar_values))) if bar_values.size > 0 else 0 |
| 81 | +# num_chars = int((bar_value / max_fft) * cols) |
| 82 | + |
| 83 | +# for j in range(cols - num_chars, cols): |
| 84 | +# current_frame[(i, j)] = '█' |
| 85 | + |
| 86 | +# frame_buffer = [' ' * rows for _ in range(cols)] |
| 87 | +# for x in range(cols): # Iterate over each bar |
| 88 | +# for y in range(rows): # Iterate over each character in the bar |
| 89 | +# if (x, y) in current_frame: |
| 90 | +# frame_buffer[x] = frame_buffer[x][:y] + \ |
| 91 | +# current_frame[(x, y)] + frame_buffer[x][y+1:] |
| 92 | + |
| 93 | +# os.system('cls' if os.name == 'nt' else 'clear') |
| 94 | +# print('\n'.join(frame_buffer), end="", flush=True) |
| 95 | + |
| 96 | +# time.sleep(0.1) |
0 commit comments