123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

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.

            
              <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>
            
          
!
            
              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%
		

            
          
!
            
              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 audio_ctx = new (window.AudioContext || window.webkitAudioContext)()

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

const audio_analyser = audio_ctx.createAnalyser()

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 cor = 0

const bufferSize = audio_analyser.frequencyBinCount
const dataArray = new Uint8Array(bufferSize)

audio_analyser.fftSize = 4096;

audio_player_src.connect(audio_ctx.destination)
audio_player_src.connect(audio_analyser)
function render_progress(){
	
	ctx_progress.clearRect(0, 0, progress.width, progress.height)
	
	const channels = audio_src.buffer.numberOfChannels
	const steps = progress.width
	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 d = 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 => {
	
	audio_src = audio_ctx.createBufferSource()
		
	audio_ctx.decodeAudioData(fr.result, (buffer) => {
		
		audio_src.buffer = buffer
		audio.play()
		
		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 * .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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console