Package Exports
- html-midi-player
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (html-midi-player) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
html-midi-player
<midi-player>
and <midi-visualizer>
HTML elements powered by @magenta/music (Magenta.js), fully stylable and scriptable.
See a demo here.
Getting started
Add the necessary scripts to your page:
<script src="https://cdn.jsdelivr.net/combine/npm/tone@13,npm/@magenta/music@1/es6/core.js,npm/focus-visible@5,npm/html-midi-player@1"></script>
Add a player and a visualizer:
<midi-player src="https://magenta.github.io/magenta-js/music/demos/melody.mid" sound-font visualizer="#myVisualizer"> </midi-player> <midi-visualizer type="piano-roll" id="myVisualizer"></midi-visualizer>
That's it!
Installing from NPM
You can also add the package to your project from NPM, e.g. npm install --save html-midi-player or yarn add html-midi-player. Then you can either:
import 'html-midi-player'in your JavaScript code (as an ES Module), or- add the
node_modules/html-midi-player/dist/midi-player.min.jsbundle directly to your page, along with the dependencies (node_modules/tone/build/Tone.js,node_modules/@magenta/music/es6/core.js).
In both cases, you should also add the focus-visible polyfill to enable outlines on keyboard focus.
API basics
See also the API reference for both elements:
midi-player,
midi-visualizer.
src and noteSequence
Both midi-player and midi-visualizer support two different ways of specifying the input file:
- By setting the
srcattribute to a MIDI file URL, e.g.:<midi-player src="twinkle-twinkle.mid"></midi-player>
player.src = "twinkle-twinkle.mid";
- By assigning a Magenta
NoteSequenceto thenoteSequenceproperty, e.g.:player.noteSequence = TWINKLE_TWINKLE;
SoundFonts
By default, the player will use a simple oscillator synth. To use a SoundFont, add the sound-font attribute:
<midi-player sound-font></midi-player> <!-- default SoundFont (same as below) -->
<midi-player sound-font="https://storage.googleapis.com/magentadata/js/soundfonts/sgm_plus"></midi-player>player.soundFont = null; // no SoundFont
player.soundFont = ''; // default SoundFont (same as below)
player.soundFont = 'https://storage.googleapis.com/magentadata/js/soundfonts/sgm_plus';See the Magenta.js docs for a list of available SoundFonts.
Visualizer settings
The visualizer type is specified via the type attribute. Three visualizer types are supported: piano-roll, waterfall and staff.
Each visualizer type has a set of settings that can be specified using the config attribute, e.g.:
visualizer.config = {
noteHeight: 4,
pixelsPerTimeStep: 60,
minPitch: 30
};The settings are documented in the Magenta.js docs.
Binding visualizers
A player supports binding one or more visualizers to it using the visualizer attribute (a selector) or the addVisualizer method:
<midi-player visualizer="#myVisualizer, #myOtherVisualizer"></midi-player>player.addVisualizer(document.getElementById('myVisualizer'));
player.addVisualizer(document.getElementById('myOtherVisualizer'));The visualizer only gets updated while the player is playing, which allows a single visualizer to be bound to multiple players.