Tone.js/examples/meter.html

109 lines
2.7 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Meter</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link rel="icon" type="image/png" sizes="174x174" href="./style/favicon.png">
<script src="../build/Tone.js"></script>
<script src="./scripts/jquery.min.js"></script>
<script src="./scripts/draggabilly.js"></script>
<script src="https://tonejs.github.io/Logo/build/Logo.js"></script>
<script src="./scripts/StartAudioContext.js"></script>
<script src="./scripts/Interface.js"></script>
<link rel="stylesheet" type="text/css" href="./style/examples.css">
<script>
// jshint ignore: start
</script>
<style type="text/css">
canvas {
margin-top: 2px;
width: 100%;
height: 90px;
border-radius: 10px;
}
</style>
</head>
<body>
<div id="Content" class="FullScreen">
<div id="Title">Meter</div>
<div id="Explanation">
<a href="https://tonejs.github.io/docs/#Meter" target="_blank">Tone.Meter</a>
gives you the level of the incoming signal in decibels.
</div>
</div>
<script>
//create a level meter
var meter = new Tone.Meter();
var player = new Tone.Player({
"url" : "./audio/FWDL.[mp3|ogg]",
"loop" : true
}).connect(meter).toMaster();
// GUI //
//start button
Interface.Button({
key : 32,
type : "toggle",
text : "Start",
activeText : "Stop",
start : function(){
player.start();
},
end : function(){
player.stop();
}
});
//drawing the FFT
var meterContext = $("<canvas>",{
"id" : "fft"
}).appendTo("#Content").get(0).getContext("2d");
var meterGraident;
function drawMeter(){
var level = meter.getLevel();
level = Tone.dbToGain(level); //scale it between 0 - 1
meterContext.clearRect(0, 0, canvasWidth, canvasHeight);
meterContext.fillStyle = meterGraident;
meterContext.fillRect(0, 0, canvasWidth, canvasHeight);
meterContext.fillStyle = "white";
meterContext.fillRect(canvasWidth * level, 0, canvasWidth, canvasHeight);
}
//size the canvase
var canvasWidth, canvasHeight;
function sizeCanvases(){
canvasWidth = $("#fft").width();
canvasHeight = $("#fft").height();
meterContext.canvas.width = canvasWidth;
meterContext.canvas.height = canvasHeight;
//make the gradient
meterGraident = meterContext.createLinearGradient(0, 0, canvasWidth, canvasHeight);
meterGraident.addColorStop(0, "#BFFF02");
meterGraident.addColorStop(0.8, "#02FF24");
meterGraident.addColorStop(1, "#FF0202");
}
sizeCanvases();
$(window).resize(sizeCanvases);
function loop(){
requestAnimationFrame(loop);
//draw the meter level
drawMeter();
}
loop();
</script>
</body>
</html>