trackswitch.js

Player Versions

trackswitch.js ships one browser-ready JavaScript bundle for all player versions. The interactive sync player also needs the separate worker file.

Version Custom HTML tag
Default player <trackswitch-player>
Sync player <trackswitch-sync-player>
Interactive sync player <trackswitch-sync-interactive>

Quick Minimal Setup

<script src="dist/js/trackswitch.js"></script>

<trackswitch-player>
  <script type="application/json">
    {
      "ui": [
        {
          "type": "trackGroup",
          "trackGroup": [
            {
              "title": "Track 1",
              "sources": [{ "src": "track1.mp3", "type": "audio/mpeg" }]
            }
          ]
        }
      ]
    }
  </script>
</trackswitch-player>

Full Options Reference

TrackSwitch.createDefaultTrackSwitch(rootElement, {
  presetNames: ['Full Mix', 'Strings', 'Rhythm'],
  ui: [
    {
      type: 'image',
      src: 'cover.jpg',
      seekable: true,
      seekMarginLeft: 5,
      seekMarginRight: 5,
      style: 'margin: 0;',
    },
    {
      type: 'text',
      text: 'Choose which parts of the arrangement you want to hear.',
      bold: true,
      italic: false,
      fontSize: 18,
      align: 'center',
      style: 'margin: 0;',
    },
    {
      type: 'waveform',
      height: 160,
      waveformBarWidth: 2,
      maxZoom: 5,
      waveformSource: 'audible',
      playbackFollowMode: 'center',
      timer: false,
      seekMarginLeft: 3,
      seekMarginRight: 4,
      style: 'margin: 0;',
    },
    {
      type: 'trackGroup',
      rowHeight: 44,
      trackGroup: [
        {
          title: 'Violins',
          solo: true,
          volume: 0.9,
          pan: -0.2,
          image: 'violins.png',
          style: 'border-left: 3px solid #4f8dc9;',
          presets: [0, 1],
          sources: [
            { src: 'violins.mp3', type: 'audio/mpeg', startOffsetMs: 0, endOffsetMs: 0 },
            { src: 'violins.ogg', type: 'audio/ogg' },
          ],
        },
        {
          title: 'Drums',
          solo: false,
          volume: 1,
          pan: 0,
          image: 'drums.png',
          style: 'border-left: 3px solid #ed8c01;',
          presets: [0, 2],
          sources: [{ src: 'drums.mp3', type: 'audio/mpeg', startOffsetMs: -120, endOffsetMs: 250 }],
        },
      ],
    },
    {
      type: 'sheetMusic',
      src: 'score.musicxml',
      maxWidth: 960,
      maxHeight: 360,
      renderScale: 0.75,
      followPlayback: true,
      cursorColor: '#999999',
      cursorAlpha: 0.1,
      style: 'margin: 0;',
    },
  ],
  features: {
    exclusiveSolo: false,
    muteOtherPlayerInstances: true,
    globalVolume: true,
    trackVolumeControls: true,
    trackPanControls: true,
    repeat: false,
    tabView: false,
    iosAudioUnlock: true,
    keyboard: true,
    looping: true,
    seekBar: true,
    timer: true,
    presets: true,
    customizablePanelOrder: true,
  },
});

Configuration

ui

ui is required. It decides which sections appear in the player and in what order they appear.

Use it to add any of these section types:

At least one trackGroup section is required because that is where the tracks live.

presetNames

Use presetNames to create ensembles and name your track combinations.

Example:

presetNames: ['Full Mix', 'Vocals Only', 'Backing Track']

Notes:

features

Option Type Default Description
exclusiveSolo? boolean false Listen to one track at a time only instead of mixing several tracks together.
muteOtherPlayerInstances? boolean true Stops another player on the same page when this one starts playing.
globalVolume? boolean false Shows a main volume control for the whole player.
trackVolumeControls? boolean false Shows per-track volume controls.
trackPanControls? boolean false Shows per-track left-right pan controls.
customizablePanelOrder? boolean false Lets listeners rearrange the visible UI elements. Affects the visible sections on the page, not the track order itself.
repeat? boolean false Starts with repeat already turned on.
tabView? boolean false Changes the look of the track rows to a tab-like style.
iosAudioUnlock? boolean true Helps playback start more reliably on iPhone and iPad. Recommended to leave this on.
keyboard? boolean true Enable keyboard shortcuts.
looping? boolean false Show loop tools and allow A/B looping.
seekBar? boolean true Show the main seekbar.
timer? boolean true Show the main time display.
presets? boolean true Show preset switching UI element when presets are available.

Unknown feature keys are rejected.

alignment

Use alignment only with sync or interactive sync modes. Default-mode players reject this block.

Option Type Default Description
csv string - The timing data file used to connect the different performances.
referenceTimeColumn string - The csv column to determine the main shared timeline used by the player. A usual setup would be to align tracks to a reference timeline calculated from the score.
referenceTimeColumnSync? string none The csv column to determine the shared timeline when synchronized playback is turned on in sync mode.
outOfRange? 'clamp' | 'linear' 'clamp' What the player should do when playback reaches a part of the timing map that has no matching value.

Track Settings

trackGroup

Use type: 'trackGroup' to add one or more tracks to the player.

Example:

{
  type: 'trackGroup',
  rowHeight: 44,
  trackGroup: [
    {
      title: 'Drums',
      image: 'drums.png',
      presets: [0, 2],
      sources: [{ src: 'drums.mp3' }],
    },
  ],
}

Section options:

Option Type Default Description
rowHeight? number none Sets the height of the track rows.
trackGroup object[] - The list of tracks shown in this section.

Notes:

Track Options

Each entry inside trackGroup can use these options:

Option Type Default Description
title? string none Name shown in the track list.
solo? boolean false Starting on/off state for that track.
volume? number 1 Starting track volume. Starts at 1 if you do not set it.
pan? number 0 Starting left-right placement. Starts at 0 if you do not set it.
image? string none Image used by perTrackImage and other track-based visuals.
presets? number[] none Decides which presets include this track.
sources object[] - Audio files for this track.
alignment? object none Alignment settings for this track in a sync player.
style? string none Lets you give that track row its own visual styling.

Audio Source Options

Each entry inside sources and alignment.synchronizedSources can use these options:

Option Type Default Description
src string - Audio file to use.
type? string none Optional file-type hint. If you omit it, trackswitch.js recognizes these source file extensions automatically: .aac, .aif, .aiff, .au, .flac, .m4a, .mp1, .mp2, .mp3, .mp4, .mpeg, .mpg, .oga, .ogg, .wav, .webm.
startOffsetMs? number 0 Trims or pads the beginning of the file. Positive values trim. Negative values add silence.
endOffsetMs? number 0 Trims or pads the end of the file. Positive values trim. Negative values add silence.

Notes:

Track Alignment Options

In sync mode, each track can also use an alignment block:

trackGroup: [
    {
      title: 'Track 1',
      sources: [{ src: 'track1.mp3' }],
      alignment: {
        column: 'track_1_time',
        synchronizedSources: [{ src: 'track1-synced.mp3' }],
      }
    },
  ],
Option Type Default Description
column? string none The timing-data column for that performance. Required in sync mode.
synchronizedSources? object[] none Extra audio files used when synchronized playback is turned on.

Notes:

Visualizations

text

Use type: 'text' to add a plain text section to the player.

Example:

{
  type: 'text',
  text: 'Choose which parts of the arrangement you want to hear.',
  bold: true,
  italic: false,
  fontSize: 18,
  align: 'center',
  style: 'margin: 12px 0;',
}

Section options:

Option Type Default Description
text string - The text to show.
bold? boolean false Makes the text bold.
italic? boolean false Makes the text italic.
fontSize? number none Sets the text size in pixels.
align? 'left' | 'center' | 'right' 'center' Sets horizontal text alignment.
style? string none Lets you fine-tune the look or spacing of the section with CSS.

Notes:

image

Use type: 'image' for one main image, such as cover art, a diagram, or a screenshot.

Example:

{
  type: 'image',
  src: 'cover.jpg',
  seekable: true,
  seekMarginLeft: 5,
  seekMarginRight: 5,
  style: 'margin: 12px auto;',
}

Section options:

Option Type Default Description
src string - The image file to show.
seekable? boolean false Lets listeners click the image to jump to a different point in the audio.
seekMarginLeft? number 0 Leaves a non-seekable area on the left side of the image.
seekMarginRight? number 0 Leaves a non-seekable area on the right side of the image.
style? string none Lets you fine-tune the look of the section with CSS.

perTrackImage

Use type: 'perTrackImage' to show the image for the currently active track.

Example:

{
  type: 'perTrackImage',
  seekable: false,
  style: 'margin: 12px auto;',
}

Section options:

Option Type Default Description
seekable? boolean false Lets listeners click the current track image to jump in time.
seekMarginLeft? number 0 Leaves a non-seekable area on the left side of the image.
seekMarginRight? number 0 Leaves a non-seekable area on the right side of the image.
style? string none Lets you fine-tune the look or spacing of the section with CSS.

Notes:

waveform

Use type: 'waveform' to show an interactive waveform.

Example:

{
  type: 'waveform',
  height: 150,
  waveformBarWidth: 2,
  maxZoom: 5,
  waveformSource: 'audible',
  playbackFollowMode: 'center',
  timer: true,
  style: 'margin: 16px 0;',
}

Section options:

Option Type Default Description
height? number 150 Height of the waveform.
waveformBarWidth? number 1 Thickness of the waveform bars.
maxZoom? number 5 The closest zoom level listeners can reach, in seconds. Smaller numbers allow tighter zoom.
waveformSource? 'audible' | number | number[] 'audible' Chooses which sound the waveform represents.
playbackFollowMode? 'off' | 'center' | 'jump' 'off' Decides whether the waveform view follows playback automatically.
timer? boolean Standard: false; Sync: true Shows a small time label inside the waveform panel.
alignedPlayhead? boolean false Draws a diagonal Z-shaped indicator that shows where the reference timeline position is relative to this track’s local playhead. Only has an effect in sync mode and requires waveformSource to be a track index (number).
showAlignmentPoints? boolean false Draws a thin dashed Z-shaped line for every alignment anchor point that exists, showing the full warping path across the waveform. Only has an effect in sync mode and requires waveformSource to be a track index (number).
seekMarginLeft? number 0 Leaves a non-seekable area on the left side.
seekMarginRight? number 0 Leaves a non-seekable area on the right side.
style? string none Lets you fine-tune the look or spacing of the section with CSS.

Notes:

midi

Use type: 'midi' to show a MIDI file as a piano-roll visualization. MIDI files are only visual; they do not add playback tracks or change audio output.

Example:

{
  type: 'midi',
  src: 'notes.mid',
  alignmentColumn: 'time_notes',
  height: 180,
  maxZoom: 5,
  playbackFollowMode: 'center',
  timer: true,
  style: 'margin: 16px 0;',
}

Section options:

Option Type Default Description
src string - MIDI file to visualize.
alignmentColumn? string none Alignment CSV column containing this MIDI file’s local seconds. Used by sync players for mapped playback heads, timers, following, loops, and seeking.
height? number 180 Height of the MIDI view.
maxZoom? number 5 The closest zoom level listeners can reach, in seconds. Smaller numbers allow tighter zoom.
playbackFollowMode? 'off' | 'center' | 'jump' 'off' Decides whether the MIDI view follows playback automatically.
timer? boolean false Shows a small time label inside the MIDI panel.
seekMarginLeft? number 0 Leaves a non-seekable area on the left side.
seekMarginRight? number 0 Leaves a non-seekable area on the right side.
style? string none Lets you fine-tune the look or spacing of the section with CSS.

Notes:

sheetMusic

Use type: 'sheetMusic' to show a MusicXML score.

Example:

{
  type: 'sheetMusic',
  src: 'score.musicxml',
  measureColumn: 'measure',
  maxWidth: 960,
  maxHeight: 360,
  renderScale: 0.75,
  followPlayback: true,
  cursorColor: '#999999',
  cursorAlpha: 0.1,
  style: 'margin: 20px auto;',
}

Section options:

Option Type Default Description
src string - The MusicXML file to show.
measureColumn? string none The column in the alignment data that contains measure numbers for score following.
maxWidth? number 1000 The widest the score area should become.
maxHeight? number 380 The tallest the score area should become.
renderScale? number 0.7 Determines the size of rendered score elements.
followPlayback? boolean true Keeps the score view moving with playback.
cursorColor? string '#999999' Color of the playback follow cursor.
cursorAlpha? number 0.4 Transparency of the playback follow cursor. Values below 0 become 0; values above 1 become 1.
style? string none Lets you fine-tune the look or spacing of the section with CSS.

Notes:

warpingMatrix

Use type: 'warpingMatrix' to show interactive warping path and local tempo deviation graphs in sync mode.

Example:

{
  type: 'warpingMatrix',
  height: 240,
  tempoSmoothingSeconds: 5,
  bpm: 'infer_score',
  style: 'margin: 12px 0;',
}

Section options:

Option Type Default Description
height? number auto Height of the chart area.
tempoSmoothingSeconds? number 5 How much the local tempo deviation graph should be smoothed. Tempo Deviation is computed as a central differences variant of the warping path. Larger values give a smoother curve.
bpm? number | 'infer_score' | null null Controls whether the left tempo axis is shown as BPM. Use a positive number for a fixed global BPM, 'infer_score' to infer it from the score dynamically (changing BPM also supported), or null to hide the BPM axis and show only tempo percent.
style? string none Lets you fine-tune the look or spacing of the section with CSS.

Notes:

Keyboard and Loop Controls

When features.keyboard is on, you can use keyboard shortcuts:

Keys Action
F1 Open or close the shortcut help panel
Space Play or pause
Escape Stop and return to the start
R Toggle repeat
Left / Right Jump backward or forward by 2 seconds
Shift + Left / Shift + Right Jump backward or forward by 5 seconds
Home Go to the start
Up / Down Change global volume when globalVolume is on
1 to 0 Control tracks 1 to 10

When features.looping is on, you can also use:

Keys Action
A Set loop point A
B Set loop point B
L Turn the loop on or off
C Clear the loop

Looping is also available through the loop buttons. On seekable controls, loop regions can be marked directly using right-click on mouse.

Things to Check