Skip to content

Instantly share code, notes, and snippets.

@fstanis
Last active March 7, 2023 01:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fstanis/bc720cf8e9446314ace9edcbcb3a122e to your computer and use it in GitHub Desktop.
Save fstanis/bc720cf8e9446314ace9edcbcb3a122e to your computer and use it in GitHub Desktop.
Demo on creating SVG waveforms from an audio file.
<!--
Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Audio Visualizer</title>
<style>
body {
margin: 0;
}
#drop {
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
}
</style>
</head>
<body>
<div id="dummy"></div>
<a id="download" href="#" download="waveform.svg" hidden>Download</a>
<div id="drop" ondrop="dropHandler(event)" ondragover="event.preventDefault()">
<div>Drag and drop here!</div>
</div>
<script src="https://unpkg.com/wavesurfer.js"></script>
<script src="https://cdn.jsdelivr.net/gh/gliffy/canvas2svg/canvas2svg.js"></script>
<script>
async function dropHandler(event) {
event.preventDefault();
const items = event.dataTransfer.items;
if (items && items.length >= 1 && items[0].kind === 'file') {
const file = items[0].getAsFile();
triggerDownload(await audioToSvg(file));
}
}
function audioToSvg(file) {
return new Promise(resolve => {
const canvas = new C2S('100%', '100%');
canvas.style = {};
HTMLCanvasElement.prototype.getContext = () => canvas;
const wavesurfer = WaveSurfer.create({
container: '#dummy',
maxCanvasWidth: 100000
});
wavesurfer.on('ready', () => {
canvas.save();
requestIdleCallback(() => resolve(canvas.getSerializedSvg(true)));
});
wavesurfer.loadBlob(file);
});
}
function triggerDownload(svg) {
const blob = new Blob([svg], {type: "image/svg"});
const url = URL.createObjectURL(blob);
const download = document.querySelector('#download');
download.href = url;
download.click();
URL.revokeObjectURL(url);
}
</script>
</body>
</html>
@iftkharhussain
Copy link

Thanks brother, it is perfect you saved my time. 👍

@iftkharhussain
Copy link

Hi, i need your help to modify it,
actually i want to change some options of wavesurfer

var waveOptions = { container: '#dummy', maxCanvasWidth: 100000, barWidth: 1, hideScrollbar: true, height: 90, cursorWidth: 0, minPxPerSec: 2, responsive: true, waveColor: '#000000', barGap: 1 }

it is my waveOptions for waveSurfer.js and 'canvas2svg' lib leads to error https://i.imgur.com/niXk1iO.png

@fstanis
Copy link
Author

fstanis commented Oct 6, 2020

Seems like it still works though? These errors should be safe to ignore.

@iftkharhussain
Copy link

its works but downloaded SVG is different than the actual wavesurfer canvas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment