Initial commit
This commit is contained in:
commit
3e6ab36828
2
.dockerignore
Normal file
2
.dockerignore
Normal file
|
@ -0,0 +1,2 @@
|
|||
/output
|
||||
/example
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/output
|
||||
.idea
|
||||
cmake-*
|
672
LICENSE
Normal file
672
LICENSE
Normal file
|
@ -0,0 +1,672 @@
|
|||
WebHibiki is Copyright (C) 2022 WeebDataHoarder.
|
||||
Hibiki is Copyright (C) 2022 WeebDataHoarder.
|
||||
Panako is Copyright (C) 2014 - 2021 - Joren Six / IPEM
|
||||
|
||||
License to distribute and modify the code is hereby granted under the
|
||||
terms of the GNU Affero General Public License, version 3 (henceforth,
|
||||
the AGPLv3), but not under other versions of the AGPL. See below for
|
||||
the full text of the AGPLv3.
|
||||
|
||||
---
|
||||
|
||||
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||
Version 3, 19 November 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU Affero General Public License is a free, copyleft license for
|
||||
software and other kinds of works, specifically designed to ensure
|
||||
cooperation with the community in the case of network server software.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
our General Public Licenses are intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users.
|
||||
|
||||
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
|
||||
them 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.
|
||||
|
||||
Developers that use our General Public Licenses protect your rights
|
||||
with two steps: (1) assert copyright on the software, and (2) offer
|
||||
you this License which gives you legal permission to copy, distribute
|
||||
and/or modify the software.
|
||||
|
||||
A secondary benefit of defending all users' freedom is that
|
||||
improvements made in alternate versions of the program, if they
|
||||
receive widespread use, become available for other developers to
|
||||
incorporate. Many developers of free software are heartened and
|
||||
encouraged by the resulting cooperation. However, in the case of
|
||||
software used on network servers, this result may fail to come about.
|
||||
The GNU General Public License permits making a modified version and
|
||||
letting the public access it on a server without ever releasing its
|
||||
source code to the public.
|
||||
|
||||
The GNU Affero General Public License is designed specifically to
|
||||
ensure that, in such cases, the modified source code becomes available
|
||||
to the community. It requires the operator of a network server to
|
||||
provide the source code of the modified version running there to the
|
||||
users of that server. Therefore, public use of a modified version, on
|
||||
a publicly accessible server, gives the public access to the source
|
||||
code of the modified version.
|
||||
|
||||
An older license, called the Affero General Public License and
|
||||
published by Affero, was designed to accomplish similar goals. This is
|
||||
a different license, not a version of the Affero GPL, but Affero has
|
||||
released a new version of the Affero GPL which permits relicensing under
|
||||
this license.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU Affero General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey 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;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If 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 convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Remote Network Interaction; Use with the GNU General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, if you modify the
|
||||
Program, your modified version must prominently offer all users
|
||||
interacting with it remotely through a computer network (if your version
|
||||
supports such interaction) an opportunity to receive the Corresponding
|
||||
Source of your version by providing access to the Corresponding Source
|
||||
from a network server at no charge, through some standard or customary
|
||||
means of facilitating copying of software. This Corresponding Source
|
||||
shall include the Corresponding Source for any work covered by version 3
|
||||
of the GNU General Public License that is incorporated pursuant to the
|
||||
following paragraph.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the work with which it is combined will remain governed by version
|
||||
3 of the GNU General Public License.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU Affero 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 that a certain numbered version of the GNU Affero General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU Affero General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU Affero General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
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.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
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.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
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
|
||||
state 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 Affero General Public License as published by
|
||||
the Free Software Foundation, either version 3 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If your software can interact with users remotely through a computer
|
||||
network, you should also make sure that it provides a way for users to
|
||||
get its source. For example, if your program is a web application, its
|
||||
interface could display a "Source" link that leads users to an archive
|
||||
of the code. There are many ways you could offer source, and different
|
||||
solutions will be better for different programs; see section 13 for the
|
||||
specific requirements.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU AGPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# WebHibiki
|
||||
|
||||
Implementation of [Hibiki](https://git.gammaspectra.live/S.O.N.G/Hibiki) / [Panako](https://github.com/JorenSix/Panako) in C++ for WebAssembly.
|
11
build.sh
Executable file
11
build.sh
Executable file
|
@ -0,0 +1,11 @@
|
|||
#!/bin/bash
|
||||
|
||||
ROOT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
|
||||
|
||||
pushd "${ROOT_DIR}"
|
||||
|
||||
docker build -t webhibiki_builder -f docker/Dockerfile .
|
||||
|
||||
mkdir output 2>/dev/null
|
||||
|
||||
docker run -it --rm --volume "${ROOT_DIR}/src:/src/dist:ro" --volume "${ROOT_DIR}/output:/output:rw" webhibiki_builder
|
17
docker/Dockerfile
Normal file
17
docker/Dockerfile
Normal file
|
@ -0,0 +1,17 @@
|
|||
FROM debian:bullseye
|
||||
|
||||
ARG EMSCRIPTEN_TAG=3.1.2
|
||||
|
||||
|
||||
RUN DEBIAN_FRONTEND=noninteractive apt update && \
|
||||
DEBIAN_FRONTEND=noninteractive apt install -y \
|
||||
git build-essential python3-pip cmake
|
||||
|
||||
RUN git clone --recursive --depth 1 --branch "${EMSCRIPTEN_TAG}" https://github.com/emscripten-core/emsdk.git /src/emsdk
|
||||
|
||||
WORKDIR /src/emsdk
|
||||
RUN ./emsdk install "${EMSCRIPTEN_TAG}" && ./emsdk activate "${EMSCRIPTEN_TAG}"
|
||||
|
||||
COPY docker/entrypoint.sh /entrypoint.sh
|
||||
|
||||
ENTRYPOINT ["/entrypoint.sh"]
|
69
docker/entrypoint.sh
Executable file
69
docker/entrypoint.sh
Executable file
|
@ -0,0 +1,69 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -x;
|
||||
set -e
|
||||
set -o pipefail
|
||||
|
||||
source "/src/emsdk/emsdk_env.sh"
|
||||
|
||||
mkdir /build
|
||||
|
||||
export LIBRARY_PATH="${LIBRARY_PATH}:/build"
|
||||
export LD_LIBRARY_PATH="${LIBRARY_PATH}:/build"
|
||||
|
||||
|
||||
export CFLAGS="${CFLAGS} -I/build/include ${1} -O3 -g0 -s ENVIRONMENT=web,worker -s WASM_BIGINT=1"
|
||||
export CXXFLAGS="${CXXFLAGS} -I/build/include ${1} -O3 -g0 -s ENVIRONMENT=web,worker -s WASM_BIGINT=1"
|
||||
export PATH="${PATH}:/build"
|
||||
|
||||
git clone https://git.gammaspectra.live/S.O.N.G/c-gaborator /src/c-gaborator
|
||||
pushd /src/c-gaborator
|
||||
git reset --hard 2b7ccf50d63d3473c1ecbef0f5aab760914eeefd
|
||||
git submodule update --init --recursive
|
||||
|
||||
mkdir build && pushd build
|
||||
emcmake cmake .. -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_CXX_FLAGS_RELEASE="-march=native" -DCMAKE_C_FLAGS_RELEASE="-march=native" \
|
||||
-DCMAKE_INSTALL_PREFIX="/build"
|
||||
emmake make install
|
||||
|
||||
git clone https://github.com/libsndfile/libsamplerate /src/libsamplerate
|
||||
pushd /src/libsamplerate
|
||||
git reset --hard c96f5e3de9c4488f4e6c97f59f5245f22fda22f7
|
||||
mkdir build && pushd build
|
||||
emcmake cmake .. -DCMAKE_BUILD_TYPE=Release \
|
||||
-DLIBSAMPLERATE_EXAMPLES=OFF -DBUILD_TESTING=OFF \
|
||||
-DLIBSAMPLERATE_ENABLE_SINC_BEST_CONVERTER=OFF -DLIBSAMPLERATE_ENABLE_SINC_MEDIUM_CONVERTER=OFF -DLIBSAMPLERATE_ENABLE_SINC_FAST_CONVERTER=OFF \
|
||||
-DLIBSAMPLERATE_INSTALL_PKGCONFIG_MODULE=OFF \
|
||||
-DCMAKE_CXX_FLAGS_RELEASE="-march=native -O3 -g0" -DCMAKE_C_FLAGS_RELEASE="-march=native -O3 -g0" \
|
||||
-DCMAKE_INSTALL_PREFIX="/build"
|
||||
emmake make install
|
||||
|
||||
|
||||
mkdir /build/cpp && pushd /build/cpp
|
||||
emcmake cmake /src/dist/cpp -DCMAKE_BUILD_TYPE=Release \
|
||||
-DCMAKE_CXX_FLAGS_RELEASE="-march=native" -DCMAKE_C_FLAGS_RELEASE="-march=native" \
|
||||
-DCMAKE_INSTALL_PREFIX="/build"
|
||||
|
||||
emmake make install
|
||||
|
||||
pushd /build
|
||||
|
||||
ls -lah /build/lib
|
||||
|
||||
emcc /build/lib/libhibiki.a /build/lib/libcgaborator.a /build/lib/libsamplerate.a \
|
||||
--pre-js /src/dist/js/pre.js --post-js /src/dist/js/post.js \
|
||||
-O3 -g0 -s ENVIRONMENT=web,worker -s WASM_BIGINT=1 \
|
||||
-s ENVIRONMENT=web,worker \
|
||||
-s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=32mb -s MEMORY64=0 -s MALLOC=emmalloc -s NO_EXIT_RUNTIME=1 \
|
||||
-s EXPORTED_FUNCTIONS="_malloc,_free,_new_point_processor,_point_processor_process,_point_processor_finish,_delete_point_processor" \
|
||||
-s EXPORTED_RUNTIME_METHODS="['ccall', 'cwrap']" \
|
||||
-s LEGACY_VM_SUPPORT=0 -s MIN_CHROME_VERSION=91 -s MIN_FIREFOX_VERSION=89 -s MIN_EDGE_VERSION=0x7FFFFFFF -s MIN_SAFARI_VERSION=152000 \
|
||||
--llvm-lto 1 --memory-init-file=0 \
|
||||
--closure 1 \
|
||||
-s MODULARIZE=1 -s EXPORT_NAME=loadHibiki \
|
||||
-s WASM=1 -o webhibiki.js
|
||||
|
||||
ls -lah ./
|
||||
|
||||
cp -v webhibiki.* /output
|
164
example/index.html
Normal file
164
example/index.html
Normal file
|
@ -0,0 +1,164 @@
|
|||
<!DOCTYPE html>
|
||||
<head>
|
||||
<title>WebHibiki - Audio lookup tool.</title>
|
||||
<script src="webhibiki.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>WebHibiki</h1>
|
||||
<p>Implementation of <a href="https://git.gammaspectra.live/S.O.N.G/Hibiki">Hibiki</a> / <a href="https://github.com/JorenSix/Panako">Panako</a> in C++ for WebAssembly.</p>
|
||||
|
||||
<p>
|
||||
Select or drop an audio file to be fingerprinted. Computations are all done locally in-browser, then calculated fingerprints are sent to the query server.
|
||||
</br>
|
||||
You can process full tracks or segments of them. Or try a recording from a microphone!
|
||||
</br>
|
||||
It might take a few seconds to see results. For more progress information (thanks blocking main thread) open the Developer Tools.
|
||||
</br>
|
||||
</br>
|
||||
<input type="file" id="song-upload" accept="audio/*">
|
||||
</br>
|
||||
<progress id="song-progress" max="100" value="0"></progress>
|
||||
</p>
|
||||
|
||||
<h2>Matches</h2>
|
||||
<div id="song-matches"></div>
|
||||
|
||||
<h2>Raw fingerprints</h2>
|
||||
<p>
|
||||
<pre id="song-print">
|
||||
|
||||
</pre>
|
||||
</p>
|
||||
|
||||
</body>
|
||||
<script type="text/javascript">
|
||||
const queryUrl = "FILL_THIS";
|
||||
const blobUrl = "FILL_THIS/sha256/";
|
||||
|
||||
loadHibiki().then(function (Module){
|
||||
|
||||
let processSlice = function (pointProcessor, slice, buf) {
|
||||
Module.HEAPU8.set(new Uint8Array(slice.buffer, slice.byteOffset, slice.length*slice.BYTES_PER_ELEMENT), buf);
|
||||
Module.ccall('point_processor_process', null, ['number', 'number', 'number'], [pointProcessor, buf, slice.length]);
|
||||
}
|
||||
|
||||
let fromArrayBuffer = async function (buffer) {
|
||||
document.getElementById("song-progress").value = 0;
|
||||
let audioCtx = new AudioContext({
|
||||
latencyHint: "balanced",
|
||||
//sampleRate: 16000 //TODO: maybe this can be changed once there is a proper api
|
||||
});
|
||||
const blockSize = 1 << 17;
|
||||
|
||||
let audioData = await audioCtx.decodeAudioData(buffer);
|
||||
|
||||
let pointProcessor = Module.ccall('new_point_processor', 'number', ['number', 'number'], [audioData.sampleRate, audioData.numberOfChannels]);
|
||||
|
||||
|
||||
|
||||
let channels = [];
|
||||
for (let i = 0; i < audioData.numberOfChannels; ++i){
|
||||
channels.push(audioData.getChannelData(i));
|
||||
}
|
||||
|
||||
let interleaved = new Float32Array(blockSize*audioData.numberOfChannels);
|
||||
let buf = Module._malloc(blockSize*audioData.numberOfChannels * interleaved.BYTES_PER_ELEMENT);
|
||||
|
||||
let currentLength = 0;
|
||||
let totalSamples = 0;
|
||||
let blockNumber = 0;
|
||||
for(let i = 0; i < audioData.length; ++i){
|
||||
//Interleave
|
||||
for (let j = 0; j < audioData.numberOfChannels; ++j){
|
||||
interleaved[currentLength++] = channels[j][i];
|
||||
}
|
||||
|
||||
if (currentLength === (blockSize*audioData.numberOfChannels)){
|
||||
console.log("processing block " + blockNumber);
|
||||
processSlice(pointProcessor, interleaved, buf);
|
||||
totalSamples += currentLength;
|
||||
currentLength = 0;
|
||||
++blockNumber;
|
||||
|
||||
document.getElementById("song-progress").value = Math.ceil(totalSamples / (audioData.length*audioData.numberOfChannels) * 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (currentLength > 0){
|
||||
console.log("finish block " + blockNumber + " length " + currentLength);
|
||||
//Send remaining data
|
||||
totalSamples += currentLength;
|
||||
processSlice(pointProcessor, interleaved.subarray(0, currentLength), buf);
|
||||
}
|
||||
|
||||
console.log("total samples " + totalSamples);
|
||||
|
||||
let intPtr = Module._malloc(4);
|
||||
let resultPtr = Module.ccall('point_processor_finish', 'number', ['number', 'number', 'number'], [pointProcessor, 1, intPtr]);
|
||||
let resultLength = (new Int32Array(Module.HEAPU8.buffer, intPtr, 1))[0];
|
||||
Module._free(intPtr);
|
||||
Module._free(buf);
|
||||
//Copy result
|
||||
let printResult = new Uint8Array(new Uint8Array(Module.HEAPU8.buffer, resultPtr, resultLength));
|
||||
|
||||
Module.ccall('delete_point_processor', null, ['number'], [pointProcessor]);
|
||||
|
||||
return printResult;
|
||||
};
|
||||
|
||||
document.getElementById("song-upload").addEventListener("change", function () {
|
||||
for (let i = 0; i < this.files.length; ++i) {
|
||||
const file = this.files[i];
|
||||
file.arrayBuffer().then(function (buffer) {
|
||||
fromArrayBuffer(buffer).then(function (result) {
|
||||
|
||||
document.getElementById("song-progress").value = 100;
|
||||
let pre = document.getElementById("song-print");
|
||||
let len = 64;
|
||||
let text = "";
|
||||
for(let j = 0; j < result.length; j += len) {
|
||||
text += (result.subarray(j, j + len > result.length ? result.length : j + len).reduce((a, b) => a + b.toString(16).padStart(2, '0'), '')) + "\n";
|
||||
}
|
||||
pre.innerText = text;
|
||||
|
||||
fetch(queryUrl, {
|
||||
method: "POST",
|
||||
mode: "cors",
|
||||
body: result
|
||||
}).then(function (response) {
|
||||
response.json().then(function (data){
|
||||
let matches = document.getElementById("song-matches");
|
||||
matches.innerHTML = "";
|
||||
|
||||
data.forEach(function (e) {
|
||||
let div = document.createElement("div");
|
||||
let title = document.createElement("span");
|
||||
title.style["font-weight"] = "bold";
|
||||
title.innerText = "score " + e.score + " " + (Math.round(e.percentMatch * 10000) / 100) + "% :: " + e.fileName;
|
||||
div.append(title);
|
||||
div.append(document.createElement("br"));
|
||||
let info1 = document.createElement("span");
|
||||
info1.innerText = "match start@ " + (Math.round(e.matchStart * 100) / 100) + "s end@ " + (Math.round(e.matchEnd * 100) / 100) + "s :: hash " + e.fileHash;
|
||||
div.append(info1);
|
||||
div.append(document.createElement("br"));
|
||||
let info2 = document.createElement("span");
|
||||
info2.innerText = "query start@ " + (Math.round(e.queryStart * 100) / 100) + "s end@ " + (Math.round(e.queryEnd * 100) / 100) + "s :: factors: time " + (Math.round(e.timeFactor * 1000) / 1000) + " frequency " + (Math.round(e.frequencyFactor * 1000) / 1000);
|
||||
div.append(info2);
|
||||
div.append(document.createElement("br"));
|
||||
let audio = document.createElement("audio");
|
||||
audio.src = blobUrl + e.fileHash;
|
||||
audio.setAttribute("controls", "");
|
||||
audio.setAttribute("preload", "metadata");
|
||||
div.append(audio);
|
||||
|
||||
matches.append(div);
|
||||
matches.append(document.createElement("br"));
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
});
|
||||
})
|
||||
</script>
|
34
src/cpp/CMakeLists.txt
Normal file
34
src/cpp/CMakeLists.txt
Normal file
|
@ -0,0 +1,34 @@
|
|||
cmake_minimum_required(VERSION 3.16)
|
||||
project(hibiki
|
||||
DESCRIPTION "Small Hibiki C++ implementation"
|
||||
VERSION 1.0)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
|
||||
set(CMAKE_INSTALL_RPATH "$ORIGIN")
|
||||
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
|
||||
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Release)
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -ggdb -O0 -fno-exceptions -fno-rtti")
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -ggdb -O0 -fno-exceptions")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -fno-exceptions -fno-rtti")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -fno-exceptions")
|
||||
|
||||
add_library(hibiki SHARED hibiki.cpp)
|
||||
target_link_libraries(hibiki cgaborator samplerate)
|
||||
set_target_properties(hibiki PROPERTIES PUBLIC_HEADER "hibiki.h")
|
||||
|
||||
if(DO_TEST)
|
||||
add_executable(test test.cpp)
|
||||
target_link_libraries(test hibiki cgaborator samplerate)
|
||||
add_test(test test)
|
||||
endif()
|
||||
|
||||
install(
|
||||
TARGETS hibiki
|
||||
PUBLIC_HEADER DESTINATION include
|
||||
)
|
659
src/cpp/hibiki.cpp
Normal file
659
src/cpp/hibiki.cpp
Normal file
|
@ -0,0 +1,659 @@
|
|||
#include <cgaborator.h>
|
||||
#include <samplerate.h>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include "lemire.h"
|
||||
#include "hibiki.h"
|
||||
|
||||
|
||||
void audio_multiple_channels_to_mono(float* buffer, size_t buffer_len, int channels) {
|
||||
float f;
|
||||
switch(channels) {
|
||||
case 1:
|
||||
break;
|
||||
case 2:
|
||||
for (int i = 0; i < buffer_len; i += 2){
|
||||
buffer[i/2] = buffer[i] + buffer[i+1];
|
||||
}
|
||||
break;
|
||||
default:
|
||||
for (int i = 0; i < buffer_len; i += channels){
|
||||
f = buffer[i];
|
||||
for (int j = 1; j < channels; ++j) {
|
||||
f += buffer[i + j];
|
||||
}
|
||||
buffer[i/channels] = f / (float)channels;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void write_uint32_le(std::vector<uint8_t>& d, uint32_t v) {
|
||||
d.push_back(v & 0xFF);
|
||||
d.push_back((v >> 8ul) & 0xFF);
|
||||
d.push_back((v >> 16ul) & 0xFF);
|
||||
d.push_back((v >> 24ul) & 0xFF);
|
||||
}
|
||||
void write_uint32_be(std::vector<uint8_t>& d, uint32_t v) {
|
||||
d.push_back((v >> 24ul) & 0xFF);
|
||||
d.push_back((v >> 16ul) & 0xFF);
|
||||
d.push_back((v >> 8ul) & 0xFF);
|
||||
d.push_back(v & 0xFF);
|
||||
}
|
||||
void write_uint64_le(std::vector<uint8_t>& d, uint64_t v) {
|
||||
d.push_back(v & 0xFF);
|
||||
d.push_back((v >> 8ul) & 0xFF);
|
||||
d.push_back((v >> 16ul) & 0xFF);
|
||||
d.push_back((v >> 24ul) & 0xFF);
|
||||
d.push_back((v >> 32ul) & 0xFF);
|
||||
d.push_back((v >> 40ul) & 0xFF);
|
||||
d.push_back((v >> 48ul) & 0xFF);
|
||||
d.push_back((v >> 56ul) & 0xFF);
|
||||
}
|
||||
void write_uint64_be(std::vector<uint8_t>& d, uint64_t v) {
|
||||
d.push_back((v >> 56ul) & 0xFF);
|
||||
d.push_back((v >> 48ul) & 0xFF);
|
||||
d.push_back((v >> 40ul) & 0xFF);
|
||||
d.push_back((v >> 32ul) & 0xFF);
|
||||
d.push_back((v >> 24ul) & 0xFF);
|
||||
d.push_back((v >> 16ul) & 0xFF);
|
||||
d.push_back((v >> 8ul) & 0xFF);
|
||||
d.push_back(v & 0xFF);
|
||||
}
|
||||
|
||||
class EventPointProcessor {
|
||||
private:
|
||||
const int bandsPerOctave = PANAKO_TRANSF_BANDS_PER_OCTAVE;
|
||||
const int audioBlockSize = PANAKO_AUDIO_BLOCK_SIZE;
|
||||
const int maxFilterWindowSizeFrequency = PANAKO_FREQ_MAX_FILTER_SIZE;
|
||||
const int maxFilterWindowSizeTime = PANAKO_TIME_MAX_FILTER_SIZE;
|
||||
public:
|
||||
EventPointProcessor(int sampleRate, int channels) : sampleRate(sampleRate), channels(channels){
|
||||
gaborator = gaborator_initialize(
|
||||
audioBlockSize,
|
||||
PANAKO_SAMPLE_RATE,
|
||||
bandsPerOctave,
|
||||
PANAKO_TRANSF_MIN_FREQ,
|
||||
PANAKO_TRANSF_REF_FREQ,
|
||||
PANAKO_TRANSF_MAX_FREQ,
|
||||
PANAKO_TRANSF_TIME_RESOLUTION
|
||||
);
|
||||
|
||||
printf("hibiki: convert from %d Hz to %d Hz, %d to 1 channel(s)\n", sampleRate, PANAKO_SAMPLE_RATE, channels);
|
||||
src = src_new(SRC_LINEAR, 1, nullptr);
|
||||
bandNumber = gaborator_number_of_bands(gaborator);
|
||||
latency = gaborator_analysis_support(gaborator);
|
||||
maxFilterVertical = lemire_max_clamped_init(maxFilterWindowSizeFrequency, bandNumber);
|
||||
if (maxFilterVertical == nullptr){
|
||||
abort();
|
||||
}
|
||||
|
||||
magnitudes.resize(maxFilterWindowSizeTime);
|
||||
|
||||
maxMagnitudes.resize(maxFilterWindowSizeTime);
|
||||
for (auto& v : maxMagnitudes) {
|
||||
v.resize(bandNumber);
|
||||
}
|
||||
maxHorizontal.resize(bandNumber);
|
||||
}
|
||||
|
||||
void Process(float* data, size_t data_size) {
|
||||
if (data_size % channels != 0){
|
||||
abort();
|
||||
}
|
||||
sampleCount += data_size;
|
||||
//printf("processing %p %d elements\n", data, data_size);
|
||||
audio_multiple_channels_to_mono(data, data_size, channels);
|
||||
FeedSampler(data, data_size / channels);
|
||||
}
|
||||
|
||||
void FeedSampler(float* data, size_t data_size){
|
||||
//printf("sampler: processing %p %d elements\n", data, data_size);
|
||||
bool eof = (data_size == 0);
|
||||
if(sampleRate == PANAKO_SAMPLE_RATE){
|
||||
if(!eof){
|
||||
audioSamplesOut.insert(audioSamplesOut.end(), data, data + data_size);
|
||||
}
|
||||
}else{
|
||||
std::vector<float> buf(audioBlockSize * (double(PANAKO_SAMPLE_RATE) / double(sampleRate)) * 1.5);
|
||||
|
||||
//printf("sample: enter %d\n", data_size);
|
||||
SRC_DATA entry;
|
||||
|
||||
entry.data_in = data;
|
||||
entry.input_frames = data_size;
|
||||
entry.end_of_input = eof;
|
||||
|
||||
entry.data_out = buf.data();
|
||||
entry.output_frames = buf.size();
|
||||
|
||||
entry.src_ratio = double(PANAKO_SAMPLE_RATE) / double(sampleRate);
|
||||
|
||||
auto err = src_process(src, &entry);
|
||||
if(err != 0){
|
||||
abort();
|
||||
}
|
||||
//printf("sample: process used %d gen %d ~buf.size %d\n", entry.input_frames_used, entry.output_frames_gen, buf.size());
|
||||
|
||||
if(entry.output_frames_gen > 0){
|
||||
audioSamplesOut.insert(audioSamplesOut.end(), buf.data(), buf.data() + entry.output_frames_gen);
|
||||
}
|
||||
}
|
||||
|
||||
while ((audioSamplesOut.size() - audioSamplesOutIndex) >= audioBlockSize){
|
||||
//printf("sample: transform from %d for size %d\n", audioSamplesOutIndex, audioBlockSize);
|
||||
Transform(audioSamplesOut.data() + audioSamplesOutIndex, audioBlockSize);
|
||||
audioSamplesOutIndex += audioBlockSize;
|
||||
}
|
||||
|
||||
if(eof && (audioSamplesOut.size() - audioSamplesOutIndex) > 0){
|
||||
//printf("sample: END transform from %d for size %d\n", audioSamplesOutIndex, audioSamplesOut.size() - audioSamplesOutIndex);
|
||||
|
||||
Transform(audioSamplesOut.data() + audioSamplesOutIndex, audioSamplesOut.size() - audioSamplesOutIndex);
|
||||
audioSamplesOut.resize(0);
|
||||
audioSamplesOutIndex = 0;
|
||||
}
|
||||
}
|
||||
|
||||
double blocksToSeconds(uint32_t t) const {
|
||||
return double(t)*(double(PANAKO_TRANSF_TIME_RESOLUTION)/double(PANAKO_SAMPLE_RATE)) + double(latency)/double(PANAKO_SAMPLE_RATE);
|
||||
}
|
||||
|
||||
void Transform(const float* data, size_t data_size) {
|
||||
|
||||
//printf("transform %p for size %d\n", data, data_size);
|
||||
gaborator_transform(gaborator, const_cast<float*>(data), data_size, [](uintptr_t callback_data, float* b_data, size_t size, size_t slice_size){
|
||||
auto& self = *reinterpret_cast<EventPointProcessor*>(callback_data);
|
||||
//printf("%d got %p slices of %d size, total %d, count %d\n", callback_data, b_data, slice_size, size, size / slice_size);
|
||||
for (auto i = 0; i < size; i += slice_size){
|
||||
self.processMagnitudes(b_data + i, slice_size);
|
||||
}
|
||||
}, reinterpret_cast<uintptr_t>(this));
|
||||
}
|
||||
|
||||
const std::vector<uint8_t>& Finish(bool useCompactHash) {
|
||||
//Flush sampler
|
||||
std::vector<float> b(1);
|
||||
FeedSampler(b.data(), 0);
|
||||
|
||||
//Flush gaborator
|
||||
Transform(nullptr, 0);
|
||||
|
||||
byte_result.clear();
|
||||
|
||||
|
||||
printf("hibiki: total samples %d\n", sampleCount);
|
||||
printf("hibiki: %d event points\n", points.size());
|
||||
|
||||
auto prints = packEventPoints();
|
||||
printf("hibiki: %d fingerprints\n", prints.size());
|
||||
if (!prints.empty()){
|
||||
printf("hibiki: estimated duration %f\n", blocksToSeconds(prints[prints.size() - 1].t1()));
|
||||
}
|
||||
byte_result.reserve(prints.size() * (useCompactHash ? 8 : 16));
|
||||
for (auto& print : prints){
|
||||
if(useCompactHash){
|
||||
write_uint32_be(byte_result, print.compactHash());
|
||||
write_uint32_le(byte_result, (print.t1() & 0b11111111111111111111) | ((print.f1() & 0b111111111111) << 20));
|
||||
} else {
|
||||
write_uint64_be(byte_result, print.hash());
|
||||
write_uint32_le(byte_result, print.t1());
|
||||
write_uint32_le(byte_result, print.f1());
|
||||
}
|
||||
}
|
||||
|
||||
return byte_result;
|
||||
}
|
||||
|
||||
int getBandNumber() const {
|
||||
return bandNumber;
|
||||
}
|
||||
|
||||
~EventPointProcessor() {
|
||||
gaborator_release(gaborator);
|
||||
lemire_max_clamped_free(maxFilterVertical);
|
||||
src_delete(src);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void processMagnitudes(float* data, size_t size) {
|
||||
magnitudes[magnitudesIndex] = std::vector<float>(data, data + size);
|
||||
//printf("process magnitude %d %d %d %d\n", magnitudesIndex, analysisFrameIndex, size, magnitudes[magnitudesIndex].size());
|
||||
|
||||
//store the frame magnitudes
|
||||
previousMagnitudes[analysisFrameIndex] = &magnitudes.at(magnitudesIndex);
|
||||
|
||||
//run a max filter over frequency bins
|
||||
lemire_max_clamped_filter(maxFilterVertical, magnitudes[magnitudesIndex].data(), magnitudes[magnitudesIndex].size(), maxMagnitudes[magnitudesIndex].data());
|
||||
|
||||
//store the max filtered frequency bins
|
||||
previousMaxMagnitudes[analysisFrameIndex] = &maxMagnitudes.at(magnitudesIndex);
|
||||
|
||||
//find the horizontal maxima
|
||||
if(previousMaxMagnitudes.size() == maxFilterWindowSizeTime){
|
||||
auto time = analysisFrameIndex - maxFilterWindowSizeTime/2;
|
||||
auto& maxFrame = *previousMaxMagnitudes[time];
|
||||
auto& frameMagnitudes = *previousMagnitudes[time];
|
||||
|
||||
for (auto frequency = 2; frequency < frameMagnitudes.size()-1; frequency++) {
|
||||
auto maxVal = maxFrame[frequency];
|
||||
auto currentVal = frameMagnitudes[frequency];
|
||||
|
||||
if (maxVal == currentVal && currentVal != 0) {
|
||||
horizontalFilter(frequency);
|
||||
maxVal = maxHorizontal[frequency];
|
||||
if (currentVal == maxVal) {
|
||||
auto& prevFrameMagnitudes = *previousMagnitudes[time-1];
|
||||
auto& nextFrameMagnitudes = *previousMagnitudes[time+1];
|
||||
|
||||
//add the magnitude of surrounding bins for magnitude estimates more robust against discretization effects
|
||||
float totalMagnitude = frameMagnitudes[frequency] + prevFrameMagnitudes[frequency] + nextFrameMagnitudes[frequency] +
|
||||
frameMagnitudes[frequency+1] + prevFrameMagnitudes[frequency+1] + nextFrameMagnitudes[frequency+1] +
|
||||
frameMagnitudes[frequency-1] + prevFrameMagnitudes[frequency-1] + nextFrameMagnitudes[frequency-1];
|
||||
|
||||
points.emplace_back(uint32_t(time), uint32_t(frequency), totalMagnitude);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Remove analysis frames that are not needed any more:
|
||||
auto ix = analysisFrameIndex - maxFilterWindowSizeTime + 1;
|
||||
previousMaxMagnitudes.erase(ix);
|
||||
previousMagnitudes.erase(ix);
|
||||
}
|
||||
|
||||
magnitudesIndex++;
|
||||
if (magnitudesIndex == maxFilterWindowSizeTime){
|
||||
magnitudesIndex = 0;
|
||||
}
|
||||
analysisFrameIndex++;
|
||||
}
|
||||
|
||||
void horizontalFilter(int j) {
|
||||
maxHorizontal[j] = -1000;
|
||||
|
||||
auto centerFrameIndex = analysisFrameIndex - maxFilterWindowSizeTime/2;
|
||||
auto startFrameIndex = centerFrameIndex - maxFilterWindowSizeTime/2;
|
||||
auto stopFrameIndex = centerFrameIndex + maxFilterWindowSizeTime/2;
|
||||
|
||||
// Run a horizontal max filter
|
||||
for (auto i = startFrameIndex; i < stopFrameIndex; i++) {
|
||||
auto& maxFrame = *previousMaxMagnitudes[i];
|
||||
if (maxFrame[j] > maxHorizontal[j]) {
|
||||
maxHorizontal[j] = maxFrame[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class EventPoint {
|
||||
public:
|
||||
EventPoint(uint32_t time, uint32_t frequency, float magnitude) : time(time), frequency(frequency), magnitude(magnitude){
|
||||
|
||||
}
|
||||
|
||||
const uint32_t time; // The time expressed using an analysis frame index.
|
||||
const uint32_t frequency; // The frequency expressed using the bin number in the constant Q transform.
|
||||
const float magnitude; // The energy value of the element.
|
||||
|
||||
int64_t DeltaTime(const EventPoint& p) const {
|
||||
return static_cast<int64_t>(time) - static_cast<int64_t>(p.time);
|
||||
}
|
||||
};
|
||||
|
||||
class Fingerprint {
|
||||
private:
|
||||
const EventPoint& p1;
|
||||
const EventPoint& p2;
|
||||
const EventPoint& p3;
|
||||
public:
|
||||
Fingerprint(const EventPoint& p1, const EventPoint& p2, const EventPoint& p3) : p1(p1), p2(p2), p3(p3) {
|
||||
if(p2.DeltaTime(p1) < 0){
|
||||
abort();
|
||||
}
|
||||
if(p3.DeltaTime(p2) < 0){
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
uint32_t t1() const {
|
||||
return p1.time;
|
||||
}
|
||||
uint32_t t2() const {
|
||||
return p2.time;
|
||||
}
|
||||
uint32_t t3() const {
|
||||
return p3.time;
|
||||
}
|
||||
|
||||
uint32_t f1() const {
|
||||
return p1.frequency;
|
||||
}
|
||||
uint32_t f2() const {
|
||||
return p2.frequency;
|
||||
}
|
||||
uint32_t f3() const {
|
||||
return p3.frequency;
|
||||
}
|
||||
|
||||
float m1() const {
|
||||
return p1.magnitude;
|
||||
}
|
||||
float m2() const {
|
||||
return p2.magnitude;
|
||||
}
|
||||
float m3() const {
|
||||
return p3.magnitude;
|
||||
}
|
||||
|
||||
uint64_t hash() const {
|
||||
uint64_t f1LargerThanF2 = f1() > f2();
|
||||
uint64_t f2LargerThanF3 = f2() > f3();
|
||||
uint64_t f3LargerThanF1 = f3() > f1();
|
||||
|
||||
|
||||
uint64_t m1LargerThanM2 = m1() > m2();
|
||||
uint64_t m2LargerThanM3 = m2() > m3();
|
||||
uint64_t m3LargerThanM1 = m3() > m1();
|
||||
|
||||
int64_t diffT1T2 = int64_t(t2()) - int64_t(t1());
|
||||
int64_t diffT3T2 = int64_t(t3()) - int64_t(t2());
|
||||
|
||||
uint64_t dt1t2LargerThant3t2 = diffT1T2 > diffT3T2;
|
||||
|
||||
int64_t diffF2F1 = std::abs(int64_t(f2()) - int64_t(f1()));
|
||||
int64_t diffF3F2 = std::abs(int64_t(f3()) - int64_t(f2()));
|
||||
|
||||
|
||||
uint64_t df1f2LargerThanf3f2 = diffF2F1 > diffF3F2;
|
||||
|
||||
//9 bits f in range( 0 - 512) to 8 bits
|
||||
int64_t f1Range = f1() >> 1;
|
||||
|
||||
//7 bits (0-128) -> 5 bits
|
||||
auto df2f1 = diffF2F1 >> 2;
|
||||
auto df3f2 = diffF3F2 >> 2;
|
||||
|
||||
//6 bits max
|
||||
auto ratioT = int64_t(double(diffT1T2) / double(diffT3T2) * 64);
|
||||
|
||||
//combine the hash components into a single 64-bit unsigned integer
|
||||
|
||||
//for debugging purposes, these should be inlined later on
|
||||
auto clampBits = [](uint64_t v, int n) {
|
||||
return v & ((1 << n) - 1);
|
||||
};
|
||||
auto shiftBits = [](uint64_t v, int n) {
|
||||
return v << n;
|
||||
};
|
||||
|
||||
//34 bit output
|
||||
// +----+----------------+---|
|
||||
// | # | field | n |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 0 | ratioT | 6 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
// | 6 | F1 > f2 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 7 | f2 > f3 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 8 | f3 > F1 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 9 | m1 > m2 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 10 | m2 > m3 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 11 | m3 > m1 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 12 |d(T1,t2)>d(t3,t2)| 1 |
|
||||
// +----+----------------+----|
|
||||
// | 13 |d(F1,f2)>d(f3,f2)| 1 |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 14 | f1range | 8 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 22 | d(f2,F1) | 6 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 28 | d(f3,f2) | 6 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
return shiftBits(clampBits(uint64_t(ratioT), 6), 0) |
|
||||
shiftBits(clampBits(f1LargerThanF2, 1), 6) |
|
||||
shiftBits(clampBits(f2LargerThanF3, 1), 7) |
|
||||
shiftBits(clampBits(f3LargerThanF1, 1), 8) |
|
||||
shiftBits(clampBits(m1LargerThanM2, 1), 9) |
|
||||
shiftBits(clampBits(m2LargerThanM3, 1), 10) |
|
||||
shiftBits(clampBits(m3LargerThanM1, 1), 11) |
|
||||
shiftBits(clampBits(dt1t2LargerThant3t2, 1), 12) |
|
||||
shiftBits(clampBits(df1f2LargerThanf3f2, 1), 13) |
|
||||
shiftBits(clampBits(uint64_t(f1Range), 8), 14) |
|
||||
shiftBits(clampBits(uint64_t(df2f1), 6), 22) |
|
||||
shiftBits(clampBits(uint64_t(df3f2), 6), 28);
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint32_t compactHash() const {
|
||||
uint32_t f1LargerThanF2 = f1() > f2();
|
||||
uint32_t f2LargerThanF3 = f2() > f3();
|
||||
uint32_t f3LargerThanF1 = f3() > f1();
|
||||
|
||||
|
||||
uint32_t m1LargerThanM2 = m1() > m2();
|
||||
uint32_t m2LargerThanM3 = m2() > m3();
|
||||
uint32_t m3LargerThanM1 = m3() > m1();
|
||||
|
||||
int64_t diffT1T2 = int64_t(t2()) - int64_t(t1());
|
||||
int64_t diffT3T2 = int64_t(t3()) - int64_t(t2());
|
||||
|
||||
uint32_t dt1t2LargerThant3t2 = diffT1T2 > diffT3T2;
|
||||
|
||||
int64_t diffF2F1 = std::abs(int64_t(f2()) - int64_t(f1()));
|
||||
int64_t diffF3F2 = std::abs(int64_t(f3()) - int64_t(f2()));
|
||||
|
||||
|
||||
uint32_t df1f2LargerThanf3f2 = diffF2F1 > diffF3F2;
|
||||
|
||||
//9 bits f in range( 0 - 512) to 6 bits
|
||||
int64_t f1Range = int64_t(f1() >> 3);
|
||||
|
||||
//7 bits (0-128) -> 5 bits
|
||||
auto df2f1 = diffF2F1 >> 2;
|
||||
auto df3f2 = diffF3F2 >> 2;
|
||||
|
||||
//6 bits max
|
||||
auto ratioT = int64_t(double(diffT1T2) / double(diffT3T2) * 64);
|
||||
|
||||
//combine the hash components into a single 32-bit unsigned integer
|
||||
|
||||
//for debugging purposes, these should be inlined later on
|
||||
auto clampBits = [](uint32_t v, int n) {
|
||||
return v & ((1 << n) - 1);
|
||||
};
|
||||
auto shiftBits = [](uint32_t v, int n) {
|
||||
return v << n;
|
||||
};
|
||||
|
||||
//32 bit output
|
||||
// +----+----------------+---|
|
||||
// | # | field | n |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 0 | ratioT | 6 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
// | 6 | F1 > f2 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 7 | f2 > f3 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 8 | f3 > F1 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 9 | m1 > m2 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 10 | m2 > m3 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 11 | m3 > m1 | 1 |
|
||||
// +----+----------------+---|
|
||||
// | 12 |d(T1,t2)>d(t3,t2)| 1 |
|
||||
// +----+----------------+----|
|
||||
// | 13 |d(F1,f2)>d(f3,f2)| 1 |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 14 | f1range | 6 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 20 | d(f2,F1) | 6 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | 26 | d(f3,f2) | 6 |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// | | | |
|
||||
// +----+----------------+---|
|
||||
return shiftBits(clampBits(uint32_t(ratioT), 6), 0) |
|
||||
shiftBits(clampBits(f1LargerThanF2, 1), 6) |
|
||||
shiftBits(clampBits(f2LargerThanF3, 1), 7) |
|
||||
shiftBits(clampBits(f3LargerThanF1, 1), 8) |
|
||||
shiftBits(clampBits(m1LargerThanM2, 1), 9) |
|
||||
shiftBits(clampBits(m2LargerThanM3, 1), 10) |
|
||||
shiftBits(clampBits(m3LargerThanM1, 1), 11) |
|
||||
shiftBits(clampBits(dt1t2LargerThant3t2, 1), 12) |
|
||||
shiftBits(clampBits(df1f2LargerThanf3f2, 1), 13) |
|
||||
shiftBits(clampBits(uint32_t(f1Range), 6), 14) |
|
||||
shiftBits(clampBits(uint32_t(df2f1), 6), 20) |
|
||||
shiftBits(clampBits(uint32_t(df3f2), 6), 26);
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<Fingerprint> packEventPoints() {
|
||||
std::vector<Fingerprint> results;
|
||||
|
||||
const auto minFreqDistance = PANAKO_FP_MIN_FREQ_DIST;
|
||||
const auto maxFreqDistance = PANAKO_FP_MAX_FREQ_DIST;
|
||||
|
||||
const auto minTimeDistance = PANAKO_FP_MIN_TIME_DIST;
|
||||
const auto maxTimeDistance = PANAKO_FP_MAX_TIME_DIST;
|
||||
|
||||
for (auto i = 0; i < points.size(); ++i) {
|
||||
auto& p1 = points[i];
|
||||
for (auto j = i + 1; j < points.size(); ++j) {
|
||||
auto& p2 = points[j];
|
||||
|
||||
auto fDiff = std::abs(int64_t(p1.frequency) - int64_t(p2.frequency));
|
||||
auto tDiff = int64_t(p2.time) - int64_t(p1.time);
|
||||
|
||||
if (tDiff > maxTimeDistance) {
|
||||
break;
|
||||
}
|
||||
if (tDiff < minTimeDistance) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fDiff < minFreqDistance) {
|
||||
continue;
|
||||
}
|
||||
if (fDiff > maxFreqDistance) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (auto k = j + 1; k < points.size(); ++k) {
|
||||
auto& p3 = points[k];
|
||||
|
||||
fDiff = std::abs(int64_t(p2.frequency) - int64_t(p3.frequency));
|
||||
tDiff = int64_t(p3.time) - int64_t(p2.time);
|
||||
|
||||
if (tDiff > maxTimeDistance) {
|
||||
break;
|
||||
}
|
||||
if (tDiff < minTimeDistance) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fDiff < minFreqDistance) {
|
||||
continue;
|
||||
}
|
||||
if (fDiff > maxFreqDistance) {
|
||||
continue;
|
||||
}
|
||||
|
||||
results.emplace_back(p1, p2, p3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
std::vector<float> audioSamplesOut;
|
||||
size_t audioSamplesOutIndex = 0;
|
||||
int sampleRate;
|
||||
int channels;
|
||||
std::vector<uint8_t> byte_result;
|
||||
int magnitudesIndex = 0;
|
||||
int analysisFrameIndex = 0;
|
||||
std::vector<float> maxHorizontal;
|
||||
std::vector<EventPoint> points;
|
||||
std::vector<std::vector<float>> magnitudes;
|
||||
std::vector<std::vector<float>> maxMagnitudes;
|
||||
std::unordered_map<int,std::vector<float>*> previousMaxMagnitudes;
|
||||
std::unordered_map<int,std::vector<float>*> previousMagnitudes;
|
||||
LemireMaxClampedFilter* maxFilterVertical;
|
||||
SRC_STATE* src;
|
||||
uintptr_t gaborator;
|
||||
int64_t latency;
|
||||
int bandNumber;
|
||||
int sampleCount;
|
||||
};
|
||||
|
||||
|
||||
|
||||
uintptr_t new_point_processor(int sampleRate, int channels){
|
||||
return reinterpret_cast<uintptr_t>(new EventPointProcessor(sampleRate, channels));
|
||||
}
|
||||
|
||||
void point_processor_process(uintptr_t ptr, float* data, size_t data_size){
|
||||
reinterpret_cast<EventPointProcessor*>(ptr)->Process(data, data_size);
|
||||
}
|
||||
|
||||
const uint8_t* point_processor_finish(uintptr_t ptr, uint32_t use_compact_hash, size_t* result_size){
|
||||
auto& result = reinterpret_cast<EventPointProcessor*>(ptr)->Finish(use_compact_hash);
|
||||
*result_size = result.size();
|
||||
return result.data();
|
||||
}
|
||||
|
||||
void delete_point_processor(uintptr_t ptr){
|
||||
delete reinterpret_cast<EventPointProcessor*>(ptr);
|
||||
}
|
41
src/cpp/hibiki.h
Normal file
41
src/cpp/hibiki.h
Normal file
|
@ -0,0 +1,41 @@
|
|||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
|
||||
#define PANAKO_AUDIO_BLOCK_SIZE 1 << 17
|
||||
|
||||
#define PANAKO_FREQ_MAX_FILTER_SIZE 103
|
||||
#define PANAKO_TIME_MAX_FILTER_SIZE 25
|
||||
|
||||
#define PANAKO_FP_MIN_FREQ_DIST 1
|
||||
#define PANAKO_FP_MAX_FREQ_DIST 128
|
||||
#define PANAKO_FP_MIN_TIME_DIST 2
|
||||
#define PANAKO_FP_MAX_TIME_DIST 33
|
||||
|
||||
#define PANAKO_SAMPLE_RATE 16000
|
||||
|
||||
#define PANAKO_TRANSF_MIN_FREQ 110
|
||||
#define PANAKO_TRANSF_MAX_FREQ 7040
|
||||
#define PANAKO_TRANSF_REF_FREQ 440
|
||||
#define PANAKO_TRANSF_BANDS_PER_OCTAVE 85
|
||||
|
||||
//PANAKO_TRANSF_TIME_RESOLUTION in audio samples, 8 ms
|
||||
#define PANAKO_TRANSF_TIME_RESOLUTION 128
|
||||
|
||||
extern uintptr_t new_point_processor(int sampleRate, int channels);
|
||||
|
||||
extern void point_processor_process(uintptr_t ptr, float* data, size_t data_size);
|
||||
|
||||
extern const uint8_t* point_processor_finish(uintptr_t ptr, uint32_t use_compact_hash, size_t* result_size);
|
||||
|
||||
extern void delete_point_processor(uintptr_t ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
620
src/cpp/lemire.h
Normal file
620
src/cpp/lemire.h
Normal file
|
@ -0,0 +1,620 @@
|
|||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#define P
|
||||
#define T int
|
||||
|
||||
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 Gustav Louw
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
//
|
||||
// Double Ended Queue
|
||||
//
|
||||
|
||||
#ifndef T
|
||||
#error "Template type T undefined for <deq.h>"
|
||||
#endif
|
||||
|
||||
#ifndef __CTL_H__
|
||||
#define __CTL_H__
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define CAT(a, b) a##b
|
||||
|
||||
#define PASTE(a, b) CAT(a, b)
|
||||
|
||||
#define JOIN(prefix, name) PASTE(prefix, PASTE(_, name))
|
||||
|
||||
#define SWAP(TYPE, a, b) { TYPE temp = *(a); *(a) = *(b); *(b) = temp; }
|
||||
|
||||
#define foreach(a, b, c) for(JOIN(a, it) c = JOIN(JOIN(a, it), each) (b); !c.done; c.step(&c))
|
||||
|
||||
#define len(a) (sizeof(a) / sizeof(*(a)))
|
||||
|
||||
#endif
|
||||
|
||||
#define A JOIN(deq, T)
|
||||
#define B JOIN(A, bucket)
|
||||
#define Z JOIN(A, it)
|
||||
|
||||
#define DEQ_BUCKET_SIZE (512)
|
||||
|
||||
typedef struct B
|
||||
{
|
||||
T value[DEQ_BUCKET_SIZE];
|
||||
int16_t a;
|
||||
int16_t b;
|
||||
}
|
||||
B;
|
||||
|
||||
typedef struct A
|
||||
{
|
||||
void (*free)(T*);
|
||||
T (*copy)(T*);
|
||||
B** pages;
|
||||
size_t mark_a;
|
||||
size_t mark_b;
|
||||
size_t capacity;
|
||||
size_t size;
|
||||
}
|
||||
A;
|
||||
|
||||
typedef struct Z
|
||||
{
|
||||
void (*step)(struct Z*);
|
||||
A* container;
|
||||
T* ref;
|
||||
size_t index;
|
||||
size_t index_next;
|
||||
size_t index_last;
|
||||
int done;
|
||||
}
|
||||
Z;
|
||||
|
||||
static inline B**
|
||||
JOIN(A, first)(A* self)
|
||||
{
|
||||
return &self->pages[self->mark_a];
|
||||
}
|
||||
|
||||
static inline B**
|
||||
JOIN(A, last)(A* self)
|
||||
{
|
||||
return &self->pages[self->mark_b - 1];
|
||||
}
|
||||
|
||||
static inline T*
|
||||
JOIN(A, at)(A* self, size_t index)
|
||||
{
|
||||
if(self->size == 0)
|
||||
return NULL;
|
||||
else
|
||||
{
|
||||
B* first = *JOIN(A, first)(self);
|
||||
size_t actual = index + first->a;
|
||||
size_t q = actual / DEQ_BUCKET_SIZE;
|
||||
size_t r = actual % DEQ_BUCKET_SIZE;
|
||||
B* page = self->pages[self->mark_a + q];
|
||||
return &page->value[r];
|
||||
}
|
||||
}
|
||||
|
||||
static inline T*
|
||||
JOIN(A, front)(A* self)
|
||||
{
|
||||
return JOIN(A, at)(self, 0);
|
||||
}
|
||||
|
||||
static inline T*
|
||||
JOIN(A, back)(A* self)
|
||||
{
|
||||
return JOIN(A, at)(self, self->size - 1);
|
||||
}
|
||||
|
||||
static inline T*
|
||||
JOIN(A, begin)(A* self)
|
||||
{
|
||||
return JOIN(A, front)(self);
|
||||
}
|
||||
|
||||
static inline T*
|
||||
JOIN(A, end)(A* self)
|
||||
{
|
||||
return JOIN(A, back)(self) + 1;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(Z, step)(Z* self)
|
||||
{
|
||||
self->index = self->index_next;
|
||||
if(self->index == self->index_last)
|
||||
self->done = 1;
|
||||
else
|
||||
{
|
||||
self->ref = JOIN(A, at)(self->container, self->index);
|
||||
self->index_next += 1;
|
||||
}
|
||||
}
|
||||
|
||||
static inline Z
|
||||
JOIN(Z, range)(A* container, T* begin, T* end)
|
||||
{
|
||||
static Z zero;
|
||||
Z self = zero;
|
||||
if(begin && end)
|
||||
{
|
||||
self.container = container;
|
||||
self.step = JOIN(Z, step);
|
||||
self.index = begin - JOIN(A, begin)(container);
|
||||
self.index_next = self.index + 1;
|
||||
self.index_last = container->size - (JOIN(A, end)(container) - end);
|
||||
self.ref = JOIN(A, at)(container, self.index);
|
||||
}
|
||||
else
|
||||
self.done = 1;
|
||||
return self;
|
||||
}
|
||||
|
||||
static inline int
|
||||
JOIN(A, empty)(A* self)
|
||||
{
|
||||
return self->size == 0;
|
||||
}
|
||||
|
||||
static inline Z
|
||||
JOIN(Z, each)(A* a)
|
||||
{
|
||||
return JOIN(A, empty)(a)
|
||||
? JOIN(Z, range)(a, NULL, NULL)
|
||||
: JOIN(Z, range)(a, JOIN(A, begin)(a), JOIN(A, end)(a));
|
||||
}
|
||||
|
||||
static inline T
|
||||
JOIN(A, implicit_copy)(T* self)
|
||||
{
|
||||
return *self;
|
||||
}
|
||||
|
||||
static inline int
|
||||
JOIN(A, equal)(A* self, A* other, int _equal(T*, T*))
|
||||
{
|
||||
if(self->size != other->size)
|
||||
return 0;
|
||||
Z a = JOIN(Z, each)(self);
|
||||
Z b = JOIN(Z, each)(other);
|
||||
while(!a.done && !b.done)
|
||||
{
|
||||
if(!_equal(a.ref, b.ref))
|
||||
return 0;
|
||||
a.step(&a);
|
||||
b.step(&b);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, swap)(A* self, A* other)
|
||||
{
|
||||
A temp = *self;
|
||||
*self = *other;
|
||||
*other = temp;
|
||||
}
|
||||
|
||||
static inline A
|
||||
JOIN(A, init)(void)
|
||||
{
|
||||
static A zero;
|
||||
A self = zero;
|
||||
#ifdef P
|
||||
#undef P
|
||||
self.copy = JOIN(A, implicit_copy);
|
||||
#else
|
||||
self.free = JOIN(T, free);
|
||||
self.copy = JOIN(T, copy);
|
||||
#endif
|
||||
return self;
|
||||
}
|
||||
|
||||
static inline B*
|
||||
JOIN(B, init)(size_t cut)
|
||||
{
|
||||
B* self = (B*) malloc(sizeof(B));
|
||||
self->a = self->b = cut;
|
||||
return self;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, set)(A* self, size_t index, T value)
|
||||
{
|
||||
T* ref = JOIN(A, at)(self, index);
|
||||
if(self->free)
|
||||
self->free(ref);
|
||||
*ref = value;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, alloc)(A* self, size_t capacity, size_t shift_from)
|
||||
{
|
||||
self->capacity = capacity;
|
||||
self->pages = (B**) realloc(self->pages, capacity * sizeof(B*));
|
||||
size_t shift = (self->capacity - shift_from) / 2;
|
||||
size_t i = self->mark_b;
|
||||
while(i != 0)
|
||||
{
|
||||
i -= 1;
|
||||
self->pages[i + shift] = self->pages[i];
|
||||
}
|
||||
self->mark_a += shift;
|
||||
self->mark_b += shift;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, push_front)(A* self, T value)
|
||||
{
|
||||
if(JOIN(A, empty)(self))
|
||||
{
|
||||
self->mark_a = 0;
|
||||
self->mark_b = 1;
|
||||
JOIN(A, alloc)(self, 1, 0);
|
||||
*JOIN(A, last)(self) = JOIN(B, init)(DEQ_BUCKET_SIZE);
|
||||
}
|
||||
else
|
||||
{
|
||||
B* page = *JOIN(A, first)(self);
|
||||
if(page->a == 0)
|
||||
{
|
||||
if(self->mark_a == 0)
|
||||
JOIN(A, alloc)(self, 2 * self->capacity, self->mark_a);
|
||||
self->mark_a -= 1;
|
||||
*JOIN(A, first)(self) = JOIN(B, init)(DEQ_BUCKET_SIZE);
|
||||
}
|
||||
}
|
||||
B* page = *JOIN(A, first)(self);
|
||||
page->a -= 1;
|
||||
self->size += 1;
|
||||
page->value[page->a] = value;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, pop_front)(A* self)
|
||||
{
|
||||
B* page = *JOIN(A, first)(self);
|
||||
if(self->free)
|
||||
{
|
||||
T* ref = &page->value[page->a];
|
||||
self->free(ref);
|
||||
}
|
||||
page->a += 1;
|
||||
self->size -= 1;
|
||||
if(page->a == page->b)
|
||||
{
|
||||
free(page);
|
||||
self->mark_a += 1;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, push_back)(A* self, T value)
|
||||
{
|
||||
if(JOIN(A, empty)(self))
|
||||
{
|
||||
self->mark_a = 0;
|
||||
self->mark_b = 1;
|
||||
JOIN(A, alloc)(self, 1, 0);
|
||||
*JOIN(A, last)(self) = JOIN(B, init)(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
B* page = *JOIN(A, last)(self);
|
||||
if(page->b == DEQ_BUCKET_SIZE)
|
||||
{
|
||||
if(self->mark_b == self->capacity)
|
||||
JOIN(A, alloc)(self, 2 * self->capacity, self->mark_b);
|
||||
self->mark_b += 1;
|
||||
*JOIN(A, last)(self) = JOIN(B, init)(0);
|
||||
}
|
||||
}
|
||||
B* page = *JOIN(A, last)(self);
|
||||
page->value[page->b] = value;
|
||||
page->b += 1;
|
||||
self->size += 1;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, pop_back)(A* self)
|
||||
{
|
||||
B* page = *JOIN(A, last)(self);
|
||||
page->b -= 1;
|
||||
self->size -= 1;
|
||||
if(self->free)
|
||||
{
|
||||
T* ref = &page->value[page->b];
|
||||
self->free(ref);
|
||||
}
|
||||
if(page->b == page->a)
|
||||
{
|
||||
free(page);
|
||||
self->mark_b -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, erase)(A* self, size_t index)
|
||||
{
|
||||
static T zero;
|
||||
JOIN(A, set)(self, index, zero);
|
||||
void (*saved)(T*) = self->free;
|
||||
self->free = NULL;
|
||||
if(index < self->size / 2)
|
||||
{
|
||||
for(size_t i = index; i > 0; i--)
|
||||
*JOIN(A, at)(self, i) = *JOIN(A, at)(self, i - 1);
|
||||
JOIN(A, pop_front)(self);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(size_t i = index; i < self->size - 1; i++)
|
||||
*JOIN(A, at)(self, i) = *JOIN(A, at)(self, i + 1);
|
||||
JOIN(A, pop_back)(self);
|
||||
}
|
||||
self->free = saved;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, insert)(A* self, size_t index, T value)
|
||||
{
|
||||
if(self->size > 0)
|
||||
{
|
||||
void (*saved)(T*) = self->free;
|
||||
self->free = NULL;
|
||||
if(index < self->size / 2)
|
||||
{
|
||||
JOIN(A, push_front)(self, *JOIN(A, at)(self, 0));
|
||||
for(size_t i = 0; i < index; i++)
|
||||
*JOIN(A, at)(self, i) = *JOIN(A, at)(self, i + 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
JOIN(A, push_back)(self, *JOIN(A, at)(self, self->size - 1));
|
||||
for(size_t i = self->size - 1; i > index; i--)
|
||||
*JOIN(A, at)(self, i) = *JOIN(A, at)(self, i - 1);
|
||||
}
|
||||
*JOIN(A, at)(self, index) = value;
|
||||
self->free = saved;
|
||||
}
|
||||
else
|
||||
JOIN(A, push_back)(self, value);
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, resize)(A* self, size_t size, T value)
|
||||
{
|
||||
if(size != self->size)
|
||||
{
|
||||
while(size != self->size)
|
||||
if(size < self->size)
|
||||
JOIN(A, pop_back)(self);
|
||||
else
|
||||
JOIN(A, push_back)(self, self->copy(&value));
|
||||
}
|
||||
if(self->free)
|
||||
self->free(&value);
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, assign)(A* self, size_t size, T value)
|
||||
{
|
||||
JOIN(A, resize)(self, size, self->copy(&value));
|
||||
for(size_t i = 0; i < size; i++)
|
||||
JOIN(A, set)(self, i, self->copy(&value));
|
||||
if(self->free)
|
||||
self->free(&value);
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, clear)(A* self)
|
||||
{
|
||||
while(!JOIN(A, empty)(self))
|
||||
JOIN(A, pop_back)(self);
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, free)(A* self)
|
||||
{
|
||||
JOIN(A, clear)(self);
|
||||
free(self->pages);
|
||||
*self = JOIN(A, init)();
|
||||
}
|
||||
|
||||
static inline A
|
||||
JOIN(A, copy)(A* self)
|
||||
{
|
||||
A other = JOIN(A, init)();
|
||||
while(other.size < self->size)
|
||||
{
|
||||
T* value = JOIN(A, at)(self, other.size);
|
||||
JOIN(A, push_back)(&other, other.copy(value));
|
||||
}
|
||||
return other;
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, ranged_sort)(A* self, int64_t a, int64_t b, int _compare(T*, T*))
|
||||
{
|
||||
if(a >= b)
|
||||
return;
|
||||
int64_t mid = (a + b) / 2;
|
||||
SWAP(T, JOIN(A, at)(self, a), JOIN(A, at)(self, mid));
|
||||
int64_t z = a;
|
||||
for(int64_t i = a + 1; i <= b; i++)
|
||||
if(_compare(JOIN(A, at)(self, a), JOIN(A, at)(self, i)))
|
||||
{
|
||||
z += 1;
|
||||
SWAP(T, JOIN(A, at)(self, z), JOIN(A, at)(self, i));
|
||||
}
|
||||
SWAP(T, JOIN(A, at)(self, a), JOIN(A, at)(self, z));
|
||||
JOIN(A, ranged_sort)(self, a, z - 1, _compare);
|
||||
JOIN(A, ranged_sort)(self, z + 1, b, _compare);
|
||||
}
|
||||
|
||||
static inline void
|
||||
JOIN(A, sort)(A* self, int _compare(T*, T*))
|
||||
{
|
||||
JOIN(A, ranged_sort)(self, 0, self->size - 1, _compare);
|
||||
}
|
||||
|
||||
static inline size_t
|
||||
JOIN(A, remove_if)(A* self, int _match(T*))
|
||||
{
|
||||
size_t erases = 0;
|
||||
foreach(A, self, it)
|
||||
if(_match(it.ref))
|
||||
{
|
||||
JOIN(A, erase)(self, it.index);
|
||||
it.index_next = it.index;
|
||||
it.index_last -= 1;
|
||||
erases += 1;
|
||||
}
|
||||
return erases;
|
||||
}
|
||||
|
||||
static inline T*
|
||||
JOIN(A, find)(A* self, T key, int _equal(T*, T*))
|
||||
{
|
||||
foreach(A, self, it)
|
||||
if(_equal(it.ref, &key))
|
||||
return it.ref;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#undef T
|
||||
#undef A
|
||||
#undef B
|
||||
#undef Z
|
||||
|
||||
#undef DEQ_BUCKET_SIZE
|
||||
|
||||
//-------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// LemireMaxClampedFilter streaming max over window
|
||||
// implementation for "Daniel Lemire, Streaming Maximum-Minimum Filter Using No More than Three Comparisons per Element. Nordic Journal of Computing, 13 (4), pages 328-339, 2006."
|
||||
// preprint https://arxiv.org/abs/cs.DS/0610046
|
||||
// source https://github.com/lemire/runningmaxmin
|
||||
|
||||
typedef struct LemireMaxClampedFilter {
|
||||
int windowSize;
|
||||
int dataToFilterSize;
|
||||
deq_int fifo;
|
||||
float* dataToFilter;
|
||||
} LemireMaxClampedFilter;
|
||||
|
||||
LemireMaxClampedFilter* lemire_max_clamped_init(int windowSize, int dataLength) {
|
||||
LemireMaxClampedFilter* lem = NULL;
|
||||
if (windowSize % 2 == 0) {
|
||||
return NULL;
|
||||
}
|
||||
lem = (LemireMaxClampedFilter*)malloc(sizeof(LemireMaxClampedFilter));
|
||||
if (lem == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
lem->windowSize = windowSize;
|
||||
lem->dataToFilterSize = (dataLength + windowSize - 1);
|
||||
lem->fifo = deq_int_init();
|
||||
lem->dataToFilter = (float*)malloc(lem->dataToFilterSize * sizeof(float));
|
||||
memset(lem->dataToFilter, 0, lem->dataToFilterSize * sizeof(float));
|
||||
return lem;
|
||||
}
|
||||
|
||||
void lemire_max_clamped_free(LemireMaxClampedFilter* l) {
|
||||
if (l != NULL) {
|
||||
free(l->dataToFilter);
|
||||
l->dataToFilter = NULL;
|
||||
deq_int_free(&l->fifo);
|
||||
free(l);
|
||||
}
|
||||
}
|
||||
|
||||
void lemire_max_clamped_filter(LemireMaxClampedFilter* l, float* __restrict__ array, size_t array_size, float* __restrict__ max) {
|
||||
int ix = l->windowSize / 2;
|
||||
//TODO: avoid copy entirely?
|
||||
memcpy(&l->dataToFilter[ix], &array[0], array_size * sizeof(float));
|
||||
for (int j = 0; j < ix; ++j){
|
||||
l->dataToFilter[j] = array[0];
|
||||
}
|
||||
for (int j = l->dataToFilterSize - ix; j < l->dataToFilterSize; ++j){
|
||||
l->dataToFilter[j] = array[array_size - 1];
|
||||
}
|
||||
|
||||
deq_int_clear(&l->fifo);
|
||||
deq_int_push_back(&l->fifo, 0);
|
||||
|
||||
for (int i = 1; i < l->windowSize; ++i) {
|
||||
if (l->dataToFilter[i] > l->dataToFilter[i - 1]) { //overshoot
|
||||
deq_int_pop_back(&l->fifo);
|
||||
while (!deq_int_empty(&l->fifo)){
|
||||
if (l->dataToFilter[i] <= l->dataToFilter[*deq_int_back(&l->fifo)]) {
|
||||
break;
|
||||
}
|
||||
deq_int_pop_back(&l->fifo);
|
||||
}
|
||||
}
|
||||
|
||||
deq_int_push_back(&l->fifo, i);
|
||||
}
|
||||
|
||||
for (int i = l->windowSize; i < l->dataToFilterSize; ++i) {
|
||||
max[i - l->windowSize] = l->dataToFilter[*deq_int_front(&l->fifo)];
|
||||
|
||||
if (l->dataToFilter[i] > l->dataToFilter[i - 1]) { //overshoot
|
||||
deq_int_pop_back(&l->fifo);
|
||||
while (!deq_int_empty(&l->fifo)){
|
||||
if (l->dataToFilter[i] <= l->dataToFilter[*deq_int_back(&l->fifo)]) {
|
||||
break;
|
||||
}
|
||||
deq_int_pop_back(&l->fifo);
|
||||
}
|
||||
}
|
||||
|
||||
deq_int_push_back(&l->fifo, i);
|
||||
|
||||
if (i == (l->windowSize + *deq_int_front(&l->fifo))){
|
||||
deq_int_pop_front(&l->fifo);
|
||||
}
|
||||
}
|
||||
|
||||
max[l->dataToFilterSize - l->windowSize] = l->dataToFilter[*deq_int_front(&l->fifo)];
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
51
src/cpp/test.cpp
Normal file
51
src/cpp/test.cpp
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include "hibiki.h"
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <iomanip>
|
||||
|
||||
int main() {
|
||||
|
||||
auto fp = fopen("test.raw", "r");
|
||||
|
||||
if (fp == nullptr){
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto ptr = new_point_processor(16000, 1);
|
||||
|
||||
float audioData[PANAKO_AUDIO_BLOCK_SIZE];
|
||||
|
||||
while (true){
|
||||
auto read = fread(&audioData[0], sizeof(audioData[0]), sizeof(audioData) / sizeof(audioData[0]), fp);
|
||||
|
||||
if (read > 0){
|
||||
point_processor_process(ptr, &audioData[0], read);
|
||||
}
|
||||
|
||||
if (read < sizeof(audioData) / sizeof(audioData[0])) { //EOF
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
size_t size;
|
||||
auto result = point_processor_finish(ptr, 1, &size);
|
||||
|
||||
std::vector<uint8_t> v(result, result + size);
|
||||
std::stringstream ss;
|
||||
ss << std::hex << std::setfill('0');
|
||||
std::vector<uint8_t>::const_iterator it;
|
||||
|
||||
for (it = v.begin(); it != v.end(); it++) {
|
||||
ss << std::setw(2) << static_cast<unsigned>(*it);
|
||||
}
|
||||
|
||||
std::cout << ss.str() << std::endl;
|
||||
|
||||
|
||||
fclose(fp);
|
||||
|
||||
delete_point_processor(ptr);
|
||||
return 0;
|
||||
}
|
BIN
src/cpp/test.raw
Normal file
BIN
src/cpp/test.raw
Normal file
Binary file not shown.
0
src/js/post.js
Normal file
0
src/js/post.js
Normal file
1
src/js/pre.js
Normal file
1
src/js/pre.js
Normal file
|
@ -0,0 +1 @@
|
|||
|
Loading…
Reference in a new issue