annotate src/app/visualisations/waveform/waveform.component.ts @ 383:1241ca979fd9
Refactor based on pattern which emerged when implementing multiple components. Still some very obvious dupe regarding the ElementRef stuff, I don't think ViewChild decorated props are inherited.. but I haven't actually verified that.
author |
Lucas Thompson <dev@lucas.im> |
date |
Wed, 31 May 2017 19:14:46 +0100 |
parents |
a3b45218c311 |
children |
7119d62121f0 |
rev |
line source |
dev@347
|
1 import {
|
dev@347
|
2 Component,
|
dev@354
|
3 Input,
|
dev@347
|
4 ElementRef,
|
dev@357
|
5 ViewChild,
|
dev@357
|
6 ChangeDetectionStrategy
|
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@357
|
15 styleUrls: ['../waves-template.css'],
|
dev@357
|
16 changeDetection: ChangeDetectionStrategy.OnPush
|
dev@347
|
17 })
|
dev@383
|
18 export class WaveformComponent extends WavesComponent<AudioBuffer> {
|
dev@383
|
19
|
dev@347
|
20 @ViewChild('track') trackDiv: ElementRef;
|
dev@347
|
21 @Input() set audioBuffer(buffer: AudioBuffer) {
|
dev@383
|
22 this.duration = buffer.duration;
|
dev@383
|
23 this.timeline.pixelsPerSecond = this.timeline.visibleWidth / buffer.duration;
|
dev@383
|
24 this.feature = buffer;
|
dev@347
|
25 }
|
dev@347
|
26
|
dev@383
|
27 protected get containerHeight(): number {
|
dev@383
|
28 return this.trackDiv.nativeElement.getBoundingClientRect().height;
|
dev@347
|
29 }
|
dev@347
|
30
|
dev@383
|
31 protected get trackContainer(): ElementRef {
|
dev@383
|
32 return this.trackDiv;
|
dev@347
|
33 }
|
dev@347
|
34
|
dev@383
|
35 protected get featureLayers(): Layer[] {
|
dev@383
|
36 const nChannels = this.feature.numberOfChannels;
|
dev@383
|
37 const totalWaveHeight = this.height * 0.9;
|
dev@383
|
38 const waveHeight = totalWaveHeight / nChannels;
|
dev@383
|
39
|
dev@383
|
40 const channelLayers: Layer[] = [];
|
dev@383
|
41 for (let ch = 0; ch < nChannels; ++ch) {
|
dev@383
|
42 channelLayers.push(new wavesUI.helpers.WaveformLayer(this.feature, {
|
dev@383
|
43 top: (this.height - totalWaveHeight) / 2 + waveHeight * ch,
|
dev@383
|
44 height: waveHeight,
|
dev@383
|
45 color: this.colour,
|
dev@383
|
46 channel: ch
|
dev@383
|
47 })
|
dev@383
|
48 );
|
dev@347
|
49 }
|
dev@383
|
50 return channelLayers;
|
dev@347
|
51 }
|
dev@347
|
52 }
|