From 7cac6625d9e213d1ff183e8f15612cebaa264412 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Sat, 11 Jun 2022 03:33:42 -0700 Subject: [PATCH] [Visualizer] Align memory allocations for DFT DFT should use aligned memory blocks for best results. Also allocate one extra sample for DFT output, just in case DFT zop is as bad as zrop. Signed-off-by: Christopher Snowhill --- Audio/ThirdParty/deadbeef/fft_accelerate.c | 23 ++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Audio/ThirdParty/deadbeef/fft_accelerate.c b/Audio/ThirdParty/deadbeef/fft_accelerate.c index 320731c1f..cd2e17656 100644 --- a/Audio/ThirdParty/deadbeef/fft_accelerate.c +++ b/Audio/ThirdParty/deadbeef/fft_accelerate.c @@ -34,17 +34,28 @@ static float *_sq_mags; static vDSP_DFT_Setup _dft_setup; +// Apparently _mm_malloc is Intel-only on newer macOS targets, so use supported posix_memalign +static void *_memalign_calloc(size_t count, size_t size, size_t align) { + size *= count; + void *ret = NULL; + if(posix_memalign(&ret, align, size) != 0) { + return NULL; + } + bzero(ret, size); + return ret; +} + static void _init_buffers(int fft_size) { if(fft_size != _fft_size) { fft_free(); - _input_real = calloc(fft_size * 2, sizeof(float)); - _input_imaginary = calloc(fft_size * 2, sizeof(float)); - _hamming = calloc(fft_size * 2, sizeof(float)); - _sq_mags = calloc(fft_size, sizeof(float)); - _output_real = calloc(fft_size * 2, sizeof(float)); - _output_imaginary = calloc(fft_size * 2, sizeof(float)); + _input_real = _memalign_calloc(fft_size * 2, sizeof(float), 16); + _input_imaginary = _memalign_calloc(fft_size * 2, sizeof(float), 16); + _hamming = _memalign_calloc(fft_size * 2, sizeof(float), 16); + _sq_mags = _memalign_calloc(fft_size, sizeof(float), 16); + _output_real = _memalign_calloc(fft_size * 2 + 1, sizeof(float), 16); + _output_imaginary = _memalign_calloc(fft_size * 2 + 1, sizeof(float), 16); _dft_setup = vDSP_DFT_zop_CreateSetup(NULL, fft_size * 2, FFT_FORWARD); vDSP_hamm_window(_hamming, fft_size * 2, 0);