Skip to content

Commit

Permalink
feat: meter output can be normalRange in addition to decibels
Browse files Browse the repository at this point in the history
  • Loading branch information
tambien committed Dec 15, 2019
1 parent ed93e67 commit 2625a13
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
17 changes: 16 additions & 1 deletion Tone/component/analysis/Meter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ describe("Meter", () => {
meter.dispose();
});
});

if (ONLINE_TESTING) {

it("can get the rms level of the incoming signal", (done) => {
const meter = new Meter();
const osc = new Oscillator().connect(meter).start();
Expand All @@ -63,6 +64,20 @@ describe("Meter", () => {
done();
}, 400);
});

it("can get the values in normal range", (done) => {
const meter = new Meter({
normalRange: true,
});
const osc = new Oscillator().connect(meter).start();
osc.volume.value = -6;
setTimeout(() => {
expect(meter.getValue()).to.be.closeTo(0.35, 0.15);
meter.dispose();
osc.dispose();
done();
}, 400);
});
}
});
});
12 changes: 11 additions & 1 deletion Tone/component/analysis/Meter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { warn } from "../../core/util/Debug";

export interface MeterOptions extends MeterBaseOptions {
smoothing: NormalRange;
normalRange: boolean;
}

/**
Expand All @@ -27,6 +28,13 @@ export class Meter extends MeterBase<MeterOptions> {

readonly name: string = "Meter";

/**
* If the output should be in decibels or normal range between 0-1. If `normalRange` is false,
* the output range will be the measured decibel value, otherwise the decibel value will be converted to
* the range of 0-1
*/
normalRange: boolean;

/**
* A value from between 0 and 1 where 0 represents no time averaging with the last analysis frame.
*/
Expand All @@ -49,11 +57,13 @@ export class Meter extends MeterBase<MeterOptions> {
this.smoothing = options.smoothing;
this._analyser.size = 256;
this._analyser.type = "waveform";
this.normalRange = options.normalRange;
}

static getDefaults(): MeterOptions {
return Object.assign(MeterBase.getDefaults(), {
smoothing: 0.8,
normalRange: false,
});
}

Expand All @@ -76,7 +86,7 @@ export class Meter extends MeterBase<MeterOptions> {
// the rms can only fall at the rate of the smoothing
// but can jump up instantly
this._rms = Math.max(rms, this._rms * this.smoothing);
return gainToDb(this._rms);
return this.normalRange ? this._rms : gainToDb(this._rms);
}

dispose(): this {
Expand Down

0 comments on commit 2625a13

Please sign in to comment.