UNPKG

2.6 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { FrequencyClass } from "../core/type/Frequency.js";
3import { deepMerge, optionsFromArguments } from "../core/util/Defaults.js";
4import { readOnly } from "../core/util/Interface.js";
5import { Monophonic } from "./Monophonic.js";
6import { Synth } from "./Synth.js";
7import { range, timeRange } from "../core/util/Decorator.js";
8/**
9 * MembraneSynth makes kick and tom sounds using a single oscillator
10 * with an amplitude envelope and frequency ramp. A Tone.OmniOscillator
11 * is routed through a Tone.AmplitudeEnvelope to the output. The drum
12 * quality of the sound comes from the frequency envelope applied
13 * during MembraneSynth.triggerAttack(note). The frequency envelope
14 * starts at <code>note * .octaves</code> and ramps to <code>note</code>
15 * over the duration of <code>.pitchDecay</code>.
16 * @example
17 * const synth = new Tone.MembraneSynth().toDestination();
18 * synth.triggerAttackRelease("C2", "8n");
19 * @category Instrument
20 */
21export class MembraneSynth extends Synth {
22 constructor() {
23 const options = optionsFromArguments(MembraneSynth.getDefaults(), arguments);
24 super(options);
25 this.name = "MembraneSynth";
26 /**
27 * Portamento is ignored in this synth. use pitch decay instead.
28 */
29 this.portamento = 0;
30 this.pitchDecay = options.pitchDecay;
31 this.octaves = options.octaves;
32 readOnly(this, ["oscillator", "envelope"]);
33 }
34 static getDefaults() {
35 return deepMerge(Monophonic.getDefaults(), Synth.getDefaults(), {
36 envelope: {
37 attack: 0.001,
38 attackCurve: "exponential",
39 decay: 0.4,
40 release: 1.4,
41 sustain: 0.01,
42 },
43 octaves: 10,
44 oscillator: {
45 type: "sine",
46 },
47 pitchDecay: 0.05,
48 });
49 }
50 setNote(note, time) {
51 const seconds = this.toSeconds(time);
52 const hertz = this.toFrequency(note instanceof FrequencyClass ? note.toFrequency() : note);
53 const maxNote = hertz * this.octaves;
54 this.oscillator.frequency.setValueAtTime(maxNote, seconds);
55 this.oscillator.frequency.exponentialRampToValueAtTime(hertz, seconds + this.toSeconds(this.pitchDecay));
56 return this;
57 }
58 dispose() {
59 super.dispose();
60 return this;
61 }
62}
63__decorate([
64 range(0)
65], MembraneSynth.prototype, "octaves", void 0);
66__decorate([
67 timeRange(0)
68], MembraneSynth.prototype, "pitchDecay", void 0);
69//# sourceMappingURL=MembraneSynth.js.map
\No newline at end of file