Mercurial > hg > ugly-duckling
view src/app/spectrogram/Spectrogram.ts @ 131:4eb3cc32f6c0
Add dispose method
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Thu, 16 Mar 2017 12:35:43 +0000 |
parents | 8aa1ff061503 |
children | fab88270bccc |
line wrap: on
line source
/** * Created by lucast on 16/03/2017. */ 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 { private samples: Float32Array; private framing: Framing; private fft: RealFft; private real: Float32Array; private nCols: number; private columnHeight: number; private window: Float32Array; constructor(samples: Float32Array, options: Framing & Object) { super(); this.samples = samples; this.framing = options; this.real = new Float32Array(this.framing.blockSize); 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); } dispose(): void { this.fft.dispose(); } getColumnCount(): number { return this.nCols; } getColumnHeight(): number { return this.columnHeight; } getColumn(n: number): Float32Array { const startSample = n * this.framing.stepSize; const sz = this.framing.blockSize; this.real.fill(0); let available = sz; if (startSample + sz >= this.samples.length) { available = this.samples.length - startSample; } for (let i = 0; i < available; ++i) { this.real[i] = this.samples[startSample + i] * this.window[i]; } const complex = this.fft.forward(this.real); const h = this.getColumnHeight(); const col = new Float32Array(h); 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; } return col; } } export class WavesSpectrogramLayer extends Waves.core.Layer { constructor(buffer: AudioBuffer, options: Framing & Object) { const defaults = { normalise: 'hybrid', gain: 40.0, channel: 0, stepSize: 512, blockSize: 1024 }; const mergedOptions: Framing & Object & {channel: number} = Object.assign({}, defaults, options); super('entity', new SpectrogramEntity( buffer.getChannelData(mergedOptions.channel), mergedOptions ), mergedOptions ); this.configureShape(Waves.shapes.Matrix, {}, mergedOptions); } }