Better structure
This commit is contained in:
1 parent
6ad9f6a1ef
commit
4c597cb4f8
15 files changed
+54
-44
No files matched your search
@@ -0,0 +1,3 @@
|
||||
code is based on Performous: https://github.com/performous/performous/tree/master/game
|
||||
Some modifications were made to make it compile with VS2012, fix warnings and make it suitable for our needs
|
||||
From 25.03.2014
|
||||
@@ -0,0 +1,346 @@
|
||||
GNU GPLv2 or later. Some parts of the project may have more liberal licenses.
|
||||
See Authors.txt for information on copyright holders and licenses of specific
|
||||
parts. Fonts are licensed under SIL OFL.
|
||||
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
|
||||
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 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.
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file fft.hpp FFT and related facilities.
|
||||
*/
|
||||
|
||||
#include "sample.hpp"
|
||||
#include <complex>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.141592653589793
|
||||
#endif
|
||||
|
||||
#define M_TAU (2.0 * M_PI)
|
||||
|
||||
namespace da {
|
||||
|
||||
// With g++ optimization -fcx-limited-range should be used for 5x performance boost.
|
||||
|
||||
// Based on the description of Volodymyr Myrnyy in
|
||||
// http://www.dspdesignline.com/showArticle.jhtml?printableArticle=true&articleId=199903272
|
||||
template<unsigned P, typename T> struct DanielsonLanczos {
|
||||
static void apply(std::complex<T>* data) {
|
||||
constexpr std::size_t N = 1 << P;
|
||||
constexpr std::size_t M = N / 2;
|
||||
// Compute even and odd halves
|
||||
DanielsonLanczos<P - 1, T>().apply(data);
|
||||
DanielsonLanczos<P - 1, T>().apply(data + M);
|
||||
// Combine the results
|
||||
std::complex<T> w(1.0);
|
||||
for (std::size_t i = 0; i < M; ++i) {
|
||||
const std::complex<T> temp = data[i + M] * w;
|
||||
data[M + i] = data[i] - temp;
|
||||
data[i] += temp;
|
||||
w *= std::polar(1.0, -M_TAU / N);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T> struct DanielsonLanczos<0, T> { static void apply(std::complex<T>*) {} };
|
||||
|
||||
/** Perform FFT on data. **/
|
||||
template<unsigned P, typename T> void fft(std::complex<T>* data) {
|
||||
// Perform bit-reversal sorting of sample data.
|
||||
constexpr std::size_t N = 1 << P;
|
||||
std::size_t j = 0;
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
if (i < j) std::swap(data[i], data[j]);
|
||||
std::size_t m = N / 2;
|
||||
while (m > 1 && m <= j) { j -= m; m >>= 1; }
|
||||
j += m;
|
||||
}
|
||||
// Do the actual calculation
|
||||
DanielsonLanczos<P, T>::apply(data);
|
||||
}
|
||||
|
||||
/** Perform FFT on data from floating point iterator, windowing the input. **/
|
||||
template<unsigned P, typename InIt, typename Window> std::vector<std::complex<float> > fft(InIt begin, Window window) {
|
||||
std::vector<std::complex<float> > data(1 << P);
|
||||
// Perform bit-reversal sorting of sample data.
|
||||
constexpr std::size_t N = 1 << P;
|
||||
std::size_t j = 0;
|
||||
for (std::size_t i = 0; i < N; ++i) {
|
||||
data[j] = *begin++ * window[i];
|
||||
std::size_t m = N / 2;
|
||||
while (m > 1 && m <= j) { j -= m; m >>= 1; }
|
||||
j += m;
|
||||
}
|
||||
// Do the actual calculation
|
||||
DanielsonLanczos<P, float>::apply(&data[0]);
|
||||
return data;
|
||||
}
|
||||
|
||||
template<unsigned P, typename T> void ifft(std::complex<T>* data) {
|
||||
constexpr std::size_t N = 1 << P;
|
||||
for (std::size_t i = 0; i < N; ++i) data[i] = std::conj(data[i]); // Invert phase so that we can use FFT to do IFFT
|
||||
fft<P>(data);
|
||||
constexpr std::complex<T> scale(1.0/N, 0.0);
|
||||
for (std::size_t i = 0; i < N; ++i) data[i] = scale * std::conj(data[i]); // Invert back, and apply IFFT scaling
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include "../compatibility.h"
|
||||
|
||||
/**
|
||||
* @file sample.hpp Sample format definition and format conversions.
|
||||
*/
|
||||
|
||||
namespace da {
|
||||
|
||||
using std::round;
|
||||
|
||||
// Should be a floating-point type
|
||||
typedef float sample_t;
|
||||
|
||||
// A helper function for clamping a value to a certain range
|
||||
template <typename T> T clamp(T val, T min, T max) {
|
||||
if (val < min) val = min;
|
||||
if (val > max) val = max;
|
||||
return val;
|
||||
}
|
||||
|
||||
constexpr sample_t max_s16(32767), min_s16 = -max_s16 - sample_t(1);
|
||||
constexpr sample_t max_s24(8388607), min_s24 = -max_s24 - sample_t(1);
|
||||
constexpr sample_t max_s32(2147483647), min_s32= -max_s32 - sample_t(1);
|
||||
constexpr double tau(6.2831853071795864769252867665590057683943387987502116); ///< One full circle
|
||||
constexpr double pi = tau / sample_t(2); ///< Half circle
|
||||
constexpr double eps(1e-10); ///< Tiny positive value
|
||||
|
||||
/// Mathematical sinc function
|
||||
template<typename Float> static inline Float msinc(Float x) { return std::abs(x) < Float(eps) ? Float(1) : std::sin(x) / x; }
|
||||
|
||||
/// Normalized (signal processing) sinc function
|
||||
template<typename Float> static inline Float sinc(Float x) { return msinc(Float(pi) * x); }
|
||||
|
||||
/// Lanczos kernel of size A
|
||||
template<unsigned A, typename Float> static inline Float lanc(Float x) {
|
||||
return std::abs(x) < A ? sinc(x) * sinc(x / A) : Float();
|
||||
}
|
||||
|
||||
// The following conversions provide lossless conversions between floats
|
||||
// and integers. Be sure to use only these conversions or otherwise the
|
||||
// conversions may not be lossless, due to different scaling factors being
|
||||
// used by different libraries.
|
||||
|
||||
// The negative minimum integer value produces sample_t value slightly
|
||||
// more negative than -1.0 but this is necessary in order to prevent
|
||||
// clipping in the float-to-int conversions. Now amplitude 1.0 in floating
|
||||
// point produces -32767 .. 32767 symmetrical non-clipping range in s16.
|
||||
|
||||
static inline sample_t conv_from_s16(int s) { return s / max_s16; }
|
||||
static inline sample_t conv_from_s24(int s) { return s / max_s24; }
|
||||
static inline sample_t conv_from_s32(int s) { return s / max_s32; }
|
||||
// The rounding is strictly not necessary, but it greatly improves
|
||||
// the error tolerance if any floating point calculations are done.
|
||||
// The ugly static_casts are required to avoid warnings in MSVC.
|
||||
static inline int conv_to_s16(sample_t s) { return clamp(static_cast<int>(round(s * max_s16)), static_cast<int>(min_s16), static_cast<int>(max_s16)); }
|
||||
static inline int conv_to_s24(sample_t s) { return clamp(static_cast<int>(round(s * max_s24)), static_cast<int>(min_s24), static_cast<int>(max_s24)); }
|
||||
static inline int conv_to_s32(sample_t s) { return static_cast<int>(clamp(round(s * max_s32), min_s32, max_s32 )); }
|
||||
// Non-rounding non-clamping versions are provided for very low end devices (still lossless)
|
||||
static inline int conv_to_s16_fast(sample_t s) { return static_cast<int>(s * max_s16); }
|
||||
static inline int conv_to_s24_fast(sample_t s) { return static_cast<int>(s * max_s24); }
|
||||
static inline int conv_to_s32_fast(sample_t s) { return static_cast<int>(s * max_s32); }
|
||||
|
||||
template <typename ValueType> class step_iterator: public std::iterator<std::random_access_iterator_tag, ValueType> {
|
||||
ValueType* m_pos;
|
||||
std::ptrdiff_t m_step;
|
||||
public:
|
||||
step_iterator(ValueType* pos, std::ptrdiff_t step): m_pos(pos), m_step(step) {}
|
||||
ValueType& operator*() { return *m_pos; }
|
||||
step_iterator operator+(std::ptrdiff_t rhs) { return step_iterator(m_pos + m_step * rhs, m_step); }
|
||||
step_iterator& operator++() { m_pos += m_step; return *this; }
|
||||
step_iterator operator++(int) { step_iterator ret = *this; ++*this; return ret; }
|
||||
bool operator!=(step_iterator const& rhs) const { return m_pos != rhs.m_pos; }
|
||||
std::ptrdiff_t operator-(step_iterator const& rhs) const { return (m_pos - rhs.m_pos) / m_step; }
|
||||
// TODO: more operators
|
||||
};
|
||||
|
||||
typedef step_iterator<sample_t> sample_iterator;
|
||||
typedef step_iterator<sample_t const> sample_const_iterator;
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
#include "pitch.hh"
|
||||
|
||||
#include "util.hh"
|
||||
#include "libda/fft.hpp"
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
// Limit the range to avoid noise and useless computation
|
||||
static const double FFT_MINFREQ = 45.0;
|
||||
static const double FFT_MAXFREQ = 5000.0;
|
||||
|
||||
Tone::Tone():
|
||||
freq(0.0),
|
||||
db(-getInf()),
|
||||
stabledb(-getInf()),
|
||||
age()
|
||||
{
|
||||
for (auto& h: harmonics) h = -getInf();
|
||||
}
|
||||
|
||||
void Tone::print() const {
|
||||
if (age < Tone::MINAGE) return;
|
||||
std::cout << std::fixed << std::setprecision(1) << freq << " Hz, age " << age << ", " << db << " dB:";
|
||||
for (std::size_t i = 0; i < 8; ++i) std::cout << " " << harmonics[i];
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
bool Tone::operator==(double f) const {
|
||||
return std::abs(freq / f - 1.0) < 0.05;
|
||||
}
|
||||
|
||||
Analyzer::Analyzer(double rate, std::string id, unsigned step):
|
||||
m_step(step),
|
||||
m_resampleFactor(1.0),
|
||||
m_resamplePos(),
|
||||
m_rate(rate),
|
||||
m_id(id),
|
||||
m_window(FFT_N),
|
||||
m_fftLastPhase(FFT_N / 2),
|
||||
m_peak(0.0),
|
||||
m_oldfreq(0.0)
|
||||
{
|
||||
if (m_step > FFT_N) throw std::logic_error("Analyzer step is larger that FFT_N (ideally it should be less than a fourth of FFT_N).");
|
||||
// Hamming window
|
||||
for (size_t i=0; i < FFT_N; i++) {
|
||||
m_window[i] = static_cast<float>(0.53836 - 0.46164 * std::cos(2.0 * M_PI * i / (FFT_N - 1)));
|
||||
}
|
||||
}
|
||||
|
||||
bool Analyzer::output(float* begin, float* end, double rate) {
|
||||
constexpr unsigned a = 2;
|
||||
const size_t size = m_passthrough.size();
|
||||
const unsigned out = static_cast<unsigned>((end - begin) / 2 /* stereo */);
|
||||
if (out == 0) return false;
|
||||
const unsigned in = static_cast<unsigned>(m_resampleFactor * (m_rate / rate) * out + 2 * a /* lanczos kernel */ + 5 /* safety margin for rounding errors */);
|
||||
float pcm[m_passthrough.capacity];
|
||||
if(!m_passthrough.read(pcm, pcm + in + 4))
|
||||
return false;
|
||||
for (unsigned i = 0; i < out; ++i) {
|
||||
double s = 0.0;
|
||||
unsigned k = static_cast<unsigned>(m_resamplePos);
|
||||
double x = m_resamplePos - k;
|
||||
// Lanczos sampling of input at m_resamplePos
|
||||
for (unsigned j = 0; j <= 2 * a; ++j) s += pcm[k + j] * da::lanc<a>(x - j + a);
|
||||
s *= 5.0;
|
||||
begin[i * 2] += (float) s;
|
||||
begin[i * 2 + 1] += (float) s;
|
||||
m_resamplePos += m_resampleFactor;
|
||||
}
|
||||
unsigned num = static_cast<unsigned>(m_resamplePos);
|
||||
m_resamplePos -= num;
|
||||
if (size > 3000) {
|
||||
// Reset
|
||||
m_passthrough.pop(size - 700);
|
||||
m_resampleFactor = 1.0;
|
||||
} else {
|
||||
m_passthrough.pop(num);
|
||||
m_resampleFactor = 0.99 * m_resampleFactor + 0.01 * (size > 700 ? 1.02 : 0.98);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
namespace {
|
||||
struct Peak {
|
||||
double freq;
|
||||
double db;
|
||||
bool harm[Tone::MAXHARM];
|
||||
Peak(double _freq = 0.0, double _db = -getInf()):
|
||||
freq(_freq), db(_db)
|
||||
{
|
||||
for (auto& h: harm) h = false;
|
||||
}
|
||||
void clear() {
|
||||
freq = 0.0;
|
||||
db = -getInf();
|
||||
}
|
||||
};
|
||||
|
||||
Peak& match(std::vector<Peak>& peaks, std::size_t pos) {
|
||||
std::size_t best = pos;
|
||||
if (peaks[pos - 1].db > peaks[best].db) best = pos - 1;
|
||||
if (peaks[pos + 1].db > peaks[best].db) best = pos + 1;
|
||||
return peaks[best];
|
||||
}
|
||||
}
|
||||
|
||||
bool Analyzer::calcFFT() {
|
||||
float pcm[FFT_N];
|
||||
// Read FFT_N samples, move forward by m_step samples
|
||||
if (!m_buf.read(pcm, pcm + FFT_N)) return false;
|
||||
m_buf.pop(m_step);
|
||||
// Peak level calculation of the most recent m_step samples (the rest is overlap)
|
||||
for (float const* ptr = pcm + FFT_N - m_step; ptr != pcm + FFT_N; ++ptr) {
|
||||
float s = *ptr;
|
||||
float p = s * s;
|
||||
if (p > m_peak) m_peak = p; else m_peak *= 0.999;
|
||||
}
|
||||
// Calculate FFT
|
||||
m_fft = da::fft<FFT_P>(pcm, m_window);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Analyzer::calcTones() {
|
||||
// Precalculated constants
|
||||
const double freqPerBin = m_rate / FFT_N;
|
||||
const double phaseStep = 2.0 * M_PI * m_step / FFT_N;
|
||||
const double normCoeff = 1.0 / FFT_N;
|
||||
const double minMagnitude = pow(10, -100.0 / 20.0) / normCoeff; // -100 dB
|
||||
// Limit frequency range of processing
|
||||
const size_t kMin = std::max(size_t(1), size_t(FFT_MINFREQ / freqPerBin));
|
||||
const size_t kMax = std::min(FFT_N / 2, size_t(FFT_MAXFREQ / freqPerBin));
|
||||
std::vector<Peak> peaks(kMax + 1); // One extra to simplify loops
|
||||
for (size_t k = 1; k <= kMax; ++k) {
|
||||
double magnitude = std::abs(m_fft[k]);
|
||||
double phase = std::arg(m_fft[k]);
|
||||
// process phase difference
|
||||
double delta = phase - m_fftLastPhase[k];
|
||||
m_fftLastPhase[k] = static_cast<float>(phase);
|
||||
delta -= k * phaseStep; // subtract expected phase difference
|
||||
delta = remainder(delta, 2.0 * M_PI); // map delta phase into +/- M_PI interval
|
||||
delta /= phaseStep; // calculate diff from bin center frequency
|
||||
double freq = (k + delta) * freqPerBin; // calculate the true frequency
|
||||
if (freq > 1.0 && magnitude > minMagnitude) {
|
||||
peaks[k].freq = freq;
|
||||
peaks[k].db = 20.0 * log10(normCoeff * magnitude);
|
||||
}
|
||||
}
|
||||
// Prefilter peaks
|
||||
double prevdb = peaks[0].db;
|
||||
for (size_t k = 1; k < kMax; ++k) {
|
||||
double db = peaks[k].db;
|
||||
if (db > prevdb) peaks[k - 1].clear();
|
||||
if (db < prevdb) peaks[k].clear();
|
||||
prevdb = db;
|
||||
}
|
||||
// Find the tones (collections of harmonics) from the array of peaks
|
||||
tones_t tones;
|
||||
for (size_t k = kMax - 1; k >= kMin; --k) {
|
||||
if (peaks[k].db < -70.0) continue;
|
||||
// Find the best divider for getting the fundamental from peaks[k]
|
||||
std::size_t bestDiv = 1;
|
||||
int bestScore = 0;
|
||||
for (std::size_t div = 2; div <= Tone::MAXHARM && k / div > 1; ++div) {
|
||||
double freq = peaks[k].freq / div; // Fundamental
|
||||
int score = 0;
|
||||
for (std::size_t n = 1; n < div && n < 8; ++n) {
|
||||
Peak& p = match(peaks, k * n / div);
|
||||
--score;
|
||||
if (p.db < -90.0 || std::abs(p.freq / n / freq - 1.0) > .03) continue;
|
||||
if (n == 1) score += 4; // Extra for fundamental
|
||||
score += 2;
|
||||
}
|
||||
if (score > bestScore) {
|
||||
bestScore = score;
|
||||
bestDiv = div;
|
||||
}
|
||||
}
|
||||
// Construct a Tone by combining the fundamental frequency (freq) and all harmonics
|
||||
Tone t;
|
||||
std::size_t count = 0;
|
||||
double freq = peaks[k].freq / bestDiv;
|
||||
t.db = peaks[k].db;
|
||||
for (std::size_t n = 1; n <= bestDiv; ++n) {
|
||||
// Find the peak for n'th harmonic
|
||||
Peak& p = match(peaks, k * n / bestDiv);
|
||||
if (std::abs(p.freq / n / freq - 1.0) > .03) continue; // Does it match the fundamental freq?
|
||||
if (p.db > t.db - 10.0) {
|
||||
t.db = std::max(t.db, p.db);
|
||||
++count;
|
||||
t.freq += p.freq / n;
|
||||
}
|
||||
t.harmonics[n - 1] = p.db;
|
||||
p.clear();
|
||||
}
|
||||
t.freq /= count;
|
||||
// If the tone seems strong enough, add it (-3 dB compensation for each harmonic)
|
||||
if (t.db > -50.0 - 3.0 * count) {
|
||||
t.stabledb = t.db;
|
||||
tones.push_back(t);
|
||||
}
|
||||
}
|
||||
mergeWithOld(tones);
|
||||
m_tones.swap(tones);
|
||||
}
|
||||
|
||||
void Analyzer::mergeWithOld(tones_t& tones) const {
|
||||
tones.sort();
|
||||
auto it = tones.begin();
|
||||
// Iterate over old tones
|
||||
for (auto const& old: m_tones) {
|
||||
// Try to find a matching new tone
|
||||
while (it != tones.end() && *it < old) ++it;
|
||||
// If match found
|
||||
if (it != tones.end() && *it == old) {
|
||||
// Merge the old tone into the new tone
|
||||
it->age = old.age + 1;
|
||||
it->stabledb = 0.8 * old.stabledb + 0.2 * it->db;
|
||||
it->freq = 0.5 * old.freq + 0.5 * it->freq;
|
||||
} else if (old.db > -80.0) {
|
||||
// Insert a decayed version of the old tone into new tones
|
||||
Tone& t = *tones.insert(it, old);
|
||||
t.db -= 5.0;
|
||||
t.stabledb -= 0.1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Analyzer::process() {
|
||||
// Try calculating FFT and calculate tones until no more data in input buffer
|
||||
while (calcFFT()) calcTones();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
#pragma once
|
||||
|
||||
#include <complex>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include "../compatibility.h"
|
||||
|
||||
/// struct to represent tones
|
||||
struct Tone {
|
||||
static const std::size_t MAXHARM = 48; ///< The maximum number of harmonics tracked
|
||||
static const std::size_t MINAGE = 2; ///< The minimum age required for a tone to be output
|
||||
double freq; ///< Frequency (Hz)
|
||||
double db; ///< Level (dB)
|
||||
double stabledb; ///< Stable level, useful for graphics rendering
|
||||
double harmonics[MAXHARM]; ///< Harmonics' levels
|
||||
std::size_t age; ///< How many times the tone has been detected in row
|
||||
Tone();
|
||||
void print() const; ///< Prints Tone to std::cout
|
||||
bool operator==(double f) const; ///< Compare for rough frequency match
|
||||
/// Less-than compare by levels (instead of frequencies like operator< does)
|
||||
static bool dbCompare(Tone const& l, Tone const& r) { return l.db < r.db; }
|
||||
};
|
||||
|
||||
static inline bool operator==(Tone const& lhs, Tone const& rhs) { return lhs == rhs.freq; }
|
||||
static inline bool operator!=(Tone const& lhs, Tone const& rhs) { return !(lhs == rhs); }
|
||||
static inline bool operator<=(Tone const& lhs, Tone const& rhs) { return lhs.freq < rhs.freq || lhs == rhs; }
|
||||
static inline bool operator>=(Tone const& lhs, Tone const& rhs) { return lhs.freq > rhs.freq || lhs == rhs; }
|
||||
static inline bool operator<(Tone const& lhs, Tone const& rhs) { return lhs.freq < rhs.freq && lhs != rhs; }
|
||||
static inline bool operator>(Tone const& lhs, Tone const& rhs) { return lhs.freq > rhs.freq && lhs != rhs; }
|
||||
|
||||
static const unsigned FFT_P = 10;
|
||||
static const std::size_t FFT_N = 1 << FFT_P;
|
||||
|
||||
/// Lock-free ring buffer. Discards oldest data on overflow (not strictly thread-safe).
|
||||
template <size_t SIZE> class RingBuffer {
|
||||
public:
|
||||
constexpr static size_t capacity = SIZE;
|
||||
RingBuffer(): m_read(), m_write() {} ///< Initialize empty buffer
|
||||
template <typename InIt> void insert(InIt begin, InIt end) {
|
||||
size_t r = m_read; // The read position
|
||||
size_t w = m_write; // The write position
|
||||
bool overflow = (static_cast<size_t>(end - begin) >= SIZE - size());
|
||||
while (begin != end) {
|
||||
m_buf[w] = *begin++; // Copy sample
|
||||
w = modulo(w + 1); // Update cursor
|
||||
}
|
||||
m_write = w;
|
||||
if (overflow) m_read = modulo(w + 1); // Reset read pointer on overflow
|
||||
}
|
||||
/// Read data from current position if there is enough data to fill the range (otherwise return false). Does not move read pointer.
|
||||
template <typename OutIt> bool read(OutIt begin, OutIt end) {
|
||||
size_t r = m_read;
|
||||
if (size() < static_cast<size_t>(end - begin)) return false; // Not enough audio available
|
||||
while (begin != end) *begin++ = m_buf[r++ % SIZE]; // Copy audio to output iterator
|
||||
return true;
|
||||
}
|
||||
void pop(size_t n) { m_read = modulo(m_read + n); } ///< Move reading pointer forward.
|
||||
size_t size() const { return modulo(m_write - m_read); }
|
||||
private:
|
||||
static size_t modulo(size_t idx) { return idx % SIZE; } ///< Modulo operation with proper rounding (handles slightly "negative" idx as well)
|
||||
float m_buf[SIZE];
|
||||
volatile size_t m_read, m_write; ///< The indices of the next read/write operations. read == write implies that buffer is empty.
|
||||
};
|
||||
|
||||
/// analyzer class
|
||||
/** class to analyze input audio and transform it into useable data
|
||||
*/
|
||||
class Analyzer {
|
||||
public:
|
||||
/// fast fourier transform vector
|
||||
typedef std::vector<std::complex<float> > fft_t;
|
||||
/// list of tones
|
||||
typedef std::list<Tone> tones_t;
|
||||
/// constructor
|
||||
Analyzer(double rate, std::string id, unsigned step = 200);
|
||||
/** Add input data to buffer. This is thread-safe (against other functions). **/
|
||||
template <typename InIt> void input(InIt begin, InIt end) {
|
||||
m_buf.insert(begin, end);
|
||||
m_passthrough.insert(begin, end);
|
||||
}
|
||||
/** Call this to process all data input so far. **/
|
||||
void process();
|
||||
/** Get the raw FFT. **/
|
||||
fft_t const& getFFT() const { return m_fft; }
|
||||
/** Get the peak level in dB (negative value, 0.0 = clipping). **/
|
||||
double getPeak() const { return 10.0 * log10(m_peak); }
|
||||
/** Get a list of all tones detected. **/
|
||||
tones_t const& getTones() const { return m_tones; }
|
||||
/** Find a tone within the singing range; prefers strong tones around 200-400 Hz. **/
|
||||
Tone const* findTone(double minfreq = 65.0, double maxfreq = 1000.0) const {
|
||||
if (m_tones.empty()) { m_oldfreq = 0.0; return NULL; }
|
||||
double db = std::max_element(m_tones.begin(), m_tones.end(), Tone::dbCompare)->db;
|
||||
Tone const* best = NULL;
|
||||
double bestscore = 0;
|
||||
for (tones_t::const_iterator it = m_tones.begin(); it != m_tones.end(); ++it) {
|
||||
if (it->db < db - 20.0 || it->freq < minfreq || it->age < Tone::MINAGE) continue;
|
||||
if (it->freq > maxfreq) break;
|
||||
double score = it->db - std::max(180.0, std::abs(it->freq - 300.0)) / 10.0;
|
||||
if (m_oldfreq != 0.0 && std::fabs(it->freq/m_oldfreq - 1.0) < 0.05) score += 10.0;
|
||||
if (best && bestscore > score) break;
|
||||
best = &*it;
|
||||
bestscore = score;
|
||||
}
|
||||
m_oldfreq = (best ? best->freq : 0.0);
|
||||
return best;
|
||||
}
|
||||
/** Give data away for mic pass-through */
|
||||
bool output(float* begin, float* end, double rate);
|
||||
/** Returns the id (color name) of the mic */
|
||||
std::string const& getId() const { return m_id; }
|
||||
|
||||
private:
|
||||
const unsigned m_step;
|
||||
RingBuffer<4 * FFT_N> m_buf; // Twice the FFT size should give enough room for sliding window and for engine delays
|
||||
RingBuffer<4096> m_passthrough;
|
||||
double m_resampleFactor;
|
||||
double m_resamplePos;
|
||||
double m_rate;
|
||||
std::string m_id;
|
||||
std::vector<float> m_window;
|
||||
fft_t m_fft;
|
||||
std::vector<float> m_fftLastPhase;
|
||||
double m_peak;
|
||||
tones_t m_tones;
|
||||
mutable double m_oldfreq;
|
||||
bool calcFFT();
|
||||
void calcTones();
|
||||
void mergeWithOld(tones_t& tones) const;
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
#include <stdexcept>
|
||||
|
||||
/** Limit val to range [min, max] **/
|
||||
template <typename T> T clamp(T val, T min = 0, T max = 1) {
|
||||
if (min > max) throw std::logic_error("min > max");
|
||||
if (val < min) return min;
|
||||
if (val > max) return max;
|
||||
return val;
|
||||
}
|
||||
|
||||
template <typename Numeric> struct MinMax {
|
||||
Numeric min, max;
|
||||
explicit MinMax(Numeric min = std::numeric_limits<Numeric>::min(), Numeric max = std::numeric_limits<Numeric>::max()): min(min), max(max) {}
|
||||
bool matches(Numeric val) const { return val >= min && val <= max; }
|
||||
};
|
||||
|
||||
/** A convenient way for getting NaNs **/
|
||||
static inline double getNaN() { return std::numeric_limits<double>::quiet_NaN(); }
|
||||
|
||||
/** A convenient way for getting infs **/
|
||||
static inline double getInf() { return std::numeric_limits<double>::infinity(); }
|
||||
|
||||
static inline bool isPow2(unsigned int val) {
|
||||
if (val == 0) return false;
|
||||
if ((val & (val-1)) == 0) return true; // From Wikipedia: Power_of_two
|
||||
return false;
|
||||
}
|
||||
|
||||
static inline unsigned int nextPow2(unsigned int val) {
|
||||
unsigned int ret = 1;
|
||||
while (ret < val) ret *= 2;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline unsigned int prevPow2(unsigned int val) {
|
||||
unsigned int ret = 1;
|
||||
while ((ret*2) < val) ret *= 2;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user