UNPKG

@polygonjs/polygonjs

Version:

node-based WebGL 3D engine https://polygonjs.com

90 lines (89 loc) 2.83 kB
"use strict"; import { TypedAudioNode } from "./_Base"; import { NodeParamsConfig, ParamConfig } from "../utils/params/ParamsConfig"; import { ALL_NOTES, DEFAULT_NOTE } from "../../../core/audio/Notes"; class PlayInstrumentAudioParamsConfig extends NodeParamsConfig { constructor() { super(...arguments); /** @param note */ this.note = ParamConfig.STRING(DEFAULT_NOTE, { menuString: { entries: ALL_NOTES.sort().map((note) => { return { value: note, name: note }; }) }, cook: false }); /** @param duration */ this.duration = ParamConfig.FLOAT(0.125, { range: [0, 1], rangeLocked: [true, false], cook: false }); /** @param play the audio */ // play = ParamConfig.BUTTON(null, { // callback: (node: BaseNodeType) => { // PlayInstrumentAudioNode.PARAM_CALLBACK_play(node as PlayInstrumentAudioNode); // }, // }); /** @param stop the audio */ // stop = ParamConfig.BUTTON(null, { // callback: (node: BaseNodeType) => { // PlayInstrumentAudioNode.PARAM_CALLBACK_stop(node as PlayInstrumentAudioNode); // }, // }); this.showNotes = ParamConfig.BOOLEAN(0, { separatorBefore: true }); this.showKeys = ParamConfig.BOOLEAN(0); this.startOctave = ParamConfig.INTEGER(2, { range: [1, 8], rangeLocked: [false, false] }); this.endOctave = ParamConfig.INTEGER(4, { range: [1, 8], rangeLocked: [false, false] }); this.updateNoteFromInstrument = ParamConfig.BOOLEAN(0); } } const ParamsConfig = new PlayInstrumentAudioParamsConfig(); export class PlayInstrumentAudioNode extends TypedAudioNode { constructor() { super(...arguments); this.paramsConfig = ParamsConfig; } static type() { return "playInstrument"; } initializeNode() { this.io.inputs.setCount(1); } cook(inputContents) { const audioBuilder = inputContents[0]; this.setAudioBuilder(audioBuilder); } async play() { const instrument = await this._getInstrument(); if (!instrument) { console.log("no instrument"); return; } instrument.triggerAttackRelease(this.pv.note, this.pv.duration); } // async stop() { // const instrument = await this._getInstrument(); // if (!instrument) { // return; // } // // TODO: find out how to properly call triggerRelease, without or without the argument // } async _getInstrument() { if (this.isDirty()) { await this.compute(); } const audioBuilder = this.containerController.container().coreContent(); if (!audioBuilder) { return; } return audioBuilder.instrument(); } // static PARAM_CALLBACK_play(node: PlayInstrumentAudioNode) { // node.play(); // } // static PARAM_CALLBACK_stop(node: PlayInstrumentAudioNode) { // node.stop(); // } }