sphear_write_simple_a2b_encoder.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. %% -*- mode: octave -*-
  2. %%
  3. %% *SpHEAR Project
  4. %%
  5. %% sphear_write_a2b_encoder: write simple Faust code for an A format to B format converter
  6. %% (static A to B matrix plus 4 FIR filters, one of each B component)
  7. %%
  8. %% usage:
  9. %% [] = sphear_write_simple_a2b_encoder(A2B, FIR)
  10. %%
  11. %% A2B: static A to B matrix
  12. %% FIR: FIR filter matrix
  13. %%
  14. %% Copyright 2017, Fernando Lopez-Lezcano, All Rights Reserved
  15. %% nando@ccrma.stanford.edu
  16. %%
  17. %% This program is free software: you can redistribute it and/or modify
  18. %% it under the terms of the GNU General Public License as published by
  19. %% the Free Software Foundation, either version 3 of the License, or
  20. %% (at your option) any later version.
  21. %%
  22. %% This program is distributed in the hope that it will be useful,
  23. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. %% GNU General Public License for more details.
  26. %%
  27. %% You should have received a copy of the GNU General Public License
  28. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. function [] = sphear_write_simple_a2b_encoder(A2B, FIR, name = "A2B_encoder", faust_version = 2)
  30. filename = strcat('../data/current/cal/', name, '.dsp');
  31. [file, msg] = fopen(filename,'w');
  32. if msg
  33. error([msg ': ' filename]);
  34. end
  35. fprintf(file, strcat('declare name "', name, '";\n'));
  36. fprintf(file, 'declare version "1.0";\n');
  37. fprintf(file, 'declare author "Fernando Lopez-Lezcano";\n');
  38. fprintf(file, 'declare license "GPL";\n');
  39. fprintf(file, 'declare copyright "(c) Fernando Lopez-Lezcano 2016-17";\n\n');
  40. %% write beginning of file...
  41. if (faust_version == 2)
  42. fprintf(file, 'import("stdfaust.lib");\n');
  43. fprintf(file, '\n');
  44. firname = "fi.fir";
  45. else
  46. fprintf(file, 'import("filter.lib");\n');
  47. fprintf(file, 'import("music.lib");\n');
  48. fprintf(file, 'import("oscillator.lib");\n');
  49. fprintf(file, '\n');
  50. firname = "fir";
  51. end
  52. bnames = cellstr(['w'; 'x'; 'y'; 'z'; 's'; 't'; 'u'; 'v']);
  53. %% write static A to B matrix rows
  54. fuma = [sqrt(2)/2, 1, 1, 1, 1, 1, 1, 1];
  55. for a = 1:size(A2B,1)
  56. for b = 1:size(FIR, 1)
  57. fprintf(file, ["a" sprintf("%d", a) char(bnames(b)) " = "]);
  58. write_faust_vector(file, fuma(b) * A2B(a, b));
  59. fprintf(file, ';\n');
  60. end
  61. end
  62. fprintf(file, '\n');
  63. %% write FIR filter coefficients
  64. for b = 1:size(FIR,1)
  65. bf_filter = reshape(FIR(b, :), 1, []);
  66. fprintf(file, [char(bnames(b)) "_fir = "]);
  67. write_faust_vector(file, bf_filter);
  68. fprintf(file, ';\n\n');
  69. end
  70. %% be done with it...
  71. if (size(A2B,1) == 4)
  72. fprintf(file, 'process = _,_,_,_ <: w*gain, x*gain, y*gain, z*gain \n');
  73. else
  74. %% assume 8 input channels for now
  75. if (size(A2B,2) > 4)
  76. %% add second order horizontal
  77. if (mean(mean(A2B(:,[5,6]))) > 0)
  78. %% STUV
  79. fprintf(file, 'process = _,_,_,_,_,_,_,_ <: w*gain, x*gain, y*gain, z*gain, s*gain, t*gain, u*gain, v*gain \n');
  80. else
  81. % UV
  82. fprintf(file, 'process = _,_,_,_,_,_,_,_ <: w*gain, x*gain, y*gain, z*gain, u*gain, v*gain \n');
  83. end
  84. else
  85. fprintf(file, 'process = _,_,_,_,_,_,_,_ <: w*gain, x*gain, y*gain, z*gain \n');
  86. end
  87. end
  88. fprintf(file, 'with {\n');
  89. if (size(A2B,1) == 4)
  90. fprintf(file, sprintf(' w = *(a1w), *(a2w), *(a3w), *(a4w) :> %s(w_fir) ;\n', firname));
  91. fprintf(file, sprintf(' x = *(a1x), *(a2x), *(a3x), *(a4x) :> %s(x_fir) ;\n', firname));
  92. fprintf(file, sprintf(' y = *(a1y), *(a2y), *(a3y), *(a4y) :> %s(y_fir) ;\n', firname));
  93. fprintf(file, sprintf(' z = *(a1z), *(a2z), *(a3z), *(a4z) :> %s(z_fir) ;\n', firname));
  94. else
  95. fprintf(file, sprintf(' w = *(a1w), *(a2w), *(a3w), *(a4w), *(a5w), *(a6w), *(a7w), *(a8w) :> %s(w_fir) ;\n', firname));
  96. fprintf(file, sprintf(' x = *(a1x), *(a2x), *(a3x), *(a4x), *(a5x), *(a6x), *(a7x), *(a8x) :> %s(x_fir) ;\n', firname));
  97. fprintf(file, sprintf(' y = *(a1y), *(a2y), *(a3y), *(a4y), *(a5y), *(a6y), *(a7y), *(a8y) :> %s(y_fir) ;\n', firname));
  98. fprintf(file, sprintf(' z = *(a1z), *(a2z), *(a3z), *(a4z), *(a5z), *(a6z), *(a7z), *(a8z) :> %s(z_fir) ;\n', firname));
  99. if (size(A2B,2) > 4)
  100. %% add second order horizontal
  101. if (mean(mean(A2B(:,[5,6]))) > 0)
  102. fprintf(file, sprintf(' s = *(a1s), *(a2s), *(a3s), *(a4s), *(a5s), *(a6s), *(a7s), *(a8s) :> %s(s_fir) ;\n', firname));
  103. fprintf(file, sprintf(' t = *(a1t), *(a2t), *(a3t), *(a4t), *(a5t), *(a6t), *(a7t), *(a8t) :> %s(t_fir) ;\n', firname));
  104. fprintf(file, sprintf(' u = *(a1u), *(a2u), *(a3u), *(a4u), *(a5u), *(a6u), *(a7u), *(a8u) :> %s(u_fir) ;\n', firname));
  105. fprintf(file, sprintf(' v = *(a1v), *(a2v), *(a3v), *(a4v), *(a5v), *(a6v), *(a7v), *(a8v) :> %s(v_fir) ;\n', firname));
  106. else
  107. fprintf(file, sprintf(' u = *(a1u), *(a2u), *(a3u), *(a4u), *(a5u), *(a6u), *(a7u), *(a8u) :> %s(u_fir) ;\n', firname));
  108. fprintf(file, sprintf(' v = *(a1v), *(a2v), *(a3v), *(a4v), *(a5v), *(a6v), *(a7v), *(a8v) :> %s(v_fir) ;\n', firname));
  109. end
  110. end
  111. end
  112. fprintf(file, ' gain = hslider("Gain",1,0,1,0.01);\n');
  113. fprintf(file, '};\n');
  114. fprintf(file, '\n');
  115. fclose(file);
  116. end
  117. function [] = write_faust_vector(file, v)
  118. v = v(:);
  119. fprintf(file, '(');
  120. fprintf(file, '% 14.10g,', v(1:end-1));
  121. fprintf(file, '% 14.10g)', v(end));
  122. end