cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <a href="https://www.soundcloud.com/" id="soundcloud-logo">
<img width=104 height=32 src="https://developers.soundcloud.com/assets/powered_by_white-371bd6967352fcc89673d4c81f7e5661.png" />
</a>
<a href="https://www.soundcloud.com/" id="soundcloud-logo-dark">
<img width=104 height=32 src="https://developers.soundcloud.com/assets/powered_by_black-4339b4c3c9cf88da9bfb15a16c4f6914.png" />
</a>
<div id="links"><a href="https://twitter.com/simon_bq" id="twitterhandle">@simon_bq</a> <a href="http://www.simonbergqvist.com">simonbergqvist.com</a></div>

<div id="container">
<div id="track-info" class="invisible">
<h3 id="track-owner">Artist<h3>	
<h1 id="track-title">Title</h1>
</div>
	
<form id="track-form">
<a href="https://soundcloud.com" id="track-url"><img width=64 height=43 id="track-soundcloud" src="https://developers.soundcloud.com/assets/logo_big_white-65c2b096da68dd533db18b9f07d14054.png" /></a>
<input id="track-input" placeholder="https://soundcloud.com/user/track" value="https://soundcloud.com/kilowatts/07-kilowatts-living-stones"></input>
<button type="submit">GO</button>
</form>
<div id="explain">Enter a highly dynamic and punchy SoundCloud track. Press GO.</div>
<audio id="audio-player" controls crossorigin="anonymous"></audio>
</div>
<div id="psychoacoustics">P S Y C H O A C O U S T I C S_1</div>
<canvas id="canvas"></canvas>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Dosis:500);

body{
	font-family: 'Dosis', sans-serif;
}

#psychoacoustics{
	position: absolute;
	line-height: 0;
	padding: 0;
	margin: 0;
	width: 80%;
	top: 50%;
	text-align: center;
	font-size: 3em;
	z-index: 0;
	color: black;
	text-shadow: 0px 0px 8px rgba(0,0,0,0.4);
}

#canvas{
	background: #EEEEEE;
  width: 100%;
  height: 100%;
  display: block;
  left: 0;
  top: 0;
  position: absolute;
  z-index: -1;
	transition: background 400ms ease;
}

#soundcloud-logo {
	position: absolute;
	top: 5px;
	left: 5px;
}
#soundcloud-logo-dark {
	position: absolute;
	top: 5px;
	left: 5px;
	opacity: 1;
	transition: opacity 400ms ease;
}
#links{
	position: absolute;
	top: 18px;
	left: 120px;
	color: black;
	transition: color 400ms ease;
}
#links a{
	color: inherit;
}

#audio-player {
	margin: auto;
  width: 100%;
	height: 50px;
	display: block;
	opacity: 0;
	transition: opacity 400ms ease;
}
#explain{
	padding-left: 74px;
	padding-top: 10px;
	font-size: 1em;
	transition: opacity 400ms ease;
	opacity: 1;
	text-shadow: 1px 1px 1px rgba(255,255,255,0.8);
}

#track-url {
	float: left;
}

#track-soundcloud {
	background: rgba(0,0,0,0.15);
}

#track-input{
	display: inline-block;
	float: left;
	width: 676px;
	height: 43px;
	font-size: 1.2em;
	padding: 0;
	padding-left: 8px;
	padding-right: 8px;
	background: rgba(0,0,0,0.15);
	color: white;
	border: none;
	transition: background ease 200ms;
	text-shadow: 1px 1px 1px rgba(0,0,0,0.6);
}
#track-input:hover{
	background: rgba(0,0,0,0.4);
	transition: background ease 200ms;
}

#track-form {
	margin: auto;
	width: 100%;
	height: 43px;
	display: block;
}

#track-form button{
	float: right;
	display: inline;
	height: 43px;
	width: 43px;
	color: #212121;
	font-weight: bolder;
	background-color: rgba(255,255,255,0.6);
	font-family: 'Dosis', sans-serif;
	font-size: 1.6em;
	border: 0;
	box-shadow: 0 0 0px white;
	transition: box-shadow 200ms ease;
	transition: background 200ms ease;
	transition: color 200ms ease;
}
#track-form button:hover{
	box-shadow: 0 0 5px white;
	transition: box-shadow 200ms ease;
	background-color: rgba(255,255,255,1);
	transition: background 200ms ease;
	transition: color 200ms ease;
}
#track-form button:active{
	color: white;
	box-shadow: 0;
	background-color: rgba(33,33,33,1);
	transition: background 200ms ease;
	transition: color 200ms ease;
}

#track-info {
	width: 100%;
	text-align: center;
	color: white;
	opacity: 0;
	transition: opacity 400ms ease;
	display: block;
	font-family: "Trebuchet MS", Helvetica, sans-serif;
}
#track-info h1, h2, h3{
	font-weight: normal;
}
h1{
	font-size: 2em;
	line-height: 160%;
}
h2{
	font-size: 1.6em;
}
h3{
	font-size: 1.4em; 
}

#container{
	width: 800px;
	margin: auto;
	margin-top: 20px;
}
            
          
!
            
              /*
Copyright Simon Bergqvist 2016. 
*/

audioCtx = new (window.AudioContext || window.webkitAudioContext);

var audioSource;
var audioController;
var visualizer;
var ui;

var autoplay = false;

var clientId = "db05ee5c4d063940bbcb7140f37eb712";

var timeLastFrame = Date.now();
var now = Date.now();
var dt = 0;
var timeElapsed = 0;

var mouseX = canvas.width/2;
var mouseY = canvas.height/2;

var AudioController = function(_player){
	this.player = _player;
	this.audioSource = audioCtx.createMediaElementSource(this.player);
  this.analyser = audioCtx.createAnalyser();
  this.analyser.fftSize = 256;
	//this.analyser.maxDecibels = 0;
	this.analyser.smoothingTimeConstant = 0.9;
  this.frequencyData = new Uint8Array(this.analyser.frequencyBinCount);
	this.audioSource.connect(this.analyser);
	this.analyser.connect(audioCtx.destination);
	
  this.setTrack = function(track){
		this.player.pause();
		this.player.load();
    this.player.setAttribute("src", track.streamUrl + "?client_id=" + clientId);
		this.player.oncanplaythrough = this.player.play();
	};
	
	this.update = function(){
		this.analyser.getByteFrequencyData(this.frequencyData);
	}
}
var Track = function(title, owner, ownerUrl, streamUrl, permanentUrl){
  this.title = title;
  this.owner = owner;
  this.ownerUrl = ownerUrl;
  this.streamUrl = streamUrl;
  this.permanentUrl = permanentUrl;
}

var SoundcloudSource = function(){
  this.currentTrack = "";
  this.loadTrack = function(trackUrl, callback){
    SC.resolve(trackUrl).then(function(track){
			if(track.errors){
				var errorMessage = "";
				for(var i = 0; i < track.errors.length; i++){
					errorMessage += track.errors[i];
				}
				console.log(errorMessage);
			}
      this.currentTrack = new Track(track.title, track.user.username, track.user.permalink_url, track.stream_url, track.permalink_url);
      if(typeof callback==="function"){
        callback(this.currentTrack);
      }
    });
  };
}

var UI = function(){
  this.artistText = document.getElementById("track-owner");
	this.titleText = document.getElementById("track-title");
	this.trackUrl = document.getElementById("track-url");
	this.trackInfo = document.getElementById("track-info");
	this.soundcloudLogoDark = document.getElementById("soundcloud-logo-dark");
	this.explain = document.getElementById("explain");
	this.links = document.getElementById("links");
	
	this.setTrackInfo = function(track){
		this.titleText.innerHTML = track.title;
		this.artistText.innerHTML = track.owner;
		this.trackUrl.href = track.permanentUrl;
		this.trackUrl.style.opacity = 1;
		this.trackInfo.style.opacity = 1;
		audioController.player.style.opacity = 1;
		this.soundcloudLogoDark.style.opacity = 0;
		this.explain.style.opacity = 0;
		this.links.style.color = "white";
	}
	
	this.draw = function(){
	}
}

var Visualizer = function(){
  this.canvas = document.getElementById("canvas");
  this.ctx = this.canvas.getContext("2d");
	this.initialized = false;
	
	this.binsUsed = audioController.analyser.frequencyBinCount - (audioController.analyser.frequencyBinCount/4);
	this.verticalAmplitude = 150;
	this.sinSpeed = 2;
	this.movementSpeed = 4;
	this.glowSize = 0;
	this.lineWidth = 1.7;
  
	this.useMouse = false;
	
	var elements = [];
	var elementCount = 128;
	if(audioController.analyser.frequencyBinCount > 0){
		elementCount = audioController.analyser.frequencyBinCount;
	}
	for(var i = 0; i < elementCount; i++){
		elements.push({
			x: canvas.width/2,
			y: canvas.height/2,
			targetX: canvas.width/2,
			targetY: canvas.height/2,
			radius: 50,
			style: "#212121",
			glowColor: "#212121"
		})
	}
	
  this.update = function(){
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;
		for(var i = 0; i < elements.length; i++){
			var currentMovementSpeed = this.movementSpeed;
			var currentAmplitude = this.verticalAmplitude;
			elements[i].targetX = 0;
			elements[i].targetY = 0;
			if(this.useMouse){
				currentMovementSpeed = this.movementSpeed - ((this.movementSpeed/audioController.analyser.frequencyBinCount)*i);
				currentAmplitude = (currentAmplitude/audioController.analyser.frequencyBinCount) * i;
				elements[i].targetX += mouseX;
				elements[i].targetY += mouseY;
			}else{
				elements[i].targetX += (canvas.width / this.binsUsed) * i;
				elements[i].targetY += canvas.height/2;
			}
			
			elements[i].targetY += currentAmplitude * Math.sin((Math.PI*4 / this.binsUsed) * i - timeElapsed * this.sinSpeed);
			//offsetY = currentAmplitude * Math.sin((Math.PI/canvas.height)*offsetY + timeElapsed*this.verticalSpeed);
			
			//Move towards target position
			var dx = elements[i].targetX - elements[i].x;
			var dy = elements[i].targetY - elements[i].y;
			elements[i].x += currentMovementSpeed * dx * dt;
			elements[i].y += currentMovementSpeed * dy * dt;
			
			if(this.initialized){
				if(audioController.analyser.frequencyBinCount > 0){
					elements[i].radius = audioController.frequencyData[i];
					elements[i].glowColor = "hsla(" + (audioController.frequencyData[i]/255)*360 + ", 75%, 50%, 1)";
					elements[i].style = "hsla(" + (audioController.frequencyData[i]/255)*360 + ", 75%, 50%, 1)";
				}else{
					elements[i].style = "#EEEEEE";
				}
			}
		}
  };
	
  this.draw = function(){
    //this.ctx.clearRect(0, 0, canvas.width, canvas.height);
		
		var shadowGradient = this.ctx.createLinearGradient(0,0,0,canvas.height);
		shadowGradient.addColorStop(0.0, "rgba(0,0,0,0.2)");
		shadowGradient.addColorStop(0.2, "rgba(0,0,0,0)");
		this.ctx.fillStyle = shadowGradient;
		this.ctx.fillRect(0,0,canvas.width, canvas.height);
		
		
		//draw from backwards to give illusion of depth
		for(var i = this.binsUsed; i > 0; i--){
			this.ctx.beginPath();
			if(this.glowSize > 0){
			this.ctx.shadowBlur = this.glowSize;
			this.ctx.shadowColor = elements[i].glowColor;
			}
				
			this.ctx.strokeStyle = elements[i].style;
			this.ctx.lineWidth = this.lineWidth;
			this.ctx.arc(elements[i].x, elements[i].y, elements[i].radius, 0, 2*Math.PI);
			this.ctx.stroke();
		}
  };
}

function Loop(){
	timeLastFrame = now;
	now = Date.now();
	dt = (now-timeLastFrame)/1000;
	timeElapsed += dt;
	
	requestAnimationFrame(Loop);
	audioController.update();
	visualizer.update();
	ui.draw();
	visualizer.draw();
}

function Initialize(){
  SC.initialize({
    client_id: "db05ee5c4d063940bbcb7140f37eb712",
  });
  var audioPlayer = document.getElementById("audio-player");
	
  audioSource = new SoundcloudSource();
  audioController = new AudioController(audioPlayer);
  visualizer = new Visualizer();
	ui = new UI();
	
	var gui = new dat.GUI();
	gui.add(visualizer, "verticalAmplitude", 0, 800);
	gui.add(visualizer, "sinSpeed", -10, 10);
	gui.add(visualizer, "useMouse", false, true);
	gui.add(visualizer, "movementSpeed");
	gui.add(audioController.analyser, "smoothingTimeConstant", 0, 1);
	gui.add(visualizer, "glowSize", 0, 5);
	gui.add(visualizer, "lineWidth", 1, 3);
	
	window.addEventListener("keydown", OnKeyDown);		
	window.addEventListener("mousemove", OnMouseMove);
	var trackForm = document.getElementById("track-form");
	trackForm.addEventListener("submit", SubmitTrack, false);
	requestAnimationFrame(Loop);
}

function SubmitTrack(e){
	//prevent default submission
	e.preventDefault();
	
	var input = document.getElementById("track-input");
	LoadTrack(input.value);
}

function LoadTrack(url){
	audioSource.loadTrack(url, function(track){
		canvas.style.background = "#212121";
		visualizer.initialized = true;
		ui.setTrackInfo(track);
		audioController.setTrack(track);
	});
}

function OnKeyDown(e){
	if(e.keyCode==32){
	}
}

function OnMouseMove(e){
  mouseX = e.pageX;
  mouseY = e.pageY;
}

function PrintFrequencyData(){
	console.log(audioController.frequencyData);
}

Initialize();
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console