-
Notifications
You must be signed in to change notification settings - Fork 8
/
jit_funcs.py
123 lines (104 loc) · 3.24 KB
/
jit_funcs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import warnings
import numpy as np
from maestro import config
from maestro.config import print_to_logfile
try:
from numba import jit
except: # pylint: disable=bare-except
jit = lambda x: x
print_to_logfile("Numba not installed. Visualization will be slower.")
try:
from numba.core.errors import NumbaWarning
warnings.simplefilter("ignore", category=NumbaWarning)
except: # pylint: disable=bare-except
pass
@jit
def lerp(start, stop, t):
return start + t * (stop - start)
@jit(forceobj=True)
def bin_average(arr: np.ndarray, n, include_remainder=False, func=None):
if func is None:
func = np.max
remainder = arr.shape[1] % n
if remainder == 0:
return func(arr.reshape(arr.shape[0], -1, n), axis=1)
avg_head = func(arr[:, :-remainder].reshape(arr.shape[0], -1, n), axis=1)
if include_remainder:
avg_tail = func(
arr[:, -remainder:].reshape(arr.shape[0], -1, remainder), axis=1
)
return np.concatenate((avg_head, avg_tail), axis=1)
return avg_head
@jit(forceobj=True)
def render(
num_bins,
freqs: np.ndarray,
frame,
visualizer_height,
mono=None,
include_remainder=None,
func=None,
):
"""
mono:
True: forces one-channel visualization
False: forces two-channel visualization
None: if freqs[0] == freqs[1], one-channel, else two
"""
if func is None:
func = np.max
if mono is None:
mono = np.array_equal(freqs[0], freqs[1])
if not mono:
gap_bins = 1 if num_bins % 2 else 2
num_bins = (num_bins - 1) // 2
else:
gap_bins = 0
freqs[0, :, frame] = (freqs[0, :, frame] + freqs[1, :, frame]) / 2
num_vertical_block_sizes = len(config.VERTICAL_BLOCKS) - 1
freqs = np.round(
bin_average(
freqs[:, :, frame],
num_bins,
(
(freqs.shape[-2] % num_bins) > num_bins / 2
if include_remainder is None
else include_remainder
),
func=func,
)
/ 80
* visualizer_height
* num_vertical_block_sizes
)
arr = np.zeros((int(not mono) + 1, visualizer_height, num_bins))
for b in range(num_bins):
bin_height = freqs[0, b]
h = 0
while bin_height > num_vertical_block_sizes:
arr[0, h, b] = num_vertical_block_sizes
bin_height -= num_vertical_block_sizes
h += 1
arr[0, h, b] = bin_height
if not mono:
bin_height = freqs[1, b]
h = 0
while bin_height > num_vertical_block_sizes:
arr[1, h, b] = num_vertical_block_sizes
bin_height -= num_vertical_block_sizes
h += 1
arr[1, h, b] = bin_height
res = []
for h in range(visualizer_height - 1, -1, -1):
s = ""
for b in range(num_bins):
if mono:
s += config.VERTICAL_BLOCKS[arr[0, h, b]]
else:
s += config.VERTICAL_BLOCKS[arr[0, h, num_bins - b - 1]]
if not mono:
s += " " * gap_bins
for b in range(num_bins):
s += config.VERTICAL_BLOCKS[arr[1, h, b]]
res.append(s)
return res