An Observable based library for the use of Web MIDI API with Angular
npm install @ng-web-apis/midi


This library contains abstractions and helpful utils to use Web MIDI API idiomatically
with Angular.
If you do not have @ng-web-apis/common:
``bash`
npm i @ng-web-apis/common
You would also need @types/webmidi package until it is included in TypeScript. Now install the package:
`bash`
npm i @ng-web-apis/midi
To use Web MIDI API with your Angular application you can use tokens, RxJs operators
and utils included with this package:
- WA_MIDI_SUPPORT — boolean value checking browser supportWA_SYSEX
- — boolean token responsible for system exclusive access, false by defaultWA_MIDI_ACCESS
- — a PromiseWA_SYSEX
with MIDIAccess object, depends on token forWA_MIDI_INPUT
access level
- — a PromiseWA_MIDI_OUTPUT
with MIDIInput. You would need to provide it yourself
see utility functions below
- — a PromiseWA_MIDI_MESSAGES
with MIDIOutput. You would need to provide it yourself
see utility functions below
- — an Observable of
MIDIMessageEvent from all
MIDIInputs, use rxjs function below to narrow and
process the stream
- You can provide WA_MIDI_INPUT and WA_MIDI_OUTPUT tokens with following functions:
inputById, inputByName, outputById, outputByName:
`ts
import {Component, Inject} from '@angular/core';
import {inputById, WA_MIDI_INPUT, WA_MIDI_OUTPUT, outputByName} from '@ng-web-apis/midi';
@Component({
selector: 'my-comp',
template: '...',
providers: [inputById('input-0'), outputByName('VirtualMIDISynth')],
})
export class Example {
constructor(@Inject(WA_MIDI_INPUT) input: Promise
}
`
- You can convert MIDI note to frequency and back using toFrequency and toNote functions. They optionally acceptfrequency
second argument for tuning of middle A note using 440 as default value
- You can use pipe from FrequencyPipeModule to convert MIDI note to frequency directly in template
#### Monotype operators
These are filtering operators which you can use on WA_MIDI_MESSAGES stream to narrow it to your needs. All of them are
applied like that:
`ts`
messages$.pipe(filterByChannel(1), aftertouch());
- filterByChannel only lets through messages from given channel (0 to 15)filterById
- only lets through messages from particularid
MIDIInput identifying it by propertyfilterByName
- only lets through messages from particularname
MIDIInput identifying it by propertynotes
- only lets through played notes messages, normalizing noteOff messages to noteOn with 0 velocityaftertouch
- only lets through aftertouch messages, same logic goes fow all functions belowmodulationWheel
- pan
- pitchBend
- polyphonicAftertouch
- programChange
- sustainPedal
-
If you believe other operators could be helpful, please
file an issue explaining what would you like to be added and why.
These are used to convert message to something necessary for you, since it turns
MIDIMessageEvents to different objects, use it
after all monotype operations from the list above have been applied.
- toData — extracts datatoTime
Uint8Array from each
MIDIMessageEvent
- — extracts receivedTime timestamp from eachtoStatusByte
MIDIMessageEvent
- — extracts first element from datatoDataByte
Uint8Array
- — extracts second element from datatoValueByte
Uint8Array
- — extracts third element from data`
Uint8Array
> Keep in mind some messages might not contain third or even second byte so only use those extractions when you are sure
> (i.e. filtered the stream to compliant messages beforehand).
You can try online demo here
Other Web APIs for Angular by
@ng-web-apis