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 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.

Quick-add: + add another resource

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.

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.

            
              html,body,canvas{
  width: 100%;
  height: 100%;
  padding:0;
  margin:0;
  overflow: hidden;
  background-color: black;
}
            
          
!
            
              $(function(){
  class MusicPayer {
    private animationId:number;
    private audioCtx:{} = new AudioContext();
    private analyser:{} = this.audioCtx.createAnalyser();
    private source:{};
    private visualizer:Visualizer;
    private isPlaying:boolean = false;
    private title:string;
    constructor(fftSize:number) {
      this.analyser.fftSize = fftSize;
      this.analyser.connect(this.audioCtx.destination);
      this.visualizer = new Visualizer(this.analyser.frequencyBinCount, 10);
    }
    setMusicTitle(musicTitle:string):void {
      this.title = musicTitle;
    }
    setMusic(musicData:{}):void {
      var self = this;
      this.audioCtx.decodeAudioData(musicData, function(buffer) {
        if(self.isPlaying){
          self.stop();
        }
        $("title").text(self.title);
        self.source = self.audioCtx.createBufferSource();
        self.source.buffer = buffer;
        self.source.loop = true;
        self.source.connect(self.analyser);
        self.source.start(0);
        self.isPlaying = true;
        self.animationId = requestAnimationFrame(function() {
          self.render();
        });
      })
      
    }
    private stop():void {
      var self = this;
      this.source.stop();
      cancelAnimationFrame(self.animationId);
    }
    private render():void{
      var self = this;
      var spectrums = new Uint8Array(this.analyser.frequencyBinCount);
      this.analyser.getByteFrequencyData(spectrums);
      this.visualizer.update(spectrums, function() {
        self.animationId = requestAnimationFrame(function() {
          self.render();
        });
      });
    }
  }
  class Visualizer{
    private ctx:{} = $("#canvas")
      .get(0)
      .getContext("2d")
    ;
    private width:number;
    private graphWidth:number;
    private height:number = 255;
    private graphCount:number;
    constructor(graphCount:number, graphWidth:number){
      this.graphCount = graphCount;
      this.width = graphCount * graphWidth;
      this.graphWidth = graphWidth;
      var self = this;
      $("#canvas").attr({
        "width":self.width,
        "height":self.height
      });
    }
    update(data:Uint8Array, callBack:Function):void {
      this.ctx.clearRect(0, 0, this.width, this.height);
      for(let i = 0;i < this.graphCount;i++){
        this.ctx.fillStyle = "hsl(" + (360 / this.graphCount * i) + ", 100%, 50%)";
        this.ctx.fillRect(
          i * this.graphWidth,
          this.height - data[i],
          this.graphWidth,
          this.height
        );
      }
      callBack();
    }
  }
  var musicPlayer = new MusicPayer(2048);
  var $canvas = $("#canvas");
  var cancelEvent = function(event) {
    event.preventDefault();
    event.stopPropagation();
    return false;
  }
  $canvas
    .bind("dragenter", cancelEvent)
    .bind("dragover", cancelEvent)
    .bind("drop", function(event) {
      var file = event.originalEvent.dataTransfer.files[0];
      musicPlayer.setMusicTitle(file.name);
      var fileReader = new FileReader();
      fileReader.onload = function() {
        musicPlayer.setMusic(fileReader.result);
      }
      fileReader.readAsArrayBuffer(file);
      cancelEvent(event);
    })
  ;
})
            
          
!
999px
Loading ..................

Console