comparison src/app/waveform/waveform.component.ts @ 31:f6ea31a3b1a3

Encapsulate audio playing and decoding logic in a ng2 service, provided by the root module.
author Lucas Thompson <dev@lucas.im>
date Wed, 30 Nov 2016 10:21:27 +0000
parents aabfa7a693dc
children 7e155ee93db3
comparison
equal deleted inserted replaced
30:5bdfcf493646 31:f6ea31a3b1a3
1 import { 1 import {
2 Component, OnInit, ViewChild, ElementRef, Input, AfterViewInit 2 Component, OnInit, ViewChild, ElementRef, Input, AfterViewInit, NgZone
3 } from '@angular/core'; 3 } from '@angular/core';
4 import {AudioPlayerService} from "../services/audio-player.service";
4 5
5 declare var wavesUI: any; // TODO non-global app scope import 6 declare var wavesUI: any; // TODO non-global app scope import
6 type Timeline = any; // TODO what type actually is it.. start a .d.ts for waves-ui? 7 type Timeline = any; // TODO what type actually is it.. start a .d.ts for waves-ui?
7 8
8 @Component({ 9 @Component({
25 26
26 get audioBuffer(): AudioBuffer { 27 get audioBuffer(): AudioBuffer {
27 return this._audioBuffer; 28 return this._audioBuffer;
28 } 29 }
29 30
30 constructor() {} 31 constructor(private audioService: AudioPlayerService,
32 public ngZone: NgZone) {}
31 ngOnInit() {} 33 ngOnInit() {}
32 34
33 ngAfterViewInit(): void { 35 ngAfterViewInit(): void {
34 this.renderTimeline(); 36 this.renderTimeline();
35 } 37 }
48 height: height, 50 height: height,
49 color: 'gray' 51 color: 'gray'
50 }); 52 });
51 53
52 timeline.addLayer(timeAxis, 'main', 'default', true); 54 timeline.addLayer(timeAxis, 'main', 'default', true);
53 timeline.state = new wavesUI.states.CenteredZoomState(timeline);
54 return timeline; 55 return timeline;
55 } 56 }
56 57
57 renderWaveform(buffer: AudioBuffer): void { 58 renderWaveform(buffer: AudioBuffer): void {
58 const height: number = this.trackDiv.nativeElement.getBoundingClientRect().height; 59 const height: number = this.trackDiv.nativeElement.getBoundingClientRect().height;
61 top: 10, 62 top: 10,
62 height: height * 0.9, 63 height: height * 0.9,
63 color: 'darkblue' 64 color: 'darkblue'
64 }); 65 });
65 (timeline as any).addLayer(waveformLayer, 'main'); 66 (timeline as any).addLayer(waveformLayer, 'main');
67
68 const cursorLayer = new wavesUI.helpers.CursorLayer({
69 height: height
70 });
71 timeline.addLayer(cursorLayer, 'main');
72 timeline.state = new wavesUI.states.CenteredZoomState(timeline);
73 this.ngZone.runOutsideAngular(() => {
74 // listen for time passing...
75 // TODO this gets the fans going on large files... worth fixing? or waiting to write a better component?
76 // or, can this be updated in a more efficient manner?
77 const updateSeekingCursor = () => {
78 cursorLayer.currentPosition = this.audioService.getCurrentTime();
79 cursorLayer.update();
80 requestAnimationFrame(updateSeekingCursor);
81 };
82 updateSeekingCursor();
83 });
66 } 84 }
67 85
68 } 86 }