# Peaks.js API Documentation This document describes the Peaks.js API, including configuration options, function calls, and events. ## Contents - [Configuration](#configuration) - [Marker customization](#marker-customization) - [Player customization](#player-customization) - [Time label customization](#time-label-customization) - [Methods](#methods) - [Initialization](#initialization) - [Peaks.init()](#peaksinitoptions-callback) - [instance.setSource()](#instancesetsourceoptions-callback) - [instance.getWaveformData()](#instancegetwaveformdata) - [Player API](#player-api) - [instance.player.play()](#instanceplayerplay) - [instance.player.pause()](#instanceplayerpause) - [instance.player.getCurrentTime()](#instanceplayergetcurrenttime) - [instance.player.getDuration()](#instanceplayergetduration) - [instance.player.seek()](#instanceplayerseektime) - [instance.player.playSegment()](#instanceplayerplaysegmentsegment-loop) - [Views API](#views-api) - [instance.views.getView()](#instanceviewsgetviewname) - [instance.views.getScrollbar()](#instanceviewsgetscrollbar) - [instance.views.createZoomview()](#instanceviewscreatezoomviewcontainer) - [instance.views.createOverview()](#instanceviewscreateoverviewcontainer) - [instance.views.destroyZoomview()](#instanceviewsdestroyzoomview) - [instance.views.destroyOverview()](#instanceviewsdestroyoverview) - [View API](#view-api) - [view.setAmplitudeScale()](#viewsetamplitudescalescale) - [view.setWaveformColor()](#viewsetwaveformcolorcolor) - [view.setPlayedWaveformColor()](#viewsetplayedwaveformcolorcolor) - [view.showPlayheadTime()](#viewshowplayheadtimeshow) - [view.setTimeLabelPrecision()](#viewsettimeLabelPrecisionprecision) - [view.showAxisLabels()](#viewshowaxislabelsshow-options) - [view.setAxisLabelColor()](#viewsetaxislabelcolorcolor) - [view.setAxisGridlineColor()](#viewsetaxisgridlinecolorcolor) - [view.enableAutoScroll()](#viewenableautoscrollenable-options) - [view.enableMarkerEditing()](#viewenablemarkereditingenable) - [view.enableSegmentDragging()](#viewenablesegmentdraggingenable) - [view.setSegmentDragMode()](#viewsetsegmentdragmodemode) - [view.setMinSegmentDragWidth()](#viewsetminsegmentdragwidthwidth) - [view.setWaveformDragMode()](#viewsetwaveformdragmodemode) - [view.fitToContainer()](#viewfittocontainer) - [view.setZoom()](#viewsetzoomoptions) - [view.setStartTime()](#viewsetstarttimetime) - [view.setWheelMode()](#viewsetwheelmodemode-options) - [view.enableSeek()](#viewenableseekenable) - [Scrollbar API](#scrollbar-api) - [scrollbar.fitToContainer()](#scrollbarfittocontainer) - [Zoom API](#zoom-api) - [instance.zoom.zoomIn()](#instancezoomzoomin) - [instance.zoom.zoomOut()](#instancezoomzoomout) - [instance.zoom.setZoom()](#instancezoomsetzoomindex) - [instance.zoom.getZoom()](#instancezoomgetzoom) - [Segments API](#segments-api) - [instance.segments.add()](#instancesegmentsaddsegment) - [instance.segments.getSegments()](#instancesegmentsgetsegments) - [instance.segments.getSegment()](#instancesegmentsgetsegmentid) - [instance.segments.removeByTime()](#instancesegmentsremovebytimestarttime-endtime) - [instance.segments.removeById()](#instancesegmentsremovebyidsegmentid) - [instance.segments.removeAll()](#instancesegmentsremoveall) - [Segment API](#segment-api) - [segment.update()](#segmentupdateoptions) - [Points API](#points-api) - [instance.points.add()](#instancepointsaddpoint) - [instance.points.getPoints()](#instancepointsgetpoints) - [instance.points.getPoint()](#instancepointsgetpointid) - [instance.points.removeByTime()](#instancepointsremovebytimetime) - [instance.points.removeById()](#instancepointsremovebyidpointid) - [instance.points.removeAll()](#instancepointsremoveall) - [Point API](#point-api) - [point.update()](#pointupdateoptions) - [Event Handling](#event-handling) - [instance.on()](#instanceonevent-callback) - [instance.once()](#instanceonceevent-callback) - [instance.off()](#instanceoffevent-callback) - [Destruction](#destruction) - [instance.destroy()](#instancedestroy) - [Events](#events) - [Player Events](#player-events) - [player.canplay](#playercanplay) - [player.error](#playererror) - [player.pause](#playerpause) - [player.playing](#playerplaying) - [player.seeked](#playerseeked) - [player.timeupdate](#playertimeupdate) - [player.ended](#playerended) - [Waveform View Events](#waveform-view-events) - [overview.click](#overviewclick) - [overview.dblclick](#overviewdblclick) - [overview.contextmenu](#overviewcontextmenu) - [zoomview.click](#zoomviewclick) - [zoomview.dblclick](#zoomviewdblclick) - [zoomview.contextmenu](#zoomviewcontextmenu) - [zoomview.update](#zoomviewupdate) - [zoom.update](#zoomupdate) - [Point Events](#point-events) - [points.add](#pointsadd) - [points.remove](#pointsremove) - [points.remove_all](#pointsremove_all) - [points.dragstart](#pointsdragstart) - [points.dragmove](#pointsdragmove) - [points.dragend](#pointsdragend) - [points.mouseenter](#pointsmouseenter) - [points.mouseleave](#pointsmouseleave) - [points.click](#pointsclick) - [points.dblclick](#pointsdblclick) - [points.contextmenu](#pointscontextmenu) - [points.enter](#pointsenter) - [Segment Events](#segment-events) - [segments.add](#segmentsadd) - [segments.insert](#segmentsinsert) - [segments.remove](#segmentsremove) - [segments.remove_all](#segmentsremove_all) - [segments.dragstart](#segmentsdragstart) - [segments.dragged](#segmentsdragged) - [segments.dragend](#segmentsdragend) - [segments.mouseenter](#segmentsmouseenter) - [segments.mouseleave](#segmentsmouseleave) - [segments.mousedown](#segmentsmousedown) - [segments.mouseup](#segmentsmouseup) - [segments.click](#segmentsclick) - [segments.dblclick](#segmentsdblclick) - [segments.contextmenu](#segmentscontextmenu) - [segments.enter](#segmentsenter) - [segments.exit](#segmentsexit) # Configuration Peaks.js provides a number of configuration options, as follows: ```javascript var options = { // // Zoomable waveform view options // zoomview: { // Container
element for the zoomable waveform view container: document.getElementById('zoomview-container'), // Color for the zoomable waveform // You can also use a 2 stop gradient here. See setWaveformColor() waveformColor: 'rgba(0, 225, 128, 1)', // Color for the played region of the zoomable waveform // You can also use a 2 stop gradient here. See setWaveformColor() playedWaveformColor: 'rgba(0, 225, 128, 1)', // Color of the playhead playheadColor: 'rgba(0, 0, 0, 1)', // Color of the playhead text playheadTextColor: '#aaa', // Background color of the playhead text playheadBackgroundColor: 'transparent', // Padding around the playhead text (pixels) playheadPadding: 2, // Width of the playhead (pixels) playheadWidth: 1, // Tolerance for clicks in the zoomview to be interpreted as // dragging the playhead (pixels) playheadClickTolerance: 3, // Returns a string for the playhead timestamp label formatPlayheadTime: function, // Show current time next to the playhead showPlayheadTime: false, // Precision of time label of playhead and point/segment markers timeLabelPrecision: 2, // Color of the axis gridlines axisGridlineColor: '#ccc', // Color of the axis labels axisLabelColor: '#aaa', // Returns a string for the axis label timestamps formatAxisTime: function, // Show or hide the axis label timestamps showAxisLabels: true, // Height of the axis markers at the top of the waveform axisTopMarkerHeight: 10, // Height of the axis markers at the top of the waveform axisBottomMarkerHeight: 10, // Font family for axis labels, playhead, and point and segment markers fontFamily: 'sans-serif', // Font size for axis labels, playhead, and point and segment markers fontSize: 11, // Font style for axis labels, playhead, and point and segment markers // (either 'normal', 'bold', or 'italic') fontStyle: 'normal', // Mouse-wheel mode: either 'none' or 'scroll' wheelMode: 'none', // Auto-scroll the waveform when the playhead reaches the edge of // the visible waveform autoScroll: true, // The offset in pixels edge of the visible waveform to trigger // auto-scroll autoScrollOffset: 100, // Enables point markers to be shown on the zoomable waveform enablePoints: true, // Enables segments to be shown on the zoomable waveform enableSegments: true, segmentOptions: { // Some segment options can be overridden for the zoomable waveform, // see segmentOptions below } }, // // Overview waveform options // overview: { // Container
element for the non-zoomable "overview" waveform container: document.getElementById('overview-container') // Color for the overview waveform // You can also use a 2 stop gradient here. See setWaveformColor() waveformColor: 'rgba(0,0,0,0.2)', // Color for the played region of the overview waveform // You can also use a 2 stop gradient here. See setWaveformColor() playedWaveformColor: 'rgba(0, 225, 128, 1)', // Color for the overview waveform rectangle // that shows what the zoomable view shows highlightColor: 'grey', // Stroke color for the zoomed region highlightStrokeColor: 'transparent', // Opacity for the zoomed region highlightOpacity: 0.3, // Corner Radius for the zoomed region highlightCornerRadius: 2, // The default number of pixels from the top and bottom of the canvas // that the overviewHighlight takes up highlightOffset: 11, // Color of the playhead playheadColor: 'rgba(0, 0, 0, 1)', // Color of the playhead text playheadTextColor: '#aaa', // Background color of the playhead text playheadBackgroundColor: 'transparent', // Padding around the playhead text (pixels) playheadPadding: 2, // Returns a string for the playhead timestamp label formatPlayheadTime: function, // Show current time next to the playhead showPlayheadTime: false, // Precision of time label of playhead and point/segment markers timeLabelPrecision: 2, // Color of the axis gridlines axisGridlineColor: '#ccc', // Color of the axis labels axisLabelColor: '#aaa', // Returns a string for the axis label timestamps formatAxisTime: function, // Show or hide the axis label timestamps showAxisLabels: true, // Height of the axis markers at the top of the waveform axisTopMarkerHeight: 10, // Height of the axis markers at the top of the waveform axisBottomMarkerHeight: 10, // Font family for axis labels, playhead, and point and segment markers fontFamily: 'sans-serif', // Font size for axis labels, playhead, and point and segment markers fontSize: 11, // Font style for axis labels, playhead, and point and segment markers // (either 'normal', 'bold', or 'italic') fontStyle: 'normal', // Enables point markers to be shown on the overview waveform enablePoints: true, // Enables segments to be shown on the overview waveform enableSegments: true, segmentOptions: { // Some segment options can be overridden for the overview waveform, // see segmentOptions below } }, // // Scrollbar options // scrollbar: { // Container
element for the scrollbar container: document.getElementById('scrollbar-container'), // Scrollbar color. The background color can be set using CSS on the // scrollbar container element color: '#888888', // Minimum scrollbar handle width, in pixels minWidth: 50 } // HTML media element containing an audio track mediaElement: document.querySelector('audio'), // // Pre-computed waveform data options // dataUri: { // Binary format waveform data URL arraybuffer: '/data/sample.dat', // JSON format waveform data URL json: '/data/sample.json', }, waveformData: { // ArrayBuffer containing binary format waveform data arraybuffer: null, // Object containing JSON format waveform data json: null }, // If true, Peaks.js will send credentials with all network requests, // i.e., when fetching waveform data withCredentials: false, // // Web Audio generated waveform data options // webAudio: { // A Web Audio AudioContext instance which can be used // to render the waveform if dataUri is not provided audioContext: new AudioContext(), // Alternatively, provide an AudioBuffer containing the decoded audio // samples. In this case, an AudioContext is not needed audioBuffer: null, // If true, the waveform will show all available channels // If false, the audio is shown as a single channel waveform multiChannel: false }, // Array of zoom levels in samples per pixel. Smaller numbers represent // being more "zoomed in". zoomLevels: [512, 1024, 2048, 4096], // To avoid computation when changing zoom level, Peaks.js maintains a cache // of waveforms at different zoom levels. This is enabled by default, but // can be disabled by setting waveformCache to false waveformCache: true // // Keyboard input options // // Bind keyboard controls keyboard: false, // Keyboard nudge increment in seconds (left arrow/right arrow) nudgeIncrement: 0.01, // // Default view options. Each of these can be set independently for each // waveform view, under the 'zoomview' and 'overview' options // (described above). // // Waveform color // You can also use a 2 stop gradient here. See setWaveformColor() waveformColor: 'rgba(0, 225, 128, 1)', // Color for the played waveform region // You can also use a 2 stop gradient here. See setWaveformColor() playedWaveformColor: 'rgba(0, 225, 128, 1)', // Color of the playhead playheadColor: 'rgba(0, 0, 0, 1)', // Color of the playhead text playheadTextColor: '#aaa', // Background color of the playhead text playheadBackgroundColor: 'transparent', // Padding around the playhead text (pixels) playheadPadding: 2, // Color of the axis gridlines axisGridlineColor: '#ccc', // Color of the axis labels axisLabelColor: '#aaa', // Font family for axis labels, playhead, and point and segment markers fontFamily: 'sans-serif', // Font size for axis labels, playhead, and point and segment markers fontSize: 11, // Font style for axis labels, playhead, and point and segment markers // (either 'normal', 'bold', or 'italic') fontStyle: 'normal', // Precision of time label of playhead and point/segment markers timeLabelPrecision: 2, // Show current time next to the playhead (zoomview only) showPlayheadTime: false, // // Point and segment options // // Default point marker color pointMarkerColor: '#ff0000', // if true, emit cue events on the Peaks instance (see Cue Events) emitCueEvents: false, // Enables point markers to be shown on the waveform views enablePoints: true, // Enables segments to be shown on the waveform views enableSegments: true, segmentOptions: { // Enable segment markers markers: true, // Enable segment overlays overlay: false, // Color for segment start marker handles startMarkerColor: '#aaaaaa', // Color for segment end marker handles endMarkerColor: '#aaaaaa', // Segment waveform color waveformColor: '#ff851b', // Segment overlay color overlayColor: '#ff0000', // Segment overlay opacity overlayOpacity: 0.3, // Segment overlay border color overlayBorderColor: '#ff0000', // Segment overlay border width overlayBorderWidth: 2, // Segment overlay border corner radius overlayCornerRadius: 5, // Segment overlay offset from the top and bottom of the waveform view, in pixels overlayOffset: 25, // Segment overlay label alignment, either 'top-left' or 'center' overlayLabelAlign: 'top-left', // Segment overlay label offset, in pixels overlayLabelPadding: 8, // Segment overlay label color overlayLabelColor: '#000000', // Segment overlay font family overlayFontFamily: 'sans-serif', // Segment overlay font size overlayFontSize: 12, // Segment overlay font style overlayFontStyle: 'normal' }, // // Customization options (see customizing.md) // createSegmentMarker: null, createSegmentLabel: null, createPointMarker: null, player: null, // // Point and segment initialization // segments: [ { startTime: 120, endTime: 140, editable: true, color: "#ff0000", labelText: "My label" }, { startTime: 220, endTime: 240, editable: false, color: "#00ff00", labelText: "My Second label" } ], points: [ { time: 150, editable: true, color: "#00ff00", labelText: "A point" }, { time: 160, editable: true, color: "#00ff00", labelText: "Another point" } ], // // Debugging options // // Diagnostic or error information is written to this function. // The default is console.error logger: console.error.bind(console) }; ``` ## Marker customization Peaks.js allows you to customize the appearance of the point and segment markers, by specifying the following configuration options: `createPointMarker`, `createSegmentMarker`, and `createSegmentLabel`. Please read [Customizing Peaks.js](customizing.md) for more details. ## Player customization By default, Peaks.js supports audio playback using the HTML `