added audioplayer contrib module
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,8 @@
|
||||
region.wavesurfer-region:before {
|
||||
content: attr(data-region-label);
|
||||
}
|
||||
|
||||
region.wavesurfer-region[data-region-highlight] {
|
||||
border: 1px solid rgb(86, 180, 239);
|
||||
box-shadow: 0px 1px 3px rgba(0, 0, 0, 0.05) inset, 0px 0px 8px rgba(82, 168, 236, 0.6);
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
/**
|
||||
* Create a WaveSurfer instance.
|
||||
*/
|
||||
var wavesurfer;
|
||||
|
||||
/**
|
||||
* Init & load.
|
||||
*/
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
// Init wavesurfer
|
||||
wavesurfer = WaveSurfer.create({
|
||||
container: '#waveform',
|
||||
height: 100,
|
||||
pixelRatio: 1,
|
||||
scrollParent: true,
|
||||
normalize: true,
|
||||
minimap: true,
|
||||
backend: 'MediaElement',
|
||||
plugins: [
|
||||
WaveSurfer.regions.create(),
|
||||
WaveSurfer.minimap.create({
|
||||
height: 30,
|
||||
waveColor: '#ddd',
|
||||
progressColor: '#999',
|
||||
cursorColor: '#999'
|
||||
}),
|
||||
WaveSurfer.timeline.create({
|
||||
container: "#wave-timeline"
|
||||
})
|
||||
]
|
||||
});
|
||||
|
||||
wavesurfer.util.ajax({
|
||||
responseType: 'json',
|
||||
url: 'rashomon.json'
|
||||
}).on('success', function (data) {
|
||||
wavesurfer.load(
|
||||
'http://www.archive.org/download/mshortworks_001_1202_librivox/msw001_03_rashomon_akutagawa_mt_64kb.mp3',
|
||||
data
|
||||
);
|
||||
});
|
||||
|
||||
/* Regions */
|
||||
|
||||
wavesurfer.on('ready', function () {
|
||||
wavesurfer.enableDragSelection({
|
||||
color: randomColor(0.1)
|
||||
});
|
||||
|
||||
if (localStorage.regions) {
|
||||
loadRegions(JSON.parse(localStorage.regions));
|
||||
} else {
|
||||
// loadRegions(
|
||||
// extractRegions(
|
||||
// wavesurfer.backend.getPeaks(512),
|
||||
// wavesurfer.getDuration()
|
||||
// )
|
||||
// );
|
||||
wavesurfer.util.ajax({
|
||||
responseType: 'json',
|
||||
url: 'annotations.json'
|
||||
}).on('success', function (data) {
|
||||
loadRegions(data);
|
||||
saveRegions();
|
||||
});
|
||||
}
|
||||
});
|
||||
wavesurfer.on('region-click', function (region, e) {
|
||||
e.stopPropagation();
|
||||
// Play on click, loop on shift click
|
||||
e.shiftKey ? region.playLoop() : region.play();
|
||||
});
|
||||
wavesurfer.on('region-click', editAnnotation);
|
||||
wavesurfer.on('region-updated', saveRegions);
|
||||
wavesurfer.on('region-removed', saveRegions);
|
||||
wavesurfer.on('region-in', showNote);
|
||||
|
||||
wavesurfer.on('region-play', function (region) {
|
||||
region.once('out', function () {
|
||||
wavesurfer.play(region.start);
|
||||
wavesurfer.pause();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
/* Toggle play/pause buttons. */
|
||||
var playButton = document.querySelector('#play');
|
||||
var pauseButton = document.querySelector('#pause');
|
||||
wavesurfer.on('play', function () {
|
||||
playButton.style.display = 'none';
|
||||
pauseButton.style.display = '';
|
||||
});
|
||||
wavesurfer.on('pause', function () {
|
||||
playButton.style.display = '';
|
||||
pauseButton.style.display = 'none';
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Save annotations to localStorage.
|
||||
*/
|
||||
function saveRegions() {
|
||||
localStorage.regions = JSON.stringify(
|
||||
Object.keys(wavesurfer.regions.list).map(function (id) {
|
||||
var region = wavesurfer.regions.list[id];
|
||||
return {
|
||||
start: region.start,
|
||||
end: region.end,
|
||||
attributes: region.attributes,
|
||||
data: region.data
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Load regions from localStorage.
|
||||
*/
|
||||
function loadRegions(regions) {
|
||||
regions.forEach(function (region) {
|
||||
region.color = randomColor(0.1);
|
||||
wavesurfer.addRegion(region);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Extract regions separated by silence.
|
||||
*/
|
||||
function extractRegions(peaks, duration) {
|
||||
// Silence params
|
||||
var minValue = 0.0015;
|
||||
var minSeconds = 0.25;
|
||||
|
||||
var length = peaks.length;
|
||||
var coef = duration / length;
|
||||
var minLen = minSeconds / coef;
|
||||
|
||||
// Gather silence indeces
|
||||
var silences = [];
|
||||
Array.prototype.forEach.call(peaks, function (val, index) {
|
||||
if (Math.abs(val) <= minValue) {
|
||||
silences.push(index);
|
||||
}
|
||||
});
|
||||
|
||||
// Cluster silence values
|
||||
var clusters = [];
|
||||
silences.forEach(function (val, index) {
|
||||
if (clusters.length && val == silences[index - 1] + 1) {
|
||||
clusters[clusters.length - 1].push(val);
|
||||
} else {
|
||||
clusters.push([ val ]);
|
||||
}
|
||||
});
|
||||
|
||||
// Filter silence clusters by minimum length
|
||||
var fClusters = clusters.filter(function (cluster) {
|
||||
return cluster.length >= minLen;
|
||||
});
|
||||
|
||||
// Create regions on the edges of silences
|
||||
var regions = fClusters.map(function (cluster, index) {
|
||||
var next = fClusters[index + 1];
|
||||
return {
|
||||
start: cluster[cluster.length - 1],
|
||||
end: (next ? next[0] : length - 1)
|
||||
};
|
||||
});
|
||||
|
||||
// Add an initial region if the audio doesn't start with silence
|
||||
var firstCluster = fClusters[0];
|
||||
if (firstCluster && firstCluster[0] != 0) {
|
||||
regions.unshift({
|
||||
start: 0,
|
||||
end: firstCluster[firstCluster.length - 1]
|
||||
});
|
||||
}
|
||||
|
||||
// Filter regions by minimum length
|
||||
var fRegions = regions.filter(function (reg) {
|
||||
return reg.end - reg.start >= minLen;
|
||||
});
|
||||
|
||||
// Return time-based regions
|
||||
return fRegions.map(function (reg) {
|
||||
return {
|
||||
start: Math.round(reg.start * coef * 10) / 10,
|
||||
end: Math.round(reg.end * coef * 10) / 10
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Random RGBA color.
|
||||
*/
|
||||
function randomColor(alpha) {
|
||||
return 'rgba(' + [
|
||||
~~(Math.random() * 255),
|
||||
~~(Math.random() * 255),
|
||||
~~(Math.random() * 255),
|
||||
alpha || 1
|
||||
] + ')';
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Edit annotation for a region.
|
||||
*/
|
||||
function editAnnotation (region) {
|
||||
var form = document.forms.edit;
|
||||
form.style.opacity = 1;
|
||||
form.elements.start.value = Math.round(region.start * 10) / 10,
|
||||
form.elements.end.value = Math.round(region.end * 10) / 10;
|
||||
form.elements.note.value = region.data.note || '';
|
||||
form.onsubmit = function (e) {
|
||||
e.preventDefault();
|
||||
region.update({
|
||||
start: form.elements.start.value,
|
||||
end: form.elements.end.value,
|
||||
data: {
|
||||
note: form.elements.note.value
|
||||
}
|
||||
});
|
||||
form.style.opacity = 0;
|
||||
};
|
||||
form.onreset = function () {
|
||||
form.style.opacity = 0;
|
||||
form.dataset.region = null;
|
||||
};
|
||||
form.dataset.region = region.id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Display annotation.
|
||||
*/
|
||||
function showNote (region) {
|
||||
if (!showNote.el) {
|
||||
showNote.el = document.querySelector('#subtitle');
|
||||
}
|
||||
showNote.el.textContent = region.data.note || '–';
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind controls.
|
||||
*/
|
||||
GLOBAL_ACTIONS['delete-region'] = function () {
|
||||
var form = document.forms.edit;
|
||||
var regionId = form.dataset.region;
|
||||
if (regionId) {
|
||||
wavesurfer.regions.list[regionId].remove();
|
||||
form.reset();
|
||||
}
|
||||
};
|
||||
|
||||
GLOBAL_ACTIONS['export'] = function () {
|
||||
window.open('data:application/json;charset=utf-8,' +
|
||||
encodeURIComponent(localStorage.regions));
|
||||
};
|
||||
@@ -0,0 +1,260 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<title>wavesurfer.js | Annotation tool</title>
|
||||
|
||||
<link href="data:image/gif;" rel="icon" type="image/x-icon" />
|
||||
|
||||
<!-- Bootstrap -->
|
||||
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
|
||||
|
||||
<link rel="stylesheet" href="../css/style.css" />
|
||||
<link rel="stylesheet" href="../css/ribbon.css" />
|
||||
<link rel="stylesheet" href="app.css" />
|
||||
<link rel="screenshot" itemprop="screenshot" href="https://katspaugh.github.io/wavesurfer.js/example/screenshot.png" />
|
||||
|
||||
<!-- wavesurfer.js -->
|
||||
<script src="../../dist/wavesurfer.js"></script>
|
||||
|
||||
<!-- plugins -->
|
||||
<script src="../../dist/plugin/wavesurfer.timeline.js"></script>
|
||||
<script src="../../dist/plugin/wavesurfer.regions.js"></script>
|
||||
<script src="../../dist/plugin/wavesurfer.minimap.js"></script>
|
||||
|
||||
<!-- App -->
|
||||
<script src="../trivia.js"></script>
|
||||
<script src="app.js"></script>
|
||||
</head>
|
||||
|
||||
<body itemscope itemtype="http://schema.org/WebApplication">
|
||||
<div class="container">
|
||||
<div class="header">
|
||||
<ul class="nav nav-pills pull-right">
|
||||
<li><a href="/"><i class="glyphicon glyphicon-home"></i></a></li>
|
||||
</ul>
|
||||
|
||||
<h1 itemprop="name">wavesurfer.js Annotations Tool</h1>
|
||||
</div>
|
||||
|
||||
<div id="demo">
|
||||
<p id="subtitle" class="text-center text-info"> </p>
|
||||
|
||||
<div id="wave-timeline"></div>
|
||||
|
||||
<div id="waveform">
|
||||
<!-- Here be waveform -->
|
||||
</div>
|
||||
|
||||
<div class="row" style="margin: 30px 0">
|
||||
<div class="col-sm-8">
|
||||
<p>
|
||||
Click on a region to enter an annotation.<br />
|
||||
Shift-click plays a region in a loop.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div class="col-sm-2">
|
||||
<button class="btn btn-primary btn-block" data-action="play">
|
||||
<span id="play">
|
||||
<i class="glyphicon glyphicon-play"></i>
|
||||
Play
|
||||
</span>
|
||||
|
||||
<span id="pause" style="display: none">
|
||||
<i class="glyphicon glyphicon-pause"></i>
|
||||
Pause
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="col-sm-2">
|
||||
<button class="btn btn-info btn-block" data-action="export" title="Export annotations to JSON">
|
||||
<i class="glyphicon glyphicon-file"></i>
|
||||
Export
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form role="form" name="edit" style="opacity: 0; transition: opacity 300ms linear; margin: 30px 0;">
|
||||
<div class="form-group">
|
||||
<label for="start">Start</label>
|
||||
<input class="form-control" id="start" name="start" />
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="end">End</label>
|
||||
<input class="form-control" id="end" name="end" />
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="note">Note</label>
|
||||
<textarea id="note" class="form-control" rows="3" name="note"></textarea>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="btn btn-success btn-block">Save</button>
|
||||
<center><i>or</i></center>
|
||||
<button type="button" class="btn btn-danger btn-block" data-action="delete-region">Delete</button>
|
||||
</form>
|
||||
|
||||
<div class="row">
|
||||
<h4>Region events:</h4>
|
||||
|
||||
<ul>
|
||||
<li><code>region-in</code> – When playback enters a region. Callback will receive the <code>Region</code> object.</li>
|
||||
<li><code>region-out</code>– When playback leaves a region. Callback will receive the <code>Region</code> object.</li>
|
||||
<li><code>region-mouseenter</code> - When the mouse moves over a region. Callback will receive the <code>Region</code> object, and a <code>MouseEvent</code> object.</li>
|
||||
<li><code>region-mouseleave</code> - When the mouse leaves a region. Callback will receive the <code>Region</code> object, and a <code>MouseEvent</code> object.</li>
|
||||
<li><code>region-click</code> - When the mouse clicks on a region. Callback will receive the <code>Region</code> object, and a <code>MouseEvent</code> object.</li>
|
||||
<li><code>region-dblclick</code> - When the mouse double-clicks on a region. Callback will receive the <code>Region</code> object, and a <code>MouseEvent</code> object.</li>
|
||||
<li><code>region-created</code> – When a region is created. Callback will receive the <code>Region</code> object.</li>
|
||||
<li><code>region-updated</code> – When a region is updated. Callback will receive the <code>Region</code> object.</li>
|
||||
<li><code>region-update-end</code> – When dragging or resizing is finished. Callback will receive the <code>Region</code> object.</li>
|
||||
<li><code>region-removed</code> – When a region is removed. Callback will receive the <code>Region</code> object.</li>
|
||||
</ul>
|
||||
|
||||
<h4>Regions Plugin</h4>
|
||||
|
||||
<p>Regions are visual overlays on waveform that can be used to play and
|
||||
loop portions of audio. Regions can be dragged and resized.</p>
|
||||
|
||||
<p>Visual customization is possible via CSS (using the selectors
|
||||
<code>.wavesurfer-region</code> and <code>.wavesurfer-handle</code>).</p>
|
||||
|
||||
<p>To enable the plugin, add the script <code>plugin/wavesurfer.regions.js</code> to
|
||||
your page.</p>
|
||||
|
||||
<p>After doing that, use <code>wavesurfer.addRegion()</code> to create Region objects.</p>
|
||||
|
||||
<h5>Exposed Methods</h5>
|
||||
|
||||
<ul>
|
||||
<li><code>addRegion(options)</code> – Creates a region on the waveform. Returns a <code>Region</code> object. See <a href="#region-options">Region Options</a>, <a href="#region-methods">Region Methods</a> and <a href="#region-events">Region Events</a> below.
|
||||
|
||||
<ul>
|
||||
<li><strong>Note:</strong> You cannot add regions until the audio has finished loading, otherwise the <code>start:</code> and <code>end:</code> properties of the new region will be set to <code>0</code>, or an unexpected value.</li>
|
||||
</ul></li>
|
||||
<li><code>clearRegions()</code> – Removes all regions.</li>
|
||||
<li><code>enableDragSelection(options)</code> – Lets you create regions by selecting.
|
||||
areas of the waveform with mouse. <code>options</code> are Region objects' params (see <a href="#region-options">below</a>).</li>
|
||||
<li><code>disableDragSelection()</code> - Disables ability to create regions.</li>
|
||||
</ul>
|
||||
|
||||
<h5>Region Options</h5>
|
||||
|
||||
<table><thead>
|
||||
<tr>
|
||||
<th>option</th>
|
||||
<th>type</th>
|
||||
<th>default</th>
|
||||
<th>description</th>
|
||||
</tr>
|
||||
</thead><tbody>
|
||||
<tr>
|
||||
<td><code>id</code></td>
|
||||
<td>string</td>
|
||||
<td>random</td>
|
||||
<td>The id of the region.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>start</code></td>
|
||||
<td>float</td>
|
||||
<td><code>0</code></td>
|
||||
<td>The start position of the region (in seconds).</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>end</code></td>
|
||||
<td>float</td>
|
||||
<td><code>0</code></td>
|
||||
<td>The end position of the region (in seconds).</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>loop</code></td>
|
||||
<td>boolean</td>
|
||||
<td><code>false</code></td>
|
||||
<td>Whether to loop the region when played back.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>drag</code></td>
|
||||
<td>boolean</td>
|
||||
<td><code>true</code></td>
|
||||
<td>Allow/dissallow dragging the region.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>resize</code></td>
|
||||
<td>boolean</td>
|
||||
<td><code>true</code></td>
|
||||
<td>Allow/dissallow resizing the region.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><code>color</code></td>
|
||||
<td>string</td>
|
||||
<td><code>"rgba(0, 0, 0, 0.1)"</code></td>
|
||||
<td>HTML color code.</td>
|
||||
</tr>
|
||||
</tbody></table>
|
||||
|
||||
<h5>Region Methods</h5>
|
||||
|
||||
<ul>
|
||||
<li><code>remove()</code> - Remove the region object.</li>
|
||||
<li><code>update(options)</code> - Modify the settings of the region.</li>
|
||||
<li><code>play()</code> - Play the audio region from the start to end position.</li>
|
||||
</ul>
|
||||
|
||||
<h5>Region Events</h5>
|
||||
|
||||
<p>General events:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>in</code> - When playback enters the region.</li>
|
||||
<li><code>out</code> - When playback leaves the region.</li>
|
||||
<li><code>remove</code> - Happens just before the region is removed.</li>
|
||||
<li><code>update</code> - When the region's options are updated.</li>
|
||||
</ul>
|
||||
|
||||
<p>Mouse events:</p>
|
||||
|
||||
<ul>
|
||||
<li><code>click</code> - When the mouse clicks on the region. Callback will receive a <code>MouseEvent</code>.</li>
|
||||
<li><code>dblclick</code> - When the mouse double-clicks on the region. Callback will receive a <code>MouseEvent</code>.</li>
|
||||
<li><code>over</code> - When mouse moves over the region. Callback will receive a <code>MouseEvent</code>.</li>
|
||||
<li><code>leave</code> - When mouse leaves the region. Callback will receive a <code>MouseEvent</code>.</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
<div class="footer row">
|
||||
<div class="col-sm-12">
|
||||
<a rel="license" href="https://opensource.org/licenses/BSD-3-Clause"><img alt="BSD-3-Clause License" style="border-width:0" src="https://img.shields.io/badge/License-BSD%203--Clause-blue.svg" /></a>
|
||||
</div>
|
||||
|
||||
<div class="col-sm-8">
|
||||
<span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">wavesurfer.js</span> by <a href="https://github.com/katspaugh/wavesurfer.js">katspaugh</a> is licensed under a <a rel="license" href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause License</a>.
|
||||
</div>
|
||||
|
||||
<div class="col-sm-4">
|
||||
<p>
|
||||
The sound file is from <a href="https://librivox.org/librivox-multilingual-short-works-collection-001-by-various/">librivox.org</a>.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="github-fork-ribbon-wrapper right">
|
||||
<div class="github-fork-ribbon">
|
||||
<a itemprop="isBasedOnUrl" href="https://github.com/katspaugh/wavesurfer.js">Fork me on GitHub</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
|
||||
ga('create', 'UA-50026819-1', 'wavesurfer.fm');
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user