comparison src/app/app.component.ts @ 49:92c139e16b51

Disable button whilst extracting (for now) - quick fix to stop multiple process requests being sent (as they aren't queued / no request-response matching is done / process requests are synchronous)
author Lucas Thompson <dev@lucas.im>
date Tue, 06 Dec 2016 11:12:56 +0000
parents af0b4b05311c
children f31b14e2132e
comparison
equal deleted inserted replaced
48:af0b4b05311c 49:92c139e16b51
8 templateUrl: './app.component.html', 8 templateUrl: './app.component.html',
9 styleUrls: ['./app.component.css'] 9 styleUrls: ['./app.component.css']
10 }) 10 })
11 export class AppComponent { 11 export class AppComponent {
12 audioBuffer: AudioBuffer; // TODO consider revising 12 audioBuffer: AudioBuffer; // TODO consider revising
13 hasAudioBuffer: boolean; 13 canExtract: boolean;
14 14
15 constructor(private audioService: AudioPlayerService, 15 constructor(private audioService: AudioPlayerService,
16 private piperService: FeatureExtractionService) { 16 private piperService: FeatureExtractionService) {
17 this.hasAudioBuffer = false; 17 this.canExtract = false;
18 } 18 }
19 19
20 onFileOpened(file: File) { 20 onFileOpened(file: File) {
21 this.hasAudioBuffer = false; 21 this.canExtract = false;
22 const reader: FileReader = new FileReader(); 22 const reader: FileReader = new FileReader();
23 const mimeType = file.type; 23 const mimeType = file.type;
24 reader.onload = (event: any) => { 24 reader.onload = (event: any) => {
25 this.audioService.loadAudioFromUrl( 25 this.audioService.loadAudioFromUrl(
26 URL.createObjectURL(new Blob([event.target.result], {type: mimeType})) 26 URL.createObjectURL(new Blob([event.target.result], {type: mimeType}))
27 ); 27 );
28 // TODO use a rxjs/Subject instead? 28 // TODO use a rxjs/Subject instead?
29 this.audioService.decodeAudioData(event.target.result).then(audioBuffer => { 29 this.audioService.decodeAudioData(event.target.result).then(audioBuffer => {
30 this.audioBuffer = audioBuffer; 30 this.audioBuffer = audioBuffer;
31 if (this.audioBuffer) 31 if (this.audioBuffer)
32 this.hasAudioBuffer = true; 32 this.canExtract = true;
33 }); 33 });
34 }; 34 };
35 reader.readAsArrayBuffer(file); 35 reader.readAsArrayBuffer(file);
36 } 36 }
37 37
38 extractFeatures(outputInfo: ExtractorOutputInfo): void { 38 extractFeatures(outputInfo: ExtractorOutputInfo): void {
39 if (!this.hasAudioBuffer) return; 39 if (!this.canExtract) return;
40 this.canExtract = false;
40 this.piperService.process({ 41 this.piperService.process({
41 audioData: [...Array(this.audioBuffer.numberOfChannels).keys()] 42 audioData: [...Array(this.audioBuffer.numberOfChannels).keys()]
42 .map(i => this.audioBuffer.getChannelData(i)), 43 .map(i => this.audioBuffer.getChannelData(i)),
43 audioFormat: { 44 audioFormat: {
44 sampleRate: this.audioBuffer.sampleRate, 45 sampleRate: this.audioBuffer.sampleRate,
45 channelCount: this.audioBuffer.numberOfChannels 46 channelCount: this.audioBuffer.numberOfChannels
46 }, 47 },
47 key: outputInfo.extractorKey, 48 key: outputInfo.extractorKey,
48 outputId: outputInfo.outputId 49 outputId: outputInfo.outputId
49 }).then(data => console.log(data)).catch(err => console.error(err)); 50 }).then(data => {
51 this.canExtract = true;
52 console.log(data);
53 }).catch(err => console.error(err));
50 } 54 }
51 } 55 }