sphear_write_simple_octa_a2b_encoder.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_write_simple_octa_a2b_encoder: write example Faust code for an A format to B format converter
  6. %%
  7. %% capsule equalization filters -> A to B matrix -> B format equalization filters
  8. %%
  9. %% usage:
  10. %% [] = sphear_write_simple_octa_a2b_encoder(CFIR, A2B, FIR)
  11. %%
  12. %% CFIR: FIR capsule equalization filters
  13. %% A2B: static A to B matrix
  14. %% FIR: FIR filter matrix
  15. %%
  16. %% Copyright 2017-2018, Fernando Lopez-Lezcano, All Rights Reserved
  17. %% nando@ccrma.stanford.edu
  18. %%
  19. %% This program is free software: you can redistribute it and/or modify
  20. %% it under the terms of the GNU General Public License as published by
  21. %% the Free Software Foundation, either version 3 of the License, or
  22. %% (at your option) any later version.
  23. %%
  24. %% This program is distributed in the hope that it will be useful,
  25. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. %% GNU General Public License for more details.
  28. %%
  29. %% You should have received a copy of the GNU General Public License
  30. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  31. %%
  32. %% FUMA to ACN conversion
  33. %% https://sursound.music.vt.narkive.com/gaA7EHw6/fuma-to-acn-conversion
  34. %% posted by Fons in the same thread:
  35. %%
  36. %% If you want to check the conversion factors FuMa -> SN3D:
  37. %%
  38. %% W 1.414214 sqrt(2)
  39. %%
  40. %% X 1
  41. %% Y 1
  42. %% Z 1
  43. %%
  44. %% R 1
  45. %% S 0.866025 sqrt(3)/2
  46. %% T id
  47. %% U id
  48. %% V id
  49. function [] = sphear_write_simple_octa_a2b_encoder(CFIR, A2B, FIR, name = "A2B_encoder", serial = 0, faust_version = 2, create_r = 1, fuma = 1)
  50. filename = strcat('../data/current/cal/', name, '.dsp');
  51. [file, msg] = fopen(filename,'w');
  52. if msg
  53. error([msg ': ' filename]);
  54. end
  55. printf("writing A to B encoder, name is \"%s\", for Faust version %d, serial #%d, create R is %d, FUMA is %d\n", name, faust_version, serial, create_r, fuma);
  56. printf("path:\n %s\n", filename);
  57. printf("absolute path:\n %s/%s\n", pwd, filename);
  58. fprintf(file, strcat('declare name "', name, '";\n'));
  59. if (serial > 0)
  60. proto_str = sprintf("_#%d", serial);
  61. else
  62. proto_str = "";
  63. end
  64. if (fuma == 1)
  65. order_str = "_FUMA";
  66. else
  67. order_str = "_ACN";
  68. end
  69. fprintf(file, 'declare version "1.0%s%s";\n', order_str, proto_str);
  70. fprintf(file, 'declare author "Fernando Lopez-Lezcano";\n');
  71. fprintf(file, 'declare license "GPL";\n');
  72. fprintf(file, 'declare copyright "(c) Fernando Lopez-Lezcano 2016-18";\n\n');
  73. %% write beginning of file...
  74. if (faust_version == 2)
  75. fprintf(file, 'import("stdfaust.lib");\n');
  76. fprintf(file, '\n');
  77. firname = "fi.fir";
  78. else
  79. fprintf(file, 'import("filter.lib");\n');
  80. fprintf(file, 'import("music.lib");\n');
  81. fprintf(file, 'import("oscillator.lib");\n');
  82. fprintf(file, '\n');
  83. firname = "fir";
  84. end
  85. bnames = cellstr(['w'; 'x'; 'y'; 'z'; 's'; 't'; 'u'; 'v']);
  86. cnames = cellstr(['c1'; 'c2'; 'c3'; 'c4'; 'c5'; 'c6'; 'c7'; 'c8']);
  87. %% write capsule equalization FIR filter coefficients
  88. printf("writing capsule fir filters...\n");
  89. for b = 1:size(CFIR,1)
  90. bf_filter = reshape(CFIR(b, :), 1, []);
  91. fprintf(file, [char(cnames(b)) "_fir = "]);
  92. write_faust_vector(file, bf_filter);
  93. fprintf(file, ';\n\n');
  94. end
  95. %% write static A to B matrix rows
  96. printf("writing static A to B matrix...\n");
  97. if (fuma == 1)
  98. %% FUMA weights applied to WXYZSTUV
  99. scl = sqrt(2)/2;
  100. weights = [scl, 1, 1, 1, 1, 1, 1, 1];
  101. else
  102. %% SN3D weights applied to WXYZSTUV
  103. scl = sqrt(3)/2;
  104. weights = [1, 1, 1, 1, scl, scl, scl, scl];
  105. end
  106. for a = 1:size(A2B,1)
  107. for b = 1:size(FIR, 1)
  108. fprintf(file, ["a" sprintf("%d", a) char(bnames(b)) " = "]);
  109. write_faust_vector(file, weights(b) * A2B(a, b));
  110. fprintf(file, ';\n');
  111. end
  112. end
  113. fprintf(file, '\n');
  114. %% write FIR filter coefficients
  115. printf("writing B format fir filters...\n");
  116. for b = 1:size(FIR,1)
  117. bf_filter = reshape(FIR(b, :), 1, []);
  118. fprintf(file, [char(bnames(b)) "_fir = "]);
  119. write_faust_vector(file, bf_filter);
  120. fprintf(file, ';\n\n');
  121. end
  122. %% be done with it...
  123. if (size(A2B,1) == 4)
  124. fprintf(file, 'process = _,_,_,_ <: w*gain, x*gain, y*gain, z*gain \n');
  125. else
  126. %% assume 8 input channels for now
  127. if (size(A2B,2) > 4)
  128. printf("octasphear 2nd order...\n");
  129. %% add second order horizontal
  130. if (mean(mean(abs(A2B(:,[5,6])))) > 0)
  131. printf("octasphear full 2nd order...\n");
  132. %% full second order encoder
  133. %%
  134. %% expander for second order components
  135. %%
  136. %% from the guitarfx project:
  137. %% http://q-lang.sourceforge.net/examples.html#Faust
  138. %%
  139. fprintf(file, ' exp_group(x) = vgroup("[2]2nd Order Expander", x);\n');
  140. fprintf(file, ' threshold = exp_group(nentry(\"[0]Exp Threshold (dB)\", -45, -96, 10, 0.1) : smooth(tau2pole(t)));\n');
  141. %% add a display of the W derived level that triggers the expander
  142. fprintf(file, ' exp_control = hbargraph("[1]Exp Control [unit:dB]",-96,0);\n');
  143. fprintf(file, ' exp_hpf_freq = exp_group(nentry(\"[2]Exp HPF (Hz)\", 300, 60, 600, 0.1) : smooth(tau2pole(t)));\n');
  144. fprintf(file, ' ratio = exp_group(nentry(\"[3]Exp Ratio\", 2, 1, 20, 0.1));\n');
  145. fprintf(file, ' knee = exp_group(nentry(\"[4]Exp Knee\", 3, 0, 20, 0.1));\n');
  146. fprintf(file, ' attack = exp_group(hslider(\"[5]Exp Attack\", 0.001, 0, 1, 0.001) : max(1/SR));\n');
  147. fprintf(file, ' release = exp_group(hslider(\"[6]Exp Release\", 0.1, 0, 10, 0.01) : max(1/SR));\n');
  148. fprintf(file, ' t = 0.1;\n');
  149. fprintf(file, ' g = exp(-1/(SR*t));\n');
  150. fprintf(file, ' env = abs : *(1-g) : + ~ *(g);\n');
  151. fprintf(file, ' rms = sqr : *(1-g) : + ~ *(g) : sqrt;\n');
  152. fprintf(file, ' sqr(x) = x*x;\n');
  153. fprintf(file, ' env2(x) = max(x, env(x));\n');
  154. fprintf(file, ' expand(env) = level*(1 - r)\n');
  155. fprintf(file, ' with {\n');
  156. fprintf(file, ' level = env : h ~ _ : linear2db : (threshold+knee-_) : max(0)\n');
  157. fprintf(file, ' with {\n');
  158. fprintf(file, ' h(x,y) = f*x+(1-f)*y with { f = (x<y)*ga+(x>=y)*gr; };\n');
  159. fprintf(file, ' ga = exp(-1/(SR*attack));\n');
  160. fprintf(file, ' gr = exp(-1/(SR*release));\n');
  161. fprintf(file, ' };\n');
  162. fprintf(file, ' p = level/(knee+eps) : max(0) : min(1) with { eps = 0.001; };\n');
  163. fprintf(file, ' r = 1-p+p*ratio;\n');
  164. fprintf(file, ' };\n');
  165. fprintf(file, ' expand_gain(x) = env2(x : highpass(4, exp_hpf_freq)) : expand : exp_control : db2linear;\n');
  166. fprintf(file, '\n');
  167. %% (R)STUV
  168. if (create_r == 1)
  169. r_str = " r,";
  170. else
  171. r_str = "";
  172. end
  173. if (fuma == 1)
  174. %% define FUMA order (WXYZRSTUV)
  175. out_str = sprintf("w*gain, x*gain, y*gain, z*gain,%s s*gain*w_exp, t*gain*w_exp, u*gain*w_exp, v*gain*w_exp", r_str);
  176. else
  177. %% define ACN order (WYZXVTRSU)
  178. out_str = sprintf("w*gain, y*gain, z*gain, x*gain, v*gain*w_exp, t*gain*w_exp,%s s*gain*w_exp, u*gain*w_exp", r_str);
  179. end
  180. fprintf(file, 'process = _,_,_,_,_,_,_,_ : hpf, hpf, hpf, hpf, hpf, hpf, hpf, hpf : (%s(c1_fir)), (%s(c2_fir)), (%s(c3_fir)), (%s(c4_fir)), (%s(c5_fir)), (%s(c6_fir)), (%s(c7_fir)), (%s(c8_fir)) <: %s \n', firname, firname, firname, firname, firname, firname, firname, firname, out_str);
  181. else
  182. % UV
  183. fprintf(file, 'process = _,_,_,_,_,_,_,_ <: w*gain, x*gain, y*gain, z*gain, u*gain, v*gain \n');
  184. end
  185. else
  186. %% first order only, fuma only for now
  187. fprintf(file, 'process = _,_,_,_,_,_,_,_ <: w*gain, x*gain, y*gain, z*gain \n');
  188. end
  189. end
  190. fprintf(file, 'with {\n');
  191. if (size(A2B,1) == 4)
  192. fprintf(file, sprintf(' w = *(a1w), *(a2w), *(a3w), *(a4w) :> %s(w_fir) ;\n', firname));
  193. fprintf(file, sprintf(' x = *(a1x), *(a2x), *(a3x), *(a4x) :> %s(x_fir) ;\n', firname));
  194. fprintf(file, sprintf(' y = *(a1y), *(a2y), *(a3y), *(a4y) :> %s(y_fir) ;\n', firname));
  195. fprintf(file, sprintf(' z = *(a1z), *(a2z), *(a3z), *(a4z) :> %s(z_fir) ;\n', firname));
  196. else
  197. fprintf(file, sprintf(' w = *(a1w), *(a2w), *(a3w), *(a4w), *(a5w), *(a6w), *(a7w), *(a8w) :> %s(w_fir) ;\n', firname));
  198. fprintf(file, sprintf(' x = *(a1x), *(a2x), *(a3x), *(a4x), *(a5x), *(a6x), *(a7x), *(a8x) :> %s(x_fir) ;\n', firname));
  199. fprintf(file, sprintf(' y = *(a1y), *(a2y), *(a3y), *(a4y), *(a5y), *(a6y), *(a7y), *(a8y) :> %s(y_fir) ;\n', firname));
  200. fprintf(file, sprintf(' z = *(a1z), *(a2z), *(a3z), *(a4z), *(a5z), *(a6z), *(a7z), *(a8z) :> %s(z_fir) ;\n', firname));
  201. if (size(A2B,2) > 4)
  202. %% add second order horizontal
  203. if (mean(mean(abs(A2B(:,[5,6])))) > 0)
  204. %% R is always zero for this array
  205. if (create_r == 1)
  206. fprintf(file, sprintf(' r = 0.0 ;\n', firname));
  207. end
  208. fprintf(file, sprintf(' s = *(a1s), *(a2s), *(a3s), *(a4s), *(a5s), *(a6s), *(a7s), *(a8s) :> %s(s_fir) ;\n', firname));
  209. fprintf(file, sprintf(' t = *(a1t), *(a2t), *(a3t), *(a4t), *(a5t), *(a6t), *(a7t), *(a8t) :> %s(t_fir) ;\n', firname));
  210. fprintf(file, sprintf(' u = *(a1u), *(a2u), *(a3u), *(a4u), *(a5u), *(a6u), *(a7u), *(a8u) :> %s(u_fir) ;\n', firname));
  211. fprintf(file, sprintf(' v = *(a1v), *(a2v), *(a3v), *(a4v), *(a5v), *(a6v), *(a7v), *(a8v) :> %s(v_fir) ;\n', firname));
  212. else
  213. fprintf(file, sprintf(' u = *(a1u), *(a2u), *(a3u), *(a4u), *(a5u), *(a6u), *(a7u), *(a8u) :> %s(u_fir) ;\n', firname));
  214. fprintf(file, sprintf(' v = *(a1v), *(a2v), *(a3v), *(a4v), *(a5v), *(a6v), *(a7v), *(a8v) :> %s(v_fir) ;\n', firname));
  215. end
  216. end
  217. end
  218. if (size(A2B,2) > 4)
  219. %% add expander reading W as control signal
  220. fprintf(file, ' w_exp = expand_gain(w) ;\n');
  221. end
  222. %% global gain, should be in dB
  223. fprintf(file, ' gain = hslider("[0]Gain (dB)",0,-12,6,0.01) : db2linear : smooth(tau2pole(0.1));\n');
  224. %% low frequency high pass filter, four pole
  225. fprintf(file, ' hpf_freq = hslider("[1]HPF Freq (Hz)", 30, 10, 300, 0.1) : smooth(tau2pole(0.1));\n');
  226. fprintf(file, ' hpf = highpass(4, hpf_freq);\n');
  227. %% end of parameters
  228. fprintf(file, '};\n');
  229. fprintf(file, '\n');
  230. fclose(file);
  231. end
  232. function [] = write_faust_vector(file, v)
  233. v = v(:);
  234. fprintf(file, '(');
  235. fprintf(file, '% 14.10g,', v(1:end-1));
  236. fprintf(file, '% 14.10g)', v(end));
  237. end