From f1e37f199c9df06b06e02a46d0a19603b5217505 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Mon, 9 Jun 2025 05:29:42 +0800
Subject: [PATCH 20/23] viz.c: fix syntax

---
 src/viz.c | 248 +++++++++++++++++++++++++++---------------------------
 1 file changed, 126 insertions(+), 122 deletions(-)

diff --git a/src/viz.c b/src/viz.c
index dc692fbe1..a57fc33d1 100644
--- a/src/viz.c
+++ b/src/viz.c
@@ -132,156 +132,160 @@ viz_waveform_unlisten(void *ctx) {
     dispatch_sync_f(sync_queue, &args, viz_waveform_unlisten_f);
 }
 
+typedef struct {
+    void *ctx;
+    void (*callback)(void *ctx, const ddb_audio_data_t *data);
+} spec_listen_args_t;
+    static void spec_listen_f(void *p) {
+    spec_listen_args_t *args = p;
+    wavedata_listener_t *l = malloc(sizeof(wavedata_listener_t));
+    memset(l, 0, sizeof(wavedata_listener_t));
+    l->ctx = args->ctx;
+    l->callback = args->callback;
+    l->next = spectrum_listeners;
+    spectrum_listeners = l;
+}
+
 void
 viz_spectrum_listen (void *ctx, void (*callback)(void *ctx, const ddb_audio_data_t *data)) {
-    typedef struct {
-        void *ctx;
-        void (*callback)(void *ctx, const ddb_audio_data_t *data);
-    } spec_listen_args_t;
-    static void spec_listen_f(void *p) {
-        spec_listen_args_t *args = p;
-        wavedata_listener_t *l = malloc(sizeof(wavedata_listener_t));
-        memset(l, 0, sizeof(wavedata_listener_t));
-        l->ctx = args->ctx;
-        l->callback = args->callback;
-        l->next = spectrum_listeners;
-        spectrum_listeners = l;
-    }
     spec_listen_args_t args = { ctx, callback };
     dispatch_sync_f(sync_queue, &args, spec_listen_f);
 }
 
-void
-viz_spectrum_unlisten (void *ctx) {
-    static void spec_unlisten_f(void *p) {
-        void *ctx = p;
-        wavedata_listener_t *l, *prev = NULL;
-        for (l = spectrum_listeners; l; prev = l, l = l->next) {
-            if (l->ctx == ctx) {
-                if (prev) {
-                    prev->next = l->next;
-                }
-                else {
-                    spectrum_listeners = l->next;
-                }
-                free(l);
-                break;
+static void spec_unlisten_f(void *p) {
+    void *ctx = p;
+    wavedata_listener_t *l, *prev = NULL;
+    for (l = spectrum_listeners; l; prev = l, l = l->next) {
+        if (l->ctx == ctx) {
+            if (prev) {
+                prev->next = l->next;
             }
+            else {
+                spectrum_listeners = l->next;
+            }
+            free(l);
+            break;
         }
     }
+}
+
+void
+viz_spectrum_unlisten (void *ctx) {
     dispatch_sync_f(sync_queue, ctx, spec_unlisten_f);
 }
 
+static void reset_f(void *p) {
+    (void)p;
+    _need_reset = 1;
+}
+
 void
 viz_reset(void) {
-    static void reset_f(void *p) {
-        (void)p;
-        _need_reset = 1;
-    }
     dispatch_sync_f(sync_queue, NULL, reset_f);
 }
 
-void
-viz_process (char * restrict _bytes, int _bytes_size, DB_output_t *output, int fft_size, int wave_size) {
-    typedef struct {
-        char *bytes;
-        int bytes_size;
-        DB_output_t *output;
-        int fft_size;
-        int wave_size;
-    } viz_process_args_t;
-    typedef struct {
-        ddb_audio_data_t *waveform_data;
-        float *data;
-        ddb_waveformat_t *out_fmt;
-    } viz_process_async_args_t;
-    static void viz_process_async_f(void *p) {
-        viz_process_async_args_t *args = p;
-        for (wavedata_listener_t *l = waveform_listeners; l; l = l->next) {
-            l->callback(l->ctx, args->waveform_data);
-        }
-        free(args->data);
-        free(args->out_fmt);
-        free(args->waveform_data);
-        free(args);
+typedef struct {
+    char *bytes;
+    int bytes_size;
+    DB_output_t *output;
+    int fft_size;
+    int wave_size;
+} viz_process_args_t;
+typedef struct {
+    ddb_audio_data_t *waveform_data;
+    float *data;
+    ddb_waveformat_t *out_fmt;
+} viz_process_async_args_t;
+static void viz_process_async_f(void *p) {
+    viz_process_async_args_t *args = p;
+    for (wavedata_listener_t *l = waveform_listeners; l; l = l->next) {
+        l->callback(l->ctx, args->waveform_data);
+    }
+    free(args->data);
+    free(args->out_fmt);
+    free(args->waveform_data);
+    free(args);
+}
+static void viz_process_sync_f(void *p) {
+    viz_process_args_t *args = p;
+    char *bytes = args->bytes;
+    int _bytes_size = args->bytes_size;
+    DB_output_t *output = args->output;
+    int fft_size = args->fft_size;
+    int wave_size = args->wave_size;
+
+    _init_buffers(fft_size);
+    if (!waveform_listeners && !spectrum_listeners) {
+        return;
     }
-    static void viz_process_sync_f(void *p) {
-        viz_process_args_t *args = p;
-        char *bytes = args->bytes;
-        int _bytes_size = args->bytes_size;
-        DB_output_t *output = args->output;
-        int fft_size = args->fft_size;
-        int wave_size = args->wave_size;
-
-        _init_buffers(fft_size);
-        if (!waveform_listeners && !spectrum_listeners) {
-            return;
-        }
 
-        if (output->fmt.flags & DDB_WAVEFORMAT_FLAG_IS_DOP) {
-            bytes = NULL;
-        }
+    if (output->fmt.flags & DDB_WAVEFORMAT_FLAG_IS_DOP) {
+        bytes = NULL;
+    }
 
-        ddb_waveformat_t *out_fmt = calloc(1, sizeof(ddb_waveformat_t));
-        out_fmt->bps = 32;
-        out_fmt->channels = output->fmt.channels;
-        out_fmt->samplerate = output->fmt.samplerate;
-        out_fmt->channelmask = output->fmt.channelmask;
-        out_fmt->is_float = 1;
-
-        const int fft_nframes = fft_size * 2;
-        const int output_nframes = fft_nframes > wave_size ? fft_nframes : wave_size;
-        const int final_output_size = output_nframes * out_fmt->channels * sizeof(float);
-        const int final_input_size = output_nframes * output->fmt.channels * (output->fmt.bps/8);
-        float *data = calloc(1, final_output_size);
-
-        if (bytes != NULL) {
-            const int convert_size = _bytes_size < final_input_size ? _bytes_size : final_input_size;
-            pcm_convert(&output->fmt, bytes, out_fmt, (char *)data, convert_size);
-        }
+    ddb_waveformat_t *out_fmt = calloc(1, sizeof(ddb_waveformat_t));
+    out_fmt->bps = 32;
+    out_fmt->channels = output->fmt.channels;
+    out_fmt->samplerate = output->fmt.samplerate;
+    out_fmt->channelmask = output->fmt.channelmask;
+    out_fmt->is_float = 1;
+
+    const int fft_nframes = fft_size * 2;
+    const int output_nframes = fft_nframes > wave_size ? fft_nframes : wave_size;
+    const int final_output_size = output_nframes * out_fmt->channels * sizeof(float);
+    const int final_input_size = output_nframes * output->fmt.channels * (output->fmt.bps/8);
+    float *data = calloc(1, final_output_size);
+
+    if (bytes != NULL) {
+        const int convert_size = _bytes_size < final_input_size ? _bytes_size : final_input_size;
+        pcm_convert(&output->fmt, bytes, out_fmt, (char *)data, convert_size);
+    }
 
-        ddb_audio_data_t *waveform_data = calloc(1, sizeof(ddb_audio_data_t));
-        waveform_data->fmt = out_fmt;
-        waveform_data->data = data;
-        waveform_data->nframes = wave_size;
+    ddb_audio_data_t *waveform_data = calloc(1, sizeof(ddb_audio_data_t));
+    waveform_data->fmt = out_fmt;
+    waveform_data->data = data;
+    waveform_data->nframes = wave_size;
 
-        if (_need_reset || out_fmt->channels != audio_data_channels || !spectrum_listeners) {
-            audio_data_channels = out_fmt->channels;
-            memset(_freq_data, 0, sizeof(float) * _fft_size * DDB_FREQ_MAX_CHANNELS);
-            memset(_audio_data, 0, sizeof(float) * _fft_size * 2 * DDB_FREQ_MAX_CHANNELS);
-            _need_reset = 0;
-        }
+    if (_need_reset || out_fmt->channels != audio_data_channels || !spectrum_listeners) {
+        audio_data_channels = out_fmt->channels;
+        memset(_freq_data, 0, sizeof(float) * _fft_size * DDB_FREQ_MAX_CHANNELS);
+        memset(_audio_data, 0, sizeof(float) * _fft_size * 2 * DDB_FREQ_MAX_CHANNELS);
+        _need_reset = 0;
+    }
 
-        if (spectrum_listeners) {
-            assert(fft_nframes == _fft_size * 2);
-            for (int c = 0; c < audio_data_channels; c++) {
-                float *channel = &_audio_data[_fft_size * 2 * c];
-                for (int s = 0; s < fft_nframes; s++) {
-                    channel[s] = data[s * audio_data_channels + c];
-                }
+    if (spectrum_listeners) {
+        assert(fft_nframes == _fft_size * 2);
+        for (int c = 0; c < audio_data_channels; c++) {
+            float *channel = &_audio_data[_fft_size * 2 * c];
+            for (int s = 0; s < fft_nframes; s++) {
+                channel[s] = data[s * audio_data_channels + c];
             }
+        }
 
-            if (_audio_data != NULL) {
-                for (int c = 0; c < audio_data_channels; c++) {
-                    fft_calculate(&_audio_data[_fft_size * 2 * c], &_freq_data[_fft_size * c], _fft_size);
-                }
-            }
-            ddb_audio_data_t spectrum_data = {
-                .fmt = out_fmt,
-                .data = _freq_data,
-                .nframes = _fft_size
-            };
-            for (wavedata_listener_t *l = spectrum_listeners; l; l = l->next) {
-                l->callback(l->ctx, &spectrum_data);
+        if (_audio_data != NULL) {
+            for (int c = 0; c < audio_data_channels; c++) {
+                fft_calculate(&_audio_data[_fft_size * 2 * c], &_freq_data[_fft_size * c], _fft_size);
             }
         }
-
-        viz_process_async_args_t *async_args = malloc(sizeof(viz_process_async_args_t));
-        async_args->waveform_data = waveform_data;
-        async_args->data = data;
-        async_args->out_fmt = out_fmt;
-        dispatch_async_f(process_queue, async_args, viz_process_async_f);
+        ddb_audio_data_t spectrum_data = {
+            .fmt = out_fmt,
+            .data = _freq_data,
+            .nframes = _fft_size
+        };
+        for (wavedata_listener_t *l = spectrum_listeners; l; l = l->next) {
+            l->callback(l->ctx, &spectrum_data);
+        }
     }
+
+    viz_process_async_args_t *async_args = malloc(sizeof(viz_process_async_args_t));
+    async_args->waveform_data = waveform_data;
+    async_args->data = data;
+    async_args->out_fmt = out_fmt;
+    dispatch_async_f(process_queue, async_args, viz_process_async_f);
+}
+
+void
+viz_process (char * restrict _bytes, int _bytes_size, DB_output_t *output, int fft_size, int wave_size) {
     viz_process_args_t args = { _bytes, _bytes_size, output, fft_size, wave_size };
     dispatch_sync_f(sync_queue, &args, viz_process_sync_f);
 }
-- 
2.49.0

