UNPKG

shimi

Version:

A JS framework for building complex MIDI applications

164 lines (163 loc) 7.28 kB
import { IMidiMessage } from './MidiMessages'; import { IMidiOut } from './MidiOut'; import Note from './Note'; import * as messages from './MidiMessages'; import { ClockChildFinishedEvent, IClockChild } from './Clock'; /** * The ToneJSMidiOutChannel class represents one particular MIDI channel within the ToneJSMidiOut. It holds a reference to a ToneJS instrument object, and handles the conversion from received MIDI messages into actions to perform on the ToneJS instrument. * * @category Midi IO */ export declare class ToneJSMidiOutChannel { /** Returns the name of this type. This can be used rather than instanceof which is sometimes unreliable. */ get typeName(): string; /** * The ToneJS instrument which will be responsible for playback. */ get target(): any; private _target; /** * A collection of current control values. This array is instanciated in the constructor with 128 indices, each one initially set to 0. As control change MIDI messages are received, these values get updated. */ controlValues: Array<number>; /** * Stores whether the ToneJS instrument being held is polyphonic. This is mainly for internal purposes, to determine how calls are made to the target. */ get isPolyphonic(): boolean; private _isPolyphonic; private _pitchBendPercent; /** * The ToneJSMidiOut instance which this channel object belongs to. */ get parent(): ToneJSMidiOut; private _parent; /** * @param target The ToneJS instrument which will be responsible for playback. * @param parent The ToneJSMidiOut instance which this channel object belongs to. */ constructor(target: any, parent: ToneJSMidiOut); /** * This method gets called by ToneJSMidiOut whenever a new note needs to be started. * @param note The note object that playback is to be started for. */ onNoteStart(note: Note): void; /** * This method gets called by ToneJSMidiOut whenever a note needs to be stopped. * @param note The note object that playback is to be stopped for. */ onNoteStop(note: Note): void; /** * This method gets called by ToneJSMidiOut whenever a pitch bend message is received for this channel. Note, this method will only take action if the attached ToneJS instrument is monophonic. Using pitch bends in conjunction with frequency manipulation through ToneJS's set of tools will lead to weird results * @param pitchBend The pitch bend message that was received * @returns Returns true if any action was taken. */ onPitchBend(pitchBend: messages.PitchBendMessage): boolean; /** * This property is null by default, but can take a function value which gets called every time the channel receives a new control change message. */ onControlChange: (controlChange: messages.ControlChangeMessage, target: any) => void; } /** * The ToneJSMidiOut implements the IMidiOut, and provides an integration with the ToneJS javascript library. This allows for MIDI data coming from shimi to control sound generation in the browser by ToneJS. * * @category Midi IO */ export default class ToneJSMidiOut implements IMidiOut, IClockChild { /** Returns the name of this type. This can be used rather than instanceof which is sometimes unreliable. */ get typeName(): string; /** * The collection of channels that MIDI data can be sent out to. Each channel that's being used should hold a reference to a ToneJS instrument object. */ get channels(): Array<ToneJSMidiOutChannel>; private _channels; /** * A reference to the ToneJS library. */ get toneJS(): any; private _toneJS; /** * Requires that a reference to the toneJS library be passed in as a parameter, for example: * * ``` * import * as Tone from 'tone'; * new ToneJSMidiOut(Tone); * ``` */ constructor(toneJS: any); /** * Sets up a target destination against a particular channel number, which MIDI messages with the corresponding number will be sent to. * * Example: * * ``` * const midiOut = new ToneJSMidiOut(Tone); * midiOut.setChannel(0, new Tone.Synth().toDestination()); * ``` * * @param channel The channel number to be set up. Valid values range from 0 to 15. * @param target The ToneJS instrument object which will MIDI messages will ultimately end up affecting. */ setChannel(channel: number, target: any): void; /** * The notes collection consists of notes which have been started, but not ended yet. * * The ToneJSMidiOut will cycle through this collection on each update, checking to see if it needs to stop the playback of any notes. */ get notes(): Array<Note>; private _notes; /** * Adds a new note to ToneJSMidiOut's collection, returning the note that was added. * * Note, if the ToneJSMidiOut has not set anything for the channel which the note is targetting, then no action will be performed. * * If `note.on == true`, then the ToneJS Instrument for the corresponding channel is immediately instructed to triggerAttack. * @param note The note to add to the ToneJSMidiOut. * @returns The note that was added */ addNote(note: Note): Note; /** * Calls the stop() method of all notes which have been added to the MidiOut that meet the passed in criteria. * @param filter The criteria for determining which notes need to be stopped. If no filter provided, then all notes are stopped. */ stopNotes(filter?: (note: Note) => boolean): void; /** * This method accepts an IMidiMessage object, which it converts to a MIDI byte array to send to the connected MIDI port. * @param message The IMidiMessage object to be converted and sent out. * @returns */ sendMessage(message: IMidiMessage): boolean; /** * Sends a raw byte-array MIDI message * @param data An array of the data to be sent. */ sendRawData(data: number[]): boolean; /** * This method is intended to be called by a clock to provide regular updates. It should be called by consumers of the library. * @param deltaMs How many milliseconds have passed since the last update cycle. * @returns */ update(deltaMs: number): void; /** Returns true if the clip player has finished playing its clip. */ get isFinished(): boolean; private _isFinished; /** This event fires when the clip player finishes. */ get finished(): ClockChildFinishedEvent; private _finished; /** Calling this tells the clip player to stop whatever it's doing and that it will no longer be used. */ finish(): void; /** Provides a way of identifying a clip player so that it can be easily retrieved later. */ get ref(): string; set ref(value: string); private _ref; /** * Provides a way for setting the ref through a chained function call. For example: * * ``` * clock.addChild(new ClipPlayer(clip, metronome, midiOut).withRef('player')); * ``` * * @param ref The ref to set on the object. * @returns The calling object. */ withRef(ref: string): IClockChild; }