dev@31
|
1 import {Component} from '@angular/core';
|
dev@37
|
2 import {AudioPlayerService} from "./services/audio-player/audio-player.service";
|
dev@47
|
3 import {FeatureExtractionService} from "./services/feature-extraction/feature-extraction.service";
|
dev@47
|
4 import {ExtractorOutputInfo} from "./feature-extraction-menu/feature-extraction-menu.component";
|
dev@89
|
5 import {DomSanitizer} from '@angular/platform-browser';
|
dev@89
|
6 import {MdIconRegistry} from '@angular/material';
|
angular-cli@0
|
7
|
angular-cli@0
|
8 @Component({
|
angular-cli@0
|
9 selector: 'app-root',
|
angular-cli@0
|
10 templateUrl: './app.component.html',
|
angular-cli@0
|
11 styleUrls: ['./app.component.css']
|
angular-cli@0
|
12 })
|
angular-cli@0
|
13 export class AppComponent {
|
dev@31
|
14 audioBuffer: AudioBuffer; // TODO consider revising
|
dev@49
|
15 canExtract: boolean;
|
dev@115
|
16 isProcessing: boolean;
|
dev@1
|
17
|
dev@47
|
18 constructor(private audioService: AudioPlayerService,
|
dev@89
|
19 private piperService: FeatureExtractionService,
|
dev@89
|
20 private iconRegistry: MdIconRegistry,
|
dev@89
|
21 private sanitizer: DomSanitizer) {
|
dev@49
|
22 this.canExtract = false;
|
dev@115
|
23 this.isProcessing = false;
|
dev@89
|
24 iconRegistry.addSvgIcon(
|
dev@89
|
25 'duck',
|
dev@89
|
26 sanitizer.bypassSecurityTrustResourceUrl('assets/duck.svg')
|
dev@89
|
27 );
|
dev@48
|
28 }
|
dev@16
|
29
|
dev@134
|
30 onFileOpened(file: File | Blob) {
|
dev@49
|
31 this.canExtract = false;
|
dev@115
|
32 this.isProcessing = true;
|
dev@31
|
33 const reader: FileReader = new FileReader();
|
dev@31
|
34 const mimeType = file.type;
|
dev@31
|
35 reader.onload = (event: any) => {
|
dev@134
|
36 const url: string = file instanceof Blob ? URL.createObjectURL(file) :
|
dev@134
|
37 URL.createObjectURL(new Blob([event.target.result], {type: mimeType}));
|
dev@134
|
38 this.audioService.loadAudioFromUrl(url);
|
dev@31
|
39 // TODO use a rxjs/Subject instead?
|
dev@134
|
40 this.audioService.decodeAudioData(event.target.result)
|
dev@134
|
41 .then(audioBuffer => {
|
dev@31
|
42 this.audioBuffer = audioBuffer;
|
dev@115
|
43 if (this.audioBuffer) {
|
dev@49
|
44 this.canExtract = true;
|
dev@115
|
45 this.isProcessing = false;
|
dev@115
|
46 }
|
dev@134
|
47 }).catch(error => {
|
dev@134
|
48 this.canExtract = false;
|
dev@134
|
49 this.isProcessing = false;
|
dev@134
|
50 console.warn(error);
|
dev@31
|
51 });
|
dev@31
|
52 };
|
dev@31
|
53 reader.readAsArrayBuffer(file);
|
dev@16
|
54 }
|
dev@47
|
55
|
dev@48
|
56 extractFeatures(outputInfo: ExtractorOutputInfo): void {
|
dev@50
|
57 if (!this.canExtract || !outputInfo) return;
|
dev@49
|
58 this.canExtract = false;
|
dev@115
|
59 this.isProcessing = true;
|
dev@63
|
60 this.piperService.collect({
|
dev@47
|
61 audioData: [...Array(this.audioBuffer.numberOfChannels).keys()]
|
dev@47
|
62 .map(i => this.audioBuffer.getChannelData(i)),
|
dev@47
|
63 audioFormat: {
|
dev@47
|
64 sampleRate: this.audioBuffer.sampleRate,
|
dev@47
|
65 channelCount: this.audioBuffer.numberOfChannels
|
dev@47
|
66 },
|
dev@47
|
67 key: outputInfo.extractorKey,
|
dev@47
|
68 outputId: outputInfo.outputId
|
dev@50
|
69 }).then(() => {
|
dev@49
|
70 this.canExtract = true;
|
dev@115
|
71 this.isProcessing = false;
|
dev@115
|
72 }).catch(err => {
|
dev@115
|
73 this.canExtract = true;
|
dev@115
|
74 this.isProcessing = false;
|
dev@115
|
75 console.error(err)
|
dev@115
|
76 });
|
dev@47
|
77 }
|
angular-cli@0
|
78 }
|