Skip to content

Commit 8161e96

Browse files
Merge pull request #18 from gituser12981u2/release-v0.2.0
Release v0.2.0
2 parents 0bbb56d + dba69b2 commit 8161e96

9 files changed

Lines changed: 462 additions & 282 deletions

audio_visualizer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def main():
1111
parser = argparse.ArgumentParser(description="Terminal Audio Visualizer")
1212
parser.add_argument(
1313
"--mode",
14-
choices=["vertical", "horizontal"],
14+
choices=["vertical", "horizontal-ltr", "horizontal-rtl"],
1515
default="vertical",
1616
help="Choose visualization mode: vertical or horizontal",
1717
)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
horizontal_right_to_left_visualizer.py
3+
4+
This module visualizes audio data in a horizontal bar chart
5+
from right to left.
6+
"""
7+
8+
import numpy as np
9+
import os
10+
import time
11+
12+
# NOTE rows are the width and cols are the height for os.get_terminal_size
13+
14+
15+
def visualize_horizontal_right_to_left(
16+
stream, chunk, rate, alpha, bar_count, window, smoothed_fft):
17+
# Initialize smoothed FFT with zeros
18+
smoothed_fft = np.zeros(chunk // 2 + 1)
19+
20+
while True:
21+
data = stream.read_data()
22+
if data is None:
23+
continue
24+
25+
data = np.frombuffer(data, dtype=np.int16)
26+
data = data.reshape(-1, 2).mean(axis=1) # Average the two channels
27+
28+
# Apply window function if needed
29+
windowed_data = data * window
30+
fft_data = np.abs(np.fft.rfft(windowed_data))
31+
32+
# Apply exponential moving average for smoothing
33+
smoothed_fft = alpha * smoothed_fft + (1 - alpha) * fft_data
34+
35+
cols, rows = os.get_terminal_size()
36+
max_fft = np.max(smoothed_fft, initial=1) # Avoid division by zero
37+
scaled_fft = np.int16((smoothed_fft / max_fft) * cols)
38+
39+
frame_buffer = [' ' * cols for _ in range(rows)]
40+
for row in range(min(rows, len(scaled_fft))):
41+
bar_width = scaled_fft[row]
42+
frame_buffer[row] = ' ' * (cols - bar_width) + '█' * bar_width
43+
44+
os.system('cls' if os.name == 'nt' else 'clear')
45+
print('\n'.join(frame_buffer), end='', flush=True)
46+
47+
time.sleep(0.1) # control frame rate

audio_visualizer/horizontal_visualizer.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)