view src/app/waveform/waveform.component.ts @ 18:dcfcba8fde0a

Uncomment initial rendering code for waveform.
author Lucas Thompson <dev@lucas.im>
date Thu, 27 Oct 2016 17:48:23 +0100
parents ff964b28a272
children aabfa7a693dc
line wrap: on
line source
import {
  Component, OnInit, ViewChild, ElementRef,
  AfterViewInit, OnChanges, SimpleChanges, Input
} from '@angular/core';

declare var wavesUI: any; // TODO non-global app scope import

@Component({
  selector: 'app-waveform',
  templateUrl: './waveform.component.html',
  styleUrls: ['./waveform.component.css']
})
export class WaveformComponent implements OnInit, AfterViewInit, OnChanges {
  @ViewChild('track') trackDiv: ElementRef;

  private _audioBuffer: AudioBuffer = undefined;

  @Input()
  set audioBuffer(buffer: AudioBuffer) {
    this._audioBuffer = buffer || undefined;
  }

  get audioBuffer(): AudioBuffer {
    return this._audioBuffer;
  }



  private timeline: any; // TODO what type is it?

  constructor() {
  }

  ngOnInit() {}

  ngAfterViewInit(): void {
    const track: HTMLElement = this.trackDiv.nativeElement;
    const duration: number = 1.0;
    const height: number = track.getBoundingClientRect().height;
    const width: number = track.getBoundingClientRect().width;
    const pixelsPerSecond = width / duration;
    const timeline = new wavesUI.core.Timeline(pixelsPerSecond, width);
    timeline.createTrack(track, height, 'main');

    // time axis
    const timeAxis = new wavesUI.helpers.TimeAxisLayer({
      height: height,
      top: 10,
      color: 'gray'
    });

    timeline.addLayer(timeAxis, 'main', 'default', true);
    timeline.state = new wavesUI.states.CenteredZoomState(timeline);
  }

  ngOnChanges(changes: SimpleChanges): void {
    if (changes.hasOwnProperty("audioBuffer")) { // why wouldn't it?
      if (changes["audioBuffer"].currentValue)
        this.renderWaveform(changes["audioBuffer"].currentValue);
    }
  }

  renderWaveform(buffer: AudioBuffer) {
    // TODO reduce dupe from original timeline state, anyway to actually not instantiate new timeline?
    const track: HTMLElement = this.trackDiv.nativeElement;
    track.innerHTML = "";
    const duration: number = buffer.duration;
    const height: number = track.getBoundingClientRect().height;
    const width: number = track.getBoundingClientRect().width;
    const pixelsPerSecond = width / duration;
    const timeline = new wavesUI.core.Timeline(pixelsPerSecond, width);
    timeline.createTrack(track, height, 'main');

    // time axis
    const timeAxis = new wavesUI.helpers.TimeAxisLayer({
      height: height,
      top: 10,
      color: 'gray'
    });

    timeline.addLayer(timeAxis, 'main', 'default', true);
    timeline.state = new wavesUI.states.CenteredZoomState(timeline);
    // TODO dispose of the existing layer?
    const waveformLayer = new wavesUI.helpers.WaveformLayer(buffer, {
      height: 600,
      color: 'darkblue'
    });
    timeline.addLayer(waveformLayer, 'main');
  }

}