this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

two versions

alice cd9a5c26 7e87648a

+76 -12
+76 -12
src/ext/fft.c
··· 18 18 float fAmplification = 1.0f; 19 19 bool bCreated = false; 20 20 kiss_fft_cpx fftBuf[FFT_SIZE + 1]; 21 - // float fFFTSmoothingFactor = 0.9f; // higher value, smoother FFT 22 - // static float fftDataSmoothed[FFT_SIZE]; 23 21 24 22 void OnReceiveFrames(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) 25 23 { ··· 193 191 } 194 192 195 193 ////////////////////////////////////////////////////////////////////////// 194 + float fFFTSmoothingFactor = 0.9f; // higher value, smoother FFT 195 + float fPeakMinValue = 0.01f; 196 + float fPeakSmoothing = 0.995f; 197 + float fPeakSmoothValue = 0.0f; 198 + static float fftDataSmoothed[FFT_SIZE]; 199 + 200 + float peakValues[FFT_SIZE] = {0.0000001}; // Initialize peakValues array with a small value 201 + float ticValues[FFT_SIZE] = {0}; 202 + 203 + double tic_api_fft2(tic_mem* memory, s32 freq, bool normalize, double smooth) 204 + { 205 + u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 206 + freq = freq * interval; 207 + freq = fmin(freq, FFT_SIZE - interval); 208 + freq = fmax(freq, 0); 209 + 210 + float res = 0; 211 + static const float scaling = 1.0f / (float)FFT_SIZE; 212 + 213 + bool fPeakNormalize = true; 214 + if (fPeakNormalize) { 215 + float peakValue = fPeakMinValue; 216 + for (int i = freq; i < freq + interval; ++i) { 217 + float val = 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 218 + if (val > peakValue) peakValue = val; 219 + 220 + if (normalize) { 221 + peakValues[i] = fmax(peakValues[i] * 0.995f, val); 222 + ticValues[i] = val / peakValues[i]; 223 + val = ticValues[i] * fAmplification; 224 + } else { 225 + val *= fAmplification; 226 + } 227 + 228 + fftDataSmoothed[i] = fftDataSmoothed[i] * smooth + val * (1 - smooth); 229 + res += fftDataSmoothed[i]; 230 + } 231 + if (peakValue > fPeakSmoothValue) { 232 + fPeakSmoothValue = peakValue; 233 + } 234 + if (peakValue < fPeakSmoothValue) { 235 + fPeakSmoothValue = fPeakSmoothValue * fPeakSmoothing + peakValue * (1 - fPeakSmoothing); 236 + } 237 + fAmplification = 1.0f / fPeakSmoothValue; 238 + } else { 239 + for (int i = freq; i < freq + interval; ++i) { 240 + float val = 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 241 + 242 + if (normalize) { 243 + peakValues[i] = fmax(peakValues[i] * 0.995f, val); 244 + ticValues[i] = val / peakValues[i]; 245 + res += ticValues[i]; 246 + } else { 247 + res += val; 248 + } 249 + } 250 + } 251 + 252 + return res; 253 + } 254 + 196 255 double tic_api_fft(tic_mem* memory, s32 freq) { 197 - static const float fPeakMinValue = 0.01f; 198 - static const float fPeakSmoothing = 0.995f; 199 - static float fPeakSmoothValue = 0.0f; 200 256 201 257 bool bPeakNormalization = true; 202 258 if (bPeakNormalization) { ··· 215 271 } 216 272 if (fPeakSmoothValue > 0.0f) { 217 273 fAmplification = 1.0f / fPeakSmoothValue; 218 - if (freq < 10) { 219 - printf("freq: %d, fAmplification: %.2f\n", freq, fAmplification); 220 - } 274 + // if (freq < 5) { 275 + // printf("freq: %d, fAmplification: %.2f\n", freq, fAmplification); 276 + // } 221 277 } else { 222 278 fAmplification = 1.0f; 223 279 } ··· 230 286 231 287 static const float scaling = 1.0f / (float)FFT_SIZE; 232 288 float res = 0; 289 + bool bSmoothing = true; 290 + 233 291 for (int i = freq; i < freq + interval; ++i) { 234 - res += 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling * fAmplification; 292 + float val = 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling * fAmplification; 293 + if (bSmoothing) { 294 + fftDataSmoothed[i] = fftDataSmoothed[i] * fFFTSmoothingFactor + (1 - fFFTSmoothingFactor) * val; 295 + res += fftDataSmoothed[i]; 296 + } else { 297 + res += val; 298 + } 235 299 } 236 300 237 - if (freq < 10) { 238 - printf("freq: %d, res: %.2f\n", freq, res); 239 - } 301 + // if (freq < 5) { 302 + // printf("freq: %d, res: %.2f\n", freq, res); 303 + // } 240 304 241 305 return res; 242 306 }