sourav-das/stem-separator
3
1/**2 * FFT (Fast Fourier Transform) implementation3 * Based on https://github.com/corbanbrook/dsp.js4 *5 * Centralized FFT functionality for spectrogram plugins6 */7// eslint-disable-next-line8// @ts-nocheck9export const ERB_A = (1000 * Math.log(10)) / (24.7 * 4.37);10// Frequency scaling functions11export function hzToMel(hz) {12 return 2595 * Math.log10(1 + hz / 700);13}14export function melToHz(mel) {15 return 700 * (Math.pow(10, mel / 2595) - 1);16}17export function hzToLog(hz) {18 return Math.log10(Math.max(1, hz));19}20export function logToHz(log) {21 return Math.pow(10, log);22}23export function hzToBark(hz) {24 // https://www.mathworks.com/help/audio/ref/hz2bark.html#function_hz2bark_sep_mw_06bea6f7-353b-4479-a58d-ccadb90e44de25 let bark = (26.81 * hz) / (1960 + hz) - 0.53;26 if (bark < 2) {27 bark += 0.15 * (2 - bark);28 }29 if (bark > 20.1) {30 bark += 0.22 * (bark - 20.1);31 }32 return bark;33}34export function barkToHz(bark) {35 // https://www.mathworks.com/help/audio/ref/bark2hz.html#function_bark2hz_sep_mw_bee310ea-48ac-4d95-ae3d-80f3e414955536 if (bark < 2) {37 bark = (bark - 0.3) / 0.85;38 }39 if (bark > 20.1) {40 bark = (bark + 4.422) / 1.22;41 }42 return 1960 * ((bark + 0.53) / (26.28 - bark));43}44export function hzToErb(hz) {45 // https://www.mathworks.com/help/audio/ref/hz2erb.html#function_hz2erb_sep_mw_06bea6f7-353b-4479-a58d-ccadb90e44de46 return ERB_A * Math.log10(1 + hz * 0.00437);47}48export function erbToHz(erb) {49 // https://it.mathworks.com/help/audio/ref/erb2hz.html?#function_erb2hz_sep_mw_bee310ea-48ac-4d95-ae3d-80f3e414955550 return (Math.pow(10, erb / ERB_A) - 1) / 0.00437;51}52// Generic scale conversion functions53export function hzToScale(hz, scale) {54 switch (scale) {55 case 'mel':56 return hzToMel(hz);57 case 'logarithmic':58 return hzToLog(hz);59 case 'bark':60 return hzToBark(hz);61 case 'erb':62 return hzToErb(hz);63 default:64 return hz;65 }66}67export function scaleToHz(scale, scaleType) {68 switch (scaleType) {69 case 'mel':70 return melToHz(scale);71 case 'logarithmic':72 return logToHz(scale);73 case 'bark':74 return barkToHz(scale);75 case 'erb':76 return erbToHz(scale);77 default:78 return scale;79 }80}81// Filter bank functions82function createFilterBank(numFilters, fftSamples, sampleRate, hzToScaleFunc, scaleToHzFunc) {83 const filterMin = hzToScaleFunc(0);84 const filterMax = hzToScaleFunc(sampleRate / 2);85 const filterBank = Array.from({ length: numFilters }, () => Array(fftSamples / 2 + 1).fill(0));86 const scale = sampleRate / fftSamples;87 for (let i = 0; i < numFilters; i++) {88 const hz = scaleToHzFunc(filterMin + (i / numFilters) * (filterMax - filterMin));89 const j = Math.floor(hz / scale);90 const hzLow = j * scale;91 const hzHigh = (j + 1) * scale;92 const r = (hz - hzLow) / (hzHigh - hzLow);93 filterBank[i][j] = 1 - r;94 filterBank[i][j + 1] = r;95 }96 return filterBank;97}98export function applyFilterBank(fftPoints, filterBank) {99 const numFilters = filterBank.length;100 const logSpectrum = Float32Array.from({ length: numFilters }, () => 0);101 for (let i = 0; i < numFilters; i++) {102 for (let j = 0; j < fftPoints.length; j++) {103 logSpectrum[i] += fftPoints[j] * filterBank[i][j];104 }105 }106 return logSpectrum;107}108// Centralized filter bank creation based on scale type109export function createFilterBankForScale(scale, numFilters, fftSamples, sampleRate) {110 switch (scale) {111 case 'mel':112 return createFilterBank(numFilters, fftSamples, sampleRate, hzToMel, melToHz);113 case 'logarithmic':114 return createFilterBank(numFilters, fftSamples, sampleRate, hzToLog, logToHz);115 case 'bark':116 return createFilterBank(numFilters, fftSamples, sampleRate, hzToBark, barkToHz);117 case 'erb':118 return createFilterBank(numFilters, fftSamples, sampleRate, hzToErb, erbToHz);119 case 'linear':120 default:121 return null; // No filter bank for linear scale122 }123}124export const COLOR_MAPS = {125 gray: () => {126 const colorMap = [];127 for (let i = 0; i < 256; i++) {128 const val = (255 - i) / 256;129 colorMap.push([val, val, val, 1]);130 }131 return colorMap;132 },133 igray: () => {134 const colorMap = [];135 for (let i = 0; i < 256; i++) {136 const val = i / 256;137 colorMap.push([val, val, val, 1]);138 }139 return colorMap;140 },141 roseus: () => [142 [0.004528, 0.004341, 0.004307, 1],143 [0.005625, 0.006156, 0.00601, 1],144 [0.006628, 0.008293, 0.008161, 1],145 [0.007551, 0.010738, 0.01079, 1],146 [0.008382, 0.013482, 0.013941, 1],147 [0.009111, 0.01652, 0.017662, 1],148 [0.009727, 0.019846, 0.022009, 1],149 [0.010223, 0.023452, 0.027035, 1],150 [0.010593, 0.027331, 0.032799, 1],151 [0.010833, 0.031475, 0.039361, 1],152 [0.010941, 0.035875, 0.046415, 1],153 [0.010918, 0.04052, 0.053597, 1],154 [0.010768, 0.045158, 0.060914, 1],155 [0.010492, 0.049708, 0.068367, 1],156 [0.010098, 0.054171, 0.075954, 1],157 [0.009594, 0.058549, 0.083672, 1],158 [0.008989, 0.06284, 0.091521, 1],159 [0.008297, 0.067046, 0.099499, 1],160 [0.00753, 0.071165, 0.107603, 1],161 [0.006704, 0.075196, 0.11583, 1],162 [0.005838, 0.07914, 0.124178, 1],163 [0.004949, 0.082994, 0.132643, 1],164 [0.004062, 0.086758, 0.141223, 1],165 [0.003198, 0.09043, 0.149913, 1],166 [0.002382, 0.09401, 0.158711, 1],167 [0.001643, 0.097494, 0.167612, 1],168 [0.001009, 0.100883, 0.176612, 1],169 [0.000514, 0.104174, 0.185704, 1],170 [0.000187, 0.107366, 0.194886, 1],171 [0.000066, 0.110457, 0.204151, 1],172 [0.000186, 0.113445, 0.213496, 1],173 [0.000587, 0.116329, 0.222914, 1],174 [0.001309, 0.119106, 0.232397, 1],175 [0.002394, 0.121776, 0.241942, 1],176 [0.003886, 0.124336, 0.251542, 1],177 [0.005831, 0.126784, 0.261189, 1],178 [0.008276, 0.12912, 0.270876, 1],179 [0.011268, 0.131342, 0.280598, 1],180 [0.014859, 0.133447, 0.290345, 1],181 [0.0191, 0.135435, 0.300111, 1],182 [0.024043, 0.137305, 0.309888, 1],183 [0.029742, 0.139054, 0.319669, 1],184 [0.036252, 0.140683, 0.329441, 1],185 [0.043507, 0.142189, 0.339203, 1],186 [0.050922, 0.143571, 0.348942, 1],187 [0.058432, 0.144831, 0.358649, 1],188 [0.066041, 0.145965, 0.368319, 1],189 [0.073744, 0.146974, 0.377938, 1],190 [0.081541, 0.147858, 0.387501, 1],191 [0.089431, 0.148616, 0.396998, 1],192 [0.097411, 0.149248, 0.406419, 1],193 [0.105479, 0.149754, 0.415755, 1],194 [0.113634, 0.150134, 0.424998, 1],195 [0.121873, 0.150389, 0.434139, 1],196 [0.130192, 0.150521, 0.443167, 1],197 [0.138591, 0.150528, 0.452075, 1],198 [0.147065, 0.150413, 0.460852, 1],199 [0.155614, 0.150175, 0.469493, 1],200 [0.164232, 0.149818, 0.477985, 1],201 [0.172917, 0.149343, 0.486322, 1],202 [0.181666, 0.148751, 0.494494, 1],203 [0.190476, 0.148046, 0.502493, 1],204 [0.199344, 0.147229, 0.510313, 1],205 [0.208267, 0.146302, 0.517944, 1],206 [0.217242, 0.145267, 0.52538, 1],207 [0.226264, 0.144131, 0.532613, 1],208 [0.235331, 0.142894, 0.539635, 1],209 [0.24444, 0.141559, 0.546442, 1],210 [0.253587, 0.140131, 0.553026, 1],211 [0.262769, 0.138615, 0.559381, 1],212 [0.271981, 0.137016, 0.5655, 1],213 [0.281222, 0.135335, 0.571381, 1],214 [0.290487, 0.133581, 0.577017, 1],215 [0.299774, 0.131757, 0.582404, 1],216 [0.30908, 0.129867, 0.587538, 1],217 [0.318399, 0.12792, 0.592415, 1],218 [0.32773, 0.125921, 0.597032, 1],219 [0.337069, 0.123877, 0.601385, 1],220 [0.346413, 0.121793, 0.605474, 1],221 [0.355758, 0.119678, 0.609295, 1],222 [0.365102, 0.11754, 0.612846, 1],223 [0.374443, 0.115386, 0.616127, 1],224 [0.383774, 0.113226, 0.619138, 1],225 [0.393096, 0.111066, 0.621876, 1],226 [0.402404, 0.108918, 0.624343, 1],227 [0.411694, 0.106794, 0.62654, 1],228 [0.420967, 0.104698, 0.628466, 1],229 [0.430217, 0.102645, 0.630123, 1],230 [0.439442, 0.100647, 0.631513, 1],231 [0.448637, 0.098717, 0.632638, 1],232 [0.457805, 0.096861, 0.633499, 1],233 [0.46694, 0.095095, 0.6341, 1],234 [0.47604, 0.093433, 0.634443, 1],235 [0.485102, 0.091885, 0.634532, 1],236 [0.494125, 0.090466, 0.63437, 1],237 [0.503104, 0.08919, 0.633962, 1],238 [0.512041, 0.088067, 0.633311, 1],239 [0.520931, 0.087108, 0.63242, 1],240 [0.529773, 0.086329, 0.631297, 1],241 [0.538564, 0.085738, 0.629944, 1],242 [0.547302, 0.085346, 0.628367, 1],243 [0.555986, 0.085162, 0.626572, 1],244 [0.564615, 0.08519, 0.624563, 1],245 [0.573187, 0.085439, 0.622345, 1],246 [0.581698, 0.085913, 0.619926, 1],247 [0.590149, 0.086615, 0.617311, 1],248 [0.598538, 0.087543, 0.614503, 1],249 [0.606862, 0.0887, 0.611511, 1],250 [0.61512, 0.090084, 0.608343, 1],251 [0.623312, 0.09169, 0.605001, 1],252 [0.631438, 0.093511, 0.601489, 1],253 [0.639492, 0.095546, 0.597821, 1],254 [0.647476, 0.097787, 0.593999, 1],255 [0.655389, 0.100226, 0.590028, 1],256 [0.66323, 0.102856, 0.585914, 1],257 [0.670995, 0.105669, 0.581667, 1],258 [0.678686, 0.108658, 0.577291, 1],259 [0.686302, 0.111813, 0.57279, 1],260 [0.69384, 0.115129, 0.568175, 1],261 [0.7013, 0.118597, 0.563449, 1],262 [0.708682, 0.122209, 0.558616, 1],263 [0.715984, 0.125959, 0.553687, 1],264 [0.723206, 0.12984, 0.548666, 1],265 [0.730346, 0.133846, 0.543558, 1],266 [0.737406, 0.13797, 0.538366, 1],267 [0.744382, 0.142209, 0.533101, 1],268 [0.751274, 0.146556, 0.527767, 1],269 [0.758082, 0.151008, 0.522369, 1],270 [0.764805, 0.155559, 0.516912, 1],271 [0.771443, 0.160206, 0.511402, 1],272 [0.777995, 0.164946, 0.505845, 1],273 [0.784459, 0.169774, 0.500246, 1],274 [0.790836, 0.174689, 0.494607, 1],275 [0.797125, 0.179688, 0.488935, 1],276 [0.803325, 0.184767, 0.483238, 1],277 [0.809435, 0.189925, 0.477518, 1],278 [0.815455, 0.19516, 0.471781, 1],279 [0.821384, 0.200471, 0.466028, 1],280 [0.827222, 0.205854, 0.460267, 1],281 [0.832968, 0.211308, 0.454505, 1],282 [0.838621, 0.216834, 0.448738, 1],283 [0.844181, 0.222428, 0.442979, 1],284 [0.849647, 0.22809, 0.43723, 1],285 [0.855019, 0.233819, 0.431491, 1],286 [0.860295, 0.239613, 0.425771, 1],287 [0.865475, 0.245471, 0.420074, 1],288 [0.870558, 0.251393, 0.414403, 1],289 [0.875545, 0.25738, 0.408759, 1],290 [0.880433, 0.263427, 0.403152, 1],291 [0.885223, 0.269535, 0.397585, 1],292 [0.889913, 0.275705, 0.392058, 1],293 [0.894503, 0.281934, 0.386578, 1],294 [0.898993, 0.288222, 0.381152, 1],295 [0.903381, 0.294569, 0.375781, 1],296 [0.907667, 0.300974, 0.370469, 1],297 [0.911849, 0.307435, 0.365223, 1],298 [0.915928, 0.313953, 0.360048, 1],299 [0.919902, 0.320527, 0.354948, 1],300 [0.923771, 0.327155, 0.349928, 1],301 [0.927533, 0.333838, 0.344994, 1],302 [0.931188, 0.340576, 0.340149, 1],303 [0.934736, 0.347366, 0.335403, 1],304 [0.938175, 0.354207, 0.330762, 1],305 [0.941504, 0.361101, 0.326229, 1],306 [0.944723, 0.368045, 0.321814, 1],307 [0.947831, 0.375039, 0.317523, 1],308 [0.950826, 0.382083, 0.313364, 1],309 [0.953709, 0.389175, 0.309345, 1],310 [0.956478, 0.396314, 0.305477, 1],311 [0.959133, 0.403499, 0.301766, 1],312 [0.961671, 0.410731, 0.298221, 1],313 [0.964093, 0.418008, 0.294853, 1],314 [0.966399, 0.425327, 0.291676, 1],315 [0.968586, 0.43269, 0.288696, 1],316 [0.970654, 0.440095, 0.285926, 1],317 [0.972603, 0.44754, 0.28338, 1],318 [0.974431, 0.455025, 0.281067, 1],319 [0.976139, 0.462547, 0.279003, 1],320 [0.977725, 0.470107, 0.277198, 1],321 [0.979188, 0.477703, 0.275666, 1],322 [0.980529, 0.485332, 0.274422, 1],323 [0.981747, 0.492995, 0.273476, 1],324 [0.98284, 0.50069, 0.272842, 1],325 [0.983808, 0.508415, 0.272532, 1],326 [0.984653, 0.516168, 0.27256, 1],327 [0.985373, 0.523948, 0.272937, 1],328 [0.985966, 0.531754, 0.273673, 1],329 [0.986436, 0.539582, 0.274779, 1],330 [0.98678, 0.547434, 0.276264, 1],331 [0.986998, 0.555305, 0.278135, 1],332 [0.987091, 0.563195, 0.280401, 1],333 [0.987061, 0.5711, 0.283066, 1],334 [0.986907, 0.579019, 0.286137, 1],335 [0.986629, 0.58695, 0.289615, 1],336 [0.986229, 0.594891, 0.293503, 1],337 [0.985709, 0.602839, 0.297802, 1],338 [0.985069, 0.610792, 0.302512, 1],339 [0.98431, 0.618748, 0.307632, 1],340 [0.983435, 0.626704, 0.313159, 1],341 [0.982445, 0.634657, 0.319089, 1],342 [0.981341, 0.642606, 0.32542, 1],343 [0.98013, 0.650546, 0.332144, 1],344 [0.978812, 0.658475, 0.339257, 1],345 [0.977392, 0.666391, 0.346753, 1],346 [0.97587, 0.67429, 0.354625, 1],347 [0.974252, 0.68217, 0.362865, 1],348 [0.972545, 0.690026, 0.371466, 1],349 [0.97075, 0.697856, 0.380419, 1],350 [0.968873, 0.705658, 0.389718, 1],351 [0.966921, 0.713426, 0.399353, 1],352 [0.964901, 0.721157, 0.409313, 1],353 [0.962815, 0.728851, 0.419594, 1],354 [0.960677, 0.7365, 0.430181, 1],355 [0.95849, 0.744103, 0.44107, 1],356 [0.956263, 0.751656, 0.452248, 1],357 [0.954009, 0.759153, 0.463702, 1],358 [0.951732, 0.766595, 0.475429, 1],359 [0.949445, 0.773974, 0.487414, 1],360 [0.947158, 0.781289, 0.499647, 1],361 [0.944885, 0.788535, 0.512116, 1],362 [0.942634, 0.795709, 0.524811, 1],363 [0.940423, 0.802807, 0.537717, 1],364 [0.938261, 0.809825, 0.550825, 1],365 [0.936163, 0.81676, 0.564121, 1],366 [0.934146, 0.823608, 0.577591, 1],367 [0.932224, 0.830366, 0.59122, 1],368 [0.930412, 0.837031, 0.604997, 1],369 [0.928727, 0.843599, 0.618904, 1],370 [0.927187, 0.850066, 0.632926, 1],371 [0.925809, 0.856432, 0.647047, 1],372 [0.92461, 0.862691, 0.661249, 1],373 [0.923607, 0.868843, 0.675517, 1],374 [0.92282, 0.874884, 0.689832, 1],375 [0.922265, 0.880812, 0.704174, 1],376 [0.921962, 0.886626, 0.718523, 1],377 [0.92193, 0.892323, 0.732859, 1],378 [0.922183, 0.897903, 0.747163, 1],379 [0.922741, 0.903364, 0.76141, 1],380 [0.92362, 0.908706, 0.77558, 1],381 [0.924837, 0.913928, 0.789648, 1],382 [0.926405, 0.919031, 0.80359, 1],383 [0.92834, 0.924015, 0.817381, 1],384 [0.930655, 0.928881, 0.830995, 1],385 [0.93336, 0.933631, 0.844405, 1],386 [0.936466, 0.938267, 0.857583, 1],387 [0.939982, 0.942791, 0.870499, 1],388 [0.943914, 0.947207, 0.883122, 1],389 [0.948267, 0.951519, 0.895421, 1],390 [0.953044, 0.955732, 0.907359, 1],391 [0.958246, 0.959852, 0.918901, 1],392 [0.963869, 0.963887, 0.930004, 1],393 [0.969909, 0.967845, 0.940623, 1],394 [0.976355, 0.971737, 0.950704, 1],395 [0.983195, 0.97558, 0.960181, 1],396 [0.990402, 0.979395, 0.968966, 1],397 [0.99793, 0.983217, 0.97692, 1],398 ],399};400/**401 * Set up color map based on options402 */403export function setupColorMap(colorMap = 'roseus') {404 if (colorMap && typeof colorMap !== 'string') {405 if (colorMap.length < 256) {406 throw new Error('Colormap must contain 256 elements');407 }408 for (let i = 0; i < colorMap.length; i++) {409 const cmEntry = colorMap[i];410 if (cmEntry.length !== 4) {411 throw new Error('ColorMap entries must contain 4 values');412 }413 }414 return colorMap;415 }416 const mapGenerator = COLOR_MAPS[colorMap];417 if (!mapGenerator) {418 throw Error("No such colormap '" + colorMap + "'");419 }420 return mapGenerator();421}422/**423 * Format frequency value for display424 */425export function freqType(freq) {426 return freq >= 1000 ? (freq / 1000).toFixed(1) : Math.round(freq).toString();427}428/**429 * Get frequency unit for display430 */431export function unitType(freq) {432 return freq >= 1000 ? 'kHz' : 'Hz';433}434/**435 * Get frequency value for label at given index436 */437export function getLabelFrequency(index, labelIndex, frequencyMin, frequencyMax, scale) {438 const scaleMin = hzToScale(frequencyMin, scale);439 const scaleMax = hzToScale(frequencyMax, scale);440 return scaleToHz(scaleMin + (index / labelIndex) * (scaleMax - scaleMin), scale);441}442/**443 * Create wrapper click handler for relative position calculation444 */445export function createWrapperClickHandler(wrapper, emit) {446 return (e) => {447 const rect = wrapper.getBoundingClientRect();448 const relativeX = e.clientX - rect.left;449 const relativeWidth = rect.width;450 const relativePosition = relativeX / relativeWidth;451 emit('click', relativePosition);452 };453}454/**455 * Calculate FFT - Based on https://github.com/corbanbrook/dsp.js456 */457function FFT(bufferSize, sampleRate, windowFunc, alpha) {458 this.bufferSize = bufferSize;459 this.sampleRate = sampleRate;460 this.bandwidth = (2 / bufferSize) * (sampleRate / 2);461 this.sinTable = new Float32Array(bufferSize);462 this.cosTable = new Float32Array(bufferSize);463 this.windowValues = new Float32Array(bufferSize);464 this.reverseTable = new Uint32Array(bufferSize);465 this.peakBand = 0;466 this.peak = 0;467 switch (windowFunc) {468 case 'bartlett':469 for (let i = 0; i < bufferSize; i++) {470 this.windowValues[i] = (2 / (bufferSize - 1)) * ((bufferSize - 1) / 2 - Math.abs(i - (bufferSize - 1) / 2));471 }472 break;473 case 'bartlettHann':474 for (let i = 0; i < bufferSize; i++) {475 this.windowValues[i] =476 0.62 - 0.48 * Math.abs(i / (bufferSize - 1) - 0.5) - 0.38 * Math.cos((Math.PI * 2 * i) / (bufferSize - 1));477 }478 break;479 case 'blackman':480 alpha = alpha || 0.16;481 for (let i = 0; i < bufferSize; i++) {482 this.windowValues[i] =483 (1 - alpha) / 2 -484 0.5 * Math.cos((Math.PI * 2 * i) / (bufferSize - 1)) +485 (alpha / 2) * Math.cos((4 * Math.PI * i) / (bufferSize - 1));486 }487 break;488 case 'cosine':489 for (let i = 0; i < bufferSize; i++) {490 this.windowValues[i] = Math.cos((Math.PI * i) / (bufferSize - 1) - Math.PI / 2);491 }492 break;493 case 'gauss':494 alpha = alpha || 0.25;495 for (let i = 0; i < bufferSize; i++) {496 this.windowValues[i] = Math.pow(Math.E, -0.5 * Math.pow((i - (bufferSize - 1) / 2) / ((alpha * (bufferSize - 1)) / 2), 2));497 }498 break;499 case 'hamming':500 for (let i = 0; i < bufferSize; i++) {501 this.windowValues[i] = 0.54 - 0.46 * Math.cos((Math.PI * 2 * i) / (bufferSize - 1));502 }503 break;504 case 'hann':505 case undefined:506 for (let i = 0; i < bufferSize; i++) {507 this.windowValues[i] = 0.5 * (1 - Math.cos((Math.PI * 2 * i) / (bufferSize - 1)));508 }509 break;510 case 'lanczoz':511 for (let i = 0; i < bufferSize; i++) {512 this.windowValues[i] =513 Math.sin(Math.PI * ((2 * i) / (bufferSize - 1) - 1)) / (Math.PI * ((2 * i) / (bufferSize - 1) - 1));514 }515 break;516 case 'rectangular':517 for (let i = 0; i < bufferSize; i++) {518 this.windowValues[i] = 1;519 }520 break;521 case 'triangular':522 for (let i = 0; i < bufferSize; i++) {523 this.windowValues[i] = (2 / bufferSize) * (bufferSize / 2 - Math.abs(i - (bufferSize - 1) / 2));524 }525 break;526 default:527 throw Error("No such window function '" + windowFunc + "'");528 }529 let limit = 1;530 let bit = bufferSize >> 1;531 while (limit < bufferSize) {532 for (let i = 0; i < limit; i++) {533 this.reverseTable[i + limit] = this.reverseTable[i] + bit;534 }535 limit = limit << 1;536 bit = bit >> 1;537 }538 for (let i = 0; i < bufferSize; i++) {539 this.sinTable[i] = Math.sin(-Math.PI / i);540 this.cosTable[i] = Math.cos(-Math.PI / i);541 }542 this.calculateSpectrum = function (buffer) {543 const bufferSize = this.bufferSize, cosTable = this.cosTable, sinTable = this.sinTable, reverseTable = this.reverseTable, real = new Float32Array(bufferSize), imag = new Float32Array(bufferSize), bSi = 2 / this.bufferSize, sqrt = Math.sqrt, spectrum = new Float32Array(bufferSize / 2);544 let rval, ival, mag;545 const k = Math.floor(Math.log(bufferSize) / Math.LN2);546 if (Math.pow(2, k) !== bufferSize) {547 throw 'Invalid buffer size, must be a power of 2.';548 }549 if (bufferSize !== buffer.length) {550 throw ('Supplied buffer is not the same size as defined FFT. FFT Size: ' +551 bufferSize +552 ' Buffer Size: ' +553 buffer.length);554 }555 let halfSize = 1, phaseShiftStepReal, phaseShiftStepImag, currentPhaseShiftReal, currentPhaseShiftImag, off, tr, ti, tmpReal;556 for (let i = 0; i < bufferSize; i++) {557 real[i] = buffer[reverseTable[i]] * this.windowValues[reverseTable[i]];558 imag[i] = 0;559 }560 while (halfSize < bufferSize) {561 phaseShiftStepReal = cosTable[halfSize];562 phaseShiftStepImag = sinTable[halfSize];563 currentPhaseShiftReal = 1;564 currentPhaseShiftImag = 0;565 for (let fftStep = 0; fftStep < halfSize; fftStep++) {566 let i = fftStep;567 while (i < bufferSize) {568 off = i + halfSize;569 tr = currentPhaseShiftReal * real[off] - currentPhaseShiftImag * imag[off];570 ti = currentPhaseShiftReal * imag[off] + currentPhaseShiftImag * real[off];571 real[off] = real[i] - tr;572 imag[off] = imag[i] - ti;573 real[i] += tr;574 imag[i] += ti;575 i += halfSize << 1;576 }577 tmpReal = currentPhaseShiftReal;578 currentPhaseShiftReal = tmpReal * phaseShiftStepReal - currentPhaseShiftImag * phaseShiftStepImag;579 currentPhaseShiftImag = tmpReal * phaseShiftStepImag + currentPhaseShiftImag * phaseShiftStepReal;580 }581 halfSize = halfSize << 1;582 }583 for (let i = 0, N = bufferSize / 2; i < N; i++) {584 rval = real[i];585 ival = imag[i];586 mag = bSi * sqrt(rval * rval + ival * ival);587 if (mag > this.peak) {588 this.peakBand = i;589 this.peak = mag;590 }591 spectrum[i] = mag;592 }593 return spectrum;594 };595}596export default FFT;597 