this repo has no description
0
fork

Configure Feed

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

expermiental new fft.c

alice 4719a26e c476bb6f

+419 -210
+209 -210
src/ext/fft.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 + #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 + 195 + double tic_api_fft(tic_mem* memory, s32 freq) 196 + { 197 + u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 198 + freq = freq * interval; 199 + freq = fmin(freq, FFT_SIZE); 200 + freq = fmax(freq, 0); 201 + 202 + static const float scaling = 1.0f / (float)FFT_SIZE; 203 + float res = 0; 204 + for (int i = freq; i < freq + interval; ++i) { 205 + res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 206 + } 207 + 208 + return res; 209 + }
+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 + }