Mercurial > hg > ugly-duckling
comparison src/app/audio-file-open/audio-file-open.component.ts @ 15:0571cf863026
Hack into the component the decoding of the chosen audio file, this needs refactoring and actually thinking about. Now, how to get the AudioBuffer to the waveform component?
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Thu, 27 Oct 2016 14:44:37 +0100 |
parents | b12e78d6185e |
children | 7e3ab6f8792f |
comparison
equal
deleted
inserted
replaced
14:2ae70581dafd | 15:0571cf863026 |
---|---|
1 import {Component, OnInit, ViewChild, ElementRef} from '@angular/core'; | 1 import {Component, OnInit, ViewChild, ElementRef} from '@angular/core'; |
2 | |
3 interface AudioContextConstructor { | |
4 new(): AudioContext | |
5 } | |
6 | |
7 interface WindowAudioContext { | |
8 AudioContext?: AudioContextConstructor; | |
9 webkitAudioContext?: AudioContextConstructor | |
10 } | |
2 | 11 |
3 @Component({ | 12 @Component({ |
4 selector: 'app-audio-file-open', | 13 selector: 'app-audio-file-open', |
5 templateUrl: './audio-file-open.component.html', | 14 templateUrl: './audio-file-open.component.html', |
6 styleUrls: ['./audio-file-open.component.css'] | 15 styleUrls: ['./audio-file-open.component.css'] |
7 }) | 16 }) |
8 export class AudioFileOpenComponent implements OnInit { | 17 export class AudioFileOpenComponent implements OnInit { |
9 | 18 |
10 @ViewChild('open') open: ElementRef; | 19 @ViewChild('open') open: ElementRef; |
11 | 20 |
12 constructor() { } | 21 private audioContext: AudioContext; |
22 | |
23 constructor() { | |
24 // TODO make a service which provides the AudioContext? | |
25 const factory: WindowAudioContext = (window as WindowAudioContext); | |
26 this.audioContext = new (factory.AudioContext || factory.webkitAudioContext)(); | |
27 } | |
13 | 28 |
14 ngOnInit() { | 29 ngOnInit() { |
15 } | 30 } |
16 | 31 |
17 openAudio(files: FileList) { | 32 decodeAudio(files: FileList) { |
18 console.log(files); | 33 if (files.length > 0) { |
19 console.log("open"); | 34 const reader: FileReader = new FileReader(); |
35 reader.onload = (event: any) => { | |
36 this.audioContext.decodeAudioData(event.target.result, buffer => { | |
37 console.log(buffer); | |
38 }); | |
39 }; | |
40 reader.readAsArrayBuffer(files[0]); | |
41 } | |
20 } | 42 } |
21 | 43 |
22 openAudioDialog() { | 44 openAudioDialog() { |
23 this.open.nativeElement.click(); | 45 this.open.nativeElement.click(); |
24 } | 46 } |