this repo has no description
0
fork

Configure Feed

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

peak normalization works again

alice e11ce136 50f5ee5c

+108 -458
+1 -1
src/core/core.c
··· 465 465 466 466 core->data = data; 467 467 468 + // TODO: make it conditional 468 469 FFT_GetFFT(fftData); 469 - // printf("FFT_GetFFT has been called\n"); 470 470 471 471 if (!core->state.initialized) 472 472 {
+101 -33
src/ext/fft.c
··· 16 16 ma_context context; 17 17 ma_device captureDevice; 18 18 float sampleBuf[FFT_SIZE * 2]; 19 - float fAmplification = 1.0f; 20 19 bool bCreated = false; 21 20 kiss_fft_cpx fftBuf[FFT_SIZE + 1]; 22 21 ··· 193 192 194 193 ////////////////////////////////////////////////////////////////////////// 195 194 196 - bool FFT_GetFFT( float * _samples ) 195 + bool FFT_GetFFT(float* _samples) 197 196 { 198 - kiss_fft_cpx out[ FFT_SIZE + 1 ]; 199 - kiss_fftr( fftcfg, sampleBuf, out ); 197 + kiss_fft_cpx out[FFT_SIZE + 1]; 198 + kiss_fftr(fftcfg, sampleBuf, out); 200 199 201 - // TODO: make it an option? 202 - bool bPeakNormalization = true; 203 - if (bPeakNormalization) { 204 - float peakValue = fPeakMinValue; 205 - for ( int i = 0; i < FFT_SIZE; i++ ) 206 - { 207 - float val = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i); 208 - if (val > peakValue) peakValue = val; 209 - _samples[ i ] = val * fAmplification; 200 + bool bPeakNormalization = true; 201 + if (bPeakNormalization) { 202 + float peakValue = fPeakMinValue; 203 + for (int i = 0; i < FFT_SIZE; i++) 204 + { 205 + float val = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i); 206 + if (val > peakValue) peakValue = val; 207 + } 208 + if (peakValue > fPeakSmoothValue) { 209 + fPeakSmoothValue = peakValue; 210 + } 211 + if (peakValue < fPeakSmoothValue) { 212 + fPeakSmoothValue = fPeakSmoothValue * fPeakSmoothing + peakValue * (1 - fPeakSmoothing); 213 + } 214 + fAmplification = 1.0f / fPeakSmoothValue; 215 + for (int i = 0; i < FFT_SIZE; i++) 216 + { 217 + float val = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i); 218 + _samples[i] = val * fAmplification; 219 + } 220 + } else { 221 + for (int i = 0; i < FFT_SIZE; i++) 222 + { 223 + static const float scaling = 1.0f / (float)FFT_SIZE; 224 + _samples[i] = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i) * scaling * fAmplification; 225 + } 210 226 } 211 - if (peakValue > fPeakSmoothValue) { 212 - fPeakSmoothValue = peakValue; 213 - } 214 - if (peakValue < fPeakSmoothValue) { 215 - fPeakSmoothValue = fPeakSmoothValue * fPeakSmoothing + peakValue * (1 - fPeakSmoothing); 216 - } 217 - fAmplification = 1.0f / fPeakSmoothValue; 218 - } else { 219 - for (int i = 0; i < FFT_SIZE; i++) 220 - { 221 - static const float scaling = 1.0f / (float)FFT_SIZE; 222 - _samples[i] = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i) * scaling * fAmplification; 223 - } 224 - } 225 - float fFFTSmoothingFactor = 0.9; 226 - for ( int i = 0; i < FFT_SIZE; i++ ) 227 - { 228 - _samples[i] = _samples[i] * fFFTSmoothingFactor + (1 - fFFTSmoothingFactor) * _samples[i]; 229 - } 230 227 231 - return true; 228 + return true; 232 229 } 230 + 231 + // bool FFT_GetFFT( float * _samples ) 232 + // { 233 + // kiss_fft_cpx out[ FFT_SIZE + 1 ]; 234 + // kiss_fftr( fftcfg, sampleBuf, out ); 235 + 236 + // bool bPeakNormalization = true; 237 + // if (bPeakNormalization) { 238 + // float peakValue = fPeakMinValue; 239 + // for ( int i = 0; i < FFT_SIZE; i++ ) 240 + // { 241 + // float val = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i); 242 + // if (val > peakValue) peakValue = val; 243 + // _samples[ i ] = val * fAmplification; 244 + // } 245 + // if (peakValue > fPeakSmoothValue) { 246 + // fPeakSmoothValue = peakValue; 247 + // } 248 + // if (peakValue < fPeakSmoothValue) { 249 + // fPeakSmoothValue = fPeakSmoothValue * fPeakSmoothing + peakValue * (1 - fPeakSmoothing); 250 + // } 251 + // fAmplification = 1.0f / fPeakSmoothValue; 252 + // } else { 253 + // for (int i = 0; i < FFT_SIZE; i++) 254 + // { 255 + // static const float scaling = 1.0f / (float)FFT_SIZE; 256 + // _samples[i] = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i) * scaling * fAmplification; 257 + // } 258 + // } 259 + 260 + // return true; 261 + // } 262 + 263 + 264 + // bool FFT_GetFFT( float * _samples ) 265 + // { 266 + // kiss_fft_cpx out[ FFT_SIZE + 1 ]; 267 + // kiss_fftr( fftcfg, sampleBuf, out ); 268 + 269 + // // TODO: make it an option? 270 + // bool bPeakNormalization = true; 271 + // if (bPeakNormalization) { 272 + // float peakValue = fPeakMinValue; 273 + // for ( int i = 0; i < FFT_SIZE; i++ ) 274 + // { 275 + // float val = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i); 276 + // if (val > peakValue) peakValue = val; 277 + // _samples[ i ] = val * fAmplification; 278 + // } 279 + // if (peakValue > fPeakSmoothValue) { 280 + // fPeakSmoothValue = peakValue; 281 + // } 282 + // if (peakValue < fPeakSmoothValue) { 283 + // fPeakSmoothValue = fPeakSmoothValue * fPeakSmoothing + peakValue * (1 - fPeakSmoothing); 284 + // } 285 + // fAmplification = 1.0f / fPeakSmoothValue; 286 + // } else { 287 + // for (int i = 0; i < FFT_SIZE; i++) 288 + // { 289 + // static const float scaling = 1.0f / (float)FFT_SIZE; 290 + // _samples[i] = 2.0f * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i) * scaling * fAmplification; 291 + // } 292 + // } 293 + // float fFFTSmoothingFactor = 0.9; 294 + // for ( int i = 0; i < FFT_SIZE; i++ ) 295 + // { 296 + // _samples[i] = _samples[i] * fFFTSmoothingFactor + (1 - fFFTSmoothingFactor) * _samples[i]; 297 + // } 298 + 299 + // return true; 300 + // } 233 301 234 302 double tic_api_fft(tic_mem* memory, s32 freq) 235 303 {
-2
src/ext/fft.h
··· 13 13 14 14 typedef void (*FFT_ENUMERATE_FUNC)(const bool bIsCaptureDevice, const char* szDeviceName, void* pDeviceID, void* pUserContext); 15 15 16 - extern float fAmplification; 17 - 18 16 void FFT_EnumerateDevices(FFT_ENUMERATE_FUNC pEnumerationFunction, void* pUserContext); 19 17 20 18 bool FFT_Create();
-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 - }
-210
src/ext/fft_tempold.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 - // source: https://github.com/Chatterino/chatterino2/blob/7c97e6bcc748755ee55447096ebc657be11b4789/src/controllers/sound/MiniaudioBackend.cpp#L28 75 - void miniaudioLogCallback(void *userData, ma_uint32 level, const char *message) 76 - { 77 - (void)userData; 78 - return; 79 - // printf("[FFT] [miniaudio:%p]\n %s", userData, message); 80 - // switch (level) 81 - // { 82 - // case MA_LOG_LEVEL_DEBUG: { 83 - // printf( "miniaudio debug: %s", message); 84 - // } 85 - // break; 86 - // case MA_LOG_LEVEL_INFO: { 87 - // printf( "miniaudio info: %s", message); 88 - // } 89 - // break; 90 - // case MA_LOG_LEVEL_WARNING: { 91 - // printf( "miniaudio warning: %s", message); 92 - // } 93 - // break; 94 - // case MA_LOG_LEVEL_ERROR: { 95 - // printf( "miniaudio error: %s", message); 96 - // } 97 - // break; 98 - // default: { 99 - // printf( "miniaudio unknown: %s", message); 100 - // } 101 - // break; 102 - // } 103 - } 104 - 105 - bool FFT_Create() 106 - { 107 - bCreated = false; 108 - ma_context_config context_config = ma_context_config_init(); 109 - ma_log log; 110 - ma_log_init(NULL, &log); 111 - ma_log_register_callback(&log, ma_log_callback_init(miniaudioLogCallback, NULL)); 112 - 113 - context_config.pLog = &log; 114 - ma_result result = ma_context_init(NULL, 0, &context_config, &context); 115 - if (result != MA_SUCCESS) 116 - { 117 - printf("[FFT] Failed to initialize context: %d", result); 118 - return false; 119 - } 120 - 121 - printf("[FFT] MAL context initialized, backend is '%s'\n", ma_get_backend_name(context.backend)); 122 - bCreated = true; 123 - return true; 124 - } 125 - 126 - bool FFT_Destroy() 127 - { 128 - if (!bCreated) 129 - { 130 - return false; 131 - } 132 - 133 - ma_context_uninit(&context); 134 - 135 - bCreated = false; 136 - 137 - return true; 138 - } 139 - 140 - bool FFT_Open(FFT_Settings* pSettings) 141 - { 142 - if (!bCreated) 143 - { 144 - return false; 145 - } 146 - 147 - memset(sampleBuf, 0, sizeof(float) * FFT_SIZE * 2); 148 - 149 - fftcfg = kiss_fftr_alloc(FFT_SIZE * 2, false, NULL, NULL); 150 - 151 - bool useLoopback = ma_is_loopback_supported(context.backend) && !pSettings->bUseRecordingDevice; 152 - ma_device_config config = ma_device_config_init(useLoopback ? ma_device_type_loopback : ma_device_type_capture); 153 - config.capture.pDeviceID = (ma_device_id*)pSettings->pDeviceID; 154 - config.capture.format = ma_format_f32; 155 - config.capture.channels = 2; 156 - config.sampleRate = 44100; 157 - config.dataCallback = OnReceiveFrames; 158 - config.pUserData = NULL; 159 - 160 - ma_result result = ma_device_init(&context, &config, &captureDevice); 161 - if (result != MA_SUCCESS) 162 - { 163 - printf("[FFT] Failed to initialize capture device: %d\n", result); 164 - return false; 165 - } 166 - 167 - printf("[FFT] Selected capture device: %s\n", captureDevice.capture.name); 168 - 169 - result = ma_device_start(&captureDevice); 170 - if (result != MA_SUCCESS) 171 - { 172 - ma_device_uninit(&captureDevice); 173 - printf("[FFT] Failed to start capture device: %d\n", result); 174 - return false; 175 - } 176 - 177 - return true; 178 - } 179 - 180 - void FFT_Close() 181 - { 182 - if (!bCreated) 183 - { 184 - return; 185 - } 186 - 187 - ma_device_stop(&captureDevice); 188 - 189 - ma_device_uninit(&captureDevice); 190 - 191 - kiss_fft_free(fftcfg); 192 - } 193 - 194 - ////////////////////////////////////////////////////////////////////////// 195 - 196 - double tic_api_fft(tic_mem* memory, s32 freq) 197 - { 198 - u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 199 - freq = freq * interval; 200 - freq = fmin(freq, FFT_SIZE); 201 - freq = fmax(freq, 0); 202 - 203 - static const float scaling = 1.0f / (float)FFT_SIZE; 204 - float res = 0; 205 - for (int i = freq; i < freq + interval; ++i) { 206 - res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 207 - } 208 - 209 - return res; 210 - }
+1
src/fftdata.c
··· 2 2 float fPeakMinValue = 0.01f; 3 3 float fPeakSmoothing = 0.995f; 4 4 float fPeakSmoothValue = 0.0f; 5 + float fAmplification = 1.0f; 5 6 float fftData[FFT_SIZE] = {0};
+5 -4
src/fftdata.h
··· 1 1 #pragma once 2 2 #define FFT_SIZE 1024 3 - float fPeakMinValue; 4 - float fPeakSmoothing; 5 - float fPeakSmoothValue; 6 - extern float fftData[FFT_SIZE]; 3 + extern float fPeakMinValue; 4 + extern float fPeakSmoothing; 5 + extern float fPeakSmoothValue; 6 + extern float fAmplification; 7 + extern float fftData[FFT_SIZE];