Skip to content

Instantly share code, notes, and snippets.

@hibuno
Last active November 11, 2017 02:23
Show Gist options
  • Save hibuno/63266aa79268179cd12f51879d107a66 to your computer and use it in GitHub Desktop.
Save hibuno/63266aa79268179cd12f51879d107a66 to your computer and use it in GitHub Desktop.
Script for Audio Context Tutorial at my medium account (https://medium.com/@muhibbudins)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SoundWave</title>
<style>
canvas {
background-color: #101010;
width: 600px;
height: 300px;
transform: scale(-1, -1);
}
audio {
width: 600px;
}
</style>
</head>
<body>
<div class="wrapper">
<canvas id="visualizer"></canvas>
<audio src="" id="player" controls="true"></audio>
<div>Choose Song : <input type="file" id="source"></div>
<div>Song Name : <span id="song_name"></span></div>
<div>Format : <span id="song_format"></span></div>
</div>
<script type="text/javascript">
const
source = document.getElementById('source'),
player = document.getElementById('player'),
canvas = document.getElementById('visualizer'),
name = document.getElementById('song_name'),
format = document.getElementById('song_format'),
blob = window.URL || window.webkitURL,
color = ['#1abc9c','#2ecc71','#3498db','#9b59b6','#34495e','#16a085','#27ae60','#2980b9','#8e44ad'];
// Check if blob is exist in this browser
if (!blob) {
console.warn('Your browser does not support Blob URLs.');
}
// Load song to player
source.addEventListener('change', (e) => {
let
file = e.target.files[0],
fileURL = blob.createObjectURL(file);
name.innerHTML = file.name;
format.innerHTML = file.type;
player.src = fileURL;
player.play();
stream();
});
function stream() {
const
audio = new(window.AudioContext || window.webkitAudioContext)(),
analyser = audio.createAnalyser();
player.addEventListener('canplay', (e) => {
let source = audio.createMediaElementSource(player);
source.connect(analyser);
analyser.connect(audio.destination);
analyser.fftSize = 128;
frequencyLength = analyser.frequencyBinCount;
let frequency = new Uint8Array(frequencyLength);
analyser.getByteFrequencyData(frequency);
// console.log(frequency)
// Prepare Canvas
const context = canvas.getContext('2d');
// Draw wave
function draw() {
requestAnimationFrame(draw);
analyser.getByteFrequencyData(frequency);
// console.log(frequency)
context.fillStyle = '#101010';
context.fillRect(0, 0, canvas.width, canvas.height);
context.lineWidth = 1.5;
context.strokeStyle = colorize();
context.beginPath();
var sliceWidth = canvas.width * 1.0 / frequencyLength;
var x = 0;
for (var i = 0; i < frequencyLength; i++) {
var v = frequency[i] / 256.0;
var y = v * canvas.height / 2;
if (i === 0) {
context.moveTo(x, y);
} else {
context.lineTo(x, y);
}
x += sliceWidth;
}
context.lineTo(canvas.width, canvas.height / 2);
context.stroke();
};
draw();
});
};
// Create Random Color
function colorize() {
return color[Math.round((Math.random() * 10))] || '#ffffff';
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment