diff options
Diffstat (limited to 'streamer.c')
-rw-r--r-- | streamer.c | 67 |
1 files changed, 31 insertions, 36 deletions
@@ -105,7 +105,6 @@ static char streambuffer[STREAM_BUFFER_SIZE]; static int bytes_until_next_song = 0; static uintptr_t mutex; static uintptr_t decodemutex; -static uintptr_t audio_mem_mutex; static int nextsong = -1; static int nextsong_pstate = -1; static int badsong = -1; @@ -136,8 +135,9 @@ static int streamer_buffering; static DB_FILE *streamer_file; // for vis plugins -static float freq_data[DDB_AUDIO_MEMORY_FRAMES]; -static float audio_data[DDB_AUDIO_MEMORY_FRAMES]; +static float freq_data[DDB_FREQ_BANDS]; +static float audio_data[DDB_FREQ_BANDS]; +static int audio_data_fill = 0; // message queue static struct handler_s *handler; @@ -145,7 +145,8 @@ static struct handler_s *handler; // visualization stuff typedef struct wavedata_listener_s { void *ctx; - void (*callback)(void *ctx, ddb_waveformat_t *fmt, const float *data, int nsamples); + int type; + void (*callback)(void *ctx, int type, ddb_waveformat_t *fmt, const float *data, int nsamples); struct wavedata_listener_s *next; } wavedata_listener_t; @@ -1889,7 +1890,6 @@ streamer_init (void) { #endif mutex = mutex_create (); decodemutex = mutex_create (); - audio_mem_mutex = mutex_create (); ringbuf_init (&streamer_ringbuf, streambuffer, STREAM_BUFFER_SIZE); @@ -1939,8 +1939,6 @@ streamer_free (void) { decodemutex = 0; mutex_free (mutex); mutex = 0; - mutex_free (audio_mem_mutex); - audio_mem_mutex = 0; streamer_dsp_chain_save(); @@ -2280,7 +2278,6 @@ streamer_read (char *bytes, int size) { #endif if (wavedata_listeners) { - mutex_lock (audio_mem_mutex); int in_frame_size = (output->fmt.bps >> 3) * output->fmt.channels; int in_frames = sz / in_frame_size; ddb_waveformat_t out_fmt = { @@ -2294,20 +2291,29 @@ streamer_read (char *bytes, int size) { float temp_audio_data[in_frames * out_fmt.channels]; pcm_convert (&output->fmt, bytes, &out_fmt, (char *)temp_audio_data, sz); - if (in_frames < DDB_AUDIO_MEMORY_FRAMES) { - memmove (audio_data, audio_data + in_frames, (DDB_AUDIO_MEMORY_FRAMES-in_frames)*sizeof (float)); - memcpy (audio_data + DDB_AUDIO_MEMORY_FRAMES - in_frames, temp_audio_data, sz); - } - else { - memcpy (audio_data, temp_audio_data, DDB_AUDIO_MEMORY_FRAMES * in_frame_size); - } - for (wavedata_listener_t *l = wavedata_listeners; l; l = l->next) { - l->callback (l->ctx, &out_fmt, temp_audio_data, in_frames); - } - - calc_freq (audio_data, freq_data); - mutex_unlock (audio_mem_mutex); + if (l->type == DDB_AUDIO_WAVEFORM) { + l->callback (l->ctx, l->type, &out_fmt, temp_audio_data, in_frames); + } + } + + int remaining = in_frames; + do { + int sz = DDB_FREQ_BANDS-audio_data_fill; + sz = min (sz, remaining); + memcpy (&audio_data[audio_data_fill], &temp_audio_data[in_frames-remaining], sz * sizeof (float)); + audio_data_fill += sz; + remaining -= sz; + if (audio_data_fill == DDB_FREQ_BANDS) { + calc_freq (audio_data, freq_data); + for (wavedata_listener_t *l = wavedata_listeners; l; l = l->next) { + if (l->type == DDB_AUDIO_FREQ) { + l->callback (l->ctx, l->type, &out_fmt, freq_data, DDB_FREQ_BANDS); + } + } + audio_data_fill = 0; + } + } while (remaining > 0); } if (!output->has_volume) { @@ -2581,31 +2587,20 @@ streamer_notify_order_changed (int prev_order, int new_order) { } void -audio_get_waveform_data (int type, float *data) { - DB_output_t *output = plug_get_output (); - if (!audio_mem_mutex || !output || output->state () == OUTPUT_STATE_STOPPED) { - memset (data, 0, sizeof (audio_data)); - return; - } - mutex_lock (audio_mem_mutex); - memcpy (data, type == DDB_AUDIO_WAVEFORM ? audio_data : freq_data, sizeof (audio_data)); - mutex_unlock (audio_mem_mutex); -} - -void -register_continuous_wavedata_listener (void *ctx, void (*callback)(void *ctx, ddb_waveformat_t *fmt, const float *data, int nsamples)) { +register_continuous_wavedata_listener (void *ctx, int type, void (*callback)(void *ctx, int type, ddb_waveformat_t *fmt, const float *data, int nsamples)) { wavedata_listener_t *l = malloc (sizeof (wavedata_listener_t)); memset (l, 0, sizeof (wavedata_listener_t)); l->ctx = ctx; + l->type = type; l->callback = callback; l->next = wavedata_listeners; wavedata_listeners = l; } void -unregister_continuous_wavedata_listener (void *ctx) { +unregister_continuous_wavedata_listener (void *ctx, int type) { wavedata_listener_t *l, *prev = NULL; - for (l = wavedata_listeners; l && l->ctx != ctx; prev = l, l = l->next); + for (l = wavedata_listeners; l && l->ctx != ctx && l->type != type; prev = l, l = l->next); if (l) { if (prev) { prev->next = l->next; |