annotate src/app/visualisations/waveform/waveform.component.ts @ 355:335bb6459fe8

There's nothing waveform specific about these, they'll likely be used for other waves-ui components.
author Lucas Thompson <dev@lucas.im>
date Fri, 26 May 2017 18:20:02 +0100
parents a9ce5516c17d
children b852bace6b14
rev   line source
dev@347 1 import {
dev@347 2 Component,
dev@354 3 Input,
dev@354 4 ChangeDetectorRef,
dev@347 5 ElementRef,
dev@354 6 ViewChild
dev@347 7 } from '@angular/core';
dev@347 8 import wavesUI from 'waves-ui-piper';
dev@354 9 import {WavesComponent} from '../waves-base.component';
dev@347 10
dev@347 11
dev@347 12 @Component({
dev@347 13 selector: 'ugly-waveform',
dev@355 14 templateUrl: '../waves-template.html',
dev@355 15 styleUrls: ['../waves-template.css']
dev@347 16 })
dev@354 17 export class WaveformComponent extends WavesComponent {
dev@347 18 @ViewChild('track') trackDiv: ElementRef;
dev@347 19 @Input() set audioBuffer(buffer: AudioBuffer) {
dev@347 20 this._audioBuffer = buffer || undefined;
dev@347 21 if (this.audioBuffer) {
dev@347 22 this.renderWaveform(this.audioBuffer);
dev@347 23 }
dev@347 24 }
dev@347 25
dev@347 26 get audioBuffer(): AudioBuffer {
dev@347 27 return this._audioBuffer;
dev@347 28 }
dev@347 29
dev@347 30 private _audioBuffer: AudioBuffer;
dev@347 31
dev@347 32 constructor(private ref: ChangeDetectorRef) {
dev@354 33 super();
dev@347 34 }
dev@347 35
dev@347 36 renderWaveform(buffer: AudioBuffer): void {
dev@347 37 const height = this.trackDiv.nativeElement.getBoundingClientRect().height;
dev@347 38 if (this.timeline && this.waveTrack) {
dev@347 39 // resize
dev@347 40 const width = this.trackDiv.nativeElement.getBoundingClientRect().width;
dev@347 41
dev@347 42 this.clearTimeline();
dev@347 43 this.timeline.visibleWidth = width;
dev@347 44 this.timeline.pixelsPerSecond = width / buffer.duration;
dev@347 45 this.waveTrack.height = height;
dev@347 46 } else {
dev@354 47 this.renderTimeline(this.trackDiv, buffer.duration);
dev@347 48 }
dev@347 49 this.timeline.timeContext.offset = 0.5 * this.timeline.timeContext.visibleDuration;
dev@347 50
dev@347 51 // time axis
dev@347 52 const timeAxis = new wavesUI.helpers.TimeAxisLayer({
dev@347 53 height: height,
dev@347 54 color: '#b0b0b0'
dev@347 55 });
dev@347 56 this.addLayer(timeAxis, this.waveTrack, this.timeline.timeContext, true);
dev@347 57
dev@347 58 const nchannels = buffer.numberOfChannels;
dev@347 59 const totalWaveHeight = height * 0.9;
dev@347 60 const waveHeight = totalWaveHeight / nchannels;
dev@347 61
dev@347 62 for (let ch = 0; ch < nchannels; ++ch) {
dev@347 63 const waveformLayer = new wavesUI.helpers.WaveformLayer(buffer, {
dev@347 64 top: (height - totalWaveHeight) / 2 + waveHeight * ch,
dev@347 65 height: waveHeight,
dev@347 66 color: '#0868ac',
dev@347 67 channel: ch
dev@347 68 });
dev@347 69 this.addLayer(waveformLayer, this.waveTrack, this.timeline.timeContext);
dev@347 70 }
dev@347 71
dev@347 72 this.timeline.state = new wavesUI.states.CenteredZoomState(this.timeline);
dev@347 73 this.waveTrack.render();
dev@347 74 this.waveTrack.update();
dev@347 75 this.ref.markForCheck();
dev@347 76 }
dev@347 77 }