annotate src/app/app.component.ts @ 200:d7e4bba39d20

Moving towards defining an analysis item - it is already doing too much in terms of also describing the root audio.
author Lucas Thompson <dev@lucas.im>
date Fri, 24 Mar 2017 11:05:20 +0000
parents ac57ddba8ba9
children f9088265a1fc
rev   line source
dev@193 1 import {Component, OnDestroy} from '@angular/core';
dev@193 2 import {
dev@193 3 AudioPlayerService,
dev@193 4 AudioResourceError, AudioResource
dev@193 5 } from "./services/audio-player/audio-player.service";
dev@47 6 import {FeatureExtractionService} from "./services/feature-extraction/feature-extraction.service";
dev@47 7 import {ExtractorOutputInfo} from "./feature-extraction-menu/feature-extraction-menu.component";
dev@89 8 import {DomSanitizer} from '@angular/platform-browser';
dev@89 9 import {MdIconRegistry} from '@angular/material';
dev@193 10 import {Subscription} from "rxjs";
angular-cli@0 11
angular-cli@0 12 @Component({
angular-cli@0 13 selector: 'app-root',
angular-cli@0 14 templateUrl: './app.component.html',
angular-cli@0 15 styleUrls: ['./app.component.css']
angular-cli@0 16 })
dev@193 17 export class AppComponent implements OnDestroy {
dev@31 18 audioBuffer: AudioBuffer; // TODO consider revising
dev@49 19 canExtract: boolean;
dev@115 20 isProcessing: boolean;
dev@193 21 private onAudioDataSubscription: Subscription;
dev@1 22
dev@47 23 constructor(private audioService: AudioPlayerService,
dev@89 24 private piperService: FeatureExtractionService,
dev@89 25 private iconRegistry: MdIconRegistry,
dev@89 26 private sanitizer: DomSanitizer) {
dev@49 27 this.canExtract = false;
dev@115 28 this.isProcessing = false;
dev@89 29 iconRegistry.addSvgIcon(
dev@89 30 'duck',
dev@89 31 sanitizer.bypassSecurityTrustResourceUrl('assets/duck.svg')
dev@89 32 );
dev@193 33
dev@193 34 this.onAudioDataSubscription = this.audioService.audioLoaded$.subscribe(
dev@193 35 resource => {
dev@193 36 const wasError = (resource as AudioResourceError).message != null;
dev@193 37 if (wasError) {
dev@193 38 this.isProcessing = false;
dev@193 39 this.canExtract = false;
dev@193 40 } else {
dev@193 41 this.audioBuffer = (resource as AudioResource).samples;
dev@193 42 if (this.audioBuffer) {
dev@193 43 this.canExtract = true;
dev@193 44 this.isProcessing = false;
dev@193 45 }
dev@193 46 }
dev@193 47 }
dev@193 48 );
dev@48 49 }
dev@16 50
dev@134 51 onFileOpened(file: File | Blob) {
dev@49 52 this.canExtract = false;
dev@115 53 this.isProcessing = true;
dev@193 54 this.audioService.loadAudio(file);
dev@16 55 }
dev@47 56
dev@48 57 extractFeatures(outputInfo: ExtractorOutputInfo): void {
dev@50 58 if (!this.canExtract || !outputInfo) return;
dev@49 59 this.canExtract = false;
dev@115 60 this.isProcessing = true;
dev@63 61 this.piperService.collect({
dev@47 62 audioData: [...Array(this.audioBuffer.numberOfChannels).keys()]
dev@47 63 .map(i => this.audioBuffer.getChannelData(i)),
dev@47 64 audioFormat: {
dev@47 65 sampleRate: this.audioBuffer.sampleRate,
dev@47 66 channelCount: this.audioBuffer.numberOfChannels
dev@47 67 },
dev@47 68 key: outputInfo.extractorKey,
dev@47 69 outputId: outputInfo.outputId
dev@50 70 }).then(() => {
dev@49 71 this.canExtract = true;
dev@115 72 this.isProcessing = false;
dev@115 73 }).catch(err => {
dev@115 74 this.canExtract = true;
dev@115 75 this.isProcessing = false;
dev@115 76 console.error(err)
dev@115 77 });
dev@47 78 }
dev@193 79
dev@193 80 ngOnDestroy(): void {
dev@193 81 this.onAudioDataSubscription.unsubscribe();
dev@193 82 }
angular-cli@0 83 }