common: prevent buffer overflow
[supercollider.git] / include / server / SC_Complex.h
blob39738519e27827d180cdcdf097c3bdd9d493fb91
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef _SC_Complex_
23 #define _SC_Complex_
25 #include "SC_Types.h"
26 #include "float.h"
28 ////////////////////////////////////////////////////////////////////////////////
30 struct Polar;
32 struct Complex
34 Complex() {}
35 Complex(float r, float i) : real(r), imag(i) {}
36 void Set(float r, float i) { real = r; imag = i; }
38 Complex& operator=(Complex b) { real = b.real; imag = b.imag; return *this; }
39 Complex& operator=(float b) { real = b; imag = 0.; return *this; }
41 Polar ToPolar();
42 Polar ToPolarApx();
44 void ToPolarInPlace();
45 void ToPolarApxInPlace();
47 float real, imag;
50 struct Polar
52 Polar() {}
53 Polar(float m, float p) : mag(m), phase(p) {}
54 void Set(float m, float p) { mag = m; phase = p; }
56 Complex ToComplex();
57 Complex ToComplexApx();
59 void ToComplexInPlace();
60 void ToComplexApxInPlace();
62 float mag, phase;
65 struct ComplexFT
67 float dc, nyq;
68 Complex complex[1];
71 struct PolarFT
73 float dc, nyq;
74 Polar polar[1];
77 void ToComplex(Polar in, Complex& out);
79 inline Complex operator+(Complex a, Complex b) { return Complex(a.real + b.real, a.imag + b.imag); }
80 inline Complex operator+(Complex a, float b) { return Complex(a.real + b, a.imag); }
81 inline Complex operator+(float a, Complex b) { return Complex(a + b.real, b.imag); }
83 inline Complex& operator+=(Complex& a, const Complex& b) { a.real += b.real, a.imag += b.imag; return a; }
84 inline Complex& operator+=(Complex& a, float b) { a.real += b; return a; }
86 inline Complex operator-(Complex a, Complex b) { return Complex(a.real - b.real, a.imag - b.imag); }
87 inline Complex operator-(Complex a, float b) { return Complex(a.real - b, a.imag); }
88 inline Complex operator-(float a, Complex b) { return Complex(a - b.real, b.imag); }
90 inline Complex operator-=(Complex a, Complex b) { a.real -= b.real, a.imag -= b.imag; return a; }
91 inline Complex operator-=(Complex a, float b) { a.real -= b; return a; }
93 inline Complex operator*(Complex a, Complex b)
95 return Complex(a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real);
98 inline Complex operator*(Complex a, float b)
100 return Complex(a.real * b, a.imag * b);
103 inline Complex operator*(float a, Complex b)
105 return Complex(b.real * a, b.imag * a);
108 inline Complex operator*=(Complex a, Complex b)
110 a.Set(
111 a.real * b.real - a.imag * b.imag,
112 a.real * b.imag + a.imag * b.real
114 return a;
117 inline Complex operator*=(Complex a, float b)
119 a.real *= b;
120 a.imag *= b;
121 return a;
125 inline Polar operator*(Polar a, float b)
127 return Polar(a.mag * b, a.phase);
130 inline Polar operator*(float a, Polar b)
132 return Polar(a * b.mag, b.phase);
135 inline Polar operator*=(Polar a, float b)
137 a.mag *= b;
138 return a;
142 #endif