Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <div id="player">
	<input type="file" class="file" accept="audio/*">
	<br>
	<canvas width="1200" height="128" class="progress"></canvas>
	<canvas width="1200" height="240" class="view"></canvas>
	<audio controls class="audio"></audio>
</div>
              
            
!

CSS

              
                html, body
	background #000
	font 15px Arial, sans-serif
	color #EEE
	height 100%
	width 100%
	
body
	display flex
	justify-content center
	align-items center
	
#player
	.progress, .view, .audio
		display block
	
	.audio
		width 100%
		

              
            
!

JS

              
                const $player = document.getElementById("player");
const file = $player.querySelector(".file");
const progress = $player.querySelector(".progress");
const view = $player.querySelector(".view");
const audio = $player.querySelector(".audio");

const fr = new FileReader();

const ctx_progress = progress.getContext("2d");
const ctx_view = view.getContext("2d");

const channel_colors = ["#0AF", "#F0A"];
const line_colors = ["#F0A", "#FA0", "#AF0", "#A0F", "#0FA", "#0AF"];

let audio_analyser, dataArray, bufferSize, audio_src, cor = 0;

function render_progress() {
	ctx_progress.clearRect(0, 0, progress.width, progress.height);

	const channels = audio_src.buffer.numberOfChannels;
	const steps = progress.width * 2;
	const cursor_width = 1;
	const bar_width = progress.width / steps;
	const max_height = progress.height / channels;
	const v_total = audio.duration || 0;
	const v_played = audio.currentTime || 0;
	const v_percent = v_played / v_total;

	for (let channel = 0; channel < channels; channel++) {
		const channel_data = audio_src.buffer.getChannelData(channel);
		const data_steps = Math.round(channel_data.length / steps);

		for (let b = 0, b = 0; d < channel_data.length; d += data_steps) {
			const x = bar_width * b;

			const bar_height = channel_data[d] * max_height;
			const color = channel_colors[channel];
			let y = max_height / 2 - bar_height / 2;

			y += max_height * channel;

			ctx_progress.fillStyle =
				v_percent * steps > b ? color : "rgba(100,100,100,.15)";
			ctx_progress.fillRect(x, y, bar_width, bar_height);
			b++;
		}

		ctx_progress.fillStyle = "rgba(100,100,100,.25)";
		ctx_progress.fillRect(
			v_percent * progress.width - cursor_width / 2,
			0,
			cursor_width,
			progress.height
		);
	}
}

fr.addEventListener("load", p => {
	/////
	const audio_ctx = new (window.AudioContext || window.webkitAudioContext)();

	let audio_player_src = audio_ctx.createMediaElementSource(audio);
	audio_src = audio_ctx.createBufferSource();

	audio_analyser = audio_ctx.createAnalyser();
	
	bufferSize = audio_analyser.frequencyBinCount;
	dataArray = new Uint8Array(bufferSize);

	audio_analyser.fftSize = 4096;

	audio_player_src.connect(audio_ctx.destination);
	audio_player_src.connect(audio_analyser);
	/////

	audio_src = audio_ctx.createBufferSource();

	audio_ctx.decodeAudioData(fr.result, buffer => {
		audio_src.buffer = buffer;

		render();
	});
});

file.addEventListener("change", () => {
	const fileData = file.files[0];
	audio.src = URL.createObjectURL(fileData);
	audio.load();
	fr.readAsArrayBuffer(fileData);
});

function render() {
	ctx_view.clearRect(0, 0, view.width, view.height);
	audio_analyser.getByteFrequencyData(dataArray);

	const bars = 128;
	const max_h = view.height * 0.75;
	const f_step = bufferSize / bars;
	const center_y = max_h / 2;
	let curr_step = 0;

	ctx_view.fillStyle = ctx_view.strokeStyle = line_colors[cor];
	ctx_view.lineWidth = 4;

	ctx_view.beginPath();

	const getData = step => {
		const index = step * f_step;
		const data = dataArray[index] || 0;
		const w = view.width / bars;
		const h = data / 128 * max_h;
		const x = w * step + w / 2;
		const y = view.height - h - 4;

		return { x, y, w, h };
	};

	let avg = 0;
	ctx_view.moveTo(0, view.height);

	for (let i = 0; i < bufferSize; i++) {
		if (i % f_step == 0) {
			const p1 = getData(curr_step);
			const p2 = getData(curr_step < bars ? curr_step + 1 : curr_step);

			// ctx_view.lineTo(p2.x, p2.y)
			ctx_view.fillRect(p1.x, p1.y, p1.w, p1.h);

			curr_step++;
		}

		avg += dataArray[i];
	}

	ctx_view.lineCap = "butt";
	ctx_view.lineWidth = 5;
	ctx_view.stroke();

	avg /= bufferSize;

	// Minha tentativa
	if (avg > 105) {
		cor = cor + 1 < line_colors.length ? cor + 1 : 0;
	}

	render_progress();

	if (audio_src.buffer) requestAnimationFrame(render);
}

              
            
!
999px

Console