-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
executable file
·65 lines (50 loc) · 1.9 KB
/
visualization.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
from __future__ import print_function
from __future__ import division
import time
import numpy as np
import state
import dsp
# Handler for audio samples. Will be updated in 'on_state_change'
process_sample = lambda samples, logger: None
def on_state_change (config, visualization):
print('on_state_change')
global process_sample
# FFT window shape
fft_window = np.hanning(config['fft_samples_per_window'])
# FFT binning (mel-bank-transformation)
mel_trafo, (mel_x, fft_x) = dsp.compute_melmat(
num_mel_bands=config['FFT_N_BINS'],
freq_min=config['MIN_FREQUENCY'],
freq_max=config['MAX_FREQUENCY'],
num_fft_bands=int(config['fft_samples_per_window'] / 2),
sample_rate=config['SAMPLE_RATE']
)
#Smoothing on spectrum output
mel_smoothing = dsp.ExpFilter(np.tile(1e-1, config['FFT_N_BINS']), alpha_decay=0.8, alpha_rise=0.99)
# Audio sample rolling window
y_roll = np.random.rand(config['fft_samples_per_window']) / 1e16
def update(audio_samples, logger):
nonlocal y_roll
logger('idle')
# Normalize samples between 0 and 1
y_update = audio_samples / 2.0**15 + 0.5
# Construct a rolling window of audio samples
l = len(y_update)
y_roll[:-l] = y_roll[l:]
y_roll[-l:] = y_update
y_data = y_roll.astype(np.float32)
logger('roll')
# Fourier transform and mel transformation
N = len(y_data)
fft = np.abs(np.fft.rfft(y_data * fft_window)[:N // 2]) #np.zeros(N//2)#
mel = mel_trafo(fft)
# mel = mel**2
logger('fft')
# Visualize
# mel = mel_smoothing.update(mel)
led_output = visualization(mel, y_data, (fft, fft_x))
logger('vis')
return (y_data, mel, led_output)
# Update sample handler
process_sample = update
state.register_on_state_change_hander(on_state_change)