From 2625a134b62af117c1c525a4e631e4e52b25ba90 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Sun, 15 Dec 2019 16:01:19 -0500 Subject: [PATCH] feat: meter output can be normalRange in addition to decibels --- Tone/component/analysis/Meter.test.ts | 17 ++++++++++++++++- Tone/component/analysis/Meter.ts | 12 +++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Tone/component/analysis/Meter.test.ts b/Tone/component/analysis/Meter.test.ts index e785a2c4f..368dd5993 100644 --- a/Tone/component/analysis/Meter.test.ts +++ b/Tone/component/analysis/Meter.test.ts @@ -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(); @@ -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); + }); } }); }); diff --git a/Tone/component/analysis/Meter.ts b/Tone/component/analysis/Meter.ts index 73a2896cc..77dadac6c 100644 --- a/Tone/component/analysis/Meter.ts +++ b/Tone/component/analysis/Meter.ts @@ -6,6 +6,7 @@ import { warn } from "../../core/util/Debug"; export interface MeterOptions extends MeterBaseOptions { smoothing: NormalRange; + normalRange: boolean; } /** @@ -27,6 +28,13 @@ export class Meter extends MeterBase { 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. */ @@ -49,11 +57,13 @@ export class Meter extends MeterBase { 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, }); } @@ -76,7 +86,7 @@ export class Meter extends MeterBase { // 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 {