Allolib  1.0
C++ Components For Interactive Multimedia
al_Crossover.hpp
1 #ifndef INCLUDE_AL_CROSSOVER_HPP
2 #define INCLUDE_AL_CROSSOVER_HPP
3 
4 /* Allocore --
5  Multimedia / virtual environment application class library
6 
7  Copyright (C) 2009. AlloSphere Research Group, Media Arts & Technology, UCSB.
8  Copyright (C) 2012. The Regents of the University of California. All
9  rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without
12  modification, are permitted provided that the following conditions are
13  met:
14 
15  Redistributions of source code must retain the above copyright
16  notice, this list of conditions and the following disclaimer.
17 
18  Redistributions in binary form must reproduce the above
19  copyright notice, this list of conditions and the following disclaimer in the
20  documentation and/or other materials provided with the
21  distribution.
22 
23  Neither the name of the University of California nor the names
24  of its contributors may be used to endorse or promote products derived from
25  this software without specific prior written permission.
26 
27  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
31  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
34  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
35  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
36  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 
39 
40  File description:
41  CrossOver: a cross-over shelf filter that sums to an allpass
42  (Useful for mixing different Ambisonic encoding flavors)
43 
44  File author(s):
45  Graham Wakefield, 2010, grrrwaaa@gmail.com
46 */
47 
48 #include <float.h>
49 #include <stdio.h>
50 
51 #include "al/math/al_Constants.hpp"
52 
53 namespace al {
54 
61 template <typename T = double> class Crossover {
62 public:
64  void freq(T f, T fs);
65 
66  Crossover(T f = (T)600, T fs = (T)44100.) {
67  freq(f, fs);
68  clear();
69  }
70 
72  void next(const T in, T *lo, T *hi);
73 
74  void clear() {
75  mZ0 = (T)0;
76  mZ1 = (T)0;
77  mZ2 = (T)0;
78  }
79 
80 protected:
81  // coefficients and history
82  T mC0, mC1, mZ0, mZ1, mZ2;
83 };
84 
85 template <> void Crossover<double>::freq(double f, double fs) {
86  double rad = M_PI * 2. * f / fs;
87  double cosine = cos(rad);
88  double sine = sin(rad);
89  if (abs(c) > 0.0001) {
90  mC0 = (sine - 1.) / cosine;
91  } else {
92  mC0 = cosine * 0.5;
93  }
94  mC1 = (1. + mC0) * 0.5;
95 }
96 
97 template <>
98 inline void Crossover<double>::next(const double in, double *lo, double *hi) {
99  static const double denorm_offset = DBL_EPSILON * 2.;
100 
101  const double v0 = in - mC0 * mZ0;
102  const double x0 = mZ0 + mC0 * v0;
103 
104  const double v1 = mC1 * (in - mZ1);
105  const double x1 = v1 + mZ1;
106 
107  const double v2 = mC1 * (x1 - mZ2);
108  const double x2 = v2 + mZ2;
109 
110  mZ0 = v0 + denorm_offset;
111  mZ1 = v1 + x1 + denorm_offset;
112  mZ2 = v2 + x2 + denorm_offset;
113 
114  *lo = x2;
115  *hi = x0 - x2;
116 }
117 
118 template <> void Crossover<float>::freq(float f, float fs) {
119  float rad = M_PI * 2.f * f / fs;
120  float cosine = cosf(rad);
121  float sine = sinf(rad);
122  if (fabs(cosine) > 0.0001f) {
123  mC0 = (sine - 1.f) / cosine;
124  } else {
125  mC0 = cosine * 0.5f;
126  }
127  mC1 = (1.f + mC0) * 0.5f;
128 }
129 
130 template <>
131 inline void Crossover<float>::next(const float in, float *lo, float *hi) {
132  static const float denorm_offset = FLT_EPSILON * 2.;
133 
134  const float v0 = in - mC0 * mZ0;
135  const float x0 = mZ0 + mC0 * v0;
136 
137  const float v1 = mC1 * (in - mZ1);
138  const float x1 = v1 + mZ1;
139 
140  const float v2 = mC1 * (x1 - mZ2);
141  const float x2 = v2 + mZ2;
142 
143  mZ0 = v0 + denorm_offset;
144  mZ1 = v1 + x1 + denorm_offset;
145  mZ2 = v2 + x2 + denorm_offset;
146 
147  *lo = x2;
148  *hi = x0 - x2;
149 }
150 
151 } // namespace al
152 #endif
void freq(T f, T fs)
set the cross-over middle frequency
void next(const T in, T *lo, T *hi)
process one sample and return hi/lo shelf
Definition: al_App.hpp:23