dev@129: /** dev@129: * Created by lucast on 16/03/2017. dev@129: */ dev@129: import {RealFft, KissRealFft} from "piper/fft/RealFft"; dev@129: import {hann} from "piper/FftUtilities"; dev@129: import {Framing} from "piper"; dev@129: import Waves from 'waves-ui'; dev@129: dev@129: class SpectrogramEntity extends Waves.utils.MatrixEntity { dev@129: dev@129: private samples: Float32Array; dev@129: private framing: Framing; dev@129: private fft: RealFft; dev@129: private real: Float32Array; dev@129: private nCols: number; cannam@130: private columnHeight: number; dev@129: private window: Float32Array; dev@129: dev@129: constructor(samples: Float32Array, options: Framing & Object) { dev@129: super(); dev@129: this.samples = samples; dev@129: this.framing = options; dev@129: this.real = new Float32Array(this.framing.blockSize); dev@129: this.nCols = Math.floor(this.samples.length / this.framing.stepSize); //!!! not correct cannam@130: this.columnHeight = Math.round(this.framing.blockSize / 2) + 1; dev@129: this.fft = new KissRealFft(this.framing.blockSize); dev@129: this.window = hann(this.framing.blockSize); dev@129: } dev@129: cannam@131: dispose(): void { cannam@131: this.fft.dispose(); cannam@131: } cannam@131: dev@129: getColumnCount(): number { dev@129: return this.nCols; dev@129: } dev@129: dev@129: getColumnHeight(): number { cannam@130: return this.columnHeight; dev@129: } dev@129: dev@129: getColumn(n: number): Float32Array { dev@129: dev@129: const startSample = n * this.framing.stepSize; dev@129: const sz = this.framing.blockSize; dev@129: dev@129: this.real.fill(0); dev@129: dev@129: let available = sz; dev@129: if (startSample + sz >= this.samples.length) { dev@129: available = this.samples.length - startSample; dev@129: } dev@129: dev@129: for (let i = 0; i < available; ++i) { dev@129: this.real[i] = this.samples[startSample + i] * this.window[i]; dev@129: } dev@129: dev@129: const complex = this.fft.forward(this.real); dev@129: dev@129: const h = this.getColumnHeight(); dev@129: const col = new Float32Array(h); dev@129: cannam@130: const scale = 1.0 / Math.sqrt(sz); dev@129: for (let i = 0; i < h; ++i) { cannam@130: const re : number = complex[i*2] * scale; cannam@130: const im : number = complex[i*2+1] * scale; cannam@130: const mag = Math.sqrt(re * re + im * im); cannam@130: col[i] = mag; dev@129: } dev@129: dev@129: return col; dev@129: } dev@129: } dev@129: dev@129: export class WavesSpectrogramLayer extends Waves.core.Layer { dev@129: constructor(buffer: AudioBuffer, dev@129: options: Framing & Object) { dev@129: dev@129: const defaults = { dev@129: normalise: 'hybrid', dev@129: gain: 40.0, dev@129: channel: 0, dev@129: stepSize: 512, dev@129: blockSize: 1024 dev@129: }; dev@129: dev@129: const mergedOptions: Framing & Object & {channel: number} = dev@129: Object.assign({}, defaults, options); dev@129: dev@129: super('entity', dev@129: new SpectrogramEntity( dev@129: buffer.getChannelData(mergedOptions.channel), dev@129: mergedOptions dev@129: ), dev@129: mergedOptions dev@129: ); dev@129: dev@129: this.configureShape(Waves.shapes.Matrix, {}, mergedOptions); dev@129: } dev@129: }