UNPKG

shimi

Version:

A JS framework for building complex MIDI applications

136 lines (135 loc) 6.85 kB
import { ClockChildFinishedEvent, IClockChild } from './Clock'; import { IMidiMessage } from './MidiMessages'; import { IMidiOut } from './MidiOut'; import Note from './Note'; /** * Represents a single channel within the WebAudioMidiOut. This allows for creation of simple sine, square, triangle & sawtooth waves. * * @category Midi IO */ export declare class WebAudioMidiOutChannel { /** Returns the name of this type. This can be used rather than instanceof which is sometimes unreliable. */ get typeName(): string; /** The AudioContext instance which the synth channel is operating within. */ audioContext: AudioContext; /** The GainNode used for setting the loudness of notes. */ private _gainNode; /** The numeric value of the amount of gain being applied to each note. */ get gain(): number; set gain(value: number); /** The type of waveform being produced, valid values are: 'sine', 'square', 'triangle', or 'sawtooth'. */ type: any; /** * @param audioContext The AudioContext instance which the synth channel is operating within. * @param type The type of waveform being produced, valid values are: 'sine', 'square', 'triangle', or 'sawtooth'. * @param gain The amount of gain to be applied to the waveform. Default value is 0.1. */ constructor(audioContext: AudioContext, type: any, gain?: number); /** * This method gets called whenever a new note is to be played on the synth channel. * * It returns a new OscillatorNode instance. * @param note This is the note that is due to begin being played. * @param frequency This is the frequency of the note being played. */ createOscillator(frequency: number): OscillatorNode; } /** * The WebAudioMidiOut class is a very simple synthesizer implementation, built on top of the Web Audio API. It is intended to primarily be a quick and dirty way to get some sound out through the web browser. For more advanced sound generation, use either the ToneJSMidiOut for in-browser sounds, or MidiOut for working with external instruments. * * The WebAudioMidiOut class implements the IMidiOut interface, so that you can very easily swap out the WebAudioMidiOut for external devices as and when needed. * * @category Midi IO */ export default class WebAudioMidiOut implements IMidiOut, IClockChild { /** Returns the name of this type. This can be used rather than instanceof which is sometimes unreliable. */ get typeName(): string; private _audioContext; /** The AudioContext instance which the WebAudioMidiOut is operating within. */ get audioContext(): AudioContext; private _channels; /** * @param audioContext The AudioContext instance which the WebAudioMidiOut is operating within. * Example: `new shimi.WebAudioMidiOut(new AudioContext());` */ constructor(audioContext: AudioContext); /** * Define what sound to use for a specific MIDI channel number. * @param channelNumber The channel being defined, valid values range from 0 - 15. * @param type The type of waveform being produced, valid values are: 'sine', 'square', 'triangle', or 'sawtooth'. * @param gain The amount of gain to be applied to the waveform. Default value is 0.1. * @returns Returns the WebAudioMidiOut instance, to allow for chaining of operations. */ withChannel(channelNumber: number, type: any, gain?: number): WebAudioMidiOut; /** * Automatically sets up all undefined channels with default sounds. * It cycles through defining each channel with sine, square, sawtooth & triangle wave in turn, so that channels, 0, 4, 8 & 12 would be sine, 1, 5, 9 & 13 would be square, etc. * * Example usage: * const synth = new shimi.WebAudioMidiOut(new AudioContext()).withDefaultChannels(); * @returns Returns the WebAudioMidiOut instance, to allow for chaining of operations. */ withDefaultChannels(): WebAudioMidiOut; /** * The notes collection consists of notes which have been started, but not ended yet. * * The WebAudioMidiOut 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 the WebAudioMidiOut's collection, returning the note that was added. * * If `note.on == true`, then the the WebAudioMidiOut immediately starts up new oscillators for it. * @param note The note to add to the WebAudioMidiOut. * @returns */ addNote(note: Note): Note; /** * Calls the stop() method of all notes which have been added to the WebAudioMidiOut 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 interprets and performs the appropriate action. Currently this only supports Note On & Note Off messages. * @param message The IMidiMessage object to be acted upon. * @returns */ sendMessage(message: IMidiMessage): void; private _previousStatus; /** * Receives a byte-array representation of a MIDI message. The method interprets the message into an IMidiMessage object, and calls the `WebAudioMidiOut.sendMessage` method with it. * @param data An array of the data to be sent to the synth. */ sendRawData(data: number[]): void; /** Provides a way of identifying a WebAudioMidiOut so it can be easily retrieved later */ get ref(): string; set ref(value: string); private _ref; /** Returns true if the WebAudioMidiOut has been instructed to stop everything by the `finish()` method. */ get isFinished(): boolean; private _isFinished; /** This event fires when the WebAudioMidiOut finishes. */ get finished(): ClockChildFinishedEvent; private _finished; /** * 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; /** Calling this tells the WebAudioMidiOut to stop whatever it's doing and that it will no longer be used. */ finish(): void; /** * Provides a way for setting the ref through a chained function call. For example: * * ``` * clock.addChild(new WebAudioMidiOut(context).withDefaultChannels().withRef('output')); * ``` * * @param ref The ref to set on the object. * @returns The calling object. */ withRef(ref: string): IClockChild; private _createOscillator; }