Mercurial > hg > ugly-duckling
diff 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 |
line wrap: on
line diff
--- a/src/app/audio-file-open/audio-file-open.component.ts Thu Oct 27 13:40:48 2016 +0100 +++ b/src/app/audio-file-open/audio-file-open.component.ts Thu Oct 27 14:44:37 2016 +0100 @@ -1,5 +1,14 @@ import {Component, OnInit, ViewChild, ElementRef} from '@angular/core'; +interface AudioContextConstructor { + new(): AudioContext +} + +interface WindowAudioContext { + AudioContext?: AudioContextConstructor; + webkitAudioContext?: AudioContextConstructor +} + @Component({ selector: 'app-audio-file-open', templateUrl: './audio-file-open.component.html', @@ -9,14 +18,27 @@ @ViewChild('open') open: ElementRef; - constructor() { } + private audioContext: AudioContext; + + constructor() { + // TODO make a service which provides the AudioContext? + const factory: WindowAudioContext = (window as WindowAudioContext); + this.audioContext = new (factory.AudioContext || factory.webkitAudioContext)(); + } ngOnInit() { } - openAudio(files: FileList) { - console.log(files); - console.log("open"); + decodeAudio(files: FileList) { + if (files.length > 0) { + const reader: FileReader = new FileReader(); + reader.onload = (event: any) => { + this.audioContext.decodeAudioData(event.target.result, buffer => { + console.log(buffer); + }); + }; + reader.readAsArrayBuffer(files[0]); + } } openAudioDialog() {