Skip to content

Commit

Permalink
data: both fft and real noise
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Oct 24, 2020
1 parent 0dd117f commit e08d391
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,12 @@ export default class Data {

sample(freq) {
let presence = true;
let noise = Math.random() * 0.4;

// E6=1318
// A1=55
if (!freq) {
freq = Math.pow(2, Math.random() * 4.7) * 55;
presence = Math.random() > 0.5;
if (!presence) {
noise = 1;
}
}
const f = freq / this.sampleRate;
const phase = Math.random();
Expand All @@ -63,7 +59,7 @@ export default class Data {
for (let t = 0; t < this.fftIn.length; t++) {
let signal = 0;

for (let h = 0; h < 3; h++) {
for (let h = 0; h < 4; h++) {
const harmonicFreq = Math.pow(2, h) * f;
signal += wave(harmonicFreq, t + phase / harmonicFreq) *
Math.pow(harmonicFade, h);
Expand All @@ -74,11 +70,19 @@ export default class Data {
this.normalize(this.fftIn);
}

const noise = Math.random() * 0.4;
for (let t = 0; t < this.fftIn.length; t++) {
this.fftIn[t] = this.fftIn[t] * (1 - noise) + Math.random() * noise;
}
this.normalize(this.fftIn);

this.fft.realTransform(this.fftOut, this.fftIn);

const fftNoise = Math.random() * 0.1;
for (let i = 0; i < this.fftSize; i += 2) {
this.fftOut[i >>> 1] = Math.sqrt(
this.fftOut[i] ** 2 + this.fftOut[i + 1] ** 2) * (1 - noise) +
Math.random() * noise;
this.fftOut[i] ** 2 + this.fftOut[i + 1] ** 2) * (1 - fftNoise) +
Math.random() * fftNoise;
}

return {
Expand Down

0 comments on commit e08d391

Please sign in to comment.