-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from 5mdt/feature/fix-noise-loop
[NoiseProcessor]: rewrote noise processor
- Loading branch information
Showing
4 changed files
with
300 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# The MIT License (MIT) | ||
|
||
A short, permissive software license. Basically, you can do whatever you want as long as you include the original copyright and license notice in any copy of the software/source. | ||
|
||
## Can | ||
|
||
- Commercial Use | ||
- Modify | ||
- Distribute | ||
- Sublicense | ||
- Private Use | ||
|
||
## Cannot | ||
|
||
- Hold lible | ||
|
||
## Must | ||
|
||
- Include Copyright | ||
- Include License | ||
|
||
--- | ||
|
||
**Disclaimer:** This TL;DR summary is provided for convenience and informational | ||
purposes only. It is not a substitute for the | ||
[full text of the Apache License 2.0](LICENSE). The full license should be | ||
consulted for a comprehensive understanding of your rights and obligations. | ||
In the event of any discrepancies or legal questions, the full license text | ||
takes precedence. | ||
|
||
TL;DR for this licence was taken from | ||
[TLDRLegal](https://www.tldrlegal.com/license/apache-license-2-0-apache-2-0) | ||
|
||
|
||
--- | ||
|
||
**Disclaimer:** This TL;DR summary is provided for convenience and informational | ||
purposes only. It is not a substitute for the | ||
[full text of the License](LICENSE). The full license should be | ||
consulted for a comprehensive understanding of your rights and obligations. | ||
In the event of any discrepancies or legal questions, the full license text | ||
takes precedence. | ||
|
||
TL;DR for this licence was taken from | ||
[TLDRLegal](https://www.tldrlegal.com/license/mit-license) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const tempo = 30; | ||
const types = { | ||
pink: "pink", | ||
brown: "brown", | ||
white: "white", | ||
} | ||
|
||
function generateBrownNoise(buffer) { | ||
let lastOut = 0.0; | ||
|
||
for (let i = 0; i < buffer.length; i++) { | ||
const whiteNoise = (Math.random() * 2 - 1) / tempo; | ||
buffer[i] = (lastOut + (0.02 * whiteNoise)) / 1.02; | ||
lastOut = buffer[i]; | ||
buffer[i] *= 3.5; // Amplify the sound | ||
} | ||
} | ||
|
||
// Function to generate Pink noise | ||
function generatePinkNoise(buffer) { | ||
let b0 = 0; | ||
let b1 = 0; | ||
let b2 = 0; | ||
let b3 = 0; | ||
let b4 = 0; | ||
let b5 = 0; | ||
let b6 = 0; | ||
|
||
for (let i = 0; i < buffer.length; i++) { | ||
const whiteNoise = (Math.random() * 2 - 1) / tempo; | ||
b0 = 0.99886 * b0 + whiteNoise * 0.0555179; | ||
b1 = 0.99332 * b1 + whiteNoise * 0.0750759; | ||
b2 = 0.96900 * b2 + whiteNoise * 0.1538520; | ||
b3 = 0.86650 * b3 + whiteNoise * 0.3104856; | ||
b4 = 0.55000 * b4 + whiteNoise * 0.5329522; | ||
b5 = -0.7616 * b5 - whiteNoise * 0.0168980; | ||
buffer[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + whiteNoise * 0.5362; | ||
buffer[i] *= 0.11; // Amplify the sound | ||
b6 = whiteNoise * 0.115926; | ||
} | ||
} | ||
|
||
// Function to generate White noise | ||
function generateWhiteNoise(buffer) { | ||
|
||
for (let i = 0; i < buffer.length; i++) { | ||
buffer[i] = (Math.random() * 2 - 1) / tempo; | ||
} | ||
} | ||
|
||
|
||
class RandomNoiseProcessor extends AudioWorkletProcessor { | ||
constructor (options) { | ||
super(); | ||
this.type = options.processorOptions.type; | ||
}; | ||
process(inputs, outputs) { | ||
const output = outputs[0]; | ||
output.forEach((channel) => { | ||
switch (this.type) { | ||
case types.brown: | ||
generateBrownNoise(channel); | ||
break; | ||
case types.pink: | ||
generatePinkNoise(channel); | ||
break; | ||
case types.white: | ||
generateWhiteNoise(channel); | ||
break; | ||
default: | ||
break; | ||
} | ||
}); | ||
return true; | ||
} | ||
} | ||
|
||
registerProcessor("RandomNoiseProcessor", RandomNoiseProcessor); |
Oops, something went wrong.