UNPKG

3.14 kBJavaScriptView Raw
1import { __decorate } from "tslib";
2import { FrequencyClass } from "../core/type/Frequency.js";
3import { optionsFromArguments } from "../core/util/Defaults.js";
4import { noOp } from "../core/util/Interface.js";
5import { Instrument } from "../instrument/Instrument.js";
6import { timeRange } from "../core/util/Decorator.js";
7/**
8 * Abstract base class for other monophonic instruments to extend.
9 */
10export class Monophonic extends Instrument {
11 constructor() {
12 const options = optionsFromArguments(Monophonic.getDefaults(), arguments);
13 super(options);
14 this.portamento = options.portamento;
15 this.onsilence = options.onsilence;
16 }
17 static getDefaults() {
18 return Object.assign(Instrument.getDefaults(), {
19 detune: 0,
20 onsilence: noOp,
21 portamento: 0,
22 });
23 }
24 /**
25 * Trigger the attack of the note optionally with a given velocity.
26 * @param note The note to trigger.
27 * @param time When the note should start.
28 * @param velocity The velocity determines how "loud" the note will be.
29 * @example
30 * const synth = new Tone.Synth().toDestination();
31 * // trigger the note a half second from now at half velocity
32 * synth.triggerAttack("C4", "+0.5", 0.5);
33 */
34 triggerAttack(note, time, velocity = 1) {
35 this.log("triggerAttack", note, time, velocity);
36 const seconds = this.toSeconds(time);
37 this._triggerEnvelopeAttack(seconds, velocity);
38 this.setNote(note, seconds);
39 return this;
40 }
41 /**
42 * Trigger the release portion of the envelope.
43 * @param time If no time is given, the release happens immediately.
44 * @example
45 * const synth = new Tone.Synth().toDestination();
46 * synth.triggerAttack("C4");
47 * // trigger the release a second from now
48 * synth.triggerRelease("+1");
49 */
50 triggerRelease(time) {
51 this.log("triggerRelease", time);
52 const seconds = this.toSeconds(time);
53 this._triggerEnvelopeRelease(seconds);
54 return this;
55 }
56 /**
57 * Set the note at the given time. If no time is given, the note
58 * will set immediately.
59 * @param note The note to change to.
60 * @param time The time when the note should be set.
61 * @example
62 * const synth = new Tone.Synth().toDestination();
63 * synth.triggerAttack("C4");
64 * // change to F#6 in one quarter note from now.
65 * synth.setNote("F#6", "+4n");
66 */
67 setNote(note, time) {
68 const computedTime = this.toSeconds(time);
69 const computedFrequency = note instanceof FrequencyClass ? note.toFrequency() : note;
70 if (this.portamento > 0 && this.getLevelAtTime(computedTime) > 0.05) {
71 const portTime = this.toSeconds(this.portamento);
72 this.frequency.exponentialRampTo(computedFrequency, portTime, computedTime);
73 }
74 else {
75 this.frequency.setValueAtTime(computedFrequency, computedTime);
76 }
77 return this;
78 }
79}
80__decorate([
81 timeRange(0)
82], Monophonic.prototype, "portamento", void 0);
83//# sourceMappingURL=Monophonic.js.map
\No newline at end of file