# cp-midi-player

[![npm](https://img.shields.io/npm/v/cp-midi-player)](https://www.npmjs.com/package/cp-midi-player)
[![jsDelivr](https://data.jsdelivr.com/v1/package/npm/cp-midi-player/badge)](https://www.jsdelivr.com/package/npm/cp-midi-player)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue)](LICENSE)

Designed for music researchers building demo pages and listening tests.
Play MIDI and audio in your browser, just like in your DAW.

<p align="center">
  <a href="https://tanchihpin0517.github.io/cp-midi-player/">
    <img src="docs/player-modern-five-tracks.png" alt="Five-track MIDI player with instrument icons, notes, and mute, solo and volume controls">
  </a><br>
  <sub>(Click the image to try the live demo.)</sub>
</p>

Add these two lines to your page:

```html
<script type="module" src="https://cdn.jsdelivr.net/npm/cp-midi-player@latest"></script>

<cp-midi-player src="song.mid"></cp-midi-player>
```

No build step is needed. The player includes a default set of instrument sounds,
so you do not need to supply a SoundFont to get started.

## Features

- Track and piano-roll views.
- Mute, solo, volume and instrument controls for each MIDI track.
- Audio recordings and stems that stay in sync with MIDI.
- WAV export of the current mix.
- Light and dark themes.

## Usage

### From a CDN

```html
<script type="module" src="https://cdn.jsdelivr.net/npm/cp-midi-player@latest"></script>
```

### From npm

```sh
npm install cp-midi-player
```

```js
import "cp-midi-player"; // registers <cp-midi-player>
```

The package has no runtime dependencies. With a bundler (Vite, webpack,
esbuild), the player loads its worklet, SoundFont and font from the
same version on jsDelivr. To serve them yourself, see
[Self-hosting](#self-hosting).

### Audio tracks

```html
<cp-midi-player src="song.mid">
  <cp-midi-player-audio src="mix.mp3" label="Mixture"></cp-midi-player-audio>
  <cp-midi-player-audio src="vocals.mp3" label="Vocals stem"></cp-midi-player-audio>
  <cp-midi-player-audio src="bass.mp3" label="Bass stem" offset="0.25"></cp-midi-player-audio>
</cp-midi-player>
```

`offset="0.25"` delays a recording by 0.25 seconds; a negative value skips its
beginning. Add `before-midi-track="1"` to place a recording above the first MIDI
track. Without it, recordings appear above all MIDI tracks.

## Attributes

| attribute | effect |
|---|---|
| `src` | MIDI file URL (required) |
| `view-mode` | `arrangement` (Tracks, default) or `piano-roll` |
| `track-rail` | `open` (default); `collapsed` to its instrument icons, which visitors can still open; or `hidden`, which leaves out the track list and the Tracks/Roll switch |
| `piano-keys` | draw a piano keyboard beside the pitch rows in the Roll view; off by default |
| `track-height` | starting height of each track in px (52–180); without it tracks fit the player's height. Visitors can still change it |
| `theme` | `light` or `dark`; follows the system setting when omitted |
| `soundfont` | SF2, SF3 or DLS URL; defaults to GeneralUser GS |
| `file-name` | name shown in messages and used for the exported WAV |
| `persist-key` | remember mute, solo, volume, view and the collapsed track list per visitor in `localStorage` |
| `no-preload` | defer SoundFont loading until first play; otherwise it preloads by default |
| `preload` | retained for compatibility; preloading is now the default |
| `asset-base` | folder holding the player's assets; see [Self-hosting](#self-hosting) |
| `audio`, `audio-label`, `audio-offset` | one recording without a child element |
| `no-export` | hide the Export WAV button |
| `soundfont-menu` | show a SoundFont menu so visitors can load their own bank |
| `no-exclusive` | keep playing when another player on the page starts |

Set the player height with CSS: `cp-midi-player { height: 24rem; }`.
Without a CSS height the player fits its tracks, up to 44 rem (64 rem when the
page or the visitor has set a track height).

Players preload their SoundFont when mounted and share the download on the same
page. Playback still requires a user action. Pass `preloadSoundFont: false` to
`mount()`, or add `no-preload` to the element, to load only on first play.

## JavaScript

```js
const player = document.querySelector("cp-midi-player");

player.addEventListener("play", () => console.log("playing"));
player.addEventListener("pause", () => console.log("paused at", player.currentTime));

await player.play();
player.seek(12.5); // seconds
player.pause();
player.stop();
player.duration; // seconds
```

To render into an element you already have, or to pass bytes instead of URLs,
use `mount()`:

```js
import { mount } from "cp-midi-player";

const player = mount(document.querySelector("#slot"), {
  src: midiArrayBuffer, // URL or ArrayBuffer
  soundFont: sf2ArrayBuffer, // URL or ArrayBuffer, optional
  fileName: "take-3.mid",
  audioTracks: [{ url: "vocals.mp3", label: "Vocals" }],
  viewMode: "piano-roll"
});

player.destroy();
```

TypeScript types are included, and `document.querySelector("cp-midi-player")`
is typed as the player element.

## SoundFonts and assets

The included [GeneralUser GS](https://schristiancollins.com/generaluser.php)
SoundFont downloads on first play (8.4 MB). Use `soundfont` to choose another bank.
Players share the download; starting one pauses the others by default.
For large collections, mount players as needed and remove them when finished.
Audio recordings are decoded into memory, so short excerpts work best.

### Self-hosting

To serve everything from your own site (for offline use or a strict content
security policy), copy `node_modules/cp-midi-player/dist/` to your server
and load the script from there:

```html
<script type="module" src="/static/cp-midi-player/cp-midi-player.js"></script>
```

A script loaded from its own `dist/` folder finds the assets next to itself.
When the script is bundled into your app instead, point the player at the
copied folder with the `asset-base` attribute, or once for the whole page:

```js
import { setDefaultAssetBase } from "cp-midi-player";
setDefaultAssetBase("/static/cp-midi-player/");
```

## Browser support

Current Chrome, Edge, Firefox and Safari. Playback uses the Web Audio
AudioWorklet. Browsers start audio only after a click or key press, so
playback starts from the player's controls or from your own button.

## Development

```sh
npm install
npm test          # unit tests
npm run typecheck
npm run build     # writes dist/
npx http-server . # then open /demo/
```

The demo pages load the player from `../dist/`, so run `npm run build` after
each change and reload.

## Publishing

GitHub releases tagged `v<version>` publish to npm and redeploy the demo site
from the same tag; pushes to `main` leave both alone. Update the package
version, lockfile and changelog first. The npm Trusted Publisher must point to
`tanchihpin0517/cp-midi-player`, workflow `publish.yml`, with no environment name.
The `github-pages` environment must allow deployments from `v*` tags
(Settings → Environments → github-pages → Deployment branches and tags).

## License

MIT. SpessaSynth is Apache-2.0. GeneralUser GS and JetBrains Mono (OFL)
ship with their license files in `dist/`. The demo's music excerpt is
CC BY-NC-SA 2.5; see the demo page for credits.

## Credits

cp-midi-player originated from
[midi-realplayer](https://github.com/vaclisinc/midi-realplayer) 0.1.4 by
Song-Ze (Jimmy) Yu, released under the MIT License.
