-
Notifications
You must be signed in to change notification settings - Fork 0
/
hoac_encoder.py
195 lines (151 loc) · 5.91 KB
/
hoac_encoder.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
HOAC - Higher-Order Ambisonics Audio Compression - Encoder
@author: Chris Hold
"""
from pathlib import Path
import time
import numpy as np
import matplotlib.pyplot as plt
import spaudiopy as spa
import safpy
import hoac
profile = 'high' # 'low', 'med', 'high'
PEAK_NORM = True
PLOT = False
# load signal
fs = 48000
sig_len = 10 # in s
N_sph_in = 5
file_name = 'Audio/Ambisonics/test_scenes/bruckner_multichannelSH5N3D.wav'
# file_name = 'Audio/Ambisonics/test_scenes/em64_testScene_o5_ACN_N3D.wav'
in_path = Path('~/OneDrive - Aalto University') / Path(file_name)
hoac_file = Path("./out_file.hoac")
in_file = spa.io.load_audio(in_path, fs)
assert in_file.fs == fs
in_sig = in_file.get_signals()[:(N_sph_in+1)**2, :int(fs * sig_len)]
if any(s in str(in_path).lower() for s in ['ambix', 'sn3d']):
in_sig = spa.sph.sn3d_to_n3d(in_sig)
print("Converted SN3D input")
if PEAK_NORM:
gain = 0.3 / np.max(np.abs(in_sig))
in_sig *= gain
print(f"applied gain {gain:.2f}")
# defaults
user_pars = {'bitrateTC': 48 if profile != 'low' else 32,
'numTC': 6 if profile == 'low' else 9 if profile == 'med' else 12,
'metaDecimateFreqLim': 8 if profile == 'low' else
4 if profile == 'med' else 0,
'metaDecimate': 2,
'metaDoaGridOrder': 38,
'metaDifBins': 8
}
hopsize = 128
blocksize = 8 * hopsize
if user_pars['numTC'] == 6:
N_sph_tcs = 2
sec_dirs = spa.utils.cart2sph(*spa.grids.load_t_design((N_sph_tcs+1)).T)
w = np.ones(len(sec_dirs[0]))
if user_pars['numTC'] == 9:
N_sph_tcs = 3
v, w = spa.grids.load_maxDet(N_sph_tcs-1)
sec_dirs = spa.utils.cart2sph(*v.T)
if user_pars['numTC'] == 12:
N_sph_tcs = 3
sec_dirs = spa.utils.cart2sph(*spa.grids.load_t_design(N_sph_tcs+1).T)
w = np.ones(len(sec_dirs[0]))
# Prepare
num_secs = len(sec_dirs[0])
[A_nm, B_nm] = spa.sph.design_sph_filterbank(
N_sph_tcs, sec_dirs[0], sec_dirs[1],
spa.sph.maxre_modal_weights(N_sph_tcs), 'perfect')
beta = (w / w.sum() * len(w)) * spa.sph.sph_filterbank_reconstruction_factor(
A_nm[0, :], num_secs, mode='amplitude')
N_sph_pars = N_sph_tcs + 1
A_nm_pars = spa.parsa.sh_beamformer_from_pattern('max_re', N_sph_pars-1,
sec_dirs[0], sec_dirs[1])
A_wxyz_c = np.array(spa.parsa.sh_sector_beamformer(A_nm_pars),
dtype=np.complex64)
hSTFT = safpy.afstft.AfSTFT((N_sph_pars+1)**2, 0, hopsize, fs)
hSTFT.clear_buffers()
num_slots = blocksize // hopsize
f_qt = hoac.get_f_quantizer(hSTFT.num_bands)
num_fgroups = len(f_qt)
M_grouper = hoac.get_C_weighting(hSTFT.center_freqs)[:, None] * \
hoac.get_f_grouper(f_qt)
M_grouper = M_grouper / np.sum(M_grouper, axis=0)
qgrid, num_coarse = hoac.get_quant_grid(user_pars['metaDoaGridOrder'], None)
qdifbins = np.linspace(0.01, 0.99, user_pars['metaDifBins'], False)**1.5
# Initialize and start timer
start_time = time.time()
x_nm = in_sig[:(N_sph_pars+1)**2, :]
x_nm_buf = np.hstack((x_nm, np.zeros((x_nm.shape[0], hSTFT.processing_delay))))
azi_g = np.zeros((num_slots, num_secs, num_fgroups))
zen_g = np.zeros_like(azi_g)
dif_g = np.zeros_like(azi_g)
ene_g = np.zeros_like(azi_g)
num_blocks = x_nm_buf.shape[1] // blocksize
doa_idx_stream = np.zeros((num_blocks, num_slots, num_secs, num_fgroups),
dtype=np.uint16)
dif_q_stream = np.zeros_like(doa_idx_stream, dtype=np.uint8)
start_smpl = 0
idx_blk = 0
print(f" HOAC encoding - {num_secs} TCs")
x_transport = A_nm @ x_nm[:A_nm.shape[1], :]
while idx_blk < num_blocks:
blk_in = x_nm_buf[:, range(start_smpl, start_smpl+blocksize)]
# afstft
fd_sig_in = hSTFT.forward(blk_in)
for idx_slt in range(num_slots):
azi_g[idx_slt, ...], zen_g[idx_slt, ...], \
dif_g[idx_slt, ...], \
ene_g[idx_slt, ...], _ = hoac.grouped_sector_parameters(
fd_sig_in[idx_slt, ...], A_wxyz_c, M_grouper)
azi_g, zen_g, dif_g, ene_g = hoac.post_pars(azi_g, zen_g, dif_g, ene_g)
dif_q_stream[idx_blk, ...] = hoac.quantize_dif(dif_g, qdifbins)
doa_idx_stream[idx_blk, ...] = hoac.quantize_doa(azi_g, zen_g, qgrid,
dif_g, None, None)
start_smpl += blocksize
idx_blk += 1
# downsample side-info
doa_idx_stream, dif_q_stream = hoac.downsample_meta(
doa_idx_stream, dif_q_stream, user_pars)
print('Parameterization: ', time.time()-start_time, 'seconds.')
print(user_pars)
pars_status = {
'N_sph_in': N_sph_in,
'fs': fs,
'bitrateTC': user_pars['bitrateTC'],
'numTC': num_secs,
'metaDecimate': user_pars['metaDecimate'],
'metaDecimateFreqLim': user_pars['metaDecimateFreqLim'],
'blocksize': blocksize,
'hopsize': hopsize,
'numFreqs': num_fgroups,
'qgrid': qgrid,
'qdifbins': qdifbins,
'A_nm': A_nm,
'tc_v': sec_dirs,
'beta': beta,
}
hoac.write_hoac(pars_status, doa_idx_stream, dif_q_stream,
x_transport, hoac_file)
print('Writing output: ', time.time()-start_time, 'seconds.')
print(f'Filesize: {hoac_file.stat().st_size/10e5} MB')
Path('./audio').mkdir(parents=True, exist_ok=True)
spa.io.save_audio(spa.sph.n3d_to_sn3d(in_sig).T, './audio/in_sig_ambix.wav',
fs)
if PLOT:
spa.plot.sh_rms_map(in_sig, TODB=True, title="Input SHD Signal")
spa.plot.sh_rms_map(x_nm, TODB=True, title="Coded SHD Signal")
f_idx = np.arange(1, num_fgroups)
spa.plot.doa(azi_g[:, :, f_idx].ravel(), zen_g[:, :, f_idx].ravel(),
p=(ene_g *
(1.75**np.arange(num_fgroups)))[:, :, f_idx].ravel(),
alpha=1 - (dif_g[:, :, f_idx]**0.75).ravel(),
c=(np.arange(len(f_idx)) *
np.ones((num_slots, num_secs, len(f_idx)))).ravel(),
title="Parameterization")
plt.plot(sec_dirs[0], np.pi/2-sec_dirs[1], 'k+', markeredgewidth=2)
plt.show()