From 3865566f09067b22728386f455ff978afee57688 Mon Sep 17 00:00:00 2001 From: Sean Bartell Date: Sat, 13 Aug 2011 15:29:57 -0400 Subject: [PATCH 4/6] rbcodec refactoring: timestretch buffer allocation --- apps/main.c | 8 -------- apps/settings.c | 8 +++++++- apps/settings_list.c | 2 +- lib/rbcodec/dsp/dsp.c | 16 ++++++++++++---- lib/rbcodec/dsp/dsp.h | 3 ++- lib/rbcodec/dsp/tdspeed.c | 28 +++++++++++++++------------- lib/rbcodec/dsp/tdspeed.h | 3 ++- lib/rbcodec/test/warble.c | 3 +-- 8 files changed, 40 insertions(+), 31 deletions(-) diff --git a/apps/main.c b/apps/main.c index 9cb7245..5ff233b 100644 --- a/apps/main.c +++ b/apps/main.c @@ -406,12 +406,7 @@ static void init(void) #endif /* CONFIG_CODEC != SWCODEC */ scrobbler_init(); -#if CONFIG_CODEC == SWCODEC && defined (HAVE_PITCHSCREEN) - tdspeed_init(); -#endif /* CONFIG_CODEC == SWCODEC */ - audio_init(); - settings_apply_skins(); } @@ -662,9 +657,6 @@ static void init(void) tree_mem_init(); filetype_init(); scrobbler_init(); -#if CONFIG_CODEC == SWCODEC && defined (HAVE_PITCHSCREEN) - tdspeed_init(); -#endif /* CONFIG_CODEC == SWCODEC */ theme_init_buffer(); #if CONFIG_CODEC != SWCODEC diff --git a/apps/settings.c b/apps/settings.c index 9071648..7af74ef 100644 --- a/apps/settings.c +++ b/apps/settings.c @@ -72,6 +72,7 @@ #include "viewport.h" #include "statusbar-skinned.h" #include "bootchart.h" +#include "buffer.h" #if CONFIG_CODEC == MAS3507D void dac_line_in(bool enable); @@ -981,7 +982,12 @@ void settings_apply(bool read_disk) dsp_dither_enable(global_settings.dithering_enabled); #ifdef HAVE_PITCHSCREEN - dsp_timestretch_enable(global_settings.timestretch_enabled); + if (global_settings.timestretch_enabled) { + size_t size = dsp_timestretch_get_buffer_size(); + dsp_timestretch_enable(buffer_alloc(size)); + } else { + dsp_timestretch_enable(NULL); + } #endif dsp_set_compressor(global_settings.compressor_threshold, global_settings.compressor_makeup_gain, diff --git a/apps/settings_list.c b/apps/settings_list.c index 901c0e9..0e88c15 100644 --- a/apps/settings_list.c +++ b/apps/settings_list.c @@ -1414,7 +1414,7 @@ const struct settings_list settings[] = { #ifdef HAVE_PITCHSCREEN /* timestretch */ OFFON_SETTING(F_SOUNDSETTING, timestretch_enabled, LANG_TIMESTRETCH, false, - "timestretch enabled", dsp_timestretch_enable), + "timestretch enabled", NULL), #endif /* compressor */ diff --git a/lib/rbcodec/dsp/dsp.c b/lib/rbcodec/dsp/dsp.c index 9aba93a..b2b072c 100644 --- a/lib/rbcodec/dsp/dsp.c +++ b/lib/rbcodec/dsp/dsp.c @@ -320,17 +320,25 @@ static void tdspeed_setup(struct dsp_config *dspc) resample_buf = big_resample_buf; } -void dsp_timestretch_enable(bool enabled) +size_t dsp_timestretch_get_buffer_size() +{ + return tdspeed_get_buffer_size() + RESAMPLE_RATIO * RESAMPLE_RATIO + * SMALL_SAMPLE_BUF_COUNT * sizeof(int32_t); +} + +void dsp_timestretch_enable(void *buf) { /* Hook to set up timestretch buffer on first call to settings_apply() */ if (big_sample_buf_count < 0) /* Only do something on first call */ { - if (enabled) + if (buf) { + int32_t *ptr = (int32_t *)buf; /* Set up timestretch buffers */ big_sample_buf_count = SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO; big_sample_buf = small_resample_buf; - big_resample_buf = (int32_t *) buffer_alloc(big_sample_buf_count * RESAMPLE_RATIO * sizeof(int32_t)); + big_resample_buf = ptr; + tdspeed_init(ptr + big_sample_buf_count * RESAMPLE_RATIO); } else { @@ -354,7 +362,7 @@ int32_t dsp_get_timestretch() bool dsp_timestretch_available() { - return (global_settings.timestretch_enabled && big_sample_buf_count > 0); + return (big_sample_buf_count > 0); } #endif diff --git a/lib/rbcodec/dsp/dsp.h b/lib/rbcodec/dsp/dsp.h index e79b8be..79ef934 100644 --- a/lib/rbcodec/dsp/dsp.h +++ b/lib/rbcodec/dsp/dsp.h @@ -81,7 +81,8 @@ void dsp_set_eq_precut(int precut); void dsp_set_eq_coefs(int band, unsigned long cutoff, unsigned long q, long gain); void dsp_dither_enable(bool enable); -void dsp_timestretch_enable(bool enable); +size_t dsp_timestretch_get_buffer_size(void); +void dsp_timestretch_enable(void *buf); bool dsp_timestretch_available(void); void sound_set_pitch(int32_t r); int32_t sound_get_pitch(void); diff --git a/lib/rbcodec/dsp/tdspeed.c b/lib/rbcodec/dsp/tdspeed.c index 1c4979c..c5bd923 100644 --- a/lib/rbcodec/dsp/tdspeed.c +++ b/lib/rbcodec/dsp/tdspeed.c @@ -55,20 +55,22 @@ static struct tdspeed_state_s tdspeed_state; static int32_t *overlap_buffer[2] = { NULL, NULL }; static int32_t *outbuf[2] = { NULL, NULL }; -void tdspeed_init() +size_t tdspeed_get_buffer_size() { - if (global_settings.timestretch_enabled) - { - /* Allocate buffers */ - if (overlap_buffer[0] == NULL) - overlap_buffer[0] = (int32_t *) buffer_alloc(FIXED_BUFSIZE * sizeof(int32_t)); - if (overlap_buffer[1] == NULL) - overlap_buffer[1] = (int32_t *) buffer_alloc(FIXED_BUFSIZE * sizeof(int32_t)); - if (outbuf[0] == NULL) - outbuf[0] = (int32_t *) buffer_alloc(TDSPEED_OUTBUFSIZE * sizeof(int32_t)); - if (outbuf[1] == NULL) - outbuf[1] = (int32_t *) buffer_alloc(TDSPEED_OUTBUFSIZE * sizeof(int32_t)); - } + return 2 * sizeof(int32_t) * (FIXED_BUFSIZE + TDSPEED_OUTBUFSIZE); +} + +void tdspeed_init(void *buf) +{ + int32_t *ptr = (int32_t *)buf; + overlap_buffer[0] = ptr; + ptr += FIXED_BUFSIZE; + overlap_buffer[1] = ptr; + ptr += FIXED_BUFSIZE; + outbuf[0] = ptr; + ptr += TDSPEED_OUTBUFSIZE; + outbuf[1] = ptr; + ptr += TDSPEED_OUTBUFSIZE; } diff --git a/lib/rbcodec/dsp/tdspeed.h b/lib/rbcodec/dsp/tdspeed.h index c3b7fc4..48471d3 100644 --- a/lib/rbcodec/dsp/tdspeed.h +++ b/lib/rbcodec/dsp/tdspeed.h @@ -36,7 +36,8 @@ #define GET_STRETCH(pitch, speed) \ ((speed * PITCH_SPEED_100 + pitch / 2L) / pitch) -void tdspeed_init(void) INIT_ATTR; +size_t tdspeed_get_buffer_size(void); +void tdspeed_init(void *buf) INIT_ATTR; bool tdspeed_config(int samplerate, bool stereo, int32_t factor); long tdspeed_est_output_size(void); long tdspeed_est_input_size(long size); diff --git a/lib/rbcodec/test/warble.c b/lib/rbcodec/test/warble.c index ea150d7..8d239bf 100644 --- a/lib/rbcodec/test/warble.c +++ b/lib/rbcodec/test/warble.c @@ -673,8 +673,7 @@ static void decode_file(const char *input_fn) /* Set up global settings */ memset(&global_settings, 0, sizeof(global_settings)); global_settings.timestretch_enabled = true; - dsp_timestretch_enable(true); - tdspeed_init(); + dsp_timestretch_enable(malloc(dsp_timestretch_get_buffer_size())); /* Open file */ if (!strcmp(input_fn, "-")) { -- 1.7.6