aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2006-03-01 11:46:46 +0000
committerIOhannes m zmölnig <zmoelnig@users.sourceforge.net>2006-03-01 11:46:46 +0000
commitc7ee66edb1e467b9a6650da2c98e25ac2171a70e (patch)
treed9cb74c3d8b141b9acb21e1f972ed954abdadaf3
added the binaural ambisonic decodersvn2git-root
svn path=/trunk/externals/iem/iem_bin_ambi/; revision=4634
-rw-r--r--GnuGPL.txt340
-rw-r--r--LICENSE.txt21
-rw-r--r--src/bin_ambi_calc_HRTF.c591
-rw-r--r--src/bin_ambi_reduced_decode.c1248
-rw-r--r--src/iem_bin_ambi.c35
-rw-r--r--src/iem_bin_ambi.dsp85
-rw-r--r--src/iem_bin_ambi.dsw29
-rw-r--r--src/iem_bin_ambi.h16
-rw-r--r--src/iemlib.h79
9 files changed, 2444 insertions, 0 deletions
diff --git a/GnuGPL.txt b/GnuGPL.txt
new file mode 100644
index 0000000..d60c31a
--- /dev/null
+++ b/GnuGPL.txt
@@ -0,0 +1,340 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Library General
+Public License instead of this License.
diff --git a/LICENSE.txt b/LICENSE.txt
new file mode 100644
index 0000000..6ec4687
--- /dev/null
+++ b/LICENSE.txt
@@ -0,0 +1,21 @@
+iem_bin_ambi - binaural ambisonic rendering
+
+Copyright (C) 2000-2003 Thomas Musil (musil AT iem DOT at)
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+In the official flext distribution, the GNU General Public License is
+in the file GnuGPL.txt
+
diff --git a/src/bin_ambi_calc_HRTF.c b/src/bin_ambi_calc_HRTF.c
new file mode 100644
index 0000000..57460b3
--- /dev/null
+++ b/src/bin_ambi_calc_HRTF.c
@@ -0,0 +1,591 @@
+/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
+* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
+
+iem_bin_ambi written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2003 */
+
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+
+#include "m_pd.h"
+#include "iemlib.h"
+#include "iem_bin_ambi.h"
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+
+
+/* -------------------------- bin_ambi_calc_HRTF ------------------------------ */
+/*
+ ** berechnet ein reduziertes Ambisonic-Decoder-Set in die HRTF-Spektren **
+ ** Inputs: ls + Liste von 3 floats: Index [1 .. 16] + Elevation [-90 .. +90 degree] + Azimut [0 .. 360 degree] **
+ ** Inputs: calc_inv **
+ ** Inputs: load_HRIR + float index1..16 **
+ ** Outputs: List of 2 symbols: left-HRIR-File-name + HRIR-table-name **
+ ** Inputs: calc_reduced **
+ ** "output" ... writes the HRTF into tables **
+ ** **
+ ** **
+ ** setzt voraus , dass die HRIR-tabele-names von LS1_L_HRIR .. LS16_L_HRIR heissen und existieren **
+ ** setzt voraus , dass die HRTF-tabele-names von LS1_HRTF_re .. LS16_HRTF_re heissen und existieren **
+ ** setzt voraus , dass die HRTF-tabele-names von LS1_HRTF_im .. LS16_HRTF_im heissen und existieren **
+ */
+
+typedef struct _bin_ambi_calc_HRTF
+{
+ t_object x_obj;
+ t_atom x_at[2];
+ int x_n_ls;
+ int x_fftsize;
+ int *x_delta;
+ int *x_phi;
+ BIN_AMBI_COMPLEX *x_spec;
+ BIN_AMBI_COMPLEX *x_sin_cos;
+ float *x_beg_fade_out_hrir;
+ float *x_beg_hrir;
+ float **x_beg_hrtf_re;
+ float **x_beg_hrtf_im;
+ t_symbol **x_hrir_filename;
+ t_symbol **x_s_hrir;
+ t_symbol **x_s_hrtf_re;
+ t_symbol **x_s_hrtf_im;
+ t_symbol *x_s_fade_out_hrir;
+ double x_pi_over_180;
+} t_bin_ambi_calc_HRTF;
+
+static t_class *bin_ambi_calc_HRTF_class;
+
+static void bin_ambi_calc_HRTF_init_cos(t_bin_ambi_calc_HRTF *x)
+{
+ int i, fftsize = x->x_fftsize;
+ float f, g;
+ BIN_AMBI_COMPLEX *sincos = x->x_sin_cos;
+
+ g = 2.0f * 3.1415926538f / (float)fftsize;
+ for(i=0; i<fftsize; i++)
+ {
+ f = g * (float)i;
+ (*sincos).real = cos(f);
+ (*sincos).imag = sin(f);/*change*/
+ sincos++;
+ }
+}
+
+static void bin_ambi_calc_HRTF_quant(t_bin_ambi_calc_HRTF *x, double *delta_deg2rad, double *phi_deg2rad, int index)
+{
+ double q = 1.0;
+ double d = *delta_deg2rad;
+ double p = *phi_deg2rad;
+ int i;
+
+ if(d < -40.0)
+ d = -40.0;
+ if(d > 90.0)
+ d = 90.0;
+ while(p < 0.0)
+ p += 360.0;
+ while(p >= 360.0)
+ p -= 360.0;
+
+ if(d < -35.0)
+ {
+ *delta_deg2rad = -40.0;
+ q = 360.0 / 56.0;
+ }
+ else if(d < -25.0)
+ {
+ *delta_deg2rad = -30.0;
+ q = 6.0;
+ }
+ else if(d < -15.0)
+ {
+ *delta_deg2rad = -20.0;
+ q = 5.0;
+ }
+ else if(d < -5.0)
+ {
+ *delta_deg2rad = -10.0;
+ q = 5.0;
+ }
+ else if(d < 5.0)
+ {
+ *delta_deg2rad = 0.0;
+ q = 5.0;
+ }
+ else if(d < 15.0)
+ {
+ *delta_deg2rad = 10.0;
+ q = 5.0;
+ }
+ else if(d < 25.0)
+ {
+ *delta_deg2rad = 20.0;
+ q = 5.0;
+ }
+ else if(d < 35.0)
+ {
+ *delta_deg2rad = 30.0;
+ q = 6.0;
+ }
+ else if(d < 45.0)
+ {
+ *delta_deg2rad = 40.0;
+ q = 360.0 / 56.0;
+ }
+ else if(d < 55.0)
+ {
+ *delta_deg2rad = 50.0;
+ q = 8.0;
+ }
+ else if(d < 65.0)
+ {
+ *delta_deg2rad = 60.0;
+ q = 10.0;
+ }
+ else if(d < 75.0)
+ {
+ *delta_deg2rad = 70.0;
+ q = 15.0;
+ }
+ else if(d < 85.0)
+ {
+ *delta_deg2rad = 80.0;
+ q = 30.0;
+ }
+ else
+ {
+ *delta_deg2rad = 90.0;
+ q = 360.0;
+ }
+
+ p /= q;
+ i = (int)(p + 0.499999);
+ p = (double)i * q;
+ i = (int)(p + 0.499999);
+ while(i >= 360)
+ i -= 360;
+ p = (double)i;
+ *phi_deg2rad = p;
+
+ x->x_delta[index] = (int)(*delta_deg2rad);
+ x->x_phi[index] = i;
+
+ *delta_deg2rad *= x->x_pi_over_180;
+ *phi_deg2rad *= x->x_pi_over_180;
+}
+
+static void bin_ambi_calc_HRTF_do_2d(t_bin_ambi_calc_HRTF *x, int argc, t_atom *argv)
+{
+ double delta=0.0, phi;
+ int index;
+ int n_ls = x->x_n_ls;
+
+ if(argc < 2)
+ {
+ post("bin_ambi_calc_HRTF ERROR: ls-input needs 1 index and 1 angle: ls_index + phi [degree]");
+ return;
+ }
+ index = (int)atom_getint(argv++) - 1;
+ phi = (double)atom_getfloat(argv);
+
+ if(index < 0)
+ index = 0;
+ if(index >= n_ls)
+ index = n_ls - 1;
+
+ bin_ambi_calc_HRTF_quant(x, &delta, &phi, index);
+}
+
+static void bin_ambi_calc_HRTF_do_3d(t_bin_ambi_calc_HRTF *x, int argc, t_atom *argv)
+{
+ double delta, phi;
+ int index;
+ int n_ls=x->x_n_ls;
+
+ if(argc < 3)
+ {
+ post("bin_ambi_calc_HRTF ERROR: ls-input needs 1 index and 2 angles: ls index + delta [degree] + phi [degree]");
+ return;
+ }
+ index = (int)atom_getint(argv++) - 1;
+ delta = atom_getfloat(argv++);
+ phi = atom_getfloat(argv);
+
+ if(index < 0)
+ index = 0;
+ if(index >= n_ls)
+ index = n_ls - 1;
+
+ bin_ambi_calc_HRTF_quant(x, &delta, &phi, index);
+}
+
+static void bin_ambi_calc_HRTF_ls(t_bin_ambi_calc_HRTF *x, t_symbol *s, int argc, t_atom *argv)
+{
+ if(argc >= 3)
+ bin_ambi_calc_HRTF_do_3d(x, argc, argv);
+ else
+ bin_ambi_calc_HRTF_do_2d(x, argc, argv);
+}
+
+static void bin_ambi_calc_HRTF_check_fade_out(t_bin_ambi_calc_HRTF *x)
+{
+ t_garray *a;
+ int npoints;
+ t_float *fadevec;
+
+ if((int)(x->x_beg_fade_out_hrir) == 0)
+ {
+ if (!(a = (t_garray *)pd_findbyclass(x->x_s_fade_out_hrir, garray_class)))
+ error("%s: no such array", x->x_s_fade_out_hrir->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &fadevec))
+ error("%s: bad template for bin_ambi_calc_HRTF", x->x_s_fade_out_hrir->s_name);
+ else if (npoints < x->x_fftsize)
+ error("%s: bad array-size: %d", x->x_s_fade_out_hrir->s_name, npoints);
+ else
+ x->x_beg_fade_out_hrir = fadevec;
+ }
+}
+
+/*
+x_prod:
+n_ambi columns;
+n_ambi rows;
+*/
+
+static void bin_ambi_calc_HRTF_load_HRIR(t_bin_ambi_calc_HRTF *x, float findex)
+{
+ int index=(int)findex - 1;
+ int p;
+ char buf[60];
+
+ if(index < 0)
+ index = 0;
+ if(index >= x->x_n_ls)
+ index = x->x_n_ls - 1;
+
+ p = x->x_phi[index];
+
+ if(p)/*change*/
+ p = 360 - p;
+
+ if(p < 10)
+ sprintf(buf, "L%de00%da.wav", x->x_delta[index], p);
+ else if(p < 100)
+ sprintf(buf, "L%de0%da.wav", x->x_delta[index], p);
+ else
+ sprintf(buf, "L%de%da.wav", x->x_delta[index], p);
+ x->x_hrir_filename[index] = gensym(buf);
+
+ SETSYMBOL(x->x_at, x->x_hrir_filename[index]);
+ SETSYMBOL(x->x_at+1, x->x_s_hrir[index]);
+ outlet_list(x->x_obj.ob_outlet, &s_list, 2, x->x_at);
+}
+
+static void bin_ambi_calc_HRTF_check_arrays(t_bin_ambi_calc_HRTF *x, float findex)
+{
+ int index=(int)findex - 1;
+ int j, k, n;
+ int fftsize = x->x_fftsize;
+ int fs2=fftsize/2;
+ t_garray *a;
+ int npoints;
+ t_float *vec_hrir, *vec, *vec_fade_out_hrir;
+ t_float *vec_hrtf_re, *vec_hrtf_im;
+ t_symbol *hrir, *hrtf_re, *hrtf_im;
+ float decr, sum;
+
+ if(index < 0)
+ index = 0;
+ if(index >= x->x_n_ls)
+ index = x->x_n_ls - 1;
+
+ hrir = x->x_s_hrir[index];
+ hrtf_re = x->x_s_hrtf_re[index];
+ hrtf_im = x->x_s_hrtf_im[index];
+
+ if (!(a = (t_garray *)pd_findbyclass(hrtf_re, garray_class)))
+ error("%s: no such array", hrtf_re->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &vec_hrtf_re))
+ error("%s: bad template for bin_ambi_calc_HRTF", hrtf_re->s_name);
+ else if (npoints < fftsize)
+ error("%s: bad array-size: %d", hrtf_re->s_name, npoints);
+ else if (!(a = (t_garray *)pd_findbyclass(hrtf_im, garray_class)))
+ error("%s: no such array", hrtf_im->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &vec_hrtf_im))
+ error("%s: bad template for bin_ambi_calc_HRTF", hrtf_im->s_name);
+ else if (npoints < fftsize)
+ error("%s: bad array-size: %d", hrtf_im->s_name, npoints);
+ else if (!(a = (t_garray *)pd_findbyclass(hrir, garray_class)))
+ error("%s: no such array", hrir->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &vec_hrir))
+ error("%s: bad template for bin_ambi_calc_HRTF", hrir->s_name);
+ else
+ {
+ x->x_beg_hrtf_re[index] = vec_hrtf_re;
+ x->x_beg_hrtf_im[index] = vec_hrtf_im;
+
+ if(npoints < fftsize)
+ {
+ post("warning: %s-array-size: %d", hrir->s_name, npoints);
+ }
+ vec = x->x_beg_hrir;
+ vec += index * fftsize;
+
+ if((int)(x->x_beg_fade_out_hrir))
+ {
+ vec_fade_out_hrir = x->x_beg_fade_out_hrir;
+ for(j=0; j<fs2; j++)
+ vec[j] = vec_hrir[j]*vec_fade_out_hrir[j];
+ }
+ else
+ {
+ post("no HRIR-fade-out-window found");
+ n = fs2 * 3;
+ n /= 4;
+ for(j=0; j<n; j++)
+ vec[j] = vec_hrir[j];
+ sum = 1.0f;
+ decr = 4.0f / (float)fs2;
+ for(j=n, k=0; j<fs2; j++, k++)
+ {
+ sum -= decr;
+ vec[j] = vec_hrir[j] * sum;
+ }
+ }
+ }
+}
+
+static void bin_ambi_calc_HRTF_calc_fft(t_bin_ambi_calc_HRTF *x, float findex)
+{
+ int index=(int)findex - 1;
+ int i, j, k, w_index, w_inc, i_inc, v_index, fs1, fs2;
+ int fftsize = x->x_fftsize;
+ BIN_AMBI_COMPLEX old1, old2, w;
+ BIN_AMBI_COMPLEX *sincos = x->x_sin_cos;
+ BIN_AMBI_COMPLEX *val = x->x_spec;
+ t_float *vec_hrir, *vec_hrtf_re, *vec_hrtf_im;
+ int n_ls = x->x_n_ls;
+
+ if(index < 0)
+ index = 0;
+ if(index >= n_ls)
+ index = n_ls - 1;
+
+ vec_hrtf_re = x->x_beg_hrtf_re[index];
+ vec_hrtf_im = x->x_beg_hrtf_im[index];
+
+ vec_hrir = x->x_beg_hrir;
+ vec_hrir += index * fftsize;
+ for(k=0; k<fftsize; k++)
+ {
+ val[k].real = vec_hrir[k];
+ val[k].imag = 0.0f;
+ }
+
+ fs1 = fftsize - 1;
+ fs2 = fftsize / 2;
+ i_inc = fs2;
+ w_inc = 1;
+ for(i=1; i<fftsize; i<<=1)
+ {
+ v_index = 0;
+ for(j=0; j<i; j++)
+ {
+ w_index = 0;
+ for(k=0; k<i_inc; k++)
+ {
+ old1 = val[v_index];
+ old2 = val[v_index+i_inc];
+ w = sincos[w_index];
+ val[v_index+i_inc].real = (old1.real-old2.real)*w.real - (old1.imag-old2.imag)*w.imag;
+ val[v_index+i_inc].imag = (old1.imag - old2.imag)*w.real + (old1.real-old2.real)*w.imag;
+ val[v_index].real = old1.real+old2.real;
+ val[v_index].imag = old1.imag+old2.imag;
+ w_index += w_inc;
+ v_index++;
+ }
+ v_index += i_inc;
+ }
+ w_inc <<= 1;
+ i_inc >>= 1;
+ }
+
+ j = 0;
+ for(i=1;i<fs1;i++)
+ {
+ k = fs2;
+ while(k <= j)
+ {
+ j = j - k;
+ k >>= 1;
+ }
+ j = j + k;
+ if(i < j)
+ {
+ old1 = val[j];
+ val[j] = val[i];
+ val[i] = old1;
+ }
+ }
+
+ vec_hrtf_re[0] = val[0].real;
+ vec_hrtf_im[0] = 0.0f;
+ for( i = 1; i < fs2; i++ )
+ {
+ vec_hrtf_re[i] = 2.0f*val[i].real;
+ vec_hrtf_im[i] = 2.0f*val[i].imag;
+ }
+ vec_hrtf_re[fs2] = 0.0f;
+ vec_hrtf_im[fs2] = 0.0f;
+}
+
+static void bin_ambi_calc_HRTF_free(t_bin_ambi_calc_HRTF *x)
+{
+ freebytes(x->x_hrir_filename, x->x_n_ls * sizeof(t_symbol *));
+ freebytes(x->x_s_hrir, x->x_n_ls * sizeof(t_symbol *));
+ freebytes(x->x_s_hrtf_re, x->x_n_ls * sizeof(t_symbol *));
+ freebytes(x->x_s_hrtf_im, x->x_n_ls * sizeof(t_symbol *));
+
+ freebytes(x->x_delta, x->x_n_ls * sizeof(int));
+ freebytes(x->x_phi, x->x_n_ls * sizeof(int));
+
+ freebytes(x->x_spec, x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+ freebytes(x->x_sin_cos, x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+
+ freebytes(x->x_beg_hrir, x->x_fftsize * x->x_n_ls * sizeof(float));
+ freebytes(x->x_beg_hrtf_re, x->x_n_ls * sizeof(float *));
+ freebytes(x->x_beg_hrtf_im, x->x_n_ls * sizeof(float *));
+}
+
+/*
+ 1.arg: t_symbol *hrir_name;
+ 2.arg: t_symbol *hrtf_re_name;
+ 3.arg: t_symbol *hrtf_im_name;
+ 4.arg: t_symbol *hrir_fade_out_name;
+ 5.arg: int ambi_order;
+ 6.arg: int dim;
+ 7.arg: int n_ambi;
+ 8.arg: int fftsize;
+ */
+
+static void *bin_ambi_calc_HRTF_new(t_symbol *s, int argc, t_atom *argv)
+{
+ t_bin_ambi_calc_HRTF *x = (t_bin_ambi_calc_HRTF *)pd_new(bin_ambi_calc_HRTF_class);
+ char buf[400];
+ int i, j, fftok;
+ int n_ls, fftsize;
+ t_symbol *s_hrir;
+ t_symbol *s_hrtf_re;
+ t_symbol *s_hrtf_im;
+
+ if((argc >= 6) &&
+ IS_A_SYMBOL(argv,0) &&
+ IS_A_SYMBOL(argv,1) &&
+ IS_A_SYMBOL(argv,2) &&
+ IS_A_SYMBOL(argv,3) &&
+ IS_A_FLOAT(argv,4) &&
+ IS_A_FLOAT(argv,5))
+ {
+ s_hrir = (t_symbol *)atom_getsymbolarg(0, argc, argv);
+ s_hrtf_re = (t_symbol *)atom_getsymbolarg(1, argc, argv);
+ s_hrtf_im = (t_symbol *)atom_getsymbolarg(2, argc, argv);
+ x->x_s_fade_out_hrir = (t_symbol *)atom_getsymbolarg(3, argc, argv);
+
+ n_ls = (int)atom_getintarg(4, argc, argv);
+ fftsize = (int)atom_getintarg(5, argc, argv);
+
+ if(n_ls < 1)
+ n_ls = 1;
+
+ j = 2;
+ fftok = 0;
+ for(i=0; i<21; i++)
+ {
+ if(j == fftsize)
+ {
+ fftok = 1;
+ i = 22;
+ }
+ j *= 2;
+ }
+
+ if(!fftok)
+ {
+ post("bin_ambi_calc_HRTF-ERROR: fftsize not equal to 2 ^ n !!!");
+ return(0);
+ }
+
+ x->x_n_ls = n_ls;
+ x->x_fftsize = fftsize;
+
+ x->x_hrir_filename = (t_symbol **)getbytes(x->x_n_ls * sizeof(t_symbol *));
+ x->x_s_hrir = (t_symbol **)getbytes(x->x_n_ls * sizeof(t_symbol *));
+ x->x_s_hrtf_re = (t_symbol **)getbytes(x->x_n_ls * sizeof(t_symbol *));
+ x->x_s_hrtf_im = (t_symbol **)getbytes(x->x_n_ls * sizeof(t_symbol *));
+
+ for(i=0; i<n_ls; i++)
+ {
+ sprintf(buf, "%d%s", i+1, s_hrir->s_name);
+ x->x_s_hrir[i] = gensym(buf);
+
+ sprintf(buf, "%d%s", i+1, s_hrtf_re->s_name);
+ x->x_s_hrtf_re[i] = gensym(buf);
+
+ sprintf(buf, "%d%s", i+1, s_hrtf_im->s_name);
+ x->x_s_hrtf_im[i] = gensym(buf);
+ }
+
+ x->x_delta = (int *)getbytes(x->x_n_ls * sizeof(int));
+ x->x_phi = (int *)getbytes(x->x_n_ls * sizeof(int));
+
+ x->x_spec = (BIN_AMBI_COMPLEX *)getbytes(x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+ x->x_sin_cos = (BIN_AMBI_COMPLEX *)getbytes(x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+
+ x->x_beg_fade_out_hrir = (float *)0;
+ x->x_beg_hrir = (float *)getbytes(x->x_fftsize * x->x_n_ls * sizeof(float));
+ x->x_beg_hrtf_re = (float **)getbytes(x->x_n_ls * sizeof(float *));
+ x->x_beg_hrtf_im = (float **)getbytes(x->x_n_ls * sizeof(float *));
+
+ x->x_pi_over_180 = 4.0 * atan(1.0) / 180.0;
+
+ bin_ambi_calc_HRTF_init_cos(x);
+
+ outlet_new(&x->x_obj, &s_list);
+ return(x);
+ }
+ else
+ {
+ post("bin_ambi_calc_HRTF-ERROR: need 4 symbols + 2 floats arguments:");
+ post(" hrir_name + hrtf_re_name + hrtf_im_name + hrir_fade_out_name +");
+ post(" number_of_loudspeakers + fftsize");
+ return(0);
+ }
+}
+
+void bin_ambi_calc_HRTF_setup(void)
+{
+ bin_ambi_calc_HRTF_class = class_new(gensym("bin_ambi_calc_HRTF"), (t_newmethod)bin_ambi_calc_HRTF_new, (t_method)bin_ambi_calc_HRTF_free,
+ sizeof(t_bin_ambi_calc_HRTF), 0, A_GIMME, 0);
+ class_addmethod(bin_ambi_calc_HRTF_class, (t_method)bin_ambi_calc_HRTF_ls, gensym("ls"), A_GIMME, 0);
+ class_addmethod(bin_ambi_calc_HRTF_class, (t_method)bin_ambi_calc_HRTF_check_fade_out, gensym("check_fade_out"), 0);
+ class_addmethod(bin_ambi_calc_HRTF_class, (t_method)bin_ambi_calc_HRTF_load_HRIR, gensym("load_HRIR"), A_FLOAT, 0);
+ class_addmethod(bin_ambi_calc_HRTF_class, (t_method)bin_ambi_calc_HRTF_check_arrays, gensym("check_arrays"), A_FLOAT, 0);
+ class_addmethod(bin_ambi_calc_HRTF_class, (t_method)bin_ambi_calc_HRTF_calc_fft, gensym("calc_fft"), A_FLOAT, 0);
+ class_sethelpsymbol(bin_ambi_calc_HRTF_class, gensym("iemhelp2/help-bin_ambi_calc_HRTF"));
+}
+/*
+Reihenfolge:
+n_ls x bin_ambi_calc_HRTF_ls
+
+bin_ambi_calc_HRTF_check_fade_out
+
+n_ls x bin_ambi_calc_HRTF_load_HRIR
+
+n_ls x bin_ambi_calc_HRTF_check_arrays
+
+n_ls x bin_ambi_calc_HRTF_calc_fft
+
+*/
diff --git a/src/bin_ambi_reduced_decode.c b/src/bin_ambi_reduced_decode.c
new file mode 100644
index 0000000..8626473
--- /dev/null
+++ b/src/bin_ambi_reduced_decode.c
@@ -0,0 +1,1248 @@
+/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
+* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
+
+iem_bin_ambi written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2003 */
+
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+
+#include "m_pd.h"
+#include "iemlib.h"
+#include "iem_bin_ambi.h"
+#include <math.h>
+#include <stdio.h>
+#include <string.h>
+
+
+/* -------------------------- bin_ambi_reduced_decode ------------------------------ */
+/*
+ ** berechnet ein reduziertes Ambisonic-Decoder-Set in die HRTF-Spektren **
+ ** Inputs: ls + Liste von 3 floats: Index [1 .. 16] + Elevation [-90 .. +90 degree] + Azimut [0 .. 360 degree] **
+ ** Inputs: calc_inv **
+ ** Inputs: load_HRIR + float index1..16 **
+ ** Outputs: List of 2 symbols: left-HRIR-File-name + HRIR-table-name **
+ ** Inputs: calc_reduced **
+ ** "output" ... writes the HRTF into tables **
+ ** **
+ ** **
+ ** setzt voraus , dass die HRIR-tabele-names von 1016_1_L_HRIR .. 1016_16_L_HRIR heissen und existieren **
+ ** setzt voraus , dass die HRTF-tabele-names von 1016_1_HRTF_re .. 1016_16_HRTF_re heissen und existieren **
+ ** setzt voraus , dass die HRTF-tabele-names von 1016_1_HRTF_im .. 1016_16_HRTF_im heissen und existieren **
+ */
+
+typedef struct _bin_ambi_reduced_decode
+{
+ t_object x_obj;
+ t_atom x_at[2];
+ int x_n_dim;
+ int x_n_ambi;
+ int x_n_order;
+ int x_fftsize;
+ double *x_inv_work1;
+ double *x_inv_work2;
+ double *x_inv_buf2;
+ double *x_ambi_channel_weight;
+ int *x_delta;
+ int *x_phi;
+ int *x_phi_sym;
+ int *x_sym_flag;
+ BIN_AMBI_COMPLEX *x_spec;
+ BIN_AMBI_COMPLEX *x_sin_cos;
+ float *x_beg_fade_out_hrir;
+ float *x_beg_hrir;
+ float **x_beg_hrtf_re;
+ float **x_beg_hrtf_im;
+ t_symbol **x_hrir_filename;
+ t_symbol **x_s_hrir;
+ t_symbol **x_s_hrtf_re;
+ t_symbol **x_s_hrtf_im;
+ t_symbol *x_s_fade_out_hrir;
+ void *x_out_sign_sum;
+ double x_sqrt3;
+ double x_sqrt10_4;
+ double x_sqrt15_2;
+ double x_sqrt6_4;
+ double x_sqrt35_8;
+ double x_sqrt70_4;
+ double x_sqrt5_2;
+ double x_sqrt126_16;
+ double x_sqrt315_8;
+ double x_sqrt105_4;
+ double x_pi_over_180;
+ double x_sing_range;
+} t_bin_ambi_reduced_decode;
+
+static t_class *bin_ambi_reduced_decode_class;
+
+static void bin_ambi_reduced_decode_init_cos(t_bin_ambi_reduced_decode *x)
+{
+ int i, fftsize = x->x_fftsize;
+ float f, g;
+ BIN_AMBI_COMPLEX *sincos = x->x_sin_cos;
+
+ g = 2.0f * 3.1415926538f / (float)fftsize;
+ for(i=0; i<fftsize; i++)
+ {
+ f = g * (float)i;
+ (*sincos).real = cos(f);
+ (*sincos).imag = sin(f);/*change*/
+ sincos++;
+ }
+}
+
+static void bin_ambi_reduced_decode_quant(t_bin_ambi_reduced_decode *x, double *delta_deg2rad, double *phi_deg2rad, int index)
+{
+ double q = 1.0;
+ double d = *delta_deg2rad;
+ double p = *phi_deg2rad;
+ int i;
+
+ if(d < -40.0)
+ d = -40.0;
+ if(d > 90.0)
+ d = 90.0;
+ while(p < 0.0)
+ p += 360.0;
+ while(p >= 360.0)
+ p -= 360.0;
+
+ if(d < -35.0)
+ {
+ *delta_deg2rad = -40.0;
+ q = 360.0 / 56.0;
+ }
+ else if(d < -25.0)
+ {
+ *delta_deg2rad = -30.0;
+ q = 6.0;
+ }
+ else if(d < -15.0)
+ {
+ *delta_deg2rad = -20.0;
+ q = 5.0;
+ }
+ else if(d < -5.0)
+ {
+ *delta_deg2rad = -10.0;
+ q = 5.0;
+ }
+ else if(d < 5.0)
+ {
+ *delta_deg2rad = 0.0;
+ q = 5.0;
+ }
+ else if(d < 15.0)
+ {
+ *delta_deg2rad = 10.0;
+ q = 5.0;
+ }
+ else if(d < 25.0)
+ {
+ *delta_deg2rad = 20.0;
+ q = 5.0;
+ }
+ else if(d < 35.0)
+ {
+ *delta_deg2rad = 30.0;
+ q = 6.0;
+ }
+ else if(d < 45.0)
+ {
+ *delta_deg2rad = 40.0;
+ q = 360.0 / 56.0;
+ }
+ else if(d < 55.0)
+ {
+ *delta_deg2rad = 50.0;
+ q = 8.0;
+ }
+ else if(d < 65.0)
+ {
+ *delta_deg2rad = 60.0;
+ q = 10.0;
+ }
+ else if(d < 75.0)
+ {
+ *delta_deg2rad = 70.0;
+ q = 15.0;
+ }
+ else if(d < 85.0)
+ {
+ *delta_deg2rad = 80.0;
+ q = 30.0;
+ }
+ else
+ {
+ *delta_deg2rad = 90.0;
+ q = 360.0;
+ }
+
+ p /= q;
+ i = (int)(p + 0.499999);
+ p = (double)i * q;
+ i = (int)(p + 0.499999);
+ while(i >= 360)
+ i -= 360;
+ p = (double)i;
+ *phi_deg2rad = p;
+
+ x->x_delta[index] = (int)(*delta_deg2rad);
+ x->x_phi[index] = i;
+
+ *delta_deg2rad *= x->x_pi_over_180;
+ *phi_deg2rad *= x->x_pi_over_180;
+}
+
+static void bin_ambi_reduced_decode_do_2d(t_bin_ambi_reduced_decode *x, int argc, t_atom *argv)
+{
+ double delta=0.0, phi;
+ double *dw = x->x_inv_work1;
+ int index;
+ int order=x->x_n_order;
+ int n_ambi = x->x_n_ambi;
+
+ if(argc < 2)
+ {
+ post("bin_ambi_reduced_decode ERROR: ls-input needs 1 index and 1 angle: ls_index + phi [degree]");
+ return;
+ }
+ index = (int)atom_getint(argv++) - 1;
+ phi = (double)atom_getfloat(argv);
+
+ if(index < 0)
+ index = 0;
+ if(index >= n_ambi)
+ index = n_ambi - 1;
+
+ bin_ambi_reduced_decode_quant(x, &delta, &phi, index);
+
+ dw += index;
+
+ *dw = 1.0;
+ dw += n_ambi;
+
+ *dw = cos(phi);
+ dw += n_ambi;
+ *dw = sin(phi);
+ dw += n_ambi;
+
+ if(order >= 2)
+ {
+ *dw = cos(2.0*phi);
+ dw += n_ambi;
+ *dw = sin(2.0*phi);
+ dw += n_ambi;
+
+ if(order >= 3)
+ {
+ *dw = cos(3.0*phi);
+ dw += n_ambi;
+ *dw = sin(3.0*phi);
+ dw += n_ambi;
+ if(order >= 4)
+ {
+ *dw = cos(4.0*phi);
+ dw += n_ambi;
+ *dw = sin(4.0*phi);
+ dw += n_ambi;
+
+ if(order >= 5)
+ {
+ *dw = cos(5.0*phi);
+ dw += n_ambi;
+ *dw = sin(5.0*phi);
+ dw += n_ambi;
+
+ if(order >= 6)
+ {
+ *dw = cos(6.0*phi);
+ dw += n_ambi;
+ *dw = sin(6.0*phi);
+ dw += n_ambi;
+
+ if(order >= 7)
+ {
+ *dw = cos(7.0*phi);
+ dw += n_ambi;
+ *dw = sin(7.0*phi);
+ dw += n_ambi;
+
+ if(order >= 8)
+ {
+ *dw = cos(8.0*phi);
+ dw += n_ambi;
+ *dw = sin(8.0*phi);
+ dw += n_ambi;
+
+ if(order >= 9)
+ {
+ *dw = cos(9.0*phi);
+ dw += n_ambi;
+ *dw = sin(9.0*phi);
+ dw += n_ambi;
+
+ if(order >= 10)
+ {
+ *dw = cos(10.0*phi);
+ dw += n_ambi;
+ *dw = sin(10.0*phi);
+ dw += n_ambi;
+
+ if(order >= 11)
+ {
+ *dw = cos(11.0*phi);
+ dw += n_ambi;
+ *dw = sin(11.0*phi);
+ dw += n_ambi;
+
+ if(order >= 12)
+ {
+ *dw = cos(12.0*phi);
+ dw += n_ambi;
+ *dw = sin(12.0*phi);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+}
+
+static void bin_ambi_reduced_decode_do_3d(t_bin_ambi_reduced_decode *x, int argc, t_atom *argv)
+{
+ double delta, phi;
+ double cd, sd, cd2, cd3, sd2, csd, cp, sp, cp2, sp2, cp3, sp3, cp4, sp4;
+ double *dw = x->x_inv_work1;
+ int index;
+ int order=x->x_n_order;
+ int n_ambi=x->x_n_ambi;
+
+ if(argc < 3)
+ {
+ post("bin_ambi_reduced_decode ERROR: ls-input needs 1 index and 2 angles: ls index + delta [degree] + phi [degree]");
+ return;
+ }
+ index = (int)atom_getint(argv++) - 1;
+ delta = atom_getfloat(argv++);
+ phi = atom_getfloat(argv);
+
+ if(index < 0)
+ index = 0;
+ if(index >= n_ambi)
+ index = n_ambi - 1;
+
+ bin_ambi_reduced_decode_quant(x, &delta, &phi, index);
+
+ dw += index;
+
+ cd = cos(delta);
+ sd = sin(delta);
+ cp = cos(phi);
+ sp = sin(phi);
+
+ *dw = 1.0;
+ dw += n_ambi;
+
+ *dw = cd * cp;
+ dw += n_ambi;
+ *dw = cd * sp;
+ dw += n_ambi;
+ *dw = sd;
+ dw += n_ambi;
+
+ if(order >= 2)
+ {
+ cp2 = cos(2.0*phi);
+ sp2 = sin(2.0*phi);
+ cd2 = cd * cd;
+ sd2 = sd * sd;
+ csd = cd * sd;
+ *dw = 0.5 * x->x_sqrt3 * cd2 * cp2;
+ dw += n_ambi;
+ *dw = 0.5 * x->x_sqrt3 * cd2 * sp2;
+ dw += n_ambi;
+ *dw = x->x_sqrt3 * csd * cp;
+ dw += n_ambi;
+ *dw = x->x_sqrt3 * csd * sp;
+ dw += n_ambi;
+ *dw = 0.5 * (3.0 * sd2 - 1.0);
+ dw += n_ambi;
+
+ if(order >= 3)
+ {
+ cp3 = cos(3.0*phi);
+ sp3 = sin(3.0*phi);
+ cd3 = cd2 * cd;
+ *dw = x->x_sqrt10_4 * cd3 * cp3;
+ dw += n_ambi;
+ *dw = x->x_sqrt10_4 * cd3 * sp3;
+ dw += n_ambi;
+ *dw = x->x_sqrt15_2 * cd * csd * cp2;
+ dw += n_ambi;
+ *dw = x->x_sqrt15_2 * cd * csd * sp2;
+ dw += n_ambi;
+ *dw = x->x_sqrt6_4 * cd * (5.0 * sd2 - 1.0) * cp;
+ dw += n_ambi;
+ *dw = x->x_sqrt6_4 * cd * (5.0 * sd2 - 1.0) * sp;
+ dw += n_ambi;
+ *dw = 0.5 * sd * (5.0 * sd2 - 3.0);
+ dw += n_ambi;
+
+ if(order >= 4)
+ {
+ cp4 = cos(4.0*phi);
+ sp4 = sin(4.0*phi);
+ *dw = x->x_sqrt35_8 * cd2 * cd2 * cp4;
+ dw += n_ambi;
+ *dw = x->x_sqrt35_8 * cd2 * cd2 * sp4;
+ dw += n_ambi;
+ *dw = x->x_sqrt70_4 * cd2 * csd * cp3;
+ dw += n_ambi;
+ *dw = x->x_sqrt70_4 * cd2 * csd * sp3;
+ dw += n_ambi;
+ *dw = 0.5 * x->x_sqrt5_2 * cd2 * (7.0 * sd2 - 1.0) * cp2;
+ dw += n_ambi;
+ *dw = 0.5 * x->x_sqrt5_2 * cd2 * (7.0 * sd2 - 1.0) * sp2;
+ dw += n_ambi;
+ *dw = x->x_sqrt10_4 * csd * (7.0 * sd2 - 3.0) * cp;
+ dw += n_ambi;
+ *dw = x->x_sqrt10_4 * csd * (7.0 * sd2 - 3.0) * sp;
+ dw += n_ambi;
+ *dw = 0.125 * (sd2 * (35.0 * sd2 - 30.0) + 3.0);
+ dw += n_ambi;
+
+ if(order >= 5)
+ {
+ *dw = x->x_sqrt126_16 * cd3 * cd2 * cos(5.0*phi);
+ dw += n_ambi;
+ *dw = x->x_sqrt126_16 * cd3 * cd2 * sin(5.0*phi);
+ dw += n_ambi;
+ *dw = x->x_sqrt315_8 * cd3 * csd * cp4;
+ dw += n_ambi;
+ *dw = x->x_sqrt315_8 * cd3 * csd * sp4;
+ dw += n_ambi;
+ *dw = 0.25 * x->x_sqrt70_4 * cd3 * (9.0 * sd2 - 1.0) * cp3;
+ dw += n_ambi;
+ *dw = 0.25 * x->x_sqrt70_4 * cd3 * (9.0 * sd2 - 1.0) * sp3;
+ dw += n_ambi;
+ *dw = x->x_sqrt105_4 * cd * csd * (3.0 * sd2 - 1.0) * cp2;
+ dw += n_ambi;
+ *dw = x->x_sqrt105_4 * cd * csd * (3.0 * sd2 - 1.0) * sp2;
+ dw += n_ambi;
+ *dw = 0.25 * x->x_sqrt15_2 * cd * (sd2 * (21.0 * sd2 - 14.0) + 1.0) * cp;
+ dw += n_ambi;
+ *dw = 0.25 * x->x_sqrt15_2 * cd * (sd2 * (21.0 * sd2 - 14.0) + 1.0) * sp;
+ dw += n_ambi;
+ *dw = 0.125 * sd * (sd2 * (63.0 * sd2 - 70.0) + 15.0);
+ }
+ }
+ }
+ }
+}
+
+static void bin_ambi_reduced_decode_ls(t_bin_ambi_reduced_decode *x, t_symbol *s, int argc, t_atom *argv)
+{
+ if(x->x_n_dim == 2)
+ bin_ambi_reduced_decode_do_2d(x, argc, argv);
+ else
+ bin_ambi_reduced_decode_do_3d(x, argc, argv);
+}
+
+static void bin_ambi_reduced_decode_copy_row2buf(t_bin_ambi_reduced_decode *x, int row)
+{
+ int n_ambi2 = 2*x->x_n_ambi;
+ int i;
+ double *dw=x->x_inv_work2;
+ double *db=x->x_inv_buf2;
+
+ dw += row*n_ambi2;
+ for(i=0; i<n_ambi2; i++)
+ *db++ = *dw++;
+}
+
+static void bin_ambi_reduced_decode_copy_buf2row(t_bin_ambi_reduced_decode *x, int row)
+{
+ int n_ambi2 = 2*x->x_n_ambi;
+ int i;
+ double *dw=x->x_inv_work2;
+ double *db=x->x_inv_buf2;
+
+ dw += row*n_ambi2;
+ for(i=0; i<n_ambi2; i++)
+ *dw++ = *db++;
+}
+
+static void bin_ambi_reduced_decode_copy_row2row(t_bin_ambi_reduced_decode *x, int src_row, int dst_row)
+{
+ int n_ambi2 = 2*x->x_n_ambi;
+ int i;
+ double *dw_src=x->x_inv_work2;
+ double *dw_dst=x->x_inv_work2;
+
+ dw_src += src_row*n_ambi2;
+ dw_dst += dst_row*n_ambi2;
+ for(i=0; i<n_ambi2; i++)
+ *dw_dst++ = *dw_src++;
+}
+
+static void bin_ambi_reduced_decode_xch_rows(t_bin_ambi_reduced_decode *x, int row1, int row2)
+{
+ bin_ambi_reduced_decode_copy_row2buf(x, row1);
+ bin_ambi_reduced_decode_copy_row2row(x, row2, row1);
+ bin_ambi_reduced_decode_copy_buf2row(x, row2);
+}
+
+static void bin_ambi_reduced_decode_mul_row(t_bin_ambi_reduced_decode *x, int row, double mul)
+{
+ int n_ambi2 = 2*x->x_n_ambi;
+ int i;
+ double *dw=x->x_inv_work2;
+
+ dw += row*n_ambi2;
+ for(i=0; i<n_ambi2; i++)
+ {
+ (*dw) *= mul;
+ dw++;
+ }
+}
+
+static void bin_ambi_reduced_decode_mul_col(t_bin_ambi_reduced_decode *x, int col, double mul)
+{
+ int n_ambi = x->x_n_ambi;
+ int n_ambi2 = 2*n_ambi;
+ int i;
+ double *dw=x->x_inv_work2;
+
+ dw += col;
+ for(i=0; i<n_ambi; i++)
+ {
+ (*dw) *= mul;
+ dw += n_ambi2;
+ }
+}
+
+static void bin_ambi_reduced_decode_mul_buf_and_add2row(t_bin_ambi_reduced_decode *x, int row, double mul)
+{
+ int n_ambi2 = 2*x->x_n_ambi;
+ int i;
+ double *dw=x->x_inv_work2;
+ double *db=x->x_inv_buf2;
+
+ dw += row*n_ambi2;
+ for(i=0; i<n_ambi2; i++)
+ {
+ *dw += (*db)*mul;
+ dw++;
+ db++;
+ }
+}
+
+static int bin_ambi_reduced_decode_eval_which_element_of_col_not_zero(t_bin_ambi_reduced_decode *x, int col, int start_row)
+{
+ int n_ambi = x->x_n_ambi;
+ int n_ambi2 = 2*n_ambi;
+ int i, j;
+ double *dw=x->x_inv_work2;
+ double singrange=x->x_sing_range;
+ int ret=-1;
+
+ dw += start_row*n_ambi2 + col;
+ j = 0;
+ for(i=start_row; i<n_ambi; i++)
+ {
+ if((*dw > singrange) || (*dw < -singrange))
+ {
+ ret = i;
+ i = n_ambi+1;
+ }
+ dw += n_ambi2;
+ }
+ return(ret);
+}
+
+static void bin_ambi_reduced_decode_inverse(t_bin_ambi_reduced_decode *x)
+{
+ int n_ambi = x->x_n_ambi;
+ int n_ambi2 = 2*n_ambi;
+ int i, j, nz;
+ int r,c;
+ double *src=x->x_inv_work1;
+ double *db=x->x_inv_work2;
+ double *acw_vec=x->x_ambi_channel_weight;
+ double rcp, *dv;
+
+ dv = db;
+ for(i=0; i<n_ambi; i++) /* init */
+ {
+ for(j=0; j<n_ambi; j++)
+ {
+ *dv++ = *src++;
+ }
+ for(j=0; j<n_ambi; j++)
+ {
+ if(j == i)
+ *dv++ = 1.0;
+ else
+ *dv++ = 0.0;
+ }
+ }
+/* make 1 in main-diagonale, and 0 below */
+ for(i=0; i<n_ambi; i++)
+ {
+ nz = bin_ambi_reduced_decode_eval_which_element_of_col_not_zero(x, i, i);
+ if(nz < 0)
+ {
+ post("bin_ambi_reduced_decode ERROR: matrix not regular !!!!");
+ return;
+ }
+ else
+ {
+ if(nz != i)
+ bin_ambi_reduced_decode_xch_rows(x, i, nz);
+ dv = db + i*n_ambi2 + i;
+ rcp = 1.0 /(*dv);
+ bin_ambi_reduced_decode_mul_row(x, i, rcp);
+ bin_ambi_reduced_decode_copy_row2buf(x, i);
+ for(j=i+1; j<n_ambi; j++)
+ {
+ dv += n_ambi2;
+ rcp = -(*dv);
+ bin_ambi_reduced_decode_mul_buf_and_add2row(x, j, rcp);
+ }
+ }
+ }
+/* make 0 above the main diagonale */
+ for(i=n_ambi-1; i>=0; i--)
+ {
+ dv = db + i*n_ambi2 + i;
+ bin_ambi_reduced_decode_copy_row2buf(x, i);
+ for(j=i-1; j>=0; j--)
+ {
+ dv -= n_ambi2;
+ rcp = -(*dv);
+ bin_ambi_reduced_decode_mul_buf_and_add2row(x, j, rcp);
+ }
+ }
+
+ for(i=0; i<n_ambi; i++)
+ {
+ bin_ambi_reduced_decode_mul_col(x, i+n_ambi, acw_vec[i]);
+ }
+
+ post("matrix_inverse regular");
+}
+
+static void bin_ambi_reduced_decode_calc_pinv(t_bin_ambi_reduced_decode *x)
+{
+ t_garray *a;
+ int npoints;
+ t_float *fadevec;
+
+ if((int)(x->x_beg_fade_out_hrir) == 0)
+ {
+ if (!(a = (t_garray *)pd_findbyclass(x->x_s_fade_out_hrir, garray_class)))
+ error("%s: no such array", x->x_s_fade_out_hrir->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &fadevec))
+ error("%s: bad template for bin_ambi_reduced_decode", x->x_s_fade_out_hrir->s_name);
+ else if (npoints < x->x_fftsize)
+ error("%s: bad array-size: %d", x->x_s_fade_out_hrir->s_name, npoints);
+ else
+ x->x_beg_fade_out_hrir = fadevec;
+ }
+
+ bin_ambi_reduced_decode_inverse(x);
+}
+
+/*
+x_prod:
+n_ambi columns;
+n_ambi rows;
+*/
+
+static void bin_ambi_reduced_decode_load_HRIR(t_bin_ambi_reduced_decode *x, float findex)
+{
+ int index=(int)findex - 1;
+ int p;
+ char buf[60];
+
+ if(index < 0)
+ index = 0;
+ if(index >= x->x_n_ambi)
+ index = x->x_n_ambi - 1;
+
+ p = x->x_phi[index];
+
+ if(p)/*change*/
+ p = 360 - p;
+
+ if(p < 10)
+ sprintf(buf, "L%de00%da.wav", x->x_delta[index], p);
+ else if(p < 100)
+ sprintf(buf, "L%de0%da.wav", x->x_delta[index], p);
+ else
+ sprintf(buf, "L%de%da.wav", x->x_delta[index], p);
+ x->x_hrir_filename[index] = gensym(buf);
+
+ SETSYMBOL(x->x_at, x->x_hrir_filename[index]);
+ SETSYMBOL(x->x_at+1, x->x_s_hrir[index]);
+ outlet_list(x->x_obj.ob_outlet, &s_list, 2, x->x_at);
+}
+
+static void bin_ambi_reduced_decode_check_arrays(t_bin_ambi_reduced_decode *x, float findex)
+{
+ int index=(int)findex - 1;
+ int j, k, n;
+ int fftsize = x->x_fftsize;
+ int fs2=fftsize/2;
+ t_garray *a;
+ int npoints;
+ t_float *vec_hrir, *vec, *vec_fade_out_hrir;
+ t_float *vec_hrtf_re, *vec_hrtf_im;
+ t_symbol *hrir, *hrtf_re, *hrtf_im;
+ float decr, sum;
+
+ if(index < 0)
+ index = 0;
+ if(index >= x->x_n_ambi)
+ index = x->x_n_ambi - 1;
+
+ hrir = x->x_s_hrir[index];
+ hrtf_re = x->x_s_hrtf_re[index];
+ hrtf_im = x->x_s_hrtf_im[index];
+
+ if (!(a = (t_garray *)pd_findbyclass(hrtf_re, garray_class)))
+ error("%s: no such array", hrtf_re->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &vec_hrtf_re))
+ error("%s: bad template for bin_ambi_reduced_decode", hrtf_re->s_name);
+ else if (npoints < fftsize)
+ error("%s: bad array-size: %d", hrtf_re->s_name, npoints);
+ else if (!(a = (t_garray *)pd_findbyclass(hrtf_im, garray_class)))
+ error("%s: no such array", hrtf_im->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &vec_hrtf_im))
+ error("%s: bad template for bin_ambi_reduced_decode", hrtf_im->s_name);
+ else if (npoints < fftsize)
+ error("%s: bad array-size: %d", hrtf_im->s_name, npoints);
+ else if (!(a = (t_garray *)pd_findbyclass(hrir, garray_class)))
+ error("%s: no such array", hrir->s_name);
+ else if (!garray_getfloatarray(a, &npoints, &vec_hrir))
+ error("%s: bad template for bin_ambi_reduced_decode", hrir->s_name);
+ else
+ {
+ x->x_beg_hrtf_re[index] = vec_hrtf_re;
+ x->x_beg_hrtf_im[index] = vec_hrtf_im;
+
+ if(npoints < fftsize)
+ {
+ post("warning: %s-array-size: %d", hrir->s_name, npoints);
+ }
+ vec = x->x_beg_hrir;
+ vec += index * fftsize;
+
+ if((int)(x->x_beg_fade_out_hrir))
+ {
+ vec_fade_out_hrir = x->x_beg_fade_out_hrir;
+ for(j=0; j<fs2; j++)
+ vec[j] = vec_hrir[j]*vec_fade_out_hrir[j];
+ }
+ else
+ {
+ post("no HRIR-fade-out-window found");
+ n = fs2 * 3;
+ n /= 4;
+ for(j=0; j<n; j++)
+ vec[j] = vec_hrir[j];
+ sum = 1.0f;
+ decr = 4.0f / (float)fs2;
+ for(j=n, k=0; j<fs2; j++, k++)
+ {
+ sum -= decr;
+ vec[j] = vec_hrir[j] * sum;
+ }
+ }
+ }
+}
+
+static void bin_ambi_reduced_decode_calc_reduced(t_bin_ambi_reduced_decode *x, float findex)
+{
+ int index=(int)findex - 1;
+ int i, j, k, w_index, w_inc, i_inc, v_index, fs1, fs2;
+ int fftsize = x->x_fftsize;
+ BIN_AMBI_COMPLEX old1, old2, w;
+ BIN_AMBI_COMPLEX *sincos = x->x_sin_cos;
+ BIN_AMBI_COMPLEX *val = x->x_spec;
+ t_float *vec_hrir, *vec_hrtf_re, *vec_hrtf_im;
+ double *dv, *db=x->x_inv_work2;
+ int n_ambi = x->x_n_ambi;
+ int n_ambi2 = 2*n_ambi;
+ float mul;
+
+ if(index < 0)
+ index = 0;
+ if(index >= n_ambi)
+ index = n_ambi - 1;
+
+ vec_hrtf_re = x->x_beg_hrtf_re[index];
+ vec_hrtf_im = x->x_beg_hrtf_im[index];
+
+ dv = db + index + n_ambi;
+ mul = (float)(*dv);
+ vec_hrir = x->x_beg_hrir;
+ for(k=0; k<fftsize; k++)
+ {
+ val[k].real = mul * vec_hrir[k];
+ val[k].imag = 0.0f;
+ }
+
+ for(j=1; j<n_ambi; j++)
+ {
+ dv += n_ambi2;
+ mul = (float)(*dv);
+ vec_hrir = x->x_beg_hrir;
+ vec_hrir += j * fftsize;
+ for(k=0; k<fftsize; k++)
+ {
+ val[k].real += mul * vec_hrir[k];
+ }
+ }
+
+ fs1 = fftsize - 1;
+ fs2 = fftsize / 2;
+ i_inc = fs2;
+ w_inc = 1;
+ for(i=1; i<fftsize; i<<=1)
+ {
+ v_index = 0;
+ for(j=0; j<i; j++)
+ {
+ w_index = 0;
+ for(k=0; k<i_inc; k++)
+ {
+ old1 = val[v_index];
+ old2 = val[v_index+i_inc];
+ w = sincos[w_index];
+ val[v_index+i_inc].real = (old1.real-old2.real)*w.real - (old1.imag-old2.imag)*w.imag;
+ val[v_index+i_inc].imag = (old1.imag - old2.imag)*w.real + (old1.real-old2.real)*w.imag;
+ val[v_index].real = old1.real+old2.real;
+ val[v_index].imag = old1.imag+old2.imag;
+ w_index += w_inc;
+ v_index++;
+ }
+ v_index += i_inc;
+ }
+ w_inc <<= 1;
+ i_inc >>= 1;
+ }
+
+ j = 0;
+ for(i=1;i<fs1;i++)
+ {
+ k = fs2;
+ while(k <= j)
+ {
+ j = j - k;
+ k >>= 1;
+ }
+ j = j + k;
+ if(i < j)
+ {
+ old1 = val[j];
+ val[j] = val[i];
+ val[i] = old1;
+ }
+ }
+
+ vec_hrtf_re[0] = val[0].real;
+ vec_hrtf_im[0] = 0.0f;
+ for( i = 1; i < fs2; i++ )
+ {
+ vec_hrtf_re[i] = 2.0f*val[i].real;
+ vec_hrtf_im[i] = 2.0f*val[i].imag;
+ }
+ vec_hrtf_re[fs2] = 0.0f;
+ vec_hrtf_im[fs2] = 0.0f;
+}
+
+static void bin_ambi_reduced_decode_decoding(t_bin_ambi_reduced_decode *x)
+{
+ int *sym=x->x_phi_sym;
+ int n_ambi = x->x_n_ambi;
+ int n_ambi2 = 2*n_ambi;
+ int *phi=x->x_phi;
+ int *delta=x->x_delta;
+ int *flag=x->x_sym_flag;
+ int i, j, d, p, regular=1;
+ double *dv, *db=x->x_inv_work2;
+ double a, b, q;
+ int sym_max=0;
+ int pos_sym_counter, neg_sym_counter;
+ char plus_minus[100];
+ char abc[100];
+ char onenine[100];
+ char ten[100];
+
+ for(i=0; i<n_ambi; i++)
+ sym[i] = -1;
+
+ plus_minus[0] = 0;
+ if(x->x_n_dim == 2)
+ {
+ strcpy(abc, " WXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXYXY");
+ strcpy(ten, " 000000000000000000011111111111111111111");
+ strcpy(onenine, " 011223344556677889900112233445566778899");
+ abc[n_ambi+22] = 0;
+ ten[n_ambi+22] = 0;
+ onenine[n_ambi+22] = 0;
+ post(abc);
+ post(ten);
+ post(onenine);
+ }
+ else
+ {
+ strcpy(abc, " AABCABCDEABCDEFGABCDEFGHIABCDEFGHIJKABCDEFGHIJKLM");
+ strcpy(onenine, " 0111222223333333444444444555555555556666666666666");
+ abc[n_ambi+22] = 0;
+ onenine[n_ambi+22] = 0;
+ post(abc);
+ post(onenine);
+ }
+
+ for(i=0; i<n_ambi; i++)
+ {
+ d = delta[i];
+ p = phi[i];
+ for(j=i+1; j<n_ambi; j++)
+ {
+ if(d == delta[j])
+ {
+ if((p + phi[j]) == 360)
+ {
+ if((sym[i] < 0) && (sym[j] < 0))
+ {
+ sym[i] = j;
+ sym[j] = i;
+ j = n_ambi + 1;
+ sym_max++;
+ }
+ }
+ }
+ }
+ }
+
+ for(p=0; p<n_ambi; p++)/*each col*/
+ {
+ pos_sym_counter = 0;
+ neg_sym_counter = 0;
+ for(i=0; i<n_ambi; i++)
+ flag[i] = 1;
+ dv = db + p + n_ambi;
+ for(i=0; i<n_ambi; i++)
+ {
+ if((sym[i] >= 0) && flag[i])
+ {
+ j = sym[i];
+ flag[i] = 0;
+ flag[j] = 0;
+ a = dv[i*n_ambi2];
+ b = dv[j*n_ambi2];
+ if((a < 5.0e-4)&&(a > -5.0e-4)&&(b < 5.0e-4)&&(b > -5.0e-4))
+ {
+ pos_sym_counter++;
+ neg_sym_counter++;
+ }
+ else
+ {
+ q = a / b;
+ if((q < 1.005)&&(q > 0.995))
+ pos_sym_counter++;
+ else if((q > -1.005)&&(q < -0.995))
+ neg_sym_counter++;
+ }
+ }
+ }
+ if(pos_sym_counter == sym_max)
+ strcat(plus_minus, "+");
+ else if(neg_sym_counter == sym_max)
+ strcat(plus_minus, "-");
+ else
+ {
+ strcat(plus_minus, "?");
+ regular = 0;
+ }
+ }
+ post("sum of right channel: %s", plus_minus);
+
+ if(regular)
+ {
+ for(i=0; i<n_ambi; i++)
+ {
+ /*change*/
+ if(plus_minus[i] == '+')
+ SETFLOAT(x->x_at, 1.0f);
+ else if(plus_minus[i] == '-')
+ SETFLOAT(x->x_at, 2.0f);
+ SETFLOAT(x->x_at+1, (float)(i+1));
+ outlet_list(x->x_out_sign_sum, &s_list, 2, x->x_at);
+ }
+ }
+}
+
+static void bin_ambi_reduced_decode_ambi_weight(t_bin_ambi_reduced_decode *x, t_symbol *s, int argc, t_atom *argv)
+{
+ if(argc > x->x_n_order)
+ {
+ int i, k=0, n=x->x_n_order;
+ double d;
+
+ x->x_ambi_channel_weight[k] = (double)atom_getfloat(argv++);
+ k++;
+ if(x->x_n_dim == 2)
+ {
+ for(i=1; i<=n; i++)
+ {
+ d = (double)atom_getfloat(argv++);
+ x->x_ambi_channel_weight[k] = d;
+ k++;
+ x->x_ambi_channel_weight[k] = d;
+ k++;
+ }
+ }
+ else
+ {
+ int j, m;
+
+ for(i=1; i<=n; i++)
+ {
+ d = (double)atom_getfloat(argv++);
+ m = 2*i + 1;
+ for(j=0; j<m; j++)
+ {
+ x->x_ambi_channel_weight[k] = d;
+ k++;
+ }
+ }
+ }
+ }
+ else
+ post("bin_ambi_reduced_decode-ERROR: ambi_weight needs %d float weights", x->x_n_order);
+}
+
+static void bin_ambi_reduced_decode_sing_range(t_bin_ambi_reduced_decode *x, t_floatarg f)
+{
+ if(f < 0.0f)
+ x->x_sing_range = -(double)f;
+ else
+ x->x_sing_range = (double)f;
+}
+
+static void bin_ambi_reduced_decode_free(t_bin_ambi_reduced_decode *x)
+{
+ freebytes(x->x_hrir_filename, x->x_n_ambi * sizeof(t_symbol *));
+ freebytes(x->x_s_hrir, x->x_n_ambi * sizeof(t_symbol *));
+ freebytes(x->x_s_hrtf_re, x->x_n_ambi * sizeof(t_symbol *));
+ freebytes(x->x_s_hrtf_im, x->x_n_ambi * sizeof(t_symbol *));
+ freebytes(x->x_inv_work1, x->x_n_ambi * x->x_n_ambi * sizeof(double));
+ freebytes(x->x_inv_work2, 2 * x->x_n_ambi * x->x_n_ambi * sizeof(double));
+ freebytes(x->x_inv_buf2, 2 * x->x_n_ambi * sizeof(double));
+ freebytes(x->x_ambi_channel_weight, x->x_n_ambi * sizeof(double));
+
+ freebytes(x->x_delta, x->x_n_ambi * sizeof(int));
+ freebytes(x->x_phi, x->x_n_ambi * sizeof(int));
+ freebytes(x->x_phi_sym, x->x_n_ambi * sizeof(int));
+ freebytes(x->x_sym_flag, x->x_n_ambi * sizeof(int));
+
+ freebytes(x->x_spec, x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+ freebytes(x->x_sin_cos, x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+
+ freebytes(x->x_beg_hrir, x->x_fftsize * x->x_n_ambi * sizeof(float));
+ freebytes(x->x_beg_hrtf_re, x->x_n_ambi * sizeof(float *));
+ freebytes(x->x_beg_hrtf_im, x->x_n_ambi * sizeof(float *));
+}
+
+/*
+ 1.arg_ int prefix;
+ 2.arg: t_symbol *hrir_name;
+ 3.arg: t_symbol *hrtf_re_name;
+ 4.arg: t_symbol *hrtf_im_name;
+ 5.arg: t_symbol *hrir_fade_out_name;
+ 6.arg: int ambi_order;
+ 7.arg: int dim;
+ 8.arg: int fftsize;
+ */
+
+static void *bin_ambi_reduced_decode_new(t_symbol *s, int argc, t_atom *argv)
+{
+ t_bin_ambi_reduced_decode *x = (t_bin_ambi_reduced_decode *)pd_new(bin_ambi_reduced_decode_class);
+ char buf[400];
+ int i, j, fftok;
+ int n_order, n_dim, n_ambi, fftsize, prefix;
+ t_symbol *s_hrir;
+ t_symbol *s_hrtf_re;
+ t_symbol *s_hrtf_im;
+ t_symbol *s_fade_out_hrir;
+
+ if((argc >= 8) &&
+ IS_A_FLOAT(argv,0) &&
+ IS_A_SYMBOL(argv,1) &&
+ IS_A_SYMBOL(argv,2) &&
+ IS_A_SYMBOL(argv,3) &&
+ IS_A_SYMBOL(argv,4) &&
+ IS_A_FLOAT(argv,5) &&
+ IS_A_FLOAT(argv,6) &&
+ IS_A_FLOAT(argv,7))
+ {
+ prefix = (int)atom_getintarg(0, argc, argv);
+
+ s_hrir = (t_symbol *)atom_getsymbolarg(1, argc, argv);
+ s_hrtf_re = (t_symbol *)atom_getsymbolarg(2, argc, argv);
+ s_hrtf_im = (t_symbol *)atom_getsymbolarg(3, argc, argv);
+ s_fade_out_hrir = (t_symbol *)atom_getsymbolarg(4, argc, argv);
+
+ n_order = (int)atom_getintarg(5, argc, argv);
+ n_dim = (int)atom_getintarg(6, argc, argv);
+ fftsize = (int)atom_getintarg(7, argc, argv);
+
+ if(n_order < 1)
+ n_order = 1;
+
+ if(n_dim == 3)
+ {
+ if(n_order > 5)
+ n_order = 5;
+ n_ambi = (n_order + 1)*(n_order + 1);
+ }
+ else
+ {
+ if(n_order > 12)
+ n_order = 12;
+ n_dim = 2;
+ n_ambi = 2 * n_order + 1;
+ }
+
+ j = 2;
+ fftok = 0;
+ for(i=0; i<21; i++)
+ {
+ if(j == fftsize)
+ {
+ fftok = 1;
+ i = 22;
+ }
+ j *= 2;
+ }
+
+ if(!fftok)
+ {
+ fftsize = 512;
+ post("bin_ambi_reduced_decode-ERROR: fftsize not equal to 2 ^ n !!!");
+ }
+
+ x->x_n_dim = n_dim;
+ x->x_n_ambi = n_ambi;
+ x->x_n_order = n_order;
+ x->x_fftsize = fftsize;
+
+ x->x_hrir_filename = (t_symbol **)getbytes(x->x_n_ambi * sizeof(t_symbol *));
+ x->x_s_hrir = (t_symbol **)getbytes(x->x_n_ambi * sizeof(t_symbol *));
+ x->x_s_hrtf_re = (t_symbol **)getbytes(x->x_n_ambi * sizeof(t_symbol *));
+ x->x_s_hrtf_im = (t_symbol **)getbytes(x->x_n_ambi * sizeof(t_symbol *));
+
+ for(i=0; i<n_ambi; i++)
+ {
+ sprintf(buf, "%d_%d_%s", prefix, i+1, s_hrir->s_name);
+ x->x_s_hrir[i] = gensym(buf);
+
+ sprintf(buf, "%d_%d_%s", prefix, i+1, s_hrtf_re->s_name);
+ x->x_s_hrtf_re[i] = gensym(buf);
+
+ sprintf(buf, "%d_%d_%s", prefix, i+1, s_hrtf_im->s_name);
+ x->x_s_hrtf_im[i] = gensym(buf);
+ }
+
+ sprintf(buf, "%d_%s", prefix, s_fade_out_hrir->s_name);
+ x->x_s_fade_out_hrir = gensym(buf);
+
+ x->x_inv_work1 = (double *)getbytes(x->x_n_ambi * x->x_n_ambi * sizeof(double));
+ x->x_inv_work2 = (double *)getbytes(2 * x->x_n_ambi * x->x_n_ambi * sizeof(double));
+ x->x_inv_buf2 = (double *)getbytes(2 * x->x_n_ambi * sizeof(double));
+ x->x_ambi_channel_weight = (double *)getbytes(x->x_n_ambi * sizeof(double));
+
+ x->x_delta = (int *)getbytes(x->x_n_ambi * sizeof(int));
+ x->x_phi = (int *)getbytes(x->x_n_ambi * sizeof(int));
+ x->x_phi_sym = (int *)getbytes(x->x_n_ambi * sizeof(int));
+ x->x_sym_flag = (int *)getbytes(x->x_n_ambi * sizeof(int));
+
+ x->x_spec = (BIN_AMBI_COMPLEX *)getbytes(x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+ x->x_sin_cos = (BIN_AMBI_COMPLEX *)getbytes(x->x_fftsize * sizeof(BIN_AMBI_COMPLEX));
+
+ x->x_beg_fade_out_hrir = (float *)0;
+ x->x_beg_hrir = (float *)getbytes(x->x_fftsize * x->x_n_ambi * sizeof(float));
+ x->x_beg_hrtf_re = (float **)getbytes(x->x_n_ambi * sizeof(float *));
+ x->x_beg_hrtf_im = (float **)getbytes(x->x_n_ambi * sizeof(float *));
+
+ x->x_sqrt3 = sqrt(3.0);
+ x->x_sqrt5_2 = sqrt(5.0) / 2.0;
+ x->x_sqrt6_4 = sqrt(6.0) / 4.0;
+ x->x_sqrt10_4 = sqrt(10.0) / 4.0;
+ x->x_sqrt15_2 = sqrt(15.0) / 2.0;
+ x->x_sqrt35_8 = sqrt(35.0) / 8.0;
+ x->x_sqrt70_4 = sqrt(70.0) / 4.0;
+ x->x_sqrt126_16 = sqrt(126.0) / 16.0;
+ x->x_sqrt315_8 = sqrt(315.0) / 8.0;
+ x->x_sqrt105_4 = sqrt(105.0) / 4.0;
+ x->x_pi_over_180 = 4.0 * atan(1.0) / 180.0;
+ x->x_sing_range = 1.0e-10;
+ for(i=0; i<n_ambi; i++)
+ x->x_ambi_channel_weight[i] = 1.0;
+
+ bin_ambi_reduced_decode_init_cos(x);
+
+ outlet_new(&x->x_obj, &s_list);
+ x->x_out_sign_sum = outlet_new(&x->x_obj, &s_list);
+ return(x);
+ }
+ else
+ {
+ post("bin_ambi_reduced_decode-ERROR: need 1 float + 4 symbols + 3 floats arguments:");
+ post(" prefix + hrir_name + hrtf_re_name + hrtf_im_name + hrir_fade_out_name +");
+ post(" ambi_order + ambi_dimension + fftsize");
+ return(0);
+ }
+}
+
+void bin_ambi_reduced_decode_setup(void)
+{
+ bin_ambi_reduced_decode_class = class_new(gensym("bin_ambi_reduced_decode"), (t_newmethod)bin_ambi_reduced_decode_new, (t_method)bin_ambi_reduced_decode_free,
+ sizeof(t_bin_ambi_reduced_decode), 0, A_GIMME, 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_ls, gensym("ls"), A_GIMME, 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_calc_pinv, gensym("calc_pinv"), 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_load_HRIR, gensym("load_HRIR"), A_FLOAT, 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_check_arrays, gensym("check_arrays"), A_FLOAT, 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_calc_reduced, gensym("calc_reduced"), A_FLOAT, 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_decoding, gensym("decoding"), 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_ambi_weight, gensym("ambi_weight"), A_GIMME, 0);
+ class_addmethod(bin_ambi_reduced_decode_class, (t_method)bin_ambi_reduced_decode_sing_range, gensym("sing_range"), A_DEFFLOAT, 0);
+ class_sethelpsymbol(bin_ambi_reduced_decode_class, gensym("iemhelp2/help-bin_ambi_reduced_decode"));
+}
+/*
+Reihenfolge:
+n_ambi x bin_ambi_reduced_decode_ls
+
+bin_ambi_reduced_decode_calc_pinv
+
+n_ambi x bin_ambi_reduced_decode_load_HRIR
+
+n_ambi x bin_ambi_reduced_decode_check_arrays
+
+n_ambi x bin_ambi_reduced_decode_calc_reduced
+
+bin_ambi_reduced_decode_decoding
+
+*/
diff --git a/src/iem_bin_ambi.c b/src/iem_bin_ambi.c
new file mode 100644
index 0000000..5fff1e9
--- /dev/null
+++ b/src/iem_bin_ambi.c
@@ -0,0 +1,35 @@
+/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
+* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
+
+iem_bin_ambi written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2003 */
+
+#ifdef NT
+#pragma warning( disable : 4244 )
+#pragma warning( disable : 4305 )
+#endif
+
+
+#include "m_pd.h"
+#include "iemlib.h"
+
+static t_class *iem_bin_ambi_class;
+
+static void *iem_bin_ambi_new(void)
+{
+ t_object *x = (t_object *)pd_new(iem_bin_ambi_class);
+
+ return (x);
+}
+
+void bin_ambi_calc_HRTF_setup(void);
+void bin_ambi_reduced_decode_setup(void);
+
+/* ------------------------ setup routine ------------------------- */
+
+void iem_bin_ambi_setup(void)
+{
+ bin_ambi_calc_HRTF_setup();
+ bin_ambi_reduced_decode_setup();
+
+ post("iem_bin_ambi (R-1.15) library loaded!");
+}
diff --git a/src/iem_bin_ambi.dsp b/src/iem_bin_ambi.dsp
new file mode 100644
index 0000000..a194492
--- /dev/null
+++ b/src/iem_bin_ambi.dsp
@@ -0,0 +1,85 @@
+# Microsoft Developer Studio Project File - Name="iem_bin_ambi" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** NICHT BEARBEITEN **
+
+# TARGTYPE "Win32 (x86) External Target" 0x0106
+
+CFG=iem_bin_ambi - Win32 Debug
+!MESSAGE Dies ist kein gültiges Makefile. Zum Erstellen dieses Projekts mit NMAKE
+!MESSAGE verwenden Sie den Befehl "Makefile exportieren" und führen Sie den Befehl
+!MESSAGE
+!MESSAGE NMAKE /f "iem_bin_ambi.mak".
+!MESSAGE
+!MESSAGE Sie können beim Ausführen von NMAKE eine Konfiguration angeben
+!MESSAGE durch Definieren des Makros CFG in der Befehlszeile. Zum Beispiel:
+!MESSAGE
+!MESSAGE NMAKE /f "iem_bin_ambi.mak" CFG="iem_bin_ambi - Win32 Debug"
+!MESSAGE
+!MESSAGE Für die Konfiguration stehen zur Auswahl:
+!MESSAGE
+!MESSAGE "iem_bin_ambi - Win32 Release" (basierend auf "Win32 (x86) External Target")
+!MESSAGE "iem_bin_ambi - Win32 Debug" (basierend auf "Win32 (x86) External Target")
+!MESSAGE
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+
+!IF "$(CFG)" == "iem_bin_ambi - Win32 Release"
+
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Cmd_Line "NMAKE /f makefile_win"
+# PROP BASE Rebuild_Opt "/a"
+# PROP BASE Target_File "makefile_win.exe"
+# PROP BASE Bsc_Name "makefile_win.bsc"
+# PROP BASE Target_Dir ""
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Cmd_Line "NMAKE /f makefile_win"
+# PROP Rebuild_Opt "/a"
+# PROP Target_File "iem_bin_ambi.exe"
+# PROP Bsc_Name "iem_bin_ambi.bsc"
+# PROP Target_Dir ""
+
+!ELSEIF "$(CFG)" == "iem_bin_ambi - Win32 Debug"
+
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Cmd_Line "NMAKE /f makefile_win"
+# PROP BASE Rebuild_Opt "/a"
+# PROP BASE Target_File "makefile_win.exe"
+# PROP BASE Bsc_Name "makefile_win.bsc"
+# PROP BASE Target_Dir ""
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "Debug"
+# PROP Intermediate_Dir "Debug"
+# PROP Cmd_Line "NMAKE /f makefile_win"
+# PROP Rebuild_Opt "/a"
+# PROP Target_File "iem_bin_ambi.exe"
+# PROP Bsc_Name "iem_bin_ambi.bsc"
+# PROP Target_Dir ""
+
+!ENDIF
+
+# Begin Target
+
+# Name "iem_bin_ambi - Win32 Release"
+# Name "iem_bin_ambi - Win32 Debug"
+
+!IF "$(CFG)" == "iem_bin_ambi - Win32 Release"
+
+!ELSEIF "$(CFG)" == "iem_bin_ambi - Win32 Debug"
+
+!ENDIF
+
+# Begin Source File
+
+SOURCE=.\makefile_win
+# End Source File
+# End Target
+# End Project
diff --git a/src/iem_bin_ambi.dsw b/src/iem_bin_ambi.dsw
new file mode 100644
index 0000000..da5b544
--- /dev/null
+++ b/src/iem_bin_ambi.dsw
@@ -0,0 +1,29 @@
+Microsoft Developer Studio Workspace File, Format Version 6.00
+# WARNUNG: DIESE ARBEITSBEREICHSDATEI DARF NICHT BEARBEITET ODER GELÖSCHT WERDEN!
+
+###############################################################################
+
+Project: "iem_bin_ambi"=.\iem_bin_ambi.dsp - Package Owner=<4>
+
+Package=<5>
+{{{
+}}}
+
+Package=<4>
+{{{
+}}}
+
+###############################################################################
+
+Global:
+
+Package=<5>
+{{{
+}}}
+
+Package=<3>
+{{{
+}}}
+
+###############################################################################
+
diff --git a/src/iem_bin_ambi.h b/src/iem_bin_ambi.h
new file mode 100644
index 0000000..1757d2e
--- /dev/null
+++ b/src/iem_bin_ambi.h
@@ -0,0 +1,16 @@
+/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
+* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
+
+iem_bin_ambi written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2003 */
+
+#ifndef __IEMBINAMBI_H__
+#define __IEMBINAMBI_H__
+
+typedef struct
+{
+ float real;
+ float imag;
+}
+BIN_AMBI_COMPLEX;
+
+#endif
diff --git a/src/iemlib.h b/src/iemlib.h
new file mode 100644
index 0000000..48a02bd
--- /dev/null
+++ b/src/iemlib.h
@@ -0,0 +1,79 @@
+/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
+* WARRANTIES, see the file, "LICENSE.txt," in this distribution.
+
+iem_bin_ambi written by Thomas Musil, Copyright (c) IEM KUG Graz Austria 2000 - 2003 */
+
+#ifndef __IEMLIB_H__
+#define __IEMLIB_H__
+
+
+#define IS_A_POINTER(atom,index) ((atom+index)->a_type == A_POINTER)
+#define IS_A_FLOAT(atom,index) ((atom+index)->a_type == A_FLOAT)
+#define IS_A_SYMBOL(atom,index) ((atom+index)->a_type == A_SYMBOL)
+#define IS_A_DOLLAR(atom,index) ((atom+index)->a_type == A_DOLLAR)
+#define IS_A_DOLLSYM(atom,index) ((atom+index)->a_type == A_DOLLSYM)
+#define IS_A_SEMI(atom,index) ((atom+index)->a_type == A_SEMI)
+#define IS_A_COMMA(atom,index) ((atom+index)->a_type == A_COMMA)
+
+
+#ifdef NT
+int sys_noloadbang;
+//t_symbol *iemgui_key_sym=0;
+#include <io.h>
+#else
+extern int sys_noloadbang;
+//extern t_symbol *iemgui_key_sym;
+#include <unistd.h>
+#endif
+
+#define DEFDELVS 64
+#define XTRASAMPS 4
+#define SAMPBLK 4
+
+
+#define UNITBIT32 1572864. /* 3*2^19; bit 32 has place value 1 */
+
+ /* machine-dependent definitions. These ifdefs really
+ should have been by CPU type and not by operating system! */
+#ifdef IRIX
+ /* big-endian. Most significant byte is at low address in memory */
+#define HIOFFSET 0 /* word offset to find MSB */
+#define LOWOFFSET 1 /* word offset to find LSB */
+#define int32 long /* a data type that has 32 bits */
+#else
+#ifdef NT
+ /* little-endian; most significant byte is at highest address */
+#define HIOFFSET 1
+#define LOWOFFSET 0
+#define int32 long
+#else
+#ifdef __linux__
+
+#include <endian.h>
+
+#if !defined(__BYTE_ORDER) || !defined(__LITTLE_ENDIAN)
+#error No byte order defined
+#endif
+
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define HIOFFSET 1
+#define LOWOFFSET 0
+#else
+#define HIOFFSET 0 /* word offset to find MSB */
+#define LOWOFFSET 1 /* word offset to find LSB */
+#endif /* __BYTE_ORDER */
+
+#include <sys/types.h>
+#define int32 int32_t
+
+#endif /* __linux__ */
+#endif /* NT */
+#endif /* SGI */
+
+union tabfudge
+{
+ double tf_d;
+ int32 tf_i[2];
+};
+
+#endif