├── LICENSE ├── README.md ├── dywapitchtrack.cpp ├── dywapitchtrack.h ├── fix.h ├── fix_fft.cpp ├── fix_fft.h ├── m5Cardputer_audiospectrum.ino └── m5Cardputer_audiospectrum.ino.bin /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## M5Cardputer Audio Spectrum Display, 2 | 3 | 4 | - Código Portado por Aurélio Monteiro Avanzi 5 | - código original: M5StickC Audio Spectrum Display, Oscilloscope, and Tuner Copyright 2020 KIRA Ryouta 6 | 7 | ![image](https://github.com/cyberwisk/m5Cardputer_audiospectrum/assets/3136312/b47dfee6-7690-40d5-af1d-68f29ed2e55a) 8 | -------------------------------------------------------------------------------- /dywapitchtrack.cpp: -------------------------------------------------------------------------------- 1 | /* dywapitchtrack.c 2 | 3 | Dynamic Wavelet Algorithm Pitch Tracking library 4 | Released under the MIT open source licence 5 | 6 | Copyright (c) 2010 Antoine Schmitt 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | /* 28 | * Modified by KIRA Ryouta on 2020. 29 | */ 30 | 31 | #include "dywapitchtrack.h" 32 | #include 33 | #include 34 | #include // for memset 35 | #include 36 | #include 37 | 38 | //********************** 39 | // Utils 40 | //********************** 41 | 42 | #ifndef max 43 | #define max(x, y) ((x) > (y)) ? (x) : (y) 44 | #endif 45 | #ifndef min 46 | #define min(x, y) ((x) < (y)) ? (x) : (y) 47 | #endif 48 | 49 | // returns 1 if power of 2 50 | int _power2p(int value) { 51 | if (value == 0) return 1; 52 | if (value == 2) return 1; 53 | if (value & 0x1) return 0; 54 | return (_power2p(value >> 1)); 55 | } 56 | 57 | // count number of bits 58 | int _bitcount(int value) { 59 | float temp = (float)value + 0.5f; 60 | return (*reinterpret_cast(&temp) >> 23) - 126; 61 | } 62 | 63 | // closest power of 2 above or equal 64 | int _ceil_power2(int value) { 65 | if (_power2p(value)) return value; 66 | 67 | if (value == 1) return 2; 68 | int i = _bitcount(value); 69 | return 1 << i; 70 | } 71 | 72 | // closest power of 2 below or equal 73 | int _floor_power2(int value) { 74 | if (_power2p(value)) return value; 75 | if (value == 1) return 2; 76 | int i = _bitcount(value); 77 | return (1 << i) >> 1; 78 | } 79 | 80 | // abs value 81 | int _iabs(int x) { 82 | return abs(x); 83 | } 84 | 85 | // 2 power 86 | int _2power(int i) { 87 | return 1 << i; 88 | } 89 | 90 | //****************************** 91 | // the Wavelet algorithm itself 92 | //****************************** 93 | 94 | int dywapitch_neededsamplecount(int minFreq) { 95 | int nbSam = 3*44100/minFreq; // 1017. for 130 Hz 96 | nbSam = _ceil_power2(nbSam); // 1024 97 | return nbSam; 98 | } 99 | 100 | typedef struct _minmax { 101 | int index; 102 | struct _minmax *next; 103 | } minmax; 104 | 105 | static int distances[DYWAPT_SAMPLESIZE]; 106 | static int mins[DYWAPT_SAMPLESIZE]; 107 | static int maxs[DYWAPT_SAMPLESIZE]; 108 | 109 | DYWAPT_FLOAT _dywapitch_computeWaveletPitch(DYWAPT_INPUT *sam) { 110 | DYWAPT_FLOAT pitchF = 0.0f; 111 | 112 | int i, j; 113 | DYWAPT_TEMP si, si1; 114 | 115 | int curSamNb = DYWAPT_SAMPLESIZE; 116 | int nbMins, nbMaxs; 117 | 118 | // algorithm parameters 119 | #define maxFLWTlevels 6 120 | #define maxF (DYWAPT_SAMPLERATE/2) 121 | #define differenceLevelsN 3 122 | 123 | #define maximaThresholdRatio 0.75f 124 | #define maximaThresholdRatioMul 3 125 | #define maximaThresholdRatioRSHIFT 2 126 | 127 | DYWAPT_TEMP ampltitudeThreshold; 128 | DYWAPT_TEMP theDC = 0; 129 | 130 | { // compute ampltitudeThreshold and theDC 131 | //first compute the DC and maxAMplitude 132 | DYWAPT_TEMP maxValue = DYWAPT_TEMPMIN; 133 | DYWAPT_TEMP minValue = DYWAPT_TEMPMAX; 134 | for (i = 0; i < DYWAPT_SAMPLESIZE;i++) { 135 | si = sam[i]; 136 | theDC = theDC + si; 137 | if (si > maxValue) maxValue = si; 138 | if (si < minValue) minValue = si; 139 | } 140 | if (std::is_integral::value) { 141 | theDC = (int)theDC >> DYWAPT_SAMPLESIZELOG2; 142 | } 143 | else { 144 | theDC = theDC*(1./DYWAPT_SAMPLESIZE); 145 | } 146 | maxValue = maxValue - theDC; 147 | minValue = minValue - theDC; 148 | DYWAPT_TEMP amplitudeMax = (maxValue > -minValue ? maxValue : -minValue); 149 | 150 | if (std::is_integral::value) { 151 | ampltitudeThreshold = (int)amplitudeMax * maximaThresholdRatioMul >> maximaThresholdRatioRSHIFT; 152 | } 153 | else { 154 | ampltitudeThreshold = amplitudeMax*maximaThresholdRatio; 155 | } 156 | 157 | } 158 | 159 | // levels, start without downsampling.. 160 | int curLevel = 0; 161 | DYWAPT_FLOAT curModeDistance = -1.f; 162 | int delta; 163 | 164 | while(1) { 165 | 166 | // delta 167 | delta = (DYWAPT_SAMPLERATE / maxF) >> curLevel; 168 | //("dywapitch doing level=%ld delta=%ld\n", curLevel, delta); 169 | 170 | if (curSamNb < 2) goto cleanup; 171 | 172 | // compute the first maximums and minumums after zero-crossing 173 | // store if greater than the min threshold 174 | // and if at a greater distance than delta 175 | DYWAPT_TEMP dv, previousDV = -1000; 176 | nbMins = nbMaxs = 0; 177 | int lastMinIndex = -1000000; 178 | int lastmaxIndex = -1000000; 179 | int findMax = 0; 180 | int findMin = 0; 181 | for (i = 1; i < curSamNb; i++) { 182 | si = sam[i] - theDC; 183 | si1 = sam[i-1] - theDC; 184 | 185 | if (si1 <= 0 && si > 0) {findMax = 1; findMin = 0; } 186 | if (si1 >= 0 && si < 0) {findMin = 1; findMax = 0; } 187 | 188 | // min or max ? 189 | dv = si - si1; 190 | 191 | if (previousDV > -1000) { 192 | 193 | if (findMin && previousDV < 0 && dv >= 0) { 194 | // minimum 195 | if (abs(si1) >= ampltitudeThreshold) { 196 | if (i - 1 > lastMinIndex + delta) { 197 | mins[nbMins++] = i - 1; 198 | lastMinIndex = i - 1; 199 | findMin = 0; 200 | //if DEBUGG then put "min ok"&&si 201 | // 202 | } else { 203 | //if DEBUGG then put "min too close to previous"&&(i - lastMinIndex) 204 | // 205 | } 206 | } else { 207 | // if DEBUGG then put "min "&abs(si)&" < thresh = "&ltitudeThreshold 208 | //-- 209 | } 210 | } 211 | 212 | if (findMax && previousDV > 0 && dv <= 0) { 213 | // maximum 214 | if (abs(si1) >= ampltitudeThreshold) { 215 | if (i -1 > lastmaxIndex + delta) { 216 | maxs[nbMaxs++] = i - 1; 217 | lastmaxIndex = i - 1; 218 | findMax = 0; 219 | } else { 220 | //if DEBUGG then put "max too close to previous"&&(i - lastmaxIndex) 221 | //-- 222 | } 223 | } else { 224 | //if DEBUGG then put "max "&abs(si)&" < thresh = "&ltitudeThreshold 225 | //-- 226 | } 227 | } 228 | } 229 | 230 | previousDV = dv; 231 | } 232 | 233 | if (nbMins == 0 && nbMaxs == 0) { 234 | // no best distance ! 235 | //asLog("dywapitch no mins nor maxs, exiting\n"); 236 | 237 | // if DEBUGG then put "no mins nor maxs, exiting" 238 | goto cleanup; 239 | } 240 | //if DEBUGG then put count(maxs)&&"maxs &"&&count(mins)&&"mins" 241 | 242 | // maxs = [5, 20, 100,...] 243 | // compute distances 244 | int d; 245 | memset(distances, 0, DYWAPT_SAMPLESIZE*sizeof(int)); 246 | for (i = 0 ; i < nbMins ; i++) { 247 | for (j = 1; j < differenceLevelsN; j++) { 248 | if (i+j < nbMins) { 249 | d = _iabs(mins[i] - mins[i+j]); 250 | //asLog("dywapitch i=%ld j=%ld d=%ld\n", i, j, d); 251 | ++distances[d]; 252 | } 253 | } 254 | } 255 | for (i = 0 ; i < nbMaxs ; i++) { 256 | for (j = 1; j < differenceLevelsN; j++) { 257 | if (i+j < nbMaxs) { 258 | d = _iabs(maxs[i] - maxs[i+j]); 259 | //asLog("dywapitch i=%ld j=%ld d=%ld\n", i, j, d); 260 | ++distances[d]; 261 | } 262 | } 263 | } 264 | 265 | // find best summed distance 266 | int bestDistance = -1; 267 | int bestValue = -1; 268 | for (i = 0; i< curSamNb; i++) { 269 | int summed = 0; 270 | for (j = -delta ; j <= delta ; j++) { 271 | if (i+j >=0 && i+j < curSamNb) 272 | summed += distances[i+j]; 273 | } 274 | //asLog("dywapitch i=%ld summed=%ld bestDistance=%ld\n", i, summed, bestDistance); 275 | if (summed == bestValue) { 276 | if (i == 2*bestDistance) 277 | bestDistance = i; 278 | 279 | } else if (summed > bestValue) { 280 | bestValue = summed; 281 | bestDistance = i; 282 | } 283 | } 284 | //asLog("dywapitch bestDistance=%ld\n", bestDistance); 285 | 286 | // averaging 287 | DYWAPT_FLOAT distAvg = 0.0; 288 | DYWAPT_FLOAT nbDists = 0; 289 | for (j = -delta ; j <= delta ; j++) { 290 | if (bestDistance+j >=0 && bestDistance+j < DYWAPT_SAMPLESIZE) { 291 | int nbDist = distances[bestDistance+j]; 292 | if (nbDist > 0) { 293 | nbDists += nbDist; 294 | distAvg += (bestDistance+j)*nbDist; 295 | } 296 | } 297 | } 298 | // this is our mode distance ! 299 | distAvg /= nbDists; 300 | //asLog("dywapitch distAvg=%f\n", distAvg); 301 | 302 | // continue the levels ? 303 | if (curModeDistance > -1.) { 304 | DYWAPT_FLOAT similarity = fabs(distAvg*2 - curModeDistance); 305 | if (similarity <= 2*delta) { 306 | //if DEBUGG then put "similarity="&similarity&&"delta="&delta&&"ok" 307 | //asLog("dywapitch similarity=%f OK !\n", similarity); 308 | // two consecutive similar mode distances : ok ! 309 | pitchF = (DYWAPT_FLOAT)DYWAPT_SAMPLERATE/(_2power(curLevel-1)*curModeDistance); 310 | goto cleanup; 311 | } 312 | //if DEBUGG then put "similarity="&similarity&&"delta="&delta&&"not" 313 | } 314 | 315 | // not similar, continue next level 316 | curModeDistance = distAvg; 317 | 318 | curLevel = curLevel + 1; 319 | if (curLevel >= maxFLWTlevels) { 320 | // put "max levels reached, exiting" 321 | //asLog("dywapitch max levels reached, exiting\n"); 322 | goto cleanup; 323 | } 324 | 325 | // downsample 326 | if (curSamNb < 2) { 327 | //asLog("dywapitch not enough samples, exiting\n"); 328 | goto cleanup; 329 | } 330 | for (i = 0; i < curSamNb/2; i++) { 331 | sam[i] = (sam[2*i] + sam[2*i + 1])*0.5; 332 | } 333 | curSamNb >>= 1; 334 | } 335 | 336 | /// 337 | cleanup: 338 | 339 | return pitchF; 340 | } 341 | 342 | // *********************************** 343 | // the dynamic postprocess 344 | // *********************************** 345 | 346 | /*** 347 | It states: 348 | - a pitch cannot change much all of a sudden (20%) (impossible humanly, 349 | so if such a situation happens, consider that it is a mistake and drop it. 350 | - a pitch cannot DYWAPT_FLOAT or be divided by 2 all of a sudden : it is an 351 | algorithm side-effect : divide it or DYWAPT_FLOAT it by 2. 352 | - a lonely voiced pitch cannot happen, nor can a sudden drop in the middle 353 | of a voiced segment. Smooth the plot. 354 | ***/ 355 | 356 | DYWAPT_FLOAT _dywapitch_dynamicprocess(dywapitchtracker *pitchtracker, DYWAPT_FLOAT pitch) { 357 | 358 | // equivalence 359 | if (pitch == 0.0) pitch = -1.0; 360 | 361 | // 362 | DYWAPT_FLOAT estimatedPitch = -1; 363 | DYWAPT_FLOAT acceptedError = 0.2f; 364 | int maxConfidence = 5; 365 | 366 | if (pitch != -1) { 367 | // I have a pitch here 368 | 369 | if (pitchtracker->_prevPitch == -1) { 370 | // no previous 371 | estimatedPitch = pitch; 372 | pitchtracker->_prevPitch = pitch; 373 | pitchtracker->_pitchConfidence = 1; 374 | 375 | } else if (abs(pitchtracker->_prevPitch - pitch)/pitch < acceptedError) { 376 | // similar : remember and increment pitch 377 | pitchtracker->_prevPitch = pitch; 378 | estimatedPitch = pitch; 379 | pitchtracker->_pitchConfidence = min(maxConfidence, pitchtracker->_pitchConfidence + 1); // maximum 3 380 | 381 | } else if ((pitchtracker->_pitchConfidence >= maxConfidence-2) && abs(pitchtracker->_prevPitch - 2.*pitch)/(2.*pitch) < acceptedError) { 382 | // close to half the last pitch, which is trusted 383 | estimatedPitch = 2.*pitch; 384 | pitchtracker->_prevPitch = estimatedPitch; 385 | 386 | } else if ((pitchtracker->_pitchConfidence >= maxConfidence-2) && abs(pitchtracker->_prevPitch - 0.5*pitch)/(0.5*pitch) < acceptedError) { 387 | // close to twice the last pitch, which is trusted 388 | estimatedPitch = 0.5*pitch; 389 | pitchtracker->_prevPitch = estimatedPitch; 390 | 391 | } else { 392 | // nothing like this : very different value 393 | if (pitchtracker->_pitchConfidence >= 1) { 394 | // previous trusted : keep previous 395 | estimatedPitch = pitchtracker->_prevPitch; 396 | pitchtracker->_pitchConfidence = max(0, pitchtracker->_pitchConfidence - 1); 397 | } else { 398 | // previous not trusted : take current 399 | estimatedPitch = pitch; 400 | pitchtracker->_prevPitch = pitch; 401 | pitchtracker->_pitchConfidence = 1; 402 | } 403 | } 404 | 405 | } else { 406 | // no pitch now 407 | if (pitchtracker->_prevPitch != -1) { 408 | // was pitch before 409 | if (pitchtracker->_pitchConfidence >= 1) { 410 | // continue previous 411 | estimatedPitch = pitchtracker->_prevPitch; 412 | pitchtracker->_pitchConfidence = max(0, pitchtracker->_pitchConfidence - 1); 413 | } else { 414 | pitchtracker->_prevPitch = -1; 415 | estimatedPitch = -1.; 416 | pitchtracker->_pitchConfidence = 0; 417 | } 418 | } 419 | } 420 | 421 | // put "_pitchConfidence="&pitchtracker->_pitchConfidence 422 | if (pitchtracker->_pitchConfidence >= 1) { 423 | // ok 424 | pitch = estimatedPitch; 425 | } else { 426 | pitch = -1; 427 | } 428 | 429 | // equivalence 430 | if (pitch == -1) pitch = 0.0; 431 | 432 | return pitch; 433 | } 434 | 435 | 436 | // ************************************ 437 | // the API main entry points 438 | // ************************************ 439 | 440 | void dywapitch_inittracking(dywapitchtracker *pitchtracker) { 441 | pitchtracker->_prevPitch = -1.; 442 | pitchtracker->_pitchConfidence = -1; 443 | } 444 | 445 | DYWAPT_FLOAT dywapitch_computepitch(dywapitchtracker *pitchtracker, DYWAPT_INPUT *samples) { 446 | DYWAPT_FLOAT raw_pitch = _dywapitch_computeWaveletPitch(samples); 447 | return _dywapitch_dynamicprocess(pitchtracker, raw_pitch); 448 | } 449 | -------------------------------------------------------------------------------- /dywapitchtrack.h: -------------------------------------------------------------------------------- 1 | /* dywapitchtrack.h 2 | 3 | Dynamic Wavelet Algorithm Pitch Tracking library 4 | Released under the MIT open source licence 5 | 6 | Copyright (c) 2010 Antoine Schmitt 7 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy 9 | of this software and associated documentation files (the "Software"), to deal 10 | in the Software without restriction, including without limitation the rights 11 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in 16 | all copies or substantial portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | THE SOFTWARE. 25 | */ 26 | 27 | /* Documentation 28 | 29 | The dywapitchtrack library computes the pitch of an audio stream in real time. 30 | 31 | The pitch is the main frequency of the waveform (the 'note' being played or sung). 32 | It is expressed as a float in Hz. 33 | 34 | Unlike the human ear, pitch detection is difficult to achieve for computers. Many 35 | algorithms have been designed and experimented, but there is no 'best' algorithm. 36 | They all depend on the context and the tradeoffs acceptable in terms of speed and 37 | latency. The context includes the quality and 'cleanness' of the audio : obviously 38 | polyphonic sounds (multiple instruments playing different notes at the same time) 39 | are extremely difficult to track, percussive or noisy audio has no pitch, most 40 | real-life audio have some noisy moments, some instruments have a lot of harmonics, 41 | etc... 42 | 43 | The dywapitchtrack is based on a custom-tailored algorithm which is of very high quality: 44 | both very accurate (precision < 0.05 semitones), very low latency (< 23 ms) and 45 | very low error rate. It has been thoroughly tested on human voice. 46 | 47 | It can best be described as a dynamic wavelet algorithm (dywa): 48 | 49 | The heart of the algorithm is a very powerful wavelet algorithm, described in a paper 50 | by Eric Larson and Ross Maddox : "Real-Time Time-Domain Pitch Tracking Using Wavelets" 51 | http://online.physics.uiuc.edu/courses/phys498pom/NSF_REU_Reports/2005_reu/Real-Time_Time-Domain_Pitch_Tracking_Using_Wavelets.pdf 52 | 53 | This central algorithm has been improved by adding dynamic tracking, to reduce the 54 | common problems of frequency-halving and voiced/unvoiced errors. This dynamic tracking 55 | explains the need for a tracking structure (dywapitchtracker). The dynamic tracking assumes 56 | that the main function dywapitch_computepitch is called repeatedly, as it follows the pitch 57 | over time and makes assumptions about human voice capabilities and reallife conditions 58 | (as documented inside the code). 59 | 60 | Note : The algorithm currently assumes a 44100Hz audio sampling rate. If you use a different 61 | samplerate, you can just multiply the resulting pitch by the ratio between your samplerate and 44100. 62 | */ 63 | 64 | /* Usage 65 | 66 | // Allocate your audio buffers and start the audio stream. 67 | // Allocate a 'dywapitchtracker' structure. 68 | // Start the pitch tracking by calling 'dywapitch_inittracking'. 69 | dywapitchtracker pitchtracker; 70 | dywapitch_inittracking(&pitchtracker); 71 | 72 | // For each available audio buffer, call 'dywapitch_computepitch' 73 | double thepitch = dywapitch_computepitch(&pitchtracker, samples, start, count); 74 | 75 | */ 76 | 77 | #ifndef dywapitchtrack__H 78 | #define dywapitchtrack__H 79 | 80 | #define DYWAPT_FLOAT float 81 | #define DYWAPT_INPUT int 82 | #define DYWAPT_TEMP int 83 | #define DYWAPT_TEMPMAX (INT_MAX) 84 | #define DYWAPT_TEMPMIN (INT_MIN) 85 | #define DYWAPT_SAMPLESIZE 2048 86 | #define DYWAPT_SAMPLESIZELOG2 11 87 | #define DYWAPT_SAMPLERATE 44100 88 | /* 89 | #define DYWAPT_INPUT short 90 | #define DYWAPT_TEMP int 91 | #define DYWAPT_TEMPMAX (INT_MAX) 92 | #define DYWAPT_TEMPMIN (INT_MIN) 93 | */ 94 | /* 95 | #define DYWAPT_INPUT float 96 | #define DYWAPT_TEMP float 97 | #define DYWAPT_TEMPMAX (MAXFLOAT) 98 | #define DYWAPT_TEMPMIN (-MAXFLOAT) 99 | */ 100 | 101 | // structure to hold tracking data 102 | typedef struct _dywapitchtracker { 103 | DYWAPT_FLOAT _prevPitch; 104 | int _pitchConfidence; 105 | } dywapitchtracker; 106 | 107 | // returns the number of samples needed to compute pitch for fequencies equal and above the given minFreq (in Hz) 108 | // useful to allocate large enough audio buffer 109 | // ex : for frequencies above 130Hz, you need 1024 samples (assuming a 44100 Hz samplerate) 110 | int dywapitch_neededsamplecount(int minFreq); 111 | 112 | // call before computing any pitch, passing an allocated dywapitchtracker structure 113 | void dywapitch_inittracking(dywapitchtracker *pitchtracker); 114 | 115 | // computes the pitch. Pass the inited dywapitchtracker structure 116 | // samples : a pointer to the sample buffer 117 | // return 0.0 if no pitch was found (sound too low, noise, etc..) 118 | DYWAPT_FLOAT dywapitch_computepitch(dywapitchtracker *pitchtracker, DYWAPT_INPUT *samples); 119 | 120 | #endif 121 | -------------------------------------------------------------------------------- /fix.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | template 5 | static inline int FIX_MPY(int a, int b) 6 | { 7 | a *= b; 8 | a += 1 << (p-1); 9 | a >>= p; 10 | return a; 11 | } 12 | 13 | template 14 | static inline int FIX_SQU(int a) 15 | { 16 | return FIX_MPY

(a, a); 17 | } 18 | 19 | /** 20 | * Fixed point Log2 21 | * 22 | * https://stackoverflow.com/questions/4657468/fast-fixed-point-pow-log-exp-and-sqrt 23 | */ 24 | template 25 | static inline int FIX_LOG2(unsigned int x) 26 | { 27 | unsigned int b = 1U << (p - 1); 28 | int y = 0; 29 | 30 | if (p < 1 || p > 31) { 31 | return INT_MAX; // indicates an error 32 | } 33 | 34 | if (x == 0) { 35 | return INT_MIN; // represents negative infinity 36 | } 37 | 38 | while (x < 1U << p) { 39 | x <<= 1; 40 | y -= 1U << p; 41 | } 42 | 43 | while (x >= 2U << p) { 44 | x >>= 1; 45 | y += 1U << p; 46 | } 47 | 48 | unsigned long z = x; 49 | 50 | for (int i = 0; i < p; i++) { 51 | z = (z * z + (1 << (p-1))) >> p; 52 | if (z >= 2 << p) { 53 | z >>= 1; 54 | y += b; 55 | } 56 | b >>= 1; 57 | } 58 | 59 | return y; 60 | } 61 | -------------------------------------------------------------------------------- /fix_fft.cpp: -------------------------------------------------------------------------------- 1 | #include "fix_fft.h" 2 | 3 | const short Fixed15FFT::Sinewave[Fixed15FFT::SAMPLE_SIZE] = { 4 | 0, 201, 402, 603, 804, 1005, 1206, 1406, 5 | 1607, 1808, 2009, 2209, 2410, 2610, 2811, 3011, 6 | 3211, 3411, 3611, 3811, 4011, 4210, 4409, 4608, 7 | 4807, 5006, 5205, 5403, 5601, 5799, 5997, 6195, 8 | 6392, 6589, 6786, 6982, 7179, 7375, 7571, 7766, 9 | 7961, 8156, 8351, 8545, 8739, 8932, 9126, 9319, 10 | 9511, 9703, 9895, 10087, 10278, 10469, 10659, 10849, 11 | 11038, 11227, 11416, 11604, 11792, 11980, 12166, 12353, 12 | 12539, 12724, 12909, 13094, 13278, 13462, 13645, 13827, 13 | 14009, 14191, 14372, 14552, 14732, 14911, 15090, 15268, 14 | 15446, 15623, 15799, 15975, 16150, 16325, 16499, 16672, 15 | 16845, 17017, 17189, 17360, 17530, 17699, 17868, 18036, 16 | 18204, 18371, 18537, 18702, 18867, 19031, 19194, 19357, 17 | 19519, 19680, 19840, 20000, 20159, 20317, 20474, 20631, 18 | 20787, 20942, 21096, 21249, 21402, 21554, 21705, 21855, 19 | 22004, 22153, 22301, 22448, 22594, 22739, 22883, 23027, 20 | 23169, 23311, 23452, 23592, 23731, 23869, 24006, 24143, 21 | 24278, 24413, 24546, 24679, 24811, 24942, 25072, 25201, 22 | 25329, 25456, 25582, 25707, 25831, 25954, 26077, 26198, 23 | 26318, 26437, 26556, 26673, 26789, 26905, 27019, 27132, 24 | 27244, 27355, 27466, 27575, 27683, 27790, 27896, 28001, 25 | 28105, 28208, 28309, 28410, 28510, 28608, 28706, 28802, 26 | 28897, 28992, 29085, 29177, 29268, 29358, 29446, 29534, 27 | 29621, 29706, 29790, 29873, 29955, 30036, 30116, 30195, 28 | 30272, 30349, 30424, 30498, 30571, 30643, 30713, 30783, 29 | 30851, 30918, 30984, 31049, 31113, 31175, 31236, 31297, 30 | 31356, 31413, 31470, 31525, 31580, 31633, 31684, 31735, 31 | 31785, 31833, 31880, 31926, 31970, 32014, 32056, 32097, 32 | 32137, 32176, 32213, 32249, 32284, 32318, 32350, 32382, 33 | 32412, 32441, 32468, 32495, 32520, 32544, 32567, 32588, 34 | 32609, 32628, 32646, 32662, 32678, 32692, 32705, 32717, 35 | 32727, 32736, 32744, 32751, 32757, 32761, 32764, 32766, 36 | 32767, 32766, 32764, 32761, 32757, 32751, 32744, 32736, 37 | 32727, 32717, 32705, 32692, 32678, 32662, 32646, 32628, 38 | 32609, 32588, 32567, 32544, 32520, 32495, 32468, 32441, 39 | 32412, 32382, 32350, 32318, 32284, 32249, 32213, 32176, 40 | 32137, 32097, 32056, 32014, 31970, 31926, 31880, 31833, 41 | 31785, 31735, 31684, 31633, 31580, 31525, 31470, 31413, 42 | 31356, 31297, 31236, 31175, 31113, 31049, 30984, 30918, 43 | 30851, 30783, 30713, 30643, 30571, 30498, 30424, 30349, 44 | 30272, 30195, 30116, 30036, 29955, 29873, 29790, 29706, 45 | 29621, 29534, 29446, 29358, 29268, 29177, 29085, 28992, 46 | 28897, 28802, 28706, 28608, 28510, 28410, 28309, 28208, 47 | 28105, 28001, 27896, 27790, 27683, 27575, 27466, 27355, 48 | 27244, 27132, 27019, 26905, 26789, 26673, 26556, 26437, 49 | 26318, 26198, 26077, 25954, 25831, 25707, 25582, 25456, 50 | 25329, 25201, 25072, 24942, 24811, 24679, 24546, 24413, 51 | 24278, 24143, 24006, 23869, 23731, 23592, 23452, 23311, 52 | 23169, 23027, 22883, 22739, 22594, 22448, 22301, 22153, 53 | 22004, 21855, 21705, 21554, 21402, 21249, 21096, 20942, 54 | 20787, 20631, 20474, 20317, 20159, 20000, 19840, 19680, 55 | 19519, 19357, 19194, 19031, 18867, 18702, 18537, 18371, 56 | 18204, 18036, 17868, 17699, 17530, 17360, 17189, 17017, 57 | 16845, 16672, 16499, 16325, 16150, 15975, 15799, 15623, 58 | 15446, 15268, 15090, 14911, 14732, 14552, 14372, 14191, 59 | 14009, 13827, 13645, 13462, 13278, 13094, 12909, 12724, 60 | 12539, 12353, 12166, 11980, 11792, 11604, 11416, 11227, 61 | 11038, 10849, 10659, 10469, 10278, 10087, 9895, 9703, 62 | 9511, 9319, 9126, 8932, 8739, 8545, 8351, 8156, 63 | 7961, 7766, 7571, 7375, 7179, 6982, 6786, 6589, 64 | 6392, 6195, 5997, 5799, 5601, 5403, 5205, 5006, 65 | 4807, 4608, 4409, 4210, 4011, 3811, 3611, 3411, 66 | 3211, 3011, 2811, 2610, 2410, 2209, 2009, 1808, 67 | 1607, 1406, 1206, 1005, 804, 603, 402, 201, 68 | 0, -201, -402, -603, -804, -1005, -1206, -1406, 69 | -1607, -1808, -2009, -2209, -2410, -2610, -2811, -3011, 70 | -3211, -3411, -3611, -3811, -4011, -4210, -4409, -4608, 71 | -4807, -5006, -5205, -5403, -5601, -5799, -5997, -6195, 72 | -6392, -6589, -6786, -6982, -7179, -7375, -7571, -7766, 73 | -7961, -8156, -8351, -8545, -8739, -8932, -9126, -9319, 74 | -9511, -9703, -9895, -10087, -10278, -10469, -10659, -10849, 75 | -11038, -11227, -11416, -11604, -11792, -11980, -12166, -12353, 76 | -12539, -12724, -12909, -13094, -13278, -13462, -13645, -13827, 77 | -14009, -14191, -14372, -14552, -14732, -14911, -15090, -15268, 78 | -15446, -15623, -15799, -15975, -16150, -16325, -16499, -16672, 79 | -16845, -17017, -17189, -17360, -17530, -17699, -17868, -18036, 80 | -18204, -18371, -18537, -18702, -18867, -19031, -19194, -19357, 81 | -19519, -19680, -19840, -20000, -20159, -20317, -20474, -20631, 82 | -20787, -20942, -21096, -21249, -21402, -21554, -21705, -21855, 83 | -22004, -22153, -22301, -22448, -22594, -22739, -22883, -23027, 84 | -23169, -23311, -23452, -23592, -23731, -23869, -24006, -24143, 85 | -24278, -24413, -24546, -24679, -24811, -24942, -25072, -25201, 86 | -25329, -25456, -25582, -25707, -25831, -25954, -26077, -26198, 87 | -26318, -26437, -26556, -26673, -26789, -26905, -27019, -27132, 88 | -27244, -27355, -27466, -27575, -27683, -27790, -27896, -28001, 89 | -28105, -28208, -28309, -28410, -28510, -28608, -28706, -28802, 90 | -28897, -28992, -29085, -29177, -29268, -29358, -29446, -29534, 91 | -29621, -29706, -29790, -29873, -29955, -30036, -30116, -30195, 92 | -30272, -30349, -30424, -30498, -30571, -30643, -30713, -30783, 93 | -30851, -30918, -30984, -31049, -31113, -31175, -31236, -31297, 94 | -31356, -31413, -31470, -31525, -31580, -31633, -31684, -31735, 95 | -31785, -31833, -31880, -31926, -31970, -32014, -32056, -32097, 96 | -32137, -32176, -32213, -32249, -32284, -32318, -32350, -32382, 97 | -32412, -32441, -32468, -32495, -32520, -32544, -32567, -32588, 98 | -32609, -32628, -32646, -32662, -32678, -32692, -32705, -32717, 99 | -32727, -32736, -32744, -32751, -32757, -32761, -32764, -32766, 100 | }; 101 | 102 | const short Fixed15FFT::Window[Fixed15FFT::N_WAVE] = { 103 | 2621, 2621, 2622, 2623, 2625, 2628, 2631, 2635, 104 | 2639, 2644, 2649, 2655, 2662, 2669, 2676, 2685, 105 | 2693, 2703, 2713, 2723, 2734, 2746, 2758, 2771, 106 | 2784, 2798, 2812, 2827, 2843, 2859, 2876, 2893, 107 | 2910, 2929, 2948, 2967, 2987, 3008, 3029, 3050, 108 | 3073, 3095, 3119, 3142, 3167, 3192, 3217, 3243, 109 | 3270, 3297, 3325, 3353, 3382, 3411, 3441, 3471, 110 | 3502, 3533, 3565, 3598, 3631, 3664, 3698, 3733, 111 | 3768, 3804, 3840, 3877, 3914, 3952, 3990, 4029, 112 | 4068, 4108, 4148, 4189, 4230, 4272, 4314, 4357, 113 | 4401, 4444, 4489, 4534, 4579, 4625, 4671, 4718, 114 | 4765, 4813, 4861, 4910, 4959, 5009, 5059, 5110, 115 | 5161, 5213, 5265, 5317, 5370, 5424, 5478, 5532, 116 | 5587, 5642, 5698, 5754, 5811, 5868, 5926, 5984, 117 | 6042, 6101, 6160, 6220, 6280, 6341, 6402, 6464, 118 | 6525, 6588, 6651, 6714, 6777, 6841, 6906, 6970, 119 | 7036, 7101, 7167, 7234, 7300, 7368, 7435, 7503, 120 | 7571, 7640, 7709, 7779, 7849, 7919, 7989, 8060, 121 | 8132, 8203, 8275, 8348, 8420, 8493, 8567, 8641, 122 | 8715, 8789, 8864, 8939, 9015, 9090, 9167, 9243, 123 | 9320, 9397, 9474, 9552, 9630, 9708, 9787, 9866, 124 | 9945, 10024, 10104, 10184, 10264, 10345, 10426, 10507, 125 | 10588, 10670, 10752, 10834, 10917, 11000, 11082, 11166, 126 | 11249, 11333, 11417, 11501, 11586, 11670, 11755, 11840, 127 | 11926, 12011, 12097, 12183, 12269, 12355, 12442, 12529, 128 | 12616, 12703, 12790, 12878, 12966, 13054, 13142, 13230, 129 | 13318, 13407, 13496, 13585, 13674, 13763, 13852, 13942, 130 | 14031, 14121, 14211, 14301, 14391, 14482, 14572, 14662, 131 | 14753, 14844, 14935, 15026, 15117, 15208, 15299, 15391, 132 | 15482, 15574, 15665, 15757, 15849, 15940, 16032, 16124, 133 | 16216, 16308, 16400, 16493, 16585, 16677, 16769, 16862, 134 | 16954, 17046, 17139, 17231, 17324, 17416, 17509, 17601, 135 | 17694, 17786, 17879, 17971, 18064, 18156, 18248, 18341, 136 | 18433, 18526, 18618, 18710, 18803, 18895, 18987, 19079, 137 | 19171, 19263, 19355, 19447, 19539, 19631, 19722, 19814, 138 | 19905, 19997, 20088, 20179, 20271, 20362, 20453, 20543, 139 | 20634, 20725, 20815, 20906, 20996, 21086, 21176, 21266, 140 | 21356, 21446, 21535, 21625, 21714, 21803, 21892, 21981, 141 | 22069, 22158, 22246, 22334, 22422, 22509, 22597, 22684, 142 | 22772, 22859, 22945, 23032, 23118, 23204, 23290, 23376, 143 | 23462, 23547, 23632, 23717, 23802, 23886, 23970, 24054, 144 | 24138, 24222, 24305, 24388, 24471, 24553, 24635, 24717, 145 | 24799, 24880, 24962, 25042, 25123, 25203, 25283, 25363, 146 | 25443, 25522, 25601, 25679, 25758, 25836, 25913, 25991, 147 | 26068, 26144, 26221, 26297, 26373, 26448, 26523, 26598, 148 | 26673, 26747, 26820, 26894, 26967, 27040, 27112, 27184, 149 | 27256, 27327, 27398, 27469, 27539, 27609, 27678, 27747, 150 | 27816, 27884, 27952, 28020, 28087, 28154, 28220, 28286, 151 | 28352, 28417, 28482, 28546, 28610, 28674, 28737, 28800, 152 | 28862, 28924, 28985, 29046, 29107, 29167, 29227, 29286, 153 | 29345, 29404, 29462, 29519, 29576, 29633, 29689, 29745, 154 | 29800, 29855, 29910, 29964, 30017, 30070, 30123, 30175, 155 | 30226, 30277, 30328, 30378, 30428, 30477, 30526, 30574, 156 | 30622, 30669, 30716, 30763, 30808, 30854, 30899, 30943, 157 | 30987, 31030, 31073, 31115, 31157, 31198, 31239, 31280, 158 | 31319, 31359, 31397, 31436, 31473, 31511, 31547, 31583, 159 | 31619, 31654, 31689, 31723, 31757, 31790, 31822, 31854, 160 | 31885, 31916, 31947, 31976, 32006, 32034, 32063, 32090, 161 | 32117, 32144, 32170, 32196, 32221, 32245, 32269, 32292, 162 | 32315, 32337, 32359, 32380, 32400, 32420, 32440, 32459, 163 | 32477, 32495, 32512, 32529, 32545, 32560, 32575, 32590, 164 | 32603, 32617, 32629, 32642, 32653, 32664, 32675, 32685, 165 | 32694, 32703, 32711, 32719, 32726, 32732, 32738, 32744, 166 | 32748, 32753, 32756, 32759, 32762, 32764, 32765, 32766, 167 | 32767, 32766, 32765, 32764, 32762, 32759, 32756, 32753, 168 | 32748, 32744, 32738, 32732, 32726, 32719, 32711, 32703, 169 | 32694, 32685, 32675, 32664, 32653, 32642, 32629, 32617, 170 | 32603, 32590, 32575, 32560, 32545, 32529, 32512, 32495, 171 | 32477, 32459, 32440, 32420, 32400, 32380, 32359, 32337, 172 | 32315, 32292, 32269, 32245, 32221, 32196, 32170, 32144, 173 | 32117, 32090, 32063, 32034, 32006, 31976, 31947, 31916, 174 | 31885, 31854, 31822, 31790, 31757, 31723, 31689, 31654, 175 | 31619, 31583, 31547, 31511, 31473, 31436, 31397, 31359, 176 | 31319, 31280, 31239, 31198, 31157, 31115, 31073, 31030, 177 | 30987, 30943, 30899, 30854, 30808, 30763, 30716, 30669, 178 | 30622, 30574, 30526, 30477, 30428, 30378, 30328, 30277, 179 | 30226, 30175, 30123, 30070, 30017, 29964, 29910, 29855, 180 | 29800, 29745, 29689, 29633, 29576, 29519, 29462, 29404, 181 | 29345, 29286, 29227, 29167, 29107, 29046, 28985, 28924, 182 | 28862, 28800, 28737, 28674, 28610, 28546, 28482, 28417, 183 | 28352, 28286, 28220, 28154, 28087, 28020, 27952, 27884, 184 | 27816, 27747, 27678, 27609, 27539, 27469, 27398, 27327, 185 | 27256, 27184, 27112, 27040, 26967, 26894, 26820, 26747, 186 | 26673, 26598, 26523, 26448, 26373, 26297, 26221, 26144, 187 | 26068, 25991, 25913, 25836, 25758, 25679, 25601, 25522, 188 | 25443, 25363, 25283, 25203, 25123, 25042, 24962, 24880, 189 | 24799, 24717, 24635, 24553, 24471, 24388, 24305, 24222, 190 | 24138, 24054, 23970, 23886, 23802, 23717, 23632, 23547, 191 | 23462, 23376, 23290, 23204, 23118, 23032, 22945, 22859, 192 | 22772, 22684, 22597, 22509, 22422, 22334, 22246, 22158, 193 | 22069, 21981, 21892, 21803, 21714, 21625, 21535, 21446, 194 | 21356, 21266, 21176, 21086, 20996, 20906, 20815, 20725, 195 | 20634, 20543, 20453, 20362, 20271, 20179, 20088, 19997, 196 | 19905, 19814, 19722, 19631, 19539, 19447, 19355, 19263, 197 | 19171, 19079, 18987, 18895, 18803, 18710, 18618, 18526, 198 | 18433, 18341, 18248, 18156, 18064, 17971, 17879, 17786, 199 | 17694, 17601, 17509, 17416, 17324, 17231, 17139, 17046, 200 | 16954, 16862, 16769, 16677, 16585, 16493, 16400, 16308, 201 | 16216, 16124, 16032, 15940, 15849, 15757, 15665, 15574, 202 | 15482, 15391, 15299, 15208, 15117, 15026, 14935, 14844, 203 | 14753, 14662, 14572, 14482, 14391, 14301, 14211, 14121, 204 | 14031, 13942, 13852, 13763, 13674, 13585, 13496, 13407, 205 | 13318, 13230, 13142, 13054, 12966, 12878, 12790, 12703, 206 | 12616, 12529, 12442, 12355, 12269, 12183, 12097, 12011, 207 | 11926, 11840, 11755, 11670, 11586, 11501, 11417, 11333, 208 | 11249, 11166, 11082, 11000, 10917, 10834, 10752, 10670, 209 | 10588, 10507, 10426, 10345, 10264, 10184, 10104, 10024, 210 | 9945, 9866, 9787, 9708, 9630, 9552, 9474, 9397, 211 | 9320, 9243, 9167, 9090, 9015, 8939, 8864, 8789, 212 | 8715, 8641, 8567, 8493, 8420, 8348, 8275, 8203, 213 | 8132, 8060, 7989, 7919, 7849, 7779, 7709, 7640, 214 | 7571, 7503, 7435, 7368, 7300, 7234, 7167, 7101, 215 | 7036, 6970, 6906, 6841, 6777, 6714, 6651, 6588, 216 | 6525, 6464, 6402, 6341, 6280, 6220, 6160, 6101, 217 | 6042, 5984, 5926, 5868, 5811, 5754, 5698, 5642, 218 | 5587, 5532, 5478, 5424, 5370, 5317, 5265, 5213, 219 | 5161, 5110, 5059, 5009, 4959, 4910, 4861, 4813, 220 | 4765, 4718, 4671, 4625, 4579, 4534, 4489, 4444, 221 | 4401, 4357, 4314, 4272, 4230, 4189, 4148, 4108, 222 | 4068, 4029, 3990, 3952, 3914, 3877, 3840, 3804, 223 | 3768, 3733, 3698, 3664, 3631, 3598, 3565, 3533, 224 | 3502, 3471, 3441, 3411, 3382, 3353, 3325, 3297, 225 | 3270, 3243, 3217, 3192, 3167, 3142, 3119, 3095, 226 | 3073, 3050, 3029, 3008, 2987, 2967, 2948, 2929, 227 | 2910, 2893, 2876, 2859, 2843, 2827, 2812, 2798, 228 | 2784, 2771, 2758, 2746, 2734, 2723, 2713, 2703, 229 | 2693, 2685, 2676, 2669, 2662, 2655, 2649, 2644, 230 | 2639, 2635, 2631, 2628, 2625, 2623, 2622, 2621, 231 | }; 232 | -------------------------------------------------------------------------------- /fix_fft.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Fixed point FFT 3 | * 4 | * https://forum.arduino.cc/t/16-bit-fft-on-arduino/72061/19 5 | */ 6 | 7 | 8 | #include 9 | #include "fix.h" 10 | 11 | class Fixed15FFT { 12 | public: 13 | constexpr static int N_WAVE = 1024; 14 | constexpr static int LOG2_N_WAVE = 10; 15 | /* 16 | Since we only use 3/4 of N_WAVE, we define only 17 | this many samples, in order to conserve data space. 18 | */ 19 | constexpr static int SAMPLE_SIZE = N_WAVE-N_WAVE/4; 20 | 21 | private: 22 | static const short Sinewave[SAMPLE_SIZE]; 23 | static const short Window[N_WAVE]; 24 | 25 | public: 26 | 27 | template 28 | static void apply_window(T v[]) 29 | { 30 | static_assert(std::is_integral::value, "Parameters must be integral."); 31 | int i; 32 | constexpr int n = N_WAVE; 33 | for (i = 0; i < n; ++i) { 34 | v[i] = FIX_MPY(v[i], Window[i]); 35 | } 36 | } 37 | 38 | /* 39 | fix_fft() - perform forward/inverse fast Fourier transform. 40 | fr[n],fi[n] are real and imaginary arrays, both INPUT AND 41 | RESULT (in-place FFT), with 0 <= n < 2**m; set inverse to 42 | 0 for forward transform (FFT), or 1 for iFFT. 43 | */ 44 | template 45 | static void calc_fft(T fr[], T fi[]) 46 | { 47 | static_assert(std::is_integral::value, "Parameters must be integral."); 48 | int mr, i, j, l, k, m, istep; 49 | int qr, qi, tr, ti, wr, wi; 50 | 51 | constexpr int n = N_WAVE; 52 | constexpr int nn = n - 1; 53 | 54 | mr = 0; 55 | 56 | /* decimation in time - re-order data */ 57 | for (m=1; m<=nn; ++m) { 58 | l = n; 59 | do 60 | { 61 | l >>= 1; 62 | } while (mr+l > nn); 63 | mr = (mr & (l-1)) + l; 64 | if (mr <= m) 65 | continue; 66 | 67 | tr = fr[m]; 68 | fr[m] = fr[mr]; 69 | fr[mr] = tr; 70 | ti = fi[m]; 71 | fi[m] = fi[mr]; 72 | fi[mr] = ti; 73 | } 74 | 75 | l = 1; 76 | k = LOG2_N_WAVE-1; 77 | while (l < n) 78 | { 79 | /* 80 | it may not be obvious, but the shift will be 81 | performed on each data point exactly once, 82 | during this pass. 83 | */ 84 | istep = l << 1; 85 | for (m=0; m>= 1; 92 | wi >>= 1; 93 | for (i=m; i>15; 97 | //ti = ((long)wr*(long)fi[j] + (long)wi*(long)fr[j])>>15; 98 | tr = FIX_MPY(wr,fr[j]) - FIX_MPY(wi,fi[j]); 99 | ti = FIX_MPY(wr,fi[j]) + FIX_MPY(wi,fr[j]); 100 | qr = fr[i]; 101 | qr >>= 1; 102 | fr[i] = qr + tr; 103 | fr[j] = qr - tr; 104 | qi = fi[i]; 105 | qi >>= 1; 106 | fi[i] = qi + ti; 107 | fi[j] = qi - ti; 108 | } 109 | } 110 | --k; 111 | l = istep; 112 | } 113 | } 114 | 115 | }; 116 | -------------------------------------------------------------------------------- /m5Cardputer_audiospectrum.ino: -------------------------------------------------------------------------------- 1 | /*** 2 | M5Cardputer Audio Spectrum Display, Oscilloscope, and Tuner 3 | Portado por Aurélio Monteiro Avanzi 4 | 5 | ***/ 6 | 7 | /* 8 | M5StickC Audio Spectrum Display, Oscilloscope, and Tuner 9 | 10 | Copyright 2020 KIRA Ryouta 11 | 12 | Permission is hereby granted, free of charge, to any person obtaining a copy of 13 | this software and associated documentation files (the "Software"), to deal in 14 | the Software without restriction, including without limitation the rights to 15 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 16 | the Software, and to permit persons to whom the Software is furnished to do so, 17 | subject to the following conditions: 18 | 19 | The above copyright notice and this permission notice shall be included in all 20 | copies or substantial portions of the Software. 21 | 22 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 26 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 27 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 28 | SOFTWARE. 29 | 30 | */ 31 | 32 | /* ESP8266/32 Audio Spectrum Analyser on an SSD1306/SH1106 Display 33 | * The MIT License (MIT) Copyright (c) 2017 by David Bird. 34 | * The formulation and display of an AUdio Spectrum using an ESp8266 or ESP32 and SSD1306 or SH1106 OLED Display using a Fast Fourier Transform 35 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 36 | * (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 37 | * publish, distribute, but not to use it commercially for profit making or to sub-license and/or to sell copies of the Software or to 38 | * permit persons to whom the Software is furnished to do so, subject to the following conditions: 39 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 40 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 41 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 42 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 43 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 44 | * See more at http://dsbird.org.uk 45 | */ 46 | 47 | // M5StickC Audio Spectrum : 2019.06.01 : macsbug 48 | // https://macsbug.wordpress.com/2019/06/01/ 49 | // Audio Spectrum Display with M5STACK : 2017.12.31 : macsbug 50 | // https://macsbug.wordpress.com/2017/12/31/audio-spectrum-display-with-m5stack/ 51 | // https://github.com/tobozo/ESP32-8-Octave-Audio-Spectrum-Display/tree/wrover-kit 52 | // https://github.com/G6EJD/ESP32-8266-Audio-Spectrum-Display 53 | // https://github.com/kosme/arduinoFFT 54 | 55 | 56 | // David Bird (https://github.com/G6EJD/ESP32-8266-Audio-Spectrum-Display) 57 | // tobozo (https://github.com/tobozo/ESP32-Audio-Spectrum-Waveform-Display) 58 | // macsbug (https://macsbug.wordpress.com/) 59 | // KIRA Ryouta (https://github.com/KKQ-KKQ/m5stickc-audiospectrum) 60 | 61 | 62 | #pragma GCC optimize ("O3") 63 | #include 64 | #include 65 | #include 66 | #include 67 | #include 68 | #include "dywapitchtrack.h" 69 | #include "fix_fft.h" 70 | 71 | #define PIN_CLK 43 72 | #define PIN_DATA 46 73 | #define SAMPLES 1024 // Must be a power of 2 74 | #define READ_LEN (2 * SAMPLES) 75 | #define TFT_WIDTH 240 76 | #define TFT_HEIGHT 135 77 | #define BANDS 8 78 | #define BANDS_WIDTH ( TFT_WIDTH / BANDS ) 79 | #define BANDS_PADDING 8 80 | #define BAR_WIDTH ( BANDS_WIDTH - BANDS_PADDING ) 81 | #define NOISE_FLOOR 1 82 | #define AMPLIFIER (1 << 2) 83 | #define MAGNIFY 3 84 | #define RSHIFT 13 85 | #define RSHIFT2 1 86 | #define OSC_NOISEFLOOR 100 87 | #define OSC_SAMPLES DYWAPT_SAMPLESIZE 88 | #define OSC_SKIPCOUNT (OSC_SAMPLES/SAMPLES) 89 | #define OSC_EXTRASKIP 0 90 | 91 | //#define MAXBUFSIZE (2*SAMPLES) 92 | #define MAXBUFSIZE OSC_SAMPLES 93 | 94 | struct eqBand { 95 | const char *freqname; 96 | int peak; 97 | int lastpeak; 98 | uint16_t lastval; 99 | }; 100 | 101 | enum : uint8_t { 102 | ModeSpectrumBars, 103 | ModeOscilloscope, 104 | ModeTuner, 105 | ModeCount, 106 | }; 107 | 108 | static uint8_t runmode = 0; 109 | 110 | static volatile bool semaphore = false; 111 | static volatile bool needinit = true; 112 | 113 | static eqBand audiospectrum[BANDS] = { 114 | // freqname,peak,lastpeak,lastval, 115 | { ".1k", 0 }, 116 | { ".2k", 0 }, 117 | { ".5k", 0 }, 118 | { " 1k", 0 }, 119 | { " 2k", 0 }, 120 | { " 4k", 0 }, 121 | { " 8k", 0 }, 122 | { "16k", 0 } 123 | }; 124 | 125 | static int vTemp[2][MAXBUFSIZE]; 126 | static uint8_t curbuf = 0; 127 | static uint16_t colormap[TFT_HEIGHT];//color palette for the band meter(pre-fill in setup) 128 | 129 | static dywapitchtracker pitchTracker; 130 | static LGFX_Sprite sprite(&M5Cardputer.Display); 131 | 132 | static int bufposcount = 0; 133 | 134 | static const char *notestr[12] = { 135 | "C ", "C#", "D ", "D#", "E ", "F ", "F#", "G ", "G#", "A ", "A#", "B " 136 | }; 137 | 138 | static uint16_t oscbuf[2][OSC_SAMPLES]; 139 | #if OSC_EXTRASKIP > 0 140 | static int skipcount = 0; 141 | #endif 142 | 143 | void i2sInit(){ 144 | i2s_config_t i2s_config = { 145 | .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_PDM), 146 | .sample_rate = DYWAPT_SAMPLERATE, 147 | .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, //is fixed at 12bit,stereo,MSB 148 | .channel_format = I2S_CHANNEL_FMT_ALL_RIGHT, 149 | .communication_format = I2S_COMM_FORMAT_I2S, 150 | .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, 151 | .dma_buf_count = 2, 152 | .dma_buf_len = 128, 153 | }; 154 | i2s_pin_config_t pin_config; 155 | pin_config.bck_io_num = I2S_PIN_NO_CHANGE; 156 | pin_config.ws_io_num = PIN_CLK; 157 | pin_config.data_out_num = I2S_PIN_NO_CHANGE; 158 | pin_config.data_in_num = PIN_DATA; 159 | i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL); 160 | i2s_set_pin(I2S_NUM_0, &pin_config); 161 | i2s_set_clk(I2S_NUM_0, DYWAPT_SAMPLERATE, I2S_BITS_PER_SAMPLE_16BIT, I2S_CHANNEL_MONO); 162 | } 163 | 164 | void setup() { 165 | M5.begin(); 166 | setCpuFrequencyMhz(80); 167 | M5Cardputer.Display.setRotation(1); 168 | M5Cardputer.Display.fillScreen(BLACK); 169 | 170 | sprite.createSprite(TFT_WIDTH, TFT_HEIGHT); 171 | sprite.setTextSize(1); 172 | 173 | i2sInit(); 174 | //M5Cardputer.Speaker.tone(440, 50); 175 | 176 | for(uint8_t i=0;i g)? 255./r : 255./g; 182 | r *= mag; 183 | g *= mag; 184 | colormap[i] = M5Cardputer.Display.color565((uint8_t)r,(uint8_t)g,0); // Modified by KKQ-KKQ 185 | } 186 | 187 | int core = 1 - xPortGetCoreID(); 188 | xTaskCreatePinnedToCore(looptask,"calctask",32768,NULL,1,NULL,core); 189 | } 190 | 191 | void initMode() { 192 | M5Cardputer.Display.fillRect(0, 0, 193 | TFT_WIDTH, TFT_HEIGHT, BLACK); 194 | switch (runmode) { 195 | case ModeSpectrumBars: 196 | M5Cardputer.Display.setTextSize(1); 197 | M5Cardputer.Display.setTextColor(LIGHTGREY); 198 | for (byte band = 0; band < BANDS; band++) { 199 | M5Cardputer.Display.setCursor(BANDS_WIDTH*band + 2, 0); 200 | M5Cardputer.Display.print(audiospectrum[band].freqname); 201 | } 202 | break; 203 | 204 | case ModeOscilloscope: 205 | { 206 | sprite.setTextColor(GREEN); 207 | dywapitch_inittracking(&pitchTracker); 208 | } 209 | break; 210 | 211 | case ModeTuner: 212 | sprite.setTextSize(1); 213 | break; 214 | } 215 | } 216 | 217 | void showSpectrumBars(){ 218 | int *vTemp_ = vTemp[curbuf^1]; 219 | 220 | Fixed15FFT::apply_window(vTemp_); 221 | Fixed15FFT::calc_fft(vTemp_, vTemp_ + SAMPLES); 222 | 223 | int values[BANDS] = {}; 224 | for (int i = 2; i < (SAMPLES/2); i++){ 225 | // Don't use sample 0 and only first SAMPLES/2 are usable. 226 | // Each array element represents a frequency and its value the amplitude. 227 | int ampsq = vTemp_[i] * vTemp_[i] + vTemp_[i + SAMPLES] * vTemp_[i + SAMPLES]; 228 | if (ampsq > NOISE_FLOOR) { 229 | byte bandNum = getBand(i); 230 | if(bandNum != 8 && ampsq > values[bandNum]) { 231 | values[bandNum] = ampsq; 232 | } 233 | } 234 | } 235 | for (byte band = 0; band < BANDS; band++) { 236 | int log2 = FIX_LOG2<15-RSHIFT>(values[band]); 237 | if (log2 > -AMPLIFIER) { 238 | displayBand(band, ((log2 + AMPLIFIER) * MAGNIFY) >> RSHIFT2); 239 | } else { 240 | displayBand(band, 0); 241 | } 242 | } 243 | //long vnow = millis(); 244 | for (byte band = 0; band < BANDS; band++) { 245 | if (audiospectrum[band].peak > 0) { 246 | audiospectrum[band].peak -= 2; 247 | } 248 | if(audiospectrum[band].peak <= 0) { 249 | audiospectrum[band].peak = 0; 250 | } 251 | // only draw if peak changed 252 | if(audiospectrum[band].lastpeak != audiospectrum[band].peak) { 253 | // delete last peak 254 | uint16_t hpos = BANDS_WIDTH*band + (BANDS_PADDING/2); 255 | M5Cardputer.Display.drawFastHLine(hpos,TFT_HEIGHT-audiospectrum[band].lastpeak,BAR_WIDTH,BLACK); 256 | audiospectrum[band].lastpeak = audiospectrum[band].peak; 257 | uint16_t ypos = TFT_HEIGHT - audiospectrum[band].peak; 258 | M5Cardputer.Display.drawFastHLine(hpos, ypos, 259 | BAR_WIDTH, colormap[ypos]); 260 | } 261 | } 262 | } 263 | 264 | void displayBand(int band, int dsize){ 265 | uint16_t hpos = BANDS_WIDTH*band + (BANDS_PADDING/2); 266 | if (dsize < 0) dsize = 0; 267 | if(dsize>TFT_HEIGHT-10) { 268 | dsize = TFT_HEIGHT-10; // leave some hspace for text 269 | } 270 | if(dsize < audiospectrum[band].lastval) { 271 | // lower value, delete some lines 272 | M5Cardputer.Display.fillRect(hpos, TFT_HEIGHT-audiospectrum[band].lastval, 273 | BAR_WIDTH, audiospectrum[band].lastval - dsize,BLACK); 274 | } 275 | for (int s = 0; s <= dsize; s=s+4){ 276 | uint16_t ypos = TFT_HEIGHT - s; 277 | M5Cardputer.Display.drawFastHLine(hpos, ypos, BAR_WIDTH, colormap[ypos]); 278 | } 279 | if (dsize > audiospectrum[band].peak){audiospectrum[band].peak = dsize;} 280 | audiospectrum[band].lastval = dsize; 281 | } 282 | 283 | byte getBand(int i) { 284 | if (i >= 2 && i < 4 ) return 0; // 125Hz 285 | if (i >= 4 && i < 8 ) return 1; // 250Hz 286 | if (i >= 8 && i < 16 ) return 2; // 500Hz 287 | if (i >= 16 && i < 32 ) return 3; // 1000Hz 288 | if (i >= 32 && i < 64 ) return 4; // 2000Hz 289 | if (i >= 64 && i < 128) return 5; // 4000Hz 290 | if (i >= 128 && i < 256) return 6; // 8000Hz 291 | if (i >= 256 && i < 512) return 7; // 16000Hz 292 | return 8; 293 | } 294 | 295 | float calcNumSamples(float f) { 296 | if (f == 0.0) return SAMPLES/2; 297 | float s = (float)(DYWAPT_SAMPLERATE * 2) / f; 298 | if (s > (float)OSC_SAMPLES) { 299 | do { 300 | s *= 0.5; 301 | } while (s > (float)OSC_SAMPLES); 302 | } 303 | return s; 304 | } 305 | 306 | void showFreq(float freq) { 307 | if (freq > 0) { 308 | char strbuf[16]; 309 | sprintf(strbuf, "%8.2fHz", freq); 310 | sprite.drawString(strbuf, 0, 0, 1); 311 | float fnote = log2(freq)*12 - 36.376316562f; 312 | int note = fnote + 0.5f; 313 | if (note >= 0) { 314 | sprite.setCursor(TFT_WIDTH/2 - 4, 0); 315 | sprite.print(notestr[note % 12]); 316 | sprite.print(note / 12 - 1); 317 | float cent = (fnote - note) * 100; 318 | sprintf(strbuf, "%.1fcents", cent); 319 | sprite.drawRightString(strbuf, TFT_WIDTH, 0, 1); 320 | } 321 | } 322 | } 323 | 324 | void showOscilloscope() 325 | { 326 | uint16_t i,j; 327 | uint16_t *oscbuf_ = oscbuf[curbuf^1]; 328 | int *vTemp_ = vTemp[curbuf ^ 1]; 329 | float freq = dywapitch_computepitch(&pitchTracker, vTemp_); 330 | #if OSC_EXTRASKIP > 0 331 | if (skipcount < OSC_EXTRASKIP) { 332 | ++skipcount; 333 | return; 334 | } 335 | skipcount = 0; 336 | #endif 337 | uint16_t s = calcNumSamples(freq); 338 | float mx = (float)TFT_WIDTH / s; 339 | float my; 340 | uint16_t maxV = 0; 341 | uint16_t minV = 65535; 342 | uint16_t offset = 0; 343 | for (i = 0; i < s; ++i) { 344 | if (maxV < oscbuf_[i]) maxV = oscbuf_[i]; 345 | if (minV > oscbuf_[i]) { 346 | minV = oscbuf_[i]; 347 | if (i + s <= OSC_SAMPLES) offset = i; 348 | } 349 | } 350 | if (maxV - minV > OSC_NOISEFLOOR) { 351 | my = (float)(TFT_HEIGHT-10) / (maxV - minV); 352 | } 353 | else { 354 | my = (float)(TFT_HEIGHT-10) / OSC_NOISEFLOOR; 355 | minV = (((int)maxV + (int)minV) >> 1) - OSC_NOISEFLOOR/2; 356 | } 357 | sprite.fillSprite(BLACK); 358 | uint16_t y = TFT_HEIGHT - (oscbuf_[offset] - minV) * my; 359 | for (i = 1; i < s; ++i) { 360 | uint16_t y2 = TFT_HEIGHT - (oscbuf_[offset + i] - minV) * my; 361 | sprite.drawLine((uint16_t)((i-1) * mx), y, 362 | (uint16_t)(i * mx), y2, LIGHTGREY); 363 | y = y2; 364 | } 365 | showFreq(freq); 366 | sprite.pushSprite(0,0); 367 | } 368 | 369 | void showTuner() { 370 | int *vTemp_ = vTemp[curbuf ^ 1]; 371 | float freq = dywapitch_computepitch(&pitchTracker, vTemp_); 372 | float fnote; 373 | int note; 374 | if (freq > 0) { 375 | fnote = log2(freq)*12 - 36.376316562; 376 | note = fnote + 0.5; 377 | } 378 | else { 379 | note = -1; 380 | } 381 | uint32_t bgcolor, fgcolor; 382 | if (note >= 0) { 383 | float cent = (fnote - note) * 100; 384 | if (abs(cent) < 2.) { 385 | bgcolor = GREEN; 386 | fgcolor = BLACK; 387 | } 388 | else { 389 | bgcolor = DARKGREY; 390 | fgcolor = BLACK; 391 | } 392 | sprite.fillSprite(bgcolor); 393 | sprite.fillRect(0, 0, TFT_WIDTH, TFT_HEIGHT, bgcolor); 394 | sprite.setTextColor(fgcolor); 395 | sprite.drawRect(2, 36, TFT_WIDTH-3, TFT_HEIGHT-40, fgcolor); 396 | sprite.drawRect(TFT_WIDTH/2 + 1, 36, 1, TFT_HEIGHT - 40, fgcolor); 397 | sprite.fillCircle(((float)TFT_WIDTH/2 + 1) + cent * ((float)(TFT_WIDTH-3)/100), (TFT_HEIGHT+34)/2, 5, fgcolor); 398 | char strbuf[8]; 399 | sprintf(strbuf, "%s%d", notestr[note % 12], note / 12 - 1); 400 | sprite.drawCentreString(strbuf, TFT_WIDTH/2, 3, 4); 401 | } 402 | else { 403 | sprite.fillSprite(DARKGREY); 404 | sprite.drawRect(2, 36, TFT_WIDTH-3, TFT_HEIGHT-40, BLACK); 405 | sprite.drawLine(TFT_WIDTH/2 + 1, 36, TFT_WIDTH/2 + 1, TFT_HEIGHT - 4, BLACK); 406 | } 407 | sprite.pushSprite(0,0); 408 | } 409 | 410 | void looptask(void *) { 411 | while (1) { 412 | if (needinit) { 413 | initMode(); 414 | needinit = false; 415 | } 416 | if (semaphore) { 417 | switch(runmode) { 418 | case ModeSpectrumBars: 419 | showSpectrumBars(); 420 | break; 421 | 422 | case ModeOscilloscope: 423 | showOscilloscope(); 424 | break; 425 | 426 | case ModeTuner: 427 | showTuner(); 428 | break; 429 | } 430 | semaphore = false; 431 | } 432 | else { 433 | vTaskDelay(10); 434 | } 435 | } 436 | } 437 | 438 | void loop() { 439 | M5Cardputer.update(); 440 | if (!M5Cardputer.BtnA.isPressed()) { 441 | ++runmode; 442 | if (runmode >= ModeCount) runmode = 0; 443 | 444 | bufposcount = 0; 445 | needinit = true;; 446 | } 447 | uint16_t i,j; 448 | j = bufposcount * SAMPLES; 449 | uint16_t *adcBuffer = &oscbuf[curbuf][j]; 450 | size_t bytesread; 451 | i2s_read(I2S_NUM_0,(char*)adcBuffer,READ_LEN,&bytesread,portMAX_DELAY); 452 | int32_t dc = 0; 453 | for (int i = 0; i < SAMPLES; ++i) { 454 | dc += adcBuffer[i]; 455 | } 456 | dc /= SAMPLES; 457 | 458 | switch(runmode) { 459 | case ModeSpectrumBars: 460 | for (int i = 0; i < SAMPLES; ++i) { 461 | 462 | vTemp[curbuf][i] = (int)adcBuffer[i] - dc; 463 | vTemp[curbuf][i + SAMPLES] = 0; 464 | } 465 | curbuf ^= 1; 466 | semaphore = true; 467 | break; 468 | case ModeOscilloscope: 469 | for (i = 0; i < SAMPLES; ++i) { 470 | vTemp[curbuf][i + j] = (int)adcBuffer[i] - dc; 471 | } 472 | if (++bufposcount >= OSC_SKIPCOUNT) { 473 | bufposcount = 0; 474 | curbuf ^= 1; 475 | semaphore = true; 476 | } 477 | break; 478 | case ModeTuner: 479 | j = bufposcount * SAMPLES; 480 | for (i = 0; i < SAMPLES; ++i) { 481 | vTemp[curbuf][i + j] = (int)adcBuffer[i] - dc; 482 | } 483 | if (++bufposcount >= OSC_SKIPCOUNT) { 484 | bufposcount = 0; 485 | curbuf ^= 1; 486 | semaphore = true; 487 | } 488 | break; 489 | } 490 | } 491 | -------------------------------------------------------------------------------- /m5Cardputer_audiospectrum.ino.bin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cyberwisk/m5Cardputer_audiospectrum/7c36f3537cdbcc41d1e7598f4d877c05a54fce28/m5Cardputer_audiospectrum.ino.bin --------------------------------------------------------------------------------