this repo has no description
0
fork

Configure Feed

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

get up to parity with bonzomatic's fft.cpp; BOOL -> bool; add logging

alice 03890a54 e0736f30

+181 -157
+171 -147
src/ext/fft.c
··· 1 + #define MA_DEBUG_OUTPUT 1 2 #define MINIAUDIO_IMPLEMENTATION 2 3 #include "fft.h" 3 4 #include "miniaudio.h" ··· 8 9 #include <memory.h> 9 10 #include "api.h" 10 11 11 - #ifndef FALSE 12 - #define FALSE 0 13 - #define TRUE 1 14 - #endif 12 + ////////////////////////////////////////////////////////////////////////// 15 13 16 - ////////////////////////////////////////////////////////////////////////// 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]; 17 21 18 - kiss_fftr_cfg fftcfg; 19 - ma_context context; 20 - ma_device captureDevice; 21 - float sampleBuf[FFT_SIZE * 2]; 22 - float fAmplification = 1.0f; 23 - BOOL bCreated = FALSE; 24 - kiss_fft_cpx fftBuf[FFT_SIZE + 1]; 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 25 26 - void OnLog(ma_context* pContext, ma_device* pDevice, ma_uint32 logLevel, const char* message) 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++) 27 30 { 28 - // TODO: log 31 + *(p++) = sampleBuf[i + frameCount]; 29 32 } 30 - 31 - void OnReceiveFrames(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) 33 + for (int i = 0; i < frameCount; i++) 32 34 { 33 - frameCount = frameCount < FFT_SIZE * 2 ? frameCount : FFT_SIZE * 2; 35 + *(p++) = (samples[i * 2] + samples[i * 2 + 1]) / 2.0f * fAmplification; 36 + } 34 37 35 - // Just rotate the buffer; copy existing, append new 36 - const float* samples = (const float*)pInput; 37 - float* p = sampleBuf; 38 - for (int i = 0; i < FFT_SIZE * 2 - frameCount; i++) 39 - { 40 - *(p++) = sampleBuf[i + frameCount]; 41 - } 42 - for (int i = 0; i < frameCount; i++) 43 - { 44 - *(p++) = (samples[i * 2] + samples[i * 2 + 1]) / 2.0f * fAmplification; 45 - } 38 + kiss_fftr(fftcfg, sampleBuf, fftBuf); 39 + } 46 40 47 - kiss_fftr(fftcfg, sampleBuf, fftBuf); 41 + void FFT_EnumerateDevices(FFT_ENUMERATE_FUNC pEnumerationFunction, void* pUserContext) 42 + { 43 + if (!bCreated) 44 + { 45 + return; 48 46 } 49 47 50 - void FFT_EnumerateDevices(FFT_ENUMERATE_FUNC pEnumerationFunction, void* pUserContext) 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) 51 54 { 52 - if (!bCreated) 53 - { 54 - return; 55 - } 56 - 57 - ma_device_info* pPlaybackDevices = NULL; 58 - ma_device_info* pCaptureDevices = NULL; 59 - ma_uint32 nPlaybackDeviceCount = 0; 60 - ma_uint32 nCaptureDeviceCount = 0; 61 - ma_result result = ma_context_get_devices(&context, &pPlaybackDevices, &nPlaybackDeviceCount, &pCaptureDevices, &nCaptureDeviceCount); 62 - if (result != MA_SUCCESS) 63 - { 64 - printf("[FFT] Failed to enumerate audio devices: %d\n", result); 65 - return; 66 - } 67 - 68 - pEnumerationFunction(TRUE, "<default device>", NULL, pUserContext); 69 - for (ma_uint32 i = 0; i < nCaptureDeviceCount; i++) 70 - { 71 - pEnumerationFunction(TRUE, pCaptureDevices[i].name, &pCaptureDevices[i].id, pUserContext); 72 - } 73 - if (ma_is_loopback_supported(context.backend)) 74 - { 75 - pEnumerationFunction(FALSE, "<default device>", NULL, pUserContext); 76 - for (ma_uint32 i = 0; i < nPlaybackDeviceCount; i++) 77 - { 78 - pEnumerationFunction(FALSE, pPlaybackDevices[i].name, &pPlaybackDevices[i].id, pUserContext); 79 - } 80 - } 55 + printf("[FFT] Failed to enumerate audio devices: %d\n", result); 56 + return; 81 57 } 82 58 83 - BOOL FFT_Create() 59 + pEnumerationFunction(true, "<default device>", NULL, pUserContext); 60 + for (ma_uint32 i = 0; i < nCaptureDeviceCount; i++) 84 61 { 85 - bCreated = FALSE; 86 - ma_context_config context_config = ma_context_config_init(); 87 - // context_config.pLog = OnLog; 88 - context_config.pLog = NULL; 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; 62 + pEnumerationFunction(true, pCaptureDevices[i].name, &pCaptureDevices[i].id, pUserContext); 99 63 } 100 - 101 - BOOL FFT_Destroy() 64 + if (ma_is_loopback_supported(context.backend)) 102 65 { 103 - if (!bCreated) 66 + pEnumerationFunction(false, "<default device>", NULL, pUserContext); 67 + for (ma_uint32 i = 0; i < nPlaybackDeviceCount; i++) 104 68 { 105 - return FALSE; 69 + pEnumerationFunction(false, pPlaybackDevices[i].name, &pPlaybackDevices[i].id, pUserContext); 106 70 } 71 + } 72 + } 107 73 108 - ma_context_uninit(&context); 109 74 110 - bCreated = FALSE; 75 + void miniaudioLogCallback(void *userData, ma_uint32 level, const char *message) 76 + { 77 + (void)userData; 78 + printf("[FFT] [miniaudio:%p]\n %s", userData, message); 79 + // switch (level) 80 + // { 81 + // case MA_LOG_LEVEL_DEBUG: { 82 + // printf( "miniaudio debug: %s", message); 83 + // } 84 + // break; 85 + // case MA_LOG_LEVEL_INFO: { 86 + // printf( "miniaudio info: %s", message); 87 + // } 88 + // break; 89 + // case MA_LOG_LEVEL_WARNING: { 90 + // printf( "miniaudio warning: %s", message); 91 + // } 92 + // break; 93 + // case MA_LOG_LEVEL_ERROR: { 94 + // printf( "miniaudio error: %s", message); 95 + // } 96 + // break; 97 + // default: { 98 + // printf( "miniaudio unknown: %s", message); 99 + // } 100 + // break; 101 + // } 102 + } 111 103 112 - return TRUE; 113 - } 104 + bool FFT_Create() 105 + { 106 + bCreated = false; 107 + ma_context_config context_config = ma_context_config_init(); 108 + ma_log log; 109 + ma_log_init(NULL, &log); 110 + ma_log_register_callback(&log, ma_log_callback_init(miniaudioLogCallback, NULL)); 114 111 115 - BOOL FFT_Open(FFT_Settings* pSettings) 112 + context_config.pLog = &log; 113 + ma_result result = ma_context_init(NULL, 0, &context_config, &context); 114 + if (result != MA_SUCCESS) 116 115 { 117 - if (!bCreated) 118 - { 119 - return FALSE; 120 - } 121 - 122 - memset(sampleBuf, 0, sizeof(float) * FFT_SIZE * 2); 116 + printf("[FFT] Failed to initialize context: %d", result); 117 + return false; 118 + } 123 119 124 - fftcfg = kiss_fftr_alloc(FFT_SIZE * 2, FALSE, NULL, NULL); 120 + printf("[FFT] MAL context initialized, backend is '%s'\n", ma_get_backend_name(context.backend)); 121 + bCreated = true; 122 + return true; 123 + } 125 124 126 - BOOL useLoopback = ma_is_loopback_supported(context.backend) && !pSettings->bUseRecordingDevice; 127 - ma_device_config config = ma_device_config_init(useLoopback ? ma_device_type_loopback : ma_device_type_capture); 128 - config.capture.pDeviceID = (ma_device_id*)pSettings->pDeviceID; 129 - config.capture.format = ma_format_f32; 130 - config.capture.channels = 2; 131 - config.sampleRate = 44100; 132 - config.dataCallback = OnReceiveFrames; 133 - config.pUserData = NULL; 125 + bool FFT_Destroy() 126 + { 127 + if (!bCreated) 128 + { 129 + return false; 130 + } 134 131 135 - ma_result result = ma_device_init(&context, &config, &captureDevice); 136 - if (result != MA_SUCCESS) 137 - { 138 - printf("[FFT] Failed to initialize capture device: %d\n", result); 139 - return FALSE; 140 - } 132 + ma_context_uninit(&context); 141 133 142 - printf("[FFT] Selected capture device: %s\n", captureDevice.capture.name); 134 + bCreated = false; 143 135 144 - result = ma_device_start(&captureDevice); 145 - if (result != MA_SUCCESS) 146 - { 147 - ma_device_uninit(&captureDevice); 148 - printf("[FFT] Failed to start capture device: %d\n", result); 149 - return FALSE; 150 - } 136 + return true; 137 + } 151 138 152 - return TRUE; 153 - } 154 - BOOL FFT_GetFFT(float* _samples) 139 + bool FFT_Open(FFT_Settings* pSettings) 140 + { 141 + if (!bCreated) 155 142 { 156 - if (!bCreated) 157 - { 158 - return FALSE; 159 - } 143 + return false; 144 + } 160 145 161 - kiss_fft_cpx out[FFT_SIZE + 1]; 162 - kiss_fftr(fftcfg, sampleBuf, out); 146 + memset(sampleBuf, 0, sizeof(float) * FFT_SIZE * 2); 163 147 164 - for (int i = 0; i < FFT_SIZE; i++) 165 - { 166 - static const float scaling = 1.0f / (float)FFT_SIZE; 167 - _samples[i] = 2.0 * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i) * scaling; 168 - } 148 + fftcfg = kiss_fftr_alloc(FFT_SIZE * 2, false, NULL, NULL); 149 + 150 + bool useLoopback = ma_is_loopback_supported(context.backend) && !pSettings->bUseRecordingDevice; 151 + ma_device_config config = ma_device_config_init(useLoopback ? ma_device_type_loopback : ma_device_type_capture); 152 + config.capture.pDeviceID = (ma_device_id*)pSettings->pDeviceID; 153 + config.capture.format = ma_format_f32; 154 + config.capture.channels = 2; 155 + config.sampleRate = 44100; 156 + config.dataCallback = OnReceiveFrames; 157 + config.pUserData = NULL; 169 158 170 - return TRUE; 171 - } 172 - void FFT_Close() 159 + ma_result result = ma_device_init(&context, &config, &captureDevice); 160 + if (result != MA_SUCCESS) 173 161 { 174 - if (!bCreated) 175 - { 176 - return; 177 - } 162 + printf("[FFT] Failed to initialize capture device: %d\n", result); 163 + return false; 164 + } 178 165 179 - ma_device_stop(&captureDevice); 166 + printf("[FFT] Selected capture device: %s\n", captureDevice.capture.name); 180 167 168 + result = ma_device_start(&captureDevice); 169 + if (result != MA_SUCCESS) 170 + { 181 171 ma_device_uninit(&captureDevice); 172 + printf("[FFT] Failed to start capture device: %d\n", result); 173 + return false; 174 + } 182 175 183 - kiss_fft_free(fftcfg); 176 + return true; 177 + } 178 + bool FFT_GetFFT(float* _samples) 179 + { 180 + if (!bCreated) 181 + { 182 + return false; 184 183 } 185 184 186 - ////////////////////////////////////////////////////////////////////////// 185 + kiss_fft_cpx out[FFT_SIZE + 1]; 186 + kiss_fftr(fftcfg, sampleBuf, out); 187 187 188 - double tic_api_fft(tic_mem* memory, s32 freq) 188 + for (int i = 0; i < FFT_SIZE; i++) 189 + { 190 + static const float scaling = 1.0f / (float)FFT_SIZE; 191 + _samples[i] = 2.0 * sqrtf(out[i].r * out[i].r + out[i].i * out[i].i) * scaling; 192 + } 193 + 194 + return true; 195 + } 196 + void FFT_Close() 197 + { 198 + if (!bCreated) 189 199 { 190 - u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 191 - freq = freq * interval; 192 - freq = fmin(freq, FFT_SIZE); 193 - freq = fmax(freq, 0); 200 + return; 201 + } 202 + 203 + ma_device_stop(&captureDevice); 204 + 205 + ma_device_uninit(&captureDevice); 206 + 207 + kiss_fft_free(fftcfg); 208 + } 209 + 210 + ////////////////////////////////////////////////////////////////////////// 194 211 195 - static const float scaling = 1.0f / (float)FFT_SIZE; 196 - float res = 0; 197 - for (int i = freq; i < freq + interval; ++i) { 198 - res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 199 - } 212 + double tic_api_fft(tic_mem* memory, s32 freq) 213 + { 214 + u32 interval = FFT_SIZE / 256 / 2; // the 2 is to discard super high frequencies, they suck 215 + freq = freq * interval; 216 + freq = fmin(freq, FFT_SIZE); 217 + freq = fmax(freq, 0); 200 218 201 - return res; 219 + static const float scaling = 1.0f / (float)FFT_SIZE; 220 + float res = 0; 221 + for (int i = freq; i < freq + interval; ++i) { 222 + res += 2.0 * sqrtf(fftBuf[i].r * fftBuf[i].r + fftBuf[i].i * fftBuf[i].i) * scaling; 202 223 } 224 + 225 + return res; 226 + }
+8 -8
src/ext/fft.h
··· 1 1 #pragma once 2 2 #define FFT_SIZE 2048 3 + #include <stdbool.h> 3 4 4 - typedef int BOOL; 5 + // typedef int BOOL; 5 6 ////////////////////////////////////////////////////////////////////////// 6 7 7 8 typedef struct FFT_Settings 8 9 { 9 - BOOL bUseRecordingDevice; 10 + bool bUseRecordingDevice; 10 11 void* pDeviceID; 11 12 } FFT_Settings; 12 13 13 - typedef void (*FFT_ENUMERATE_FUNC)(const BOOL bIsCaptureDevice, const char* szDeviceName, void* pDeviceID, void* pUserContext); 14 + typedef void (*FFT_ENUMERATE_FUNC)(const bool bIsCaptureDevice, const char* szDeviceName, void* pDeviceID, void* pUserContext); 14 15 15 16 extern float fAmplification; 16 17 17 18 void FFT_EnumerateDevices(FFT_ENUMERATE_FUNC pEnumerationFunction, void* pUserContext); 18 19 19 - BOOL FFT_Create(); 20 - BOOL FFT_Destroy(); 21 - BOOL FFT_Open(FFT_Settings* pSettings); 22 - BOOL FFT_GetFFT(float* _samples); 20 + bool FFT_Create(); 21 + bool FFT_Destroy(); 22 + bool FFT_Open(FFT_Settings* pSettings); 23 + bool FFT_GetFFT(float* _samples); 23 24 void FFT_Close(); 24 25 25 26 ////////////////////////////////////////////////////////////////////////// 26 -
+1 -1
src/studio/studio.c
··· 2435 2435 return studio->alive; 2436 2436 } 2437 2437 2438 - void print_fft_devices(const int bIsCaptureDevice, const char *szDeviceName, void *pDeviceID, void *pUserContext) 2438 + void print_fft_devices(const bool bIsCaptureDevice, const char *szDeviceName, void *pDeviceID, void *pUserContext) 2439 2439 { 2440 2440 printf("%s device \"%s\"\n", bIsCaptureDevice ? "Input" : "Output", szDeviceName); 2441 2441 }
+1 -1
src/system/sdl/main.c
··· 306 306 } 307 307 308 308 void *deviceId = NULL; 309 - void find_fft_device_by_id(const int bIsCaptureDevice, const char *szDeviceName, void *pDeviceID, void *pUserContext) 309 + void find_fft_device_by_id(const bool bIsCaptureDevice, const char *szDeviceName, void *pDeviceID, void *pUserContext) 310 310 { 311 311 if (strcmp(szDeviceName, studio_config(platform.studio)->fftdevice) == 0) 312 312 {