Mercurial > hg > ugly-duckling
diff src/app/spectrogram/Spectrogram.ts @ 236:53ea6406d601
Generate new project with latest @angular/cli, including Angular 4.
author | Lucas Thompson <dev@lucas.im> |
---|---|
date | Tue, 25 Apr 2017 20:01:09 +0100 |
parents | 19ca3aaf7807 |
children | 6a83df5029fe |
line wrap: on
line diff
--- a/src/app/spectrogram/Spectrogram.ts Mon Apr 24 17:05:12 2017 +0100 +++ b/src/app/spectrogram/Spectrogram.ts Tue Apr 25 20:01:09 2017 +0100 @@ -1,9 +1,9 @@ /** * Created by lucast on 16/03/2017. */ -import {RealFft, KissRealFft} from "piper/fft/RealFft"; -import {hann} from "piper/FftUtilities"; -import {Framing} from "piper"; +import {RealFft, KissRealFft} from 'piper/fft/RealFft'; +import {hann} from 'piper/FftUtilities'; +import {Framing} from 'piper'; import Waves from 'waves-ui'; class SpectrogramEntity extends Waves.utils.MatrixEntity { @@ -23,7 +23,7 @@ this.sampleRate = sampleRate; this.framing = options; this.real = new Float32Array(this.framing.blockSize); - this.nCols = Math.floor(this.samples.length / this.framing.stepSize); //!!! not correct + this.nCols = Math.floor(this.samples.length / this.framing.stepSize); // !!! not correct this.columnHeight = Math.round(this.framing.blockSize / 2) + 1; this.fft = new KissRealFft(this.framing.blockSize); this.window = hann(this.framing.blockSize); @@ -44,7 +44,7 @@ getStepDuration(): number { return this.framing.stepSize / this.sampleRate; } - + getColumn(n: number): Float32Array { const startSample = n * this.framing.stepSize; @@ -68,10 +68,9 @@ const scale = 1.0 / Math.sqrt(sz); for (let i = 0; i < h; ++i) { - const re : number = complex[i*2] * scale; - const im : number = complex[i*2+1] * scale; - const mag = Math.sqrt(re * re + im * im); - col[i] = mag; + const re: number = complex[i * 2] * scale; + const im: number = complex[i * 2 + 1] * scale; + col[i] = Math.sqrt(re * re + im * im); } return col; @@ -79,7 +78,7 @@ } export class WavesSpectrogramLayer extends Waves.core.Layer { - constructor(buffer: AudioBuffer, + constructor(bufferIn: AudioBuffer, options: Framing & Object) { const defaults = { @@ -95,30 +94,38 @@ const getSamples = ((buffer, channel) => { const nch = buffer.numberOfChannels; - if (channel >= 0 || nch == 1) { - if (channel < 0) channel = 0; - return buffer.getChannelData(channel); + if (channel >= 0 || nch === 1) { + if (channel < 0) { + channel = 0; + } + return buffer.getChannelData(channel); } else { const before = performance.now(); - console.log("mixing down " + nch + " channels for spectrogram..."); - const mixed = Float32Array.from(buffer.getChannelData(0)); - const n = mixed.length; - for (let ch = 1; ch < nch; ++ch) { - const buf = buffer.getChannelData(ch); - for (let i = 0; i < n; ++i) mixed[i] += buf[i]; - } - const scale = 1.0 / nch; - for (let i = 0; i < n; ++i) mixed[i] *= scale; - console.log("done in " + (performance.now() - before) + "ms"); - return mixed; + console.log('mixing down ' + nch + ' channels for spectrogram...'); + const mixed = Float32Array.from(buffer.getChannelData(0)); + const n = mixed.length; + for (let ch = 1; ch < nch; ++ch) { + const buf = buffer.getChannelData(ch); + for (let i = 0; i < n; ++i) { + mixed[i] += buf[i]; + } + } + const scale = 1.0 / nch; + for (let i = 0; i < n; ++i) { + mixed[i] *= scale; + } + console.log('done in ' + (performance.now() - before) + 'ms'); + return mixed; } }); - - super('entity', - new SpectrogramEntity(getSamples(buffer, mergedOptions.channel), - mergedOptions, - buffer.sampleRate), - mergedOptions); + + super( + 'entity', + new SpectrogramEntity(getSamples(bufferIn, mergedOptions.channel), + mergedOptions, + bufferIn.sampleRate), + mergedOptions + ); this.configureShape(Waves.shapes.Matrix, {}, mergedOptions); }