From 64592562ee4e4374011edc596e28f41b94218b44 Mon Sep 17 00:00:00 2001 From: Karsten Schmidt Date: Fri, 28 Aug 2020 12:09:33 +0100 Subject: [PATCH] feat(colored-noise): import as new pkg (MBP2010) --- packages/colored-noise/LICENSE | 201 ++++++++ packages/colored-noise/README.md | 432 ++++++++++++++++++ packages/colored-noise/api-extractor.json | 3 + packages/colored-noise/package.json | 82 ++++ packages/colored-noise/src/blue.ts | 22 + packages/colored-noise/src/green.ts | 14 + packages/colored-noise/src/index.ts | 6 + packages/colored-noise/src/pink.ts | 31 ++ packages/colored-noise/src/red.ts | 20 + packages/colored-noise/src/utils.ts | 20 + packages/colored-noise/src/violet.ts | 13 + packages/colored-noise/src/white.ts | 14 + packages/colored-noise/test/index.ts | 6 + packages/colored-noise/test/tsconfig.json | 11 + packages/colored-noise/tools/hihat.ts | 22 + packages/colored-noise/tools/spectrum.ts | 45 ++ packages/colored-noise/tools/tsconfig.json | 10 + packages/colored-noise/tools/write-samples.ts | 37 ++ packages/colored-noise/tpl.readme.md | 406 ++++++++++++++++ packages/colored-noise/tsconfig.json | 11 + 20 files changed, 1406 insertions(+) create mode 100644 packages/colored-noise/LICENSE create mode 100644 packages/colored-noise/README.md create mode 100644 packages/colored-noise/api-extractor.json create mode 100644 packages/colored-noise/package.json create mode 100644 packages/colored-noise/src/blue.ts create mode 100644 packages/colored-noise/src/green.ts create mode 100644 packages/colored-noise/src/index.ts create mode 100644 packages/colored-noise/src/pink.ts create mode 100644 packages/colored-noise/src/red.ts create mode 100644 packages/colored-noise/src/utils.ts create mode 100644 packages/colored-noise/src/violet.ts create mode 100644 packages/colored-noise/src/white.ts create mode 100644 packages/colored-noise/test/index.ts create mode 100644 packages/colored-noise/test/tsconfig.json create mode 100644 packages/colored-noise/tools/hihat.ts create mode 100644 packages/colored-noise/tools/spectrum.ts create mode 100644 packages/colored-noise/tools/tsconfig.json create mode 100644 packages/colored-noise/tools/write-samples.ts create mode 100644 packages/colored-noise/tpl.readme.md create mode 100644 packages/colored-noise/tsconfig.json diff --git a/packages/colored-noise/LICENSE b/packages/colored-noise/LICENSE new file mode 100644 index 0000000000..8dada3edaf --- /dev/null +++ b/packages/colored-noise/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packages/colored-noise/README.md b/packages/colored-noise/README.md new file mode 100644 index 0000000000..ed2e37c314 --- /dev/null +++ b/packages/colored-noise/README.md @@ -0,0 +1,432 @@ + + +# ![colored-noise](https://media.thi.ng/umbrella/banners/thing-colored-noise.svg?66c81e0c) + +[![npm version](https://img.shields.io/npm/v/@thi.ng/colored-noise.svg)](https://www.npmjs.com/package/@thi.ng/colored-noise) +![npm downloads](https://img.shields.io/npm/dm/@thi.ng/colored-noise.svg) +[![Twitter Follow](https://img.shields.io/twitter/follow/thing_umbrella.svg?style=flat-square&label=twitter)](https://twitter.com/thing_umbrella) + +This project is part of the +[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo. + +- [About](#about) + - [Blue noise](#blue-noise) + - [Green noise](#green-noise) + - [Pink noise](#pink-noise) + - [Red / brown noise](#red---brown-noise) + - [Violet noise](#violet-noise) + - [White noise](#white-noise) + - [Status](#status) + - [Related packages](#related-packages) +- [Installation](#installation) +- [Dependencies](#dependencies) +- [API](#api) + - [Use as IGen in @thi.ng/dsp](#use-as-igen-in-thing-dsp) +- [Authors](#authors) +- [License](#license) + +## About + +Customizable O(1) ES6 generators for colored noise. + +[Colored noise](https://en.wikipedia.org/wiki/Colors_of_noise) is useful for a +wide variety of use cases where a more controlled approach to random number +generation is required, from DSP/synthesis to rendering, geometry generation, +animation etc. + +The following noise colors are available and illustrated via their power +spectrums / histograms (averaged 1000 runs @ 256 samples each). Each noise gen +is configurable in terms of number of internal RNG states, value range (default: +`[-1..1)` interval, always centered around 0) and the actual backing PRNG +implementation (default: `Math.random`, see +[@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random) +for alternatives). + +Code ported from Java, C & Clojure implementations and other +references/literature (see links in doc strings). Furthermore, all generators +have been refactored to be O(1). + +In alphabetical order: + +### Blue noise + +High-pass filtered noise (opposite of [red](#red-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/blue.ts) + +```text +blue2 + ▁▁▁▂▂▂▃▄▄▄▅▅▅▅▅▆▆▆▆▇▇▇▇▇▇▇█▇▇███▇ + ▁▁▂▃▄▄▅▆▇▆████████████████████████████████████ + ▂▃▄▅▇███████████████████████████████████████████████ + ▃▆▇█████████████████████████████████████████████████████ + ▁▅▇█████████████████████████████████████████████████████████ + ▂▆████████████████████████████████████████████████████████████ +▂▇██████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +blue4 + ▁▁▂▄▄▄▆▆▇█▇▆██▇ + ▁▃▄▇▇████████████████ + ▁▄▆██████████████████████ + ▁▄▅▆▇▇███████▇▇▆▄▃▁ ▄▇█████████████████████████ + ▃▅████████████████████▇▅ ▄████████████████████████████ + ▄██████████████████████████▄ ▁██████████████████████████████ + ▂██████████████████████████████▇███████████████████████████████ +▃███████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +blue16 + + ▃▅▇ + ▂████ + ▄█████ + ▂▅▅▃ ▂██████ + ▂▅▄▁ ▆████▅▁███████ + ▂ ▁▄▃ ▁▅▇▇▃ ▆████▂ ▅██████████████ + ▂▆█▆▂ ▂▆█▆▃ ▄███▅ ▆███▇ ▁█████▄ ▆██████▅███████████████ + ▃█████▄ ▃█████▄ ▅█████▆ ▇██████▁███████▇███████████████████████ + ███████▅███████▆███████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +blue32 + + ▆ + ██ + ▅██ + ▅▃ ███ + ▃ ▆██████ + ▂ ▇▃ ▆██▃███████ + ▂ ▅ ▂▇▃ ▅█▆ ███▄███████████ + ▃ ▃ ▄ ▄ ▄ ▁▇▂ ▂█▃ ▄█▄ ▆█▇ ███▁███▄███▇███████████████ + ▇██ ▇██ ███ ███ ███▁███▂███▄███▅███▇███████████████████████████ + ███████▇███████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +### Green noise + +Band-pass filtered noise (interleaved blue noise, opposite of +[violet](#violet-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/green.ts) + +```text +green4 + + ▂▃▅▆▇▇█▇▇▇▆▅▄▃▂ + ▃▅█████████████████▆▃ + ▄███████████████████████▄ ▁▁ + ▂▅▇█████▅▁ ▂███████████████████████████▁ ▂▅▇████▇▅▂ + ▁▆██████████▅ ▂█████████████████████████████▂ ▅██████████▆▁ + ▂██████████████▅███████████████████████████████▆██████████████▂ +▁███████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +green16 + + ▅▆▅ + ████▇ + ▅█████▆ + ▄▃ ███████ ▃▄ + ▄▁ ▆██████████▇██▆ ▁▄ + ▁ ▃ ▁▇▄ ▇██▄███████████████▄██▇ ▃█▁ ▄ ▁ + ▂▇▃ ▃█▄ ▅█▆ ▇██ ███▅███████████████████████▅███▁███ ▅█▅ ▄█▄ ▄▇▂ + ███▃███▄███▆███████████████████████████████████████▆███▄███▃███ +▅███████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +green32 + + ▆ + ███ + ███ + ▅▆███▆▄ + ▅ ███████ ▄ + ▂ ▇▂███████████▂▆ ▁ + ▁ ▄ ▇▁█▆███████████████▆█▂▇ ▄ ▂ + ▄ ▃ ▄ ▄ ▅ ▆ ▇▁█▃█▅█▇███████████████████████▇█▄█▃█▁▇ ▆ ▅ ▄ ▃ ▃ ▃ + █▅█▅█▅█▆█▇███████████████████████████████████████████▇█▇█▅█▅█▄█ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +### Pink noise + +Exponential decay power curve (1/f) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/pink.ts) + +```text +pink4 + ▅▄▂ +▇████▇▅▃▂ +████████████▇▆▄▃▁▁▁▁▂ +████████████████████████▇▆▅▄▃▂▂ ▁ +████████████████████████████████▇████████████▇▆▅▄▄▄▄▂▃▁▁ +█████████████████████████████████████████████████████████▇▆▅▄▁ +██████████████████████████████████████████████████████████████▇▃ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +pink16 +█ +█ +██ +███▆▁▁ +██████▇▅▁▂▁ +████████████▇▆▆▄▂▂▂▂▁▁ +███████████████████████▇▇▆▅▅▄▃▂ ▁ +████████████████████████████████▄███████████▇▆▅▄▃▃▄▃▂▂▁ +████████████████████████████████████████████████████████▆▇▆▅▂▁ +██████████████████████████████████████████████████████████████▆▁ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +pink32 +▄ +█ +█ +█▄ +██▄▁ +████▅▄▂ +████████▆▇▆▄▂▁ +████████████████▅▇▆▆▆▅▄▃▂▁▂ +██████████████████████████████▆▅ ▄▄▅▃▅▄▄▂▃▄▃▂▂ +████████████████████████████████████████████████▇▇▇▇▇▇▆▅▂▂▂ +████████████████████████████████████████████████████████████▅▄▁ +███████████████████████████████████████████████████████████████▅ +``` + +### Red / brown noise + +Low-pass filtered noise (opposite of [blue](#blue-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/red.ts) + +```text +red4 + + █▇▇▇▆▇▆▅▅▅▄▃▃▂▁ +█████████████████▇▆▅▃▁ +███████████████████████▆▃ +███████████████████████████▄ ▁▃▄▆▇█████████▇▆▅▃▁ +█████████████████████████████▄ ▁▄▇████████████████████▆▃ +██████████████████████████████▇▁ ▄██████████████████████████▄ +████████████████████████████████▇██████████████████████████████▁ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +red16 + + ▇▅▃ +█████▃ +██████▃ +███████▂ ▂▅▅▂ +████████ ▅████▅ ▄▄▂ +███████████████▅ ▂████▆ ▃▇█▆▁ ▃▃▂ ▁ +████████████████▅██████▇ ▄█████▁ ▁▇███▆ ▄███▄ ▃▇█▇▂ ▂▆█▆▂ +████████████████████████▇███████▂██████▆ ▆█████▅ ▄█████▃ ▃█████▃ +████████████████████████████████████████████████▆███████▅███████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +red32 + + ▅ +██▇ +███▅ +████ ▃▅ +████▇██▆ ▁▃ +████████▂██▇ ▂▇▁ ▁ +████████████▄███ ▆█▅ ▃▆▂ ▄ ▁ +████████████████████▄███▁███ ▇█▅ ▄█▄ ▃▇▂ ▁▆▁ ▁▅ ▄ ▄ ▃ ▃ +████████████████████████████▇███▄███▃███▁███ ███ ███ ███ ▇██ ▇█▇ +████████████████████████████████████████████████▇███▇███▇███▇███ +████████████████████████████████████████████████████████████████ +``` + +### Violet noise + +Band-stop filtered noise (interleaved red noise, opposite of +[green](#green-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/violet.ts) + +```text +violet2 + + ██▇▇█▆▇▆▆▅▅▃▃▃▂▁ ▁▂▃▄▄▅▆▆▇▆▆▇▇█▇█ +███████████████████▇▅▄▂ ▁▂▄▅▇▇█████████████████ +█████████████████████████▅▃ ▃▅████████████████████████ +████████████████████████████▄ ▄███████████████████████████ +██████████████████████████████▄ ▄█████████████████████████████ +████████████████████████████████▆███████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +violet4 + +▂▇▇▇▆▄▂▂ ▂▂▄▆▇▇▇ +█████████▆▃ ▃▆████████ +████████████▄ ▁ ▄███████████ +██████████████▁ ▂▅▇████▇▅▂ ▂▄▇████▇▅▁ ▁▇████████████ +███████████████▂ ▅██████████▆▁ ▂▇██████████▅ ▁██████████████ +████████████████▅██████████████▃ ▃██████████████▅███████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +violet16 + + ▅ ▅ +███ ██ +███▆ ▅██ +████ ▃▅ ▅▃ ███ +███████▆ ▁▅ ▃▂ ▆██████ +████████▃██▇ ▄▇▂ ▄ ▂ ▁ ▄ ▂█▄ ▇██▃███████ +████████████▅███▁██▇ ▆█▅ ▄█▄ ▂▇▃ ▃█▂ ▄█▃ ▅█▆ ▇██ ███▅███████████ +████████████████████▆███▅███▄███▃███▃███▄███▆███████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +violet32 + +██ █ +██ █ +██▄▄ ▄▆█ +████ ▃ ▃ ███ +██████▂▆ ▁ ▂ ▆▂█████ +████████▅█▂▇ ▄ ▂ ▂ ▄ ▇▁█▅███████ +████████████▇█▅█▂█▁▇ ▇ ▅ ▄ ▃ ▃ ▃ ▃ ▃ ▄ ▄ ▅ ▆ ▇▁█▃█▄█▇███████████ +██████████████████████▇█▇█▆█▆█▅█▅█▅█▅█▆█▆███████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +### White noise + +Uniform distribution (unfiltered output from source PRNG) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/blue.ts) + +```text +white + +▁█████████▇█████████▇█████▇▇▇█▇██▇▇█▇████████▇█▇███▇▇█▇█████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +### Status + +**STABLE** - used in production + +### Related packages + +- [@thi.ng/dsp](https://github.com/thi-ng/umbrella/tree/develop/packages/dsp) - Composable signal generators, oscillators, filters, FFT, spectrum, windowing & related DSP utils +- [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random) - Pseudo-random number generators w/ unified API + +## Installation + +```bash +yarn add @thi.ng/colored-noise +``` + +```html +// ES module + + +// UMD + +``` + +Package sizes (gzipped, pre-treeshake): ESM: 493 bytes / CJS: 566 bytes / UMD: 678 bytes + +## Dependencies + +- [@thi.ng/binary](https://github.com/thi-ng/umbrella/tree/develop/packages/binary) +- [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random) + +## API + +[Generated API docs](https://docs.thi.ng/umbrella/colored-noise/) + +TODO + +See +[/tools](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/tools/) +for usage examples... + +```ts +import { red } from "@thi.ng/colored-noise"; +import { take } from "@thi.ng/transducers"; + +// default config +[...take(20, red())] +// [0.03108920908982049, 0.5411110045940151, 0.26064470417452945, ...] + +// custom config +import { XsAdd } from "@thi.ng/random"; + +[...take(20, red(16, 1, new XsAdd(0xdecafbad)))] +// [ -0.17761799097704192, -0.10240132532836904, -0.1103387340810939, ...] +``` + +### Use as IGen in @thi.ng/dsp + +```ts +import { green } from "@thi.ng/colored-noise"; +import { adsr, iterable, product } from "@thi.ng/dsp"; +import { wavByteArray } from "@thi.ng/dsp-io-wav"; +import { writeFileSync } from "fs"; + +const FS = 44100; + +const signal = product( + // wrap green noise as IGen + iterable(green(16), 0), + // apply gain envelope + adsr({ a: 0.005 * FS, d: 0.2 * FS, s: 0 }) +); + +// output as 16bit mono WAV file +writeFileSync( + "export/hihat.wav", + wavByteArray( + { bits: 16, channels: 1, length: 0.25 * FS, sampleRate: FS }, + signal + ) +); +``` + +``` + +## Authors + +Karsten Schmidt + +## License + +© 2015 - 2020 Karsten Schmidt // Apache Software License 2.0 diff --git a/packages/colored-noise/api-extractor.json b/packages/colored-noise/api-extractor.json new file mode 100644 index 0000000000..94972e6bed --- /dev/null +++ b/packages/colored-noise/api-extractor.json @@ -0,0 +1,3 @@ +{ + "extends": "../../api-extractor.json" +} diff --git a/packages/colored-noise/package.json b/packages/colored-noise/package.json new file mode 100644 index 0000000000..ae301c9608 --- /dev/null +++ b/packages/colored-noise/package.json @@ -0,0 +1,82 @@ +{ + "name": "@thi.ng/colored-noise", + "version": "0.0.1", + "description": "Customizable O(1) ES6 generators for colored noise", + "module": "./index.js", + "main": "./lib/index.js", + "umd:main": "./lib/index.umd.js", + "typings": "./index.d.ts", + "repository": { + "type": "git", + "url": "https://github.com/thi-ng/umbrella.git" + }, + "homepage": "https://github.com/thi-ng/umbrella/tree/master/packages/colored-noise#readme", + "funding": { + "type": "patreon", + "url": "https://patreon.com/thing_umbrella" + }, + "author": "Karsten Schmidt ", + "license": "Apache-2.0", + "scripts": { + "build": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module", + "build:release": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module all", + "build:es6": "tsc --declaration", + "build:test": "rimraf build && tsc -p test/tsconfig.json", + "test": "mocha test", + "cover": "nyc mocha test && nyc report --reporter=lcov", + "clean": "rimraf *.js *.d.ts *.map .nyc_output build coverage doc lib", + "doc:readme": "ts-node -P ../../tools/tsconfig.json ../../tools/src/readme.ts", + "doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose", + "doc": "node_modules/.bin/typedoc --mode modules --out doc src", + "pub": "yarn build:release && yarn publish --access public" + }, + "devDependencies": { + "@istanbuljs/nyc-config-typescript": "^1.0.1", + "@microsoft/api-extractor": "^7.9.2", + "@thi.ng/api": "^6.12.2", + "@thi.ng/dsp": "^2.0.28", + "@thi.ng/dsp-io-wav": "^0.1.27", + "@thi.ng/text-canvas": "^0.2.28", + "@thi.ng/transducers": "^7.2.2", + "@thi.ng/vectors": "^4.6.3", + "@types/mocha": "^8.0.0", + "@types/node": "^14.0.26", + "mocha": "^8.1.1", + "nyc": "^15.1.0", + "ts-node": "^8.10.1", + "typedoc": "^0.18.0", + "typescript": "^3.9.7" + }, + "dependencies": { + "@thi.ng/binary": "^2.0.13", + "@thi.ng/random": "^1.4.17" + }, + "files": [ + "*.js", + "*.d.ts", + "lib" + ], + "keywords": [ + "blue", + "brown", + "es6", + "generator", + "green", + "noise", + "prng", + "red", + "typescript", + "white" + ], + "publishConfig": { + "access": "public" + }, + "sideEffects": false, + "thi.ng": { + "related": [ + "dsp", + "random" + ], + "year": 2015 + } +} diff --git a/packages/colored-noise/src/blue.ts b/packages/colored-noise/src/blue.ts new file mode 100644 index 0000000000..95034cea49 --- /dev/null +++ b/packages/colored-noise/src/blue.ts @@ -0,0 +1,22 @@ +import { INorm, SYSTEM } from "@thi.ng/random"; +import { preseed, sum } from "./utils"; + +/** + * High-pass filtered noise. Opposite of {@link red}. + * + * @param n + * @param scale + * @param rnd + */ +export function* blue(n = 2, scale = 1, rnd: INorm = SYSTEM) { + const state = preseed(n, scale, rnd); + state.forEach((x, i) => (state[i] = i & 1 ? x : -x)); + const invN = 1 / n; + let acc = sum(state); + for (let i = 0, sign = -1; true; ++i >= n && (i = 0)) { + acc -= state[i]; + acc += state[i] = sign * rnd.norm(scale); + sign ^= 0xfffffffe; + yield sign * acc * invN; + } +} diff --git a/packages/colored-noise/src/green.ts b/packages/colored-noise/src/green.ts new file mode 100644 index 0000000000..206da2ffc6 --- /dev/null +++ b/packages/colored-noise/src/green.ts @@ -0,0 +1,14 @@ +import { INorm, SYSTEM } from "@thi.ng/random"; +import { blue } from "./blue"; +import { interleave } from "./utils"; + +/** + * Band-pass filtered noise (interleaved blue noise). Opposite of + * {@link violet}. + * + * @param n + * @param scale + * @param rnd + */ +export const green = (n = 2, scale = 1, rnd: INorm = SYSTEM) => + interleave(blue(n, scale, rnd), blue(n, scale, rnd)); diff --git a/packages/colored-noise/src/index.ts b/packages/colored-noise/src/index.ts new file mode 100644 index 0000000000..e8ce77f93e --- /dev/null +++ b/packages/colored-noise/src/index.ts @@ -0,0 +1,6 @@ +export * from "./blue"; +export * from "./green"; +export * from "./pink"; +export * from "./red"; +export * from "./violet"; +export * from "./white"; diff --git a/packages/colored-noise/src/pink.ts b/packages/colored-noise/src/pink.ts new file mode 100644 index 0000000000..43a6fe7523 --- /dev/null +++ b/packages/colored-noise/src/pink.ts @@ -0,0 +1,31 @@ +import { ctz32 } from "@thi.ng/binary"; +import { INorm, SYSTEM } from "@thi.ng/random"; +import { preseed, sum } from "./utils"; + +/** + * Exponential decay (1/f) noise, based on Voss-McCarthy algorithm. + * + * @remarks + * The number of internal states should be in the [4..32] range (default: 8). + * Due to JS integer limitations, `n` > 32 are meaningless. + * + * References: + * + * - https://www.dsprelated.com/showarticle/908.php + * - https://www.firstpr.com.au/dsp/pink-noise/#Voss-McCartney + * + * @param n + * @param scale + * @param rnd + */ +export function* pink(n = 8, scale = 1, rnd: INorm = SYSTEM) { + const state = preseed(n, scale, rnd); + const invN = 1 / n; + let acc = sum(state); + for (let i = 0; true; i = (i + 1) >>> 0) { + const id = ctz32(i) % n; + acc -= state[id]; + acc += state[id] = rnd.norm(scale); + yield acc * invN; + } +} diff --git a/packages/colored-noise/src/red.ts b/packages/colored-noise/src/red.ts new file mode 100644 index 0000000000..c42c02a90b --- /dev/null +++ b/packages/colored-noise/src/red.ts @@ -0,0 +1,20 @@ +import { INorm, SYSTEM } from "@thi.ng/random"; +import { preseed, sum } from "./utils"; + +/** + * Low-pass filtered noise (same as brown noise). Opposite of {@link blue}. + * + * @param n + * @param scale + * @param rnd + */ +export function* red(n = 2, scale = 1, rnd: INorm = SYSTEM) { + const state = preseed(n, scale, rnd); + const invN = 1 / n; + let acc = sum(state); + for (let i = 0; true; ++i >= n && (i = 0)) { + acc -= state[i]; + acc += state[i] = rnd.norm(scale); + yield acc * invN; + } +} diff --git a/packages/colored-noise/src/utils.ts b/packages/colored-noise/src/utils.ts new file mode 100644 index 0000000000..556bba7f04 --- /dev/null +++ b/packages/colored-noise/src/utils.ts @@ -0,0 +1,20 @@ +import { INorm } from "@thi.ng/random"; + +export const preseed = (n: number, scale: number, rnd: INorm) => { + const state = new Array(n); + for (let i = 0; i < n; i++) { + state[i] = rnd.norm(scale); + } + return state; +}; + +export const sum = (src: number[]) => src.reduce((sum, x) => sum + x, 0); + +export function* interleave(a: Iterable, b: Iterable) { + const src = [a[Symbol.iterator](), b[Symbol.iterator]()]; + for (let i = 0; true; i ^= 1) { + const next = src[i].next(); + if (next.done) return; + yield next.value!; + } +} diff --git a/packages/colored-noise/src/violet.ts b/packages/colored-noise/src/violet.ts new file mode 100644 index 0000000000..f7a77d6849 --- /dev/null +++ b/packages/colored-noise/src/violet.ts @@ -0,0 +1,13 @@ +import { INorm, SYSTEM } from "@thi.ng/random"; +import { red } from "./red"; +import { interleave } from "./utils"; + +/** + * Band-stop filtered noise (interleaved red noise). Opposite of {@link green}. + * + * @param n + * @param scale + * @param rnd + */ +export const violet = (n = 2, scale = 1, rnd: INorm = SYSTEM) => + interleave(red(n, scale, rnd), red(n, scale, rnd)); diff --git a/packages/colored-noise/src/white.ts b/packages/colored-noise/src/white.ts new file mode 100644 index 0000000000..089b0be4a4 --- /dev/null +++ b/packages/colored-noise/src/white.ts @@ -0,0 +1,14 @@ +import { INorm, SYSTEM } from "@thi.ng/random"; + +/** + * Unfiltered noise w/ uniform distribution. Merely yields samples from + * given PRNG. + * + * @param scale + * @param rnd + */ +export function* white(scale = 1, rnd: INorm = SYSTEM) { + while (true) { + yield rnd.norm(scale); + } +} diff --git a/packages/colored-noise/test/index.ts b/packages/colored-noise/test/index.ts new file mode 100644 index 0000000000..868b141fe4 --- /dev/null +++ b/packages/colored-noise/test/index.ts @@ -0,0 +1,6 @@ +// import * as assert from "assert"; +// import { } from "../src"; + +describe("colored-noise", () => { + it("tests pending"); +}); diff --git a/packages/colored-noise/test/tsconfig.json b/packages/colored-noise/test/tsconfig.json new file mode 100644 index 0000000000..f6e63560dd --- /dev/null +++ b/packages/colored-noise/test/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "outDir": "../build", + "module": "commonjs" + }, + "include": [ + "./**/*.ts", + "../src/**/*.ts" + ] +} diff --git a/packages/colored-noise/tools/hihat.ts b/packages/colored-noise/tools/hihat.ts new file mode 100644 index 0000000000..bb01ce39c7 --- /dev/null +++ b/packages/colored-noise/tools/hihat.ts @@ -0,0 +1,22 @@ +import { green } from "@thi.ng/colored-noise"; +import { adsr, iterable, product } from "@thi.ng/dsp"; +import { wavByteArray } from "@thi.ng/dsp-io-wav"; +import { writeFileSync } from "fs"; + +const FS = 44100; + +const signal = product( + // wrap green noise as IGen + iterable(green(16), 0), + // apply gain envelope + adsr({ a: 0.005 * FS, d: 0.2 * FS, s: 0 }) +); + +// output as WAV file +writeFileSync( + "export/hihat.wav", + wavByteArray( + { bits: 16, channels: 1, length: 0.2 * FS, sampleRate: FS }, + signal + ) +); diff --git a/packages/colored-noise/tools/spectrum.ts b/packages/colored-noise/tools/spectrum.ts new file mode 100644 index 0000000000..7248c174d8 --- /dev/null +++ b/packages/colored-noise/tools/spectrum.ts @@ -0,0 +1,45 @@ +import { Fn, Fn0, NumericArray } from "@thi.ng/api"; +import { fft, spectrumPow } from "@thi.ng/dsp"; +import { barChartHStr } from "@thi.ng/text-canvas"; +import { map, range, reducer, take, transduce } from "@thi.ng/transducers"; +import { add, divN, Vec, zeroes } from "@thi.ng/vectors"; +import { blue, green, pink, red, violet } from "../src"; + +const computeSpectrum = (src: Fn0>, size = 128, num = 1000) => + divN( + null, + transduce( + map(() => spectrumPow(fft([...take(size, src())]), true)), + reducer( + () => zeroes(size / 2), + (acc, x) => add(null, acc, x) + ), + range(num) + ), + num + ); + +const spectrumString = (spec: Vec) => barChartHStr(12, spec, -72, -24); + +const printSpectrum = (id: string, fn: Fn0>) => { + console.log(id); + console.log(spectrumString(computeSpectrum(fn))); +}; + +const printBatch = ( + id: string, + fn: Fn>, + steps = [2, 4, 8, 16, 32] +) => { + for (let n of steps) { + printSpectrum(`${id}${n}`, () => fn(n)); + } +}; + +printBatch("blue", blue); +printBatch("green", green); +printBatch("pink", pink); +printBatch("red", red); +printBatch("violet", violet); + +// printSpectrum("white", () => white()); diff --git a/packages/colored-noise/tools/tsconfig.json b/packages/colored-noise/tools/tsconfig.json new file mode 100644 index 0000000000..9655cbea10 --- /dev/null +++ b/packages/colored-noise/tools/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "outDir": "../build", + "module": "commonjs", + "noUnusedLocals": false, + "noUnusedParameters": false + }, + "include": ["./**/*.ts", "../src/**/*.ts"] +} diff --git a/packages/colored-noise/tools/write-samples.ts b/packages/colored-noise/tools/write-samples.ts new file mode 100644 index 0000000000..38a793cdf8 --- /dev/null +++ b/packages/colored-noise/tools/write-samples.ts @@ -0,0 +1,37 @@ +import { wavByteArray } from "@thi.ng/dsp-io-wav"; +import { writeFileSync } from "fs"; +import { blue, green, pink, red, violet, white } from "../src"; +import { Fn } from "@thi.ng/api"; + +const FS = 44100; +const LENGTH = 5 * FS; + +const write = (id: string, src: Iterable) => { + const path = `export/${id}.wav`; + console.log(`writing: ${path}...`); + writeFileSync( + path, + wavByteArray( + { bits: 16, channels: 1, length: LENGTH, sampleRate: FS }, + src + ) + ); +}; + +const writeBatch = ( + id: string, + fn: Fn>, + steps = [2, 4, 8, 16, 32] +) => { + for (let n of steps) { + write(`${id}-${n}`, fn(n)); + } +}; + +writeBatch("blue", blue); +writeBatch("green", green); +writeBatch("pink", pink); +writeBatch("red", red); +writeBatch("violet", violet); + +write("white", white()); diff --git a/packages/colored-noise/tpl.readme.md b/packages/colored-noise/tpl.readme.md new file mode 100644 index 0000000000..9beb05656c --- /dev/null +++ b/packages/colored-noise/tpl.readme.md @@ -0,0 +1,406 @@ +# ${pkg.banner} + +[![npm version](https://img.shields.io/npm/v/${pkg.name}.svg)](https://www.npmjs.com/package/${pkg.name}) +![npm downloads](https://img.shields.io/npm/dm/${pkg.name}.svg) +[![Twitter Follow](https://img.shields.io/twitter/follow/thing_umbrella.svg?style=flat-square&label=twitter)](https://twitter.com/thing_umbrella) + +This project is part of the +[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo. + + + +## About + +${pkg.description} + +[Colored noise](https://en.wikipedia.org/wiki/Colors_of_noise) is useful for a +wide variety of use cases where a more controlled approach to random number +generation is required, from DSP/synthesis to rendering, geometry generation, +animation etc. + +The following noise colors are available and illustrated via their power +spectrums / histograms (averaged 1000 runs @ 256 samples each). Each noise gen +is configurable in terms of number of internal RNG states, value range (default: +`[-1..1)` interval, always centered around 0) and the actual backing PRNG +implementation (default: `Math.random`, see +[@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random) +for alternatives). + +Code ported from Java, C & Clojure implementations and other +references/literature (see links in doc strings). Furthermore, all generators +have been refactored to be O(1). + +In alphabetical order: + +### Blue noise + +High-pass filtered noise (opposite of [red](#red-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/blue.ts) + +```text +blue2 + ▁▁▁▂▂▂▃▄▄▄▅▅▅▅▅▆▆▆▆▇▇▇▇▇▇▇█▇▇███▇ + ▁▁▂▃▄▄▅▆▇▆████████████████████████████████████ + ▂▃▄▅▇███████████████████████████████████████████████ + ▃▆▇█████████████████████████████████████████████████████ + ▁▅▇█████████████████████████████████████████████████████████ + ▂▆████████████████████████████████████████████████████████████ +▂▇██████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +blue4 + ▁▁▂▄▄▄▆▆▇█▇▆██▇ + ▁▃▄▇▇████████████████ + ▁▄▆██████████████████████ + ▁▄▅▆▇▇███████▇▇▆▄▃▁ ▄▇█████████████████████████ + ▃▅████████████████████▇▅ ▄████████████████████████████ + ▄██████████████████████████▄ ▁██████████████████████████████ + ▂██████████████████████████████▇███████████████████████████████ +▃███████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +blue16 + + ▃▅▇ + ▂████ + ▄█████ + ▂▅▅▃ ▂██████ + ▂▅▄▁ ▆████▅▁███████ + ▂ ▁▄▃ ▁▅▇▇▃ ▆████▂ ▅██████████████ + ▂▆█▆▂ ▂▆█▆▃ ▄███▅ ▆███▇ ▁█████▄ ▆██████▅███████████████ + ▃█████▄ ▃█████▄ ▅█████▆ ▇██████▁███████▇███████████████████████ + ███████▅███████▆███████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +blue32 + + ▆ + ██ + ▅██ + ▅▃ ███ + ▃ ▆██████ + ▂ ▇▃ ▆██▃███████ + ▂ ▅ ▂▇▃ ▅█▆ ███▄███████████ + ▃ ▃ ▄ ▄ ▄ ▁▇▂ ▂█▃ ▄█▄ ▆█▇ ███▁███▄███▇███████████████ + ▇██ ▇██ ███ ███ ███▁███▂███▄███▅███▇███████████████████████████ + ███████▇███████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +### Green noise + +Band-pass filtered noise (interleaved blue noise, opposite of +[violet](#violet-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/green.ts) + +```text +green4 + + ▂▃▅▆▇▇█▇▇▇▆▅▄▃▂ + ▃▅█████████████████▆▃ + ▄███████████████████████▄ ▁▁ + ▂▅▇█████▅▁ ▂███████████████████████████▁ ▂▅▇████▇▅▂ + ▁▆██████████▅ ▂█████████████████████████████▂ ▅██████████▆▁ + ▂██████████████▅███████████████████████████████▆██████████████▂ +▁███████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +green16 + + ▅▆▅ + ████▇ + ▅█████▆ + ▄▃ ███████ ▃▄ + ▄▁ ▆██████████▇██▆ ▁▄ + ▁ ▃ ▁▇▄ ▇██▄███████████████▄██▇ ▃█▁ ▄ ▁ + ▂▇▃ ▃█▄ ▅█▆ ▇██ ███▅███████████████████████▅███▁███ ▅█▅ ▄█▄ ▄▇▂ + ███▃███▄███▆███████████████████████████████████████▆███▄███▃███ +▅███████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +green32 + + ▆ + ███ + ███ + ▅▆███▆▄ + ▅ ███████ ▄ + ▂ ▇▂███████████▂▆ ▁ + ▁ ▄ ▇▁█▆███████████████▆█▂▇ ▄ ▂ + ▄ ▃ ▄ ▄ ▅ ▆ ▇▁█▃█▅█▇███████████████████████▇█▄█▃█▁▇ ▆ ▅ ▄ ▃ ▃ ▃ + █▅█▅█▅█▆█▇███████████████████████████████████████████▇█▇█▅█▅█▄█ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +### Pink noise + +Exponential decay power curve (1/f) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/pink.ts) + +```text +pink4 + ▅▄▂ +▇████▇▅▃▂ +████████████▇▆▄▃▁▁▁▁▂ +████████████████████████▇▆▅▄▃▂▂ ▁ +████████████████████████████████▇████████████▇▆▅▄▄▄▄▂▃▁▁ +█████████████████████████████████████████████████████████▇▆▅▄▁ +██████████████████████████████████████████████████████████████▇▃ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +pink16 +█ +█ +██ +███▆▁▁ +██████▇▅▁▂▁ +████████████▇▆▆▄▂▂▂▂▁▁ +███████████████████████▇▇▆▅▅▄▃▂ ▁ +████████████████████████████████▄███████████▇▆▅▄▃▃▄▃▂▂▁ +████████████████████████████████████████████████████████▆▇▆▅▂▁ +██████████████████████████████████████████████████████████████▆▁ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +pink32 +▄ +█ +█ +█▄ +██▄▁ +████▅▄▂ +████████▆▇▆▄▂▁ +████████████████▅▇▆▆▆▅▄▃▂▁▂ +██████████████████████████████▆▅ ▄▄▅▃▅▄▄▂▃▄▃▂▂ +████████████████████████████████████████████████▇▇▇▇▇▇▆▅▂▂▂ +████████████████████████████████████████████████████████████▅▄▁ +███████████████████████████████████████████████████████████████▅ +``` + +### Red / brown noise + +Low-pass filtered noise (opposite of [blue](#blue-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/red.ts) + +```text +red4 + + █▇▇▇▆▇▆▅▅▅▄▃▃▂▁ +█████████████████▇▆▅▃▁ +███████████████████████▆▃ +███████████████████████████▄ ▁▃▄▆▇█████████▇▆▅▃▁ +█████████████████████████████▄ ▁▄▇████████████████████▆▃ +██████████████████████████████▇▁ ▄██████████████████████████▄ +████████████████████████████████▇██████████████████████████████▁ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +red16 + + ▇▅▃ +█████▃ +██████▃ +███████▂ ▂▅▅▂ +████████ ▅████▅ ▄▄▂ +███████████████▅ ▂████▆ ▃▇█▆▁ ▃▃▂ ▁ +████████████████▅██████▇ ▄█████▁ ▁▇███▆ ▄███▄ ▃▇█▇▂ ▂▆█▆▂ +████████████████████████▇███████▂██████▆ ▆█████▅ ▄█████▃ ▃█████▃ +████████████████████████████████████████████████▆███████▅███████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +red32 + + ▅ +██▇ +███▅ +████ ▃▅ +████▇██▆ ▁▃ +████████▂██▇ ▂▇▁ ▁ +████████████▄███ ▆█▅ ▃▆▂ ▄ ▁ +████████████████████▄███▁███ ▇█▅ ▄█▄ ▃▇▂ ▁▆▁ ▁▅ ▄ ▄ ▃ ▃ +████████████████████████████▇███▄███▃███▁███ ███ ███ ███ ▇██ ▇█▇ +████████████████████████████████████████████████▇███▇███▇███▇███ +████████████████████████████████████████████████████████████████ +``` + +### Violet noise + +Band-stop filtered noise (interleaved red noise, opposite of +[green](#green-noise)) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/violet.ts) + +```text +violet2 + + ██▇▇█▆▇▆▆▅▅▃▃▃▂▁ ▁▂▃▄▄▅▆▆▇▆▆▇▇█▇█ +███████████████████▇▅▄▂ ▁▂▄▅▇▇█████████████████ +█████████████████████████▅▃ ▃▅████████████████████████ +████████████████████████████▄ ▄███████████████████████████ +██████████████████████████████▄ ▄█████████████████████████████ +████████████████████████████████▆███████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +violet4 + +▂▇▇▇▆▄▂▂ ▂▂▄▆▇▇▇ +█████████▆▃ ▃▆████████ +████████████▄ ▁ ▄███████████ +██████████████▁ ▂▅▇████▇▅▂ ▂▄▇████▇▅▁ ▁▇████████████ +███████████████▂ ▅██████████▆▁ ▂▇██████████▅ ▁██████████████ +████████████████▅██████████████▃ ▃██████████████▅███████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +violet16 + + ▅ ▅ +███ ██ +███▆ ▅██ +████ ▃▅ ▅▃ ███ +███████▆ ▁▅ ▃▂ ▆██████ +████████▃██▇ ▄▇▂ ▄ ▂ ▁ ▄ ▂█▄ ▇██▃███████ +████████████▅███▁██▇ ▆█▅ ▄█▄ ▂▇▃ ▃█▂ ▄█▃ ▅█▆ ▇██ ███▅███████████ +████████████████████▆███▅███▄███▃███▃███▄███▆███████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ + +violet32 + +██ █ +██ █ +██▄▄ ▄▆█ +████ ▃ ▃ ███ +██████▂▆ ▁ ▂ ▆▂█████ +████████▅█▂▇ ▄ ▂ ▂ ▄ ▇▁█▅███████ +████████████▇█▅█▂█▁▇ ▇ ▅ ▄ ▃ ▃ ▃ ▃ ▃ ▄ ▄ ▅ ▆ ▇▁█▃█▄█▇███████████ +██████████████████████▇█▇█▆█▆█▅█▅█▅█▅█▆█▆███████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +### White noise + +Uniform distribution (unfiltered output from source PRNG) - +[Source](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/src/blue.ts) + +```text +white + +▁█████████▇█████████▇█████▇▇▇█▇██▇▇█▇████████▇█▇███▇▇█▇█████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +████████████████████████████████████████████████████████████████ +``` + +${status} + +${supportPackages} + +${relatedPackages} + +${blogPosts} + +## Installation + +${pkg.install} + +${pkg.size} + +## Dependencies + +${pkg.deps} + +${examples} + +## API + +${docLink} + +TODO + +See +[/tools](https://github.com/thi-ng/umbrella/blob/develop/packages/colored-noise/tools/) +for usage examples... + +```ts +import { red } from "@thi.ng/colored-noise"; +import { take } from "@thi.ng/transducers"; + +// default config +[...take(20, red())] +// [0.03108920908982049, 0.5411110045940151, 0.26064470417452945, ...] + +// custom config +import { XsAdd } from "@thi.ng/random"; + +[...take(20, red(16, 1, new XsAdd(0xdecafbad)))] +// [ -0.17761799097704192, -0.10240132532836904, -0.1103387340810939, ...] +``` + +### Use as IGen in @thi.ng/dsp + +```ts +import { green } from "@thi.ng/colored-noise"; +import { adsr, iterable, product } from "@thi.ng/dsp"; +import { wavByteArray } from "@thi.ng/dsp-io-wav"; +import { writeFileSync } from "fs"; + +const FS = 44100; + +const signal = product( + // wrap green noise as IGen + iterable(green(16), 0), + // apply gain envelope + adsr({ a: 0.005 * FS, d: 0.2 * FS, s: 0 }) +); + +// output as 16bit mono WAV file +writeFileSync( + "export/hihat.wav", + wavByteArray( + { bits: 16, channels: 1, length: 0.25 * FS, sampleRate: FS }, + signal + ) +); +``` + +``` + +## Authors + +${authors} + +## License + +© ${copyright} // ${license} diff --git a/packages/colored-noise/tsconfig.json b/packages/colored-noise/tsconfig.json new file mode 100644 index 0000000000..893b9979c5 --- /dev/null +++ b/packages/colored-noise/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": ".", + "module": "es6", + "target": "es6" + }, + "include": [ + "./src/**/*.ts" + ] +}