dev@10
|
1 import {
|
dev@51
|
2 Component, OnInit, ViewChild, ElementRef, Input, AfterViewInit, NgZone,
|
dev@51
|
3 OnDestroy
|
dev@10
|
4 } from '@angular/core';
|
dev@39
|
5 import {AudioPlayerService} from "../services/audio-player/audio-player.service";
|
dev@36
|
6 import wavesUI from 'waves-ui';
|
dev@63
|
7 import {
|
dev@63
|
8 FeatureExtractionService,
|
dev@63
|
9 Extracted
|
dev@63
|
10 } from "../services/feature-extraction/feature-extraction.service";
|
dev@51
|
11 import {Subscription} from "rxjs";
|
dev@63
|
12 import {
|
dev@63
|
13 FeatureCollection,
|
dev@63
|
14 FixedSpacedFeatures
|
dev@63
|
15 } from "piper/HigherLevelUtilities";
|
dev@53
|
16 import {toSeconds} from "piper";
|
dev@8
|
17
|
dev@20
|
18 type Timeline = any; // TODO what type actually is it.. start a .d.ts for waves-ui?
|
dev@54
|
19 type Layer = any;
|
dev@54
|
20 type Track = any;
|
dev@59
|
21 type DisposableIndex = number;
|
dev@59
|
22 type Colour = string;
|
dev@6
|
23
|
dev@6
|
24 @Component({
|
dev@6
|
25 selector: 'app-waveform',
|
dev@6
|
26 templateUrl: './waveform.component.html',
|
dev@6
|
27 styleUrls: ['./waveform.component.css']
|
dev@6
|
28 })
|
dev@51
|
29 export class WaveformComponent implements OnInit, AfterViewInit, OnDestroy {
|
dev@20
|
30
|
dev@8
|
31 @ViewChild('track') trackDiv: ElementRef;
|
dev@6
|
32
|
dev@54
|
33 private _audioBuffer: AudioBuffer;
|
dev@54
|
34 private timeline: Timeline;
|
dev@54
|
35 private cursorLayer: any;
|
dev@54
|
36 private disposableLayers: Layer[];
|
dev@59
|
37 private colouredLayers: Map<DisposableIndex, Colour>;
|
dev@16
|
38
|
dev@16
|
39 @Input()
|
dev@16
|
40 set audioBuffer(buffer: AudioBuffer) {
|
dev@16
|
41 this._audioBuffer = buffer || undefined;
|
dev@20
|
42 if (this.audioBuffer)
|
dev@20
|
43 this.renderWaveform(this.audioBuffer);
|
dev@16
|
44 }
|
dev@16
|
45
|
dev@16
|
46 get audioBuffer(): AudioBuffer {
|
dev@16
|
47 return this._audioBuffer;
|
dev@16
|
48 }
|
dev@16
|
49
|
dev@51
|
50 private featureExtractionSubscription: Subscription;
|
dev@53
|
51 private playingStateSubscription: Subscription;
|
dev@53
|
52 private seekedSubscription: Subscription;
|
dev@53
|
53 private isPlaying: boolean;
|
dev@51
|
54
|
dev@31
|
55 constructor(private audioService: AudioPlayerService,
|
dev@51
|
56 private piperService: FeatureExtractionService,
|
dev@51
|
57 public ngZone: NgZone) {
|
dev@59
|
58 this.colouredLayers = new Map();
|
dev@54
|
59 this.disposableLayers = [];
|
dev@54
|
60 this._audioBuffer = undefined;
|
dev@54
|
61 this.timeline = undefined;
|
dev@54
|
62 this.cursorLayer = undefined;
|
dev@53
|
63 this.isPlaying = false;
|
dev@59
|
64 const colours = function* () {
|
dev@59
|
65 const circularColours = [
|
dev@59
|
66 'black',
|
dev@59
|
67 'red',
|
dev@59
|
68 'green',
|
dev@59
|
69 'purple',
|
dev@59
|
70 'orange'
|
dev@59
|
71 ];
|
dev@59
|
72 let index = 0;
|
dev@59
|
73 const nColours = circularColours.length;
|
dev@59
|
74 while (true) {
|
dev@59
|
75 yield circularColours[index = ++index % nColours];
|
dev@59
|
76 }
|
dev@59
|
77 }();
|
dev@59
|
78
|
dev@51
|
79 this.featureExtractionSubscription = piperService.featuresExtracted$.subscribe(
|
dev@51
|
80 features => {
|
dev@59
|
81 this.renderFeatures(features, colours.next().value);
|
dev@51
|
82 });
|
dev@53
|
83 this.playingStateSubscription = audioService.playingStateChange$.subscribe(
|
dev@53
|
84 isPlaying => {
|
dev@53
|
85 this.isPlaying = isPlaying;
|
dev@53
|
86 if (this.isPlaying)
|
dev@53
|
87 this.animate();
|
dev@53
|
88 });
|
dev@53
|
89 this.seekedSubscription = audioService.seeked$.subscribe(() => {
|
dev@53
|
90 if (!this.isPlaying)
|
dev@53
|
91 this.animate();
|
dev@53
|
92 });
|
dev@51
|
93 }
|
dev@51
|
94
|
dev@53
|
95 ngOnInit() {
|
dev@53
|
96 }
|
dev@10
|
97
|
dev@10
|
98 ngAfterViewInit(): void {
|
dev@51
|
99 this.timeline = this.renderTimeline();
|
dev@20
|
100 }
|
dev@20
|
101
|
dev@20
|
102 renderTimeline(duration: number = 1.0): Timeline {
|
dev@18
|
103 const track: HTMLElement = this.trackDiv.nativeElement;
|
dev@20
|
104 track.innerHTML = "";
|
dev@18
|
105 const height: number = track.getBoundingClientRect().height;
|
dev@18
|
106 const width: number = track.getBoundingClientRect().width;
|
dev@18
|
107 const pixelsPerSecond = width / duration;
|
dev@18
|
108 const timeline = new wavesUI.core.Timeline(pixelsPerSecond, width);
|
dev@33
|
109 timeline.timeContext.offset = 0.5 * timeline.timeContext.visibleDuration;
|
dev@18
|
110 timeline.createTrack(track, height, 'main');
|
dev@54
|
111 return timeline;
|
dev@54
|
112 }
|
dev@18
|
113
|
dev@54
|
114 renderWaveform(buffer: AudioBuffer): void {
|
dev@54
|
115 const height: number = this.trackDiv.nativeElement.getBoundingClientRect().height;
|
dev@54
|
116 const mainTrack = this.timeline.getTrackById('main');
|
dev@54
|
117 if (this.timeline) {
|
dev@54
|
118 // resize
|
dev@54
|
119 const width = this.trackDiv.nativeElement.getBoundingClientRect().width;
|
dev@55
|
120
|
dev@54
|
121 // loop through layers and remove them, waves-ui provides methods for this but it seems to not work properly
|
dev@55
|
122 const timeContextChildren = this.timeline.timeContext._children;
|
dev@55
|
123
|
dev@60
|
124 for (let i = 0, length = this.disposableLayers.length; i < length; ++i) {
|
dev@54
|
125 let layer = this.disposableLayers.pop();
|
dev@54
|
126 mainTrack.remove(layer);
|
dev@55
|
127
|
dev@55
|
128 const index = timeContextChildren.indexOf(layer.timeContext);
|
dev@55
|
129 if (index >= 0)
|
dev@55
|
130 timeContextChildren.splice(index, 1);
|
dev@54
|
131 layer.destroy();
|
dev@54
|
132 }
|
dev@59
|
133 this.colouredLayers.clear();
|
dev@59
|
134
|
dev@54
|
135 this.timeline.visibleWidth = width;
|
dev@54
|
136 this.timeline.pixelsPerSecond = width / buffer.duration;
|
dev@54
|
137 mainTrack.height = height;
|
dev@54
|
138 } else {
|
dev@54
|
139 this.timeline = this.renderTimeline(buffer.duration)
|
dev@54
|
140 }
|
dev@18
|
141 // time axis
|
dev@18
|
142 const timeAxis = new wavesUI.helpers.TimeAxisLayer({
|
dev@18
|
143 height: height,
|
dev@18
|
144 color: 'gray'
|
dev@18
|
145 });
|
dev@54
|
146 this.addLayer(timeAxis, mainTrack, this.timeline.timeContext, true);
|
dev@18
|
147
|
dev@20
|
148 const waveformLayer = new wavesUI.helpers.WaveformLayer(buffer, {
|
dev@10
|
149 top: 10,
|
dev@20
|
150 height: height * 0.9,
|
dev@16
|
151 color: 'darkblue'
|
dev@16
|
152 });
|
dev@54
|
153 this.addLayer(waveformLayer, mainTrack, this.timeline.timeContext);
|
dev@31
|
154
|
dev@53
|
155 this.cursorLayer = new wavesUI.helpers.CursorLayer({
|
dev@31
|
156 height: height
|
dev@31
|
157 });
|
dev@54
|
158 this.addLayer(this.cursorLayer, mainTrack, this.timeline.timeContext);
|
dev@51
|
159 this.timeline.state = new wavesUI.states.CenteredZoomState(this.timeline);
|
dev@54
|
160 mainTrack.render();
|
dev@54
|
161 mainTrack.update();
|
dev@53
|
162 this.animate();
|
dev@53
|
163 }
|
dev@53
|
164
|
dev@53
|
165 // TODO refactor - this doesn't belong here
|
dev@63
|
166 private renderFeatures(extracted: Extracted, colour: Colour): void {
|
dev@63
|
167 if (!extracted.hasOwnProperty('shape') || !extracted.hasOwnProperty('data')) return;
|
dev@63
|
168 const features: FeatureCollection = (extracted as FeatureCollection);
|
dev@63
|
169 switch (features.shape) {
|
dev@63
|
170 case 'vector':
|
dev@63
|
171 const stepDuration = (features as FixedSpacedFeatures).stepDuration;
|
dev@63
|
172 const featureData = (features.data as Float32Array);
|
dev@63
|
173 const normalisationFactor = 1.0 /
|
dev@63
|
174 featureData.reduce(
|
dev@63
|
175 (currentMax, feature) => Math.max(currentMax, feature),
|
dev@63
|
176 -Infinity
|
dev@63
|
177 );
|
dev@63
|
178 const plotData = [...featureData].map((feature, i) => {
|
dev@63
|
179 return {
|
dev@63
|
180 cx: i * stepDuration,
|
dev@63
|
181 cy: feature * normalisationFactor
|
dev@63
|
182 };
|
dev@63
|
183 });
|
dev@63
|
184 let breakpointLayer = new wavesUI.helpers.BreakpointLayer(plotData, {
|
dev@63
|
185 color: colour,
|
dev@63
|
186 height: this.trackDiv.nativeElement.getBoundingClientRect().height
|
dev@63
|
187 });
|
dev@63
|
188 this.colouredLayers.set(this.addLayer(
|
dev@63
|
189 breakpointLayer,
|
dev@63
|
190 this.timeline.getTrackById('main'),
|
dev@63
|
191 this.timeline.timeContext
|
dev@63
|
192 ), colour);
|
dev@63
|
193 break;
|
dev@63
|
194 }
|
dev@59
|
195
|
dev@56
|
196 this.timeline.tracks.update();
|
dev@53
|
197 }
|
dev@53
|
198
|
dev@53
|
199 private animate(): void {
|
dev@31
|
200 this.ngZone.runOutsideAngular(() => {
|
dev@31
|
201 // listen for time passing...
|
dev@31
|
202 const updateSeekingCursor = () => {
|
dev@53
|
203 const currentTime = this.audioService.getCurrentTime();
|
dev@53
|
204 this.cursorLayer.currentPosition = currentTime;
|
dev@53
|
205 this.cursorLayer.update();
|
dev@53
|
206
|
dev@53
|
207 const currentOffset = this.timeline.timeContext.offset;
|
dev@53
|
208 const offsetTimestamp = currentOffset
|
dev@53
|
209 + currentTime;
|
dev@53
|
210
|
dev@53
|
211 const visibleDuration = this.timeline.timeContext.visibleDuration;
|
dev@53
|
212 // TODO reduce duplication between directions and make more declarative
|
dev@53
|
213 // this kinda logic should also be tested
|
dev@53
|
214 const mustPageForward = offsetTimestamp > visibleDuration;
|
dev@53
|
215 const mustPageBackward = currentTime < -currentOffset;
|
dev@53
|
216
|
dev@53
|
217 if (mustPageForward) {
|
dev@53
|
218 const hasSkippedMultiplePages = offsetTimestamp - visibleDuration > visibleDuration;
|
dev@53
|
219
|
dev@53
|
220 this.timeline.timeContext.offset = hasSkippedMultiplePages
|
dev@53
|
221 ? -currentTime + 0.5 * visibleDuration
|
dev@53
|
222 : currentOffset - visibleDuration;
|
dev@51
|
223 this.timeline.tracks.update();
|
dev@34
|
224 }
|
dev@53
|
225
|
dev@53
|
226 if (mustPageBackward) {
|
dev@53
|
227 const hasSkippedMultiplePages = currentTime + visibleDuration < -currentOffset;
|
dev@53
|
228 this.timeline.timeContext.offset = hasSkippedMultiplePages
|
dev@53
|
229 ? -currentTime + 0.5 * visibleDuration
|
dev@53
|
230 : currentOffset + visibleDuration;
|
dev@51
|
231 this.timeline.tracks.update();
|
dev@34
|
232 }
|
dev@53
|
233
|
dev@53
|
234 if (this.isPlaying)
|
dev@53
|
235 requestAnimationFrame(updateSeekingCursor);
|
dev@31
|
236 };
|
dev@31
|
237 updateSeekingCursor();
|
dev@31
|
238 });
|
dev@6
|
239 }
|
dev@16
|
240
|
dev@59
|
241 private addLayer(layer: Layer, track: Track, timeContext: any, isAxis: boolean = false): DisposableIndex {
|
dev@54
|
242 timeContext.zoom = 1.0;
|
dev@54
|
243 if (!layer.timeContext) {
|
dev@54
|
244 layer.setTimeContext(isAxis ?
|
dev@54
|
245 timeContext : new wavesUI.core.LayerTimeContext(timeContext));
|
dev@54
|
246 }
|
dev@54
|
247 track.add(layer);
|
dev@54
|
248 layer.render();
|
dev@54
|
249 layer.update();
|
dev@59
|
250 return this.disposableLayers.push(layer) - 1;
|
dev@59
|
251 }
|
dev@59
|
252
|
dev@59
|
253 private static changeColour(layer: Layer, colour: string): void {
|
dev@59
|
254 const butcherShapes = (shape) => {
|
dev@59
|
255 shape.install({color: () => colour});
|
dev@59
|
256 shape.params.color = colour;
|
dev@59
|
257 shape.update(layer._renderingContext, layer.data);
|
dev@59
|
258 };
|
dev@59
|
259
|
dev@59
|
260 layer._$itemCommonShapeMap.forEach(butcherShapes);
|
dev@59
|
261 layer._$itemShapeMap.forEach(butcherShapes);
|
dev@59
|
262 layer.render();
|
dev@59
|
263 layer.update();
|
dev@54
|
264 }
|
dev@54
|
265
|
dev@51
|
266 ngOnDestroy(): void {
|
dev@51
|
267 this.featureExtractionSubscription.unsubscribe();
|
dev@53
|
268 this.playingStateSubscription.unsubscribe();
|
dev@53
|
269 this.seekedSubscription.unsubscribe();
|
dev@51
|
270 }
|
dev@6
|
271 }
|