Skip to content

Commit 2625a13

Browse files
committed
feat: meter output can be normalRange in addition to decibels
1 parent ed93e67 commit 2625a13

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

Tone/component/analysis/Meter.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ describe("Meter", () => {
5050
meter.dispose();
5151
});
5252
});
53-
53+
5454
if (ONLINE_TESTING) {
55+
5556
it("can get the rms level of the incoming signal", (done) => {
5657
const meter = new Meter();
5758
const osc = new Oscillator().connect(meter).start();
@@ -63,6 +64,20 @@ describe("Meter", () => {
6364
done();
6465
}, 400);
6566
});
67+
68+
it("can get the values in normal range", (done) => {
69+
const meter = new Meter({
70+
normalRange: true,
71+
});
72+
const osc = new Oscillator().connect(meter).start();
73+
osc.volume.value = -6;
74+
setTimeout(() => {
75+
expect(meter.getValue()).to.be.closeTo(0.35, 0.15);
76+
meter.dispose();
77+
osc.dispose();
78+
done();
79+
}, 400);
80+
});
6681
}
6782
});
6883
});

Tone/component/analysis/Meter.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { warn } from "../../core/util/Debug";
66

77
export interface MeterOptions extends MeterBaseOptions {
88
smoothing: NormalRange;
9+
normalRange: boolean;
910
}
1011

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

2829
readonly name: string = "Meter";
2930

31+
/**
32+
* If the output should be in decibels or normal range between 0-1. If `normalRange` is false,
33+
* the output range will be the measured decibel value, otherwise the decibel value will be converted to
34+
* the range of 0-1
35+
*/
36+
normalRange: boolean;
37+
3038
/**
3139
* A value from between 0 and 1 where 0 represents no time averaging with the last analysis frame.
3240
*/
@@ -49,11 +57,13 @@ export class Meter extends MeterBase<MeterOptions> {
4957
this.smoothing = options.smoothing;
5058
this._analyser.size = 256;
5159
this._analyser.type = "waveform";
60+
this.normalRange = options.normalRange;
5261
}
5362

5463
static getDefaults(): MeterOptions {
5564
return Object.assign(MeterBase.getDefaults(), {
5665
smoothing: 0.8,
66+
normalRange: false,
5767
});
5868
}
5969

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

8292
dispose(): this {

0 commit comments

Comments
 (0)