# Embed a module on your website

> Put a published Maru module on any web page with one iframe, choose its controls, and drive it from your own page with the maru-preview/v1 messages.

Canonical: https://maru.audio/docs/embed-a-module
Documentation index: https://maru.audio/llms.txt

A published module plays on any web page through one `<iframe>`. The frame
runs the module's published release on the module's own address on
`maru.page`, with its interface, its playback bar and its own audio. Your page
chooses the size and which controls appear, and it can drive the player with
`postMessage`.

> **In the Help Center:** [Embed instruments, effects and racks](https://maru.audio/help/embed-audio-on-your-website.md) for embedding a player without writing code.

## Copy the embed code

On the module's page, open the **⋮** menu, choose **Share** and select
**Copy embed code**. It is offered for every public or unlisted module with
a published release. A module that has saved source but no release yet offers
it to visitors who can read that source, and the frame builds the source in
the visitor's browser. A private module cannot be embedded.

```html
<iframe src="https://alice-acid-bass.maru.page/embed"
  aria-label="Acid Bass · Maru" width="100%" height="560"
  style="border:0" loading="lazy"
  allow="midi; microphone; autoplay"></iframe>
```

| Part | Why it is there |
| --- | --- |
| `src` | The module's own origin. It plays the module's live release, so publishing a new version updates every page that embeds it. |
| `allow="midi; microphone; autoplay"` | Lets the visitor turn on a MIDI controller or a microphone inside the frame. Without it those switches cannot turn on. A visitor still has to choose them; nothing is requested when the page loads. |
| `width="100%"`, `height` | The player fills the box you give it and does not resize itself. The code suggests 560 pixels for an instrument, which shows a keyboard under its interface, and 420 for an effect. Change `height` to suit your layout. |
| `loading="lazy"` | A player below the fold starts loading only when the visitor scrolls near it. |

Leave out a `sandbox` attribute: the player is already isolated on its own
origin, and a sandbox can stop it loading its files. MIDI and the microphone
need your page to be served over HTTPS.

The module's address is `<handle>-<slug>.maru.page`. A module published
before its creator chose a handle gets `<slug>-<code>.maru.page`, and once
the creator has a handle the next publication moves it; the earlier address
redirects. An embed code that names
`maru.audio/embed/community/<owner>/<slug>` hands over to the module's own
address, so codes copied earlier keep working.

## Choose the controls

Add these to the `src` query:

| Option | Values | Effect |
| --- | --- | --- |
| `controls` | `full` (default), `transport`, `none` | `transport` hides the clock, the status and the input picker; `none` draws no playback bar. |
| `bar` | `docked` (default), `floating` | Pins the bar to the bottom edge of the frame, or places it under the interface. |
| `hide` | Comma-separated: `transport`, `meter`, `clock`, `status`, `input`, `settings`, `output`, `actions` | Hides those parts of the bar on top of `controls`. The source picker cannot be hidden. |

For example, `https://alice-acid-bass.maru.page/embed?controls=transport&hide=meter`.
Unknown values fall back to the defaults.

Sound starts only after the visitor taps or clicks inside the frame. Browsers
require that gesture, and a click elsewhere on your page does not count.

## Drive the player from your page

Your page can send the frame commands and hear what it does, with
`postMessage`. Every message is an object with `protocol: "maru-preview/v1"`,
a `type`, and the module's identity: its `moduleId` and `packageSha256`. The
frame announces both in its `ready` message. It ignores a command whose
identity does not match the release it is playing, and one that does not come
from its parent window.

To draw your own controls, load the frame with `?host=parent`
(`https://alice-acid-bass.maru.page/embed?host=parent`). The frame then draws
no playback bar, and your page supplies the controls. It still shows its own
**Tap to start** prompt when a `play` needs the visitor's gesture. The
commands below also reach a frame that keeps its own controls.

### From the frame to your page

| `type` | Carries | Meaning |
| --- | --- | --- |
| `ready` | identity | The release is loaded and verified. Address commands with this identity. |
| `schema` | `schema`, `presets`, `defaultPresetId`, `presetId`, `midiInput`, `inputChannels`, `outputChannels` | The module's parameters (path, range, unit), its factory presets, whether it takes MIDI and its audio channels. |
| `paramChanged` | `path`, `value`, `presetId` | A parameter changed, from its interface or from your `setParam`. |
| `started` | `sampleRate` | Audio is running. |
| `stopped` | identity | Audio stopped. |
| `needsGesture` | identity | A `play` could not start audio because no gesture reached the frame. The frame shows a **Tap to start** prompt; your page cannot start the audio itself. |
| `error` | `message` | Something failed. Show it as plain text. |
| `keyboard` | key event fields | With `?host=parent`: a computer-keyboard key pressed in the frame, so your page's keyboard handling keeps working while the frame has focus. |
| `outputSignal` | `samples` (2048 numbers) | With `?host=parent`: a throttled copy of the output, for drawing your own meter or scope. It never carries audio you can play. |

### From your page to the frame

| `type` | Fields | Effect |
| --- | --- | --- |
| `play` | none | Starts or resumes audio. |
| `stop` | none | Stops audio. |
| `setParam` | `path`, `value` | Sets one parameter. Use a path from `schema`; a path it does not declare is ignored. The value is in the parameter's own range and is clamped to it. |
| `noteOn` | `note` (0 to 127), `velocity` (0 to 1, optional) | Plays a note. |
| `noteOff` | `note` | Releases a note. |
| `midi` | `bytes`: three numbers, 0 to 255 | One MIDI message, for example from your own sequencer or Web MIDI input. |
| `panic` | none | Releases every note. |
| `setMonitorLevel` | `level` (0 to 2) | Output gain. |
| `selectSource` | `kind`: `mic`, `audio-file`, `guitar-take` or `none`; `deviceId` or `url` | Chooses an effect's input. The frame opens the microphone itself; a `url` must be on the module's own address, such as its demo takes. |
| `prepareSource` | `url` | Fetches a file on the module's own address ahead of use, without selecting it. |
| `loadClip` | `bytes`: an `ArrayBuffer` of an encoded audio file, at most 64 MiB | Plays audio from your page through the module. Transfer the buffer with the message. |

`play`, `stop` and `setParam` refuse any extra field. The exact shapes are
published as JSON Schemas (`preview/frame-message` and
`preview/parent-message`); see [Schemas](https://maru.audio/docs/schemas.md).

```html
<iframe id="bass" src="https://alice-acid-bass.maru.page/embed?host=parent"
  width="100%" height="480" style="border:0" allow="midi; microphone; autoplay"></iframe>
<button id="play">Play</button>
<script>
  const frame = document.getElementById("bass")
  const origin = new URL(frame.src).origin
  let identity = null
  window.addEventListener("message", (event) => {
    if (event.source !== frame.contentWindow || event.origin !== origin) return
    const message = event.data
    if (message?.protocol !== "maru-preview/v1") return
    if (message.type === "ready") {
      identity = { moduleId: message.moduleId, packageSha256: message.packageSha256 }
    }
    if (message.type === "needsGesture") console.log("Tap the player to start")
  })
  const send = (command) => {
    if (identity) frame.contentWindow.postMessage(
      { protocol: "maru-preview/v1", ...identity, ...command }, origin)
  }
  document.getElementById("play").onclick = () => {
    send({ type: "play" })
    send({ type: "noteOn", note: 48, velocity: 0.8 })
    setTimeout(() => send({ type: "noteOff", note: 48 }), 400)
  }
</script>
```

Check `event.source` against your own iframe and `event.origin` against the
module's address before you trust a message, and send to that origin rather
than `"*"`.

## What an embed cannot do

- **Process your page's live audio.** Audio crosses into the frame only as a
  file (`loadClip`). To run a module on your own audio graph, host it with the
  [module player](https://maru.audio/docs/module-player-react.md) instead, as
  [web DAW hosting](https://maru.audio/docs/host-modules-in-a-web-daw.md) describes.
- **Save and restore the player's state.** There is no message for it.
  Listen to `paramChanged` and replay values with `setParam`.
- **Coordinate several embeds.** Two players on one page play independently.
  Send `stop` to the others when one starts.
- **Use your page's MIDI device permission.** The frame asks for its own, on
  its own address, the first time the visitor turns MIDI or the microphone on.
