this repo has no description
0
fork

Configure Feed

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

nusan start

alice e85cdbd4 cd9a5c26

+223 -91
+15 -91
src/ext/fft.c
··· 192 192 193 193 ////////////////////////////////////////////////////////////////////////// 194 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}; 195 + static float fftDataSmoothed[FFT_SIZE] = {0}; // Initialize the array with zeros 196 + static const float fPeakMinValue = 0.01f; 197 + static const float fPeakSmoothing = 0.995f; 198 + static float fPeakSmoothValue = 0.0f; 202 199 203 - double tic_api_fft2(tic_mem* memory, s32 freq, bool normalize, double smooth) 200 + double tic_api_fft(tic_mem* memory, s32 freq/*, bool bPeakNormalization, bool bSmoothing*/) 204 201 { 205 202 u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 206 203 freq = freq * interval; 207 - freq = fmin(freq, FFT_SIZE - interval); 204 + freq = fmin(freq, FFT_SIZE); 208 205 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 - } 206 + 207 + bool bPeakNormalization = true; 208 + bool bSmoothing = true; 254 209 255 - double tic_api_fft(tic_mem* memory, s32 freq) { 256 - 257 - bool bPeakNormalization = true; 258 210 if (bPeakNormalization) { 259 211 float peakValue = fPeakMinValue; 260 - for (int i = 0; i < FFT_SIZE; i++) { 212 + for (int i = freq; i < freq + interval; ++i) { 261 213 float val = 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i); 262 214 if (val > peakValue) { 263 215 peakValue = val; ··· 271 223 } 272 224 if (fPeakSmoothValue > 0.0f) { 273 225 fAmplification = 1.0f / fPeakSmoothValue; 274 - // if (freq < 5) { 275 - // printf("freq: %d, fAmplification: %.2f\n", freq, fAmplification); 276 - // } 277 226 } else { 278 227 fAmplification = 1.0f; 279 228 } 280 229 } 281 230 282 - u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 283 - freq = freq * interval; 284 - freq = fmin(freq, FFT_SIZE); 285 - freq = fmax(freq, 0); 286 - 287 231 static const float scaling = 1.0f / (float)FFT_SIZE; 288 232 float res = 0; 289 - bool bSmoothing = true; 290 - 291 233 for (int i = freq; i < freq + interval; ++i) { 292 234 float val = 2.0f * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling * fAmplification; 293 235 if (bSmoothing) { 294 - fftDataSmoothed[i] = fftDataSmoothed[i] * fFFTSmoothingFactor + (1 - fFFTSmoothingFactor) * val; 295 - res += fftDataSmoothed[i]; 236 + if (i < FFT_SIZE) { 237 + fftDataSmoothed[i] = fftDataSmoothed[i] * fFFTSmoothingFactor + (1 - fFFTSmoothingFactor) * val; 238 + res += fftDataSmoothed[i]; 239 + } 296 240 } else { 297 241 res += val; 298 242 } 299 243 } 300 244 301 - // if (freq < 5) { 302 - // printf("freq: %d, res: %.2f\n", freq, res); 303 - // } 304 - 305 245 return res; 306 - } 307 - 308 - // double tic_api_fft(tic_mem* memory, s32 freq) 309 - // { 310 - // u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 311 - // freq = freq * interval; 312 - // freq = fmin(freq, FFT_SIZE); 313 - // freq = fmax(freq, 0); 314 - 315 - // static const float scaling = 1.0f / (float)FFT_SIZE; 316 - // float res = 0; 317 - // for (int i = freq; i < freq + interval; ++i) { 318 - // res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 319 - // } 320 - 321 - // return res; 322 - // } 246 + }
+208
src/ext/fft_orig.c
··· 1 + #define MA_DEBUG_OUTPUT 2 + #define MINIAUDIO_IMPLEMENTATION 3 + #include "fft.h" 4 + #include "miniaudio.h" 5 + 6 + #include "kiss_fft.h" 7 + #include "kiss_fftr.h" 8 + #include <stdio.h> 9 + #include <memory.h> 10 + #include "api.h" 11 + 12 + ////////////////////////////////////////////////////////////////////////// 13 + 14 + kiss_fftr_cfg fftcfg; 15 + ma_context context; 16 + ma_device captureDevice; 17 + float sampleBuf[FFT_SIZE * 2]; 18 + float fAmplification = 1.0f; 19 + bool bCreated = false; 20 + kiss_fft_cpx fftBuf[FFT_SIZE + 1]; 21 + 22 + void OnReceiveFrames(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) 23 + { 24 + frameCount = frameCount < FFT_SIZE * 2 ? frameCount : FFT_SIZE * 2; 25 + 26 + // Just rotate the buffer; copy existing, append new 27 + const float* samples = (const float*)pInput; 28 + float* p = sampleBuf; 29 + for (int i = 0; i < FFT_SIZE * 2 - frameCount; i++) 30 + { 31 + *(p++) = sampleBuf[i + frameCount]; 32 + } 33 + for (int i = 0; i < frameCount; i++) 34 + { 35 + *(p++) = (samples[i * 2] + samples[i * 2 + 1]) / 2.0f * fAmplification; 36 + } 37 + 38 + kiss_fftr(fftcfg, sampleBuf, fftBuf); 39 + } 40 + 41 + void FFT_EnumerateDevices(FFT_ENUMERATE_FUNC pEnumerationFunction, void* pUserContext) 42 + { 43 + if (!bCreated) 44 + { 45 + return; 46 + } 47 + 48 + ma_device_info* pPlaybackDevices = NULL; 49 + ma_device_info* pCaptureDevices = NULL; 50 + ma_uint32 nPlaybackDeviceCount = 0; 51 + ma_uint32 nCaptureDeviceCount = 0; 52 + ma_result result = ma_context_get_devices(&context, &pPlaybackDevices, &nPlaybackDeviceCount, &pCaptureDevices, &nCaptureDeviceCount); 53 + if (result != MA_SUCCESS) 54 + { 55 + printf("[FFT] Failed to enumerate audio devices: %d\n", result); 56 + return; 57 + } 58 + 59 + pEnumerationFunction(true, "<default device>", NULL, pUserContext); 60 + for (ma_uint32 i = 0; i < nCaptureDeviceCount; i++) 61 + { 62 + pEnumerationFunction(true, pCaptureDevices[i].name, &pCaptureDevices[i].id, pUserContext); 63 + } 64 + if (ma_is_loopback_supported(context.backend)) 65 + { 66 + pEnumerationFunction(false, "<default device>", NULL, pUserContext); 67 + for (ma_uint32 i = 0; i < nPlaybackDeviceCount; i++) 68 + { 69 + pEnumerationFunction(false, pPlaybackDevices[i].name, &pPlaybackDevices[i].id, pUserContext); 70 + } 71 + } 72 + } 73 + 74 + void miniaudioLogCallback(void *userData, ma_uint32 level, const char *message) 75 + { 76 + (void)userData; 77 + return; 78 + } 79 + 80 + bool FFT_Create() 81 + { 82 + bCreated = false; 83 + ma_context_config context_config = ma_context_config_init(); 84 + ma_log log; 85 + ma_log_init(NULL, &log); 86 + ma_log_register_callback(&log, ma_log_callback_init(miniaudioLogCallback, NULL)); 87 + 88 + context_config.pLog = &log; 89 + ma_result result = ma_context_init(NULL, 0, &context_config, &context); 90 + if (result != MA_SUCCESS) 91 + { 92 + printf("[FFT] Failed to initialize context: %d", result); 93 + return false; 94 + } 95 + 96 + printf("[FFT] MAL context initialized, backend is '%s'\n", ma_get_backend_name(context.backend)); 97 + bCreated = true; 98 + return true; 99 + } 100 + 101 + bool FFT_Destroy() 102 + { 103 + if (!bCreated) 104 + { 105 + return false; 106 + } 107 + 108 + ma_context_uninit(&context); 109 + 110 + bCreated = false; 111 + 112 + return true; 113 + } 114 + 115 + bool FFT_Open(FFT_Settings* pSettings) 116 + { 117 + if (!bCreated) 118 + { 119 + return false; 120 + } 121 + 122 + memset(sampleBuf, 0, sizeof(float) * FFT_SIZE * 2); 123 + 124 + fftcfg = kiss_fftr_alloc(FFT_SIZE * 2, false, NULL, NULL); 125 + 126 + ma_device_info* pPlaybackDeviceInfos; 127 + ma_uint32 playbackDeviceCount; 128 + ma_device_info* pCaptureDeviceInfos; 129 + ma_uint32 captureDeviceCount; 130 + ma_result result = ma_context_get_devices(&context, &pPlaybackDeviceInfos, &playbackDeviceCount, &pCaptureDeviceInfos, &captureDeviceCount); 131 + if (result != MA_SUCCESS) { 132 + printf("Failed to retrieve device information.\n"); 133 + return false; 134 + } 135 + 136 + printf("Playback Devices\n"); 137 + for (ma_uint32 iDevice = 0; iDevice < playbackDeviceCount; ++iDevice) { 138 + printf(" %u: %s\n", iDevice, pPlaybackDeviceInfos[iDevice].name); 139 + } 140 + 141 + printf("\n"); 142 + 143 + printf("Capture Devices\n"); 144 + for (ma_uint32 iDevice = 0; iDevice < captureDeviceCount; ++iDevice) { 145 + printf(" %u: %s\n", iDevice, pCaptureDeviceInfos[iDevice].name); 146 + } 147 + 148 + bool useLoopback = ma_is_loopback_supported(context.backend) && !pSettings->bUseRecordingDevice; 149 + ma_device_config config = ma_device_config_init(useLoopback ? ma_device_type_loopback : ma_device_type_capture); 150 + config.capture.pDeviceID = (ma_device_id*)pSettings->pDeviceID; 151 + config.capture.format = ma_format_f32; 152 + config.capture.channels = 2; 153 + config.sampleRate = 44100; 154 + config.dataCallback = OnReceiveFrames; 155 + config.pUserData = NULL; 156 + 157 + result = ma_device_init(&context, &config, &captureDevice); 158 + if (result != MA_SUCCESS) 159 + { 160 + ma_context_uninit(&context); 161 + printf("[FFT] Failed to initialize capture device: %d\n", result); 162 + return false; 163 + } 164 + 165 + printf("[FFT] Selected capture device: %s\n", captureDevice.capture.name); 166 + 167 + result = ma_device_start(&captureDevice); 168 + if (result != MA_SUCCESS) 169 + { 170 + ma_device_uninit(&captureDevice); 171 + ma_context_uninit(&context); 172 + printf("[FFT] Failed to start capture device: %d\n", result); 173 + return false; 174 + } 175 + 176 + return true; 177 + } 178 + 179 + void FFT_Close() 180 + { 181 + if (!bCreated) 182 + { 183 + return; 184 + } 185 + 186 + ma_device_stop(&captureDevice); 187 + 188 + ma_device_uninit(&captureDevice); 189 + 190 + kiss_fft_free(fftcfg); 191 + } 192 + 193 + ////////////////////////////////////////////////////////////////////////// 194 + double tic_api_fft(tic_mem* memory, s32 freq) 195 + { 196 + u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 197 + freq = freq * interval; 198 + freq = fmin(freq, FFT_SIZE); 199 + freq = fmax(freq, 0); 200 + 201 + static const float scaling = 1.0f / (float)FFT_SIZE; 202 + float res = 0; 203 + for (int i = freq; i < freq + interval; ++i) { 204 + res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 205 + } 206 + 207 + return res; 208 + }