fix: should set _sync in this.sync

This commit is contained in:
Astray-git 2020-08-26 16:58:14 +08:00
parent 826a51d594
commit 75f591a40a
5 changed files with 58 additions and 8 deletions

View file

@ -80,14 +80,25 @@ export abstract class Instrument<Options extends InstrumentOptions> extends Tone
* Tone.Transport.start();
*/
sync(): this {
if (!this._synced) {
this._synced = true;
if (this._syncState()) {
this._syncMethod("triggerAttack", 1);
this._syncMethod("triggerRelease", 0);
}
return this;
}
/**
* set _sync
*/
protected _syncState(): boolean {
let changed = false;
if (!this._synced) {
this._synced = true;
changed = true;
}
return changed;
}
/**
* Wrap the given method so that it can be synchronized
* @param method Which method to wrap and sync

View file

@ -103,8 +103,10 @@ export class NoiseSynth extends Instrument<NoiseSynthOptions> {
}
sync(): this {
if (this._syncState()) {
this._syncMethod("triggerAttack", 0);
this._syncMethod("triggerRelease", 0);
}
return this;
}

View file

@ -334,8 +334,10 @@ export class PolySynth<Voice extends Monophonic<any> = Synth> extends Instrument
}
sync(): this {
if (this._syncState()) {
this._syncMethod("triggerAttack", 1);
this._syncMethod("triggerRelease", 1);
}
return this;
}

View file

@ -254,8 +254,10 @@ export class Sampler extends Instrument<SamplerOptions> {
}
sync(): this {
if (this._syncState()) {
this._syncMethod("triggerAttack", 1);
this._syncMethod("triggerRelease", 1);
}
return this;
}

View file

@ -5,6 +5,10 @@ import { Offline } from "./Offline";
import { OutputAudio } from "./OutputAudio";
import { Monophonic } from "Tone/instrument/Monophonic";
function wait(time) {
return new Promise(done => setTimeout(done, time));
}
export function InstrumentTest(Constr, note, constrArg?, optionsIndex?): void {
context("Instrument Tests", () => {
@ -167,6 +171,35 @@ export function InstrumentTest(Constr, note, constrArg?, optionsIndex?): void {
});
});
it("can unsync and re-sync triggerAttack to the Transport", () => {
return Offline(async ({ transport }) => {
const instance = new Constr(constrArg);
instance.toDestination();
instance.sync();
if (note) {
instance.triggerAttack(note, 0.1);
} else {
instance.triggerAttack(0.1);
}
transport.start(0.1);
await wait(100);
instance.unsync();
transport.stop();
instance.sync();
if (note) {
instance.triggerAttack(note, 0.1);
} else {
instance.triggerAttack(0.1);
}
transport.start(0.1);
}, 1).then((buffer) => {
expect(buffer.getTimeOfFirstSound()).to.be.within(0.19, 0.25);
});
});
it("calling sync and unsync multiple times has no effect", () => {
return Offline(({ transport }) => {
const instance = new Constr(constrArg);