Index: docs/CREDITS =================================================================== --- docs/CREDITS (revision 20213) +++ docs/CREDITS (working copy) @@ -452,6 +452,7 @@ Sei Aoyumi Martin Pool Gareth Schakel +Nicolas Pitre The libmad team Index: apps/lang/english.lang =================================================================== --- apps/lang/english.lang (revision 20213) +++ apps/lang/english.lang (working copy) @@ -12402,6 +12402,34 @@ + id: LANG_TIMESTRETCH + desc: timestretch enable + user: core + + *: "Timestretch" + + + *: "Timestretch" + + + *: "Timestretch" + + + + id: LANG_TIMESTRETCH_PERCENT + desc: timestretch percentage + user: core + + *: "Speed Percentage" + + + *: "Speed Percentage" + + + *: "Speed Percentage" + + + id: LANG_ENABLE_SPEAKER desc: in Settings -> Sound Settings user: core Index: apps/settings.c =================================================================== --- apps/settings.c (revision 20213) +++ apps/settings.c (working copy) @@ -933,6 +933,8 @@ } dsp_dither_enable(global_settings.dithering_enabled); + dsp_timestretch_enable(global_settings.timestretch_enabled); + dsp_set_timestretch(global_settings.timestretch_percent); #endif #ifdef HAVE_SPDIF_POWER Index: apps/settings.h =================================================================== --- apps/settings.h (revision 20213) +++ apps/settings.h (working copy) @@ -384,6 +384,8 @@ int keyclick; /* keyclick volume */ int keyclick_repeats; /* keyclick on repeats */ bool dithering_enabled; + bool timestretch_enabled; + int timestretch_percent; /* timestretching speed percentage */ #endif /* CONFIG_CODEC == SWCODEC */ #ifdef HAVE_RECORDING Index: apps/menus/sound_menu.c =================================================================== --- apps/menus/sound_menu.c (revision 20213) +++ apps/menus/sound_menu.c (working copy) @@ -32,6 +32,9 @@ #include "eq_menu.h" #include "exported_menus.h" #include "menu_common.h" +#include "splash.h" +#include "kernel.h" +#include "dsp.h" /***********************************/ /* SOUND MENU */ @@ -86,7 +89,28 @@ MAKE_MENU(crossfeed_menu,ID2P(LANG_CROSSFEED), NULL, Icon_NOICON, &crossfeed, &crossfeed_direct_gain, &crossfeed_cross_gain, &crossfeed_hf_attenuation, &crossfeed_hf_cutoff); - + + /* Timestretch Submenu */ + +static int timestretch_callback(int action,const struct menu_item_ex *this_item) +{ + switch (action) + { + case ACTION_EXIT_MENUITEM: /* on exit */ + if (global_settings.timestretch_enabled && dsp_timestretch_needs_reboot()) + splash(HZ*2, ID2P(LANG_PLEASE_REBOOT)); + break; + } + lowlatency_callback(action, this_item); + return action; +} + MENUITEM_SETTING(timestretch_enabled, + &global_settings.timestretch_enabled, timestretch_callback); + MENUITEM_SETTING(timestretch_percent, &global_settings.timestretch_percent, + lowlatency_callback); + MAKE_MENU(timestretch_menu,ID2P(LANG_TIMESTRETCH), NULL, Icon_NOICON, + ×tretch_enabled, ×tretch_percent); + MENUITEM_SETTING(dithering_enabled, &global_settings.dithering_enabled, lowlatency_callback); #endif @@ -120,7 +144,8 @@ #endif &balance,&channel_config,&stereo_width #if CONFIG_CODEC == SWCODEC - ,&crossfeed_menu, &equalizer_menu, &dithering_enabled + ,&crossfeed_menu, &equalizer_menu, &dithering_enabled + ,×tretch_menu #endif #if (CONFIG_CODEC == MAS3587F) || (CONFIG_CODEC == MAS3539F) ,&loudness,&avc,&superbass,&mdb_enable,&mdb_strength Index: apps/dsp.c =================================================================== --- apps/dsp.c (revision 20213) +++ apps/dsp.c (working copy) @@ -32,22 +32,20 @@ #include "replaygain.h" #include "misc.h" #include "debug.h" +#include "tdspeed.h" +#include "buffer.h" /* 16-bit samples are scaled based on these constants. The shift should be * no more than 15. */ -#define WORD_SHIFT 12 -#define WORD_FRACBITS 27 +#define WORD_SHIFT 12 +#define WORD_FRACBITS 27 -#define NATIVE_DEPTH 16 -/* If the buffer sizes change, check the assembly code! */ -#define SAMPLE_BUF_COUNT 256 -#define RESAMPLE_BUF_COUNT (256 * 4) /* Enough for 11,025 Hz -> 44,100 Hz*/ -#define DEFAULT_GAIN 0x01000000 -#define SAMPLE_BUF_LEFT_CHANNEL 0 -#define SAMPLE_BUF_RIGHT_CHANNEL (SAMPLE_BUF_COUNT/2) -#define RESAMPLE_BUF_LEFT_CHANNEL 0 -#define RESAMPLE_BUF_RIGHT_CHANNEL (RESAMPLE_BUF_COUNT/2) +#define NATIVE_DEPTH 16 +/* If the small buffer size changes, check the assembly code! */ +#define SMALL_SAMPLE_BUF_COUNT 256 +#define BIG_SAMPLE_BUF_COUNT 1024 /* 512 samples * 2 chns (*4) */ +#define DEFAULT_GAIN 0x01000000 /* enums to index conversion properly with stereo mode and other settings */ enum @@ -101,7 +99,7 @@ struct resample_data resample_data; /* 08h */ int32_t clip_min; /* 18h */ int32_t clip_max; /* 1ch */ - int32_t gain; /* 20h - Note that this is in S8.23 format. */ + int32_t gain; /* 20h - Note that this is in S8.23 format. */ /* 24h */ }; @@ -140,7 +138,7 @@ /* Typedefs keep things much neater in this case */ typedef void (*sample_input_fn_type)(int count, const char *src[], - int32_t *dst[]); + int32_t *dst[]); typedef int (*resample_fn_type)(int count, struct dsp_data *data, int32_t *src[], int32_t *dst[]); typedef void (*sample_output_fn_type)(int count, struct dsp_data *data, @@ -163,6 +161,9 @@ int sample_depth; int sample_bytes; int stereo_mode; + bool tdspeed_enabled; /* User has enabled timstretch */ + int tdspeed_percent; /* % */ + bool tdspeed_active; /* Timestretch is initialised ok */ int frac_bits; #ifdef HAVE_SW_TONE_CONTROLS /* Filter struct for software bass/treble controls */ @@ -218,17 +219,32 @@ static long replaygain; static bool crossfeed_enabled; -#define audio_dsp (dsp_conf[CODEC_IDX_AUDIO]) -#define voice_dsp (dsp_conf[CODEC_IDX_VOICE]) +#define AUDIO_DSP (dsp_conf[CODEC_IDX_AUDIO]) +#define VOICE_DSP (dsp_conf[CODEC_IDX_VOICE]) /* The internal format is 32-bit samples, non-interleaved, stereo. This * format is similar to the raw output from several codecs, so the amount * of copying needed is minimized for that case. */ -int32_t sample_buf[SAMPLE_BUF_COUNT] IBSS_ATTR; -static int32_t resample_buf[RESAMPLE_BUF_COUNT] IBSS_ATTR; +#define RESAMPLE_RATIO 4 /* Enough for 11,025 Hz -> 44,100 Hz */ +static int32_t small_sample_buf[SMALL_SAMPLE_BUF_COUNT] IBSS_ATTR; +static int32_t small_resample_buf[SMALL_SAMPLE_BUF_COUNT * RESAMPLE_RATIO] IBSS_ATTR; + +static int32_t *big_sample_buf = NULL; +static int32_t *big_resample_buf = NULL; +static int big_sample_buf_count = -1; /* -1=unknown, 0=not available */ + +static int sample_buf_count; +static int32_t *sample_buf; +static int32_t *resample_buf; + +#define SAMPLE_BUF_LEFT_CHANNEL 0 +#define SAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2) +#define RESAMPLE_BUF_LEFT_CHANNEL 0 +#define RESAMPLE_BUF_RIGHT_CHANNEL (sample_buf_count/2 * RESAMPLE_RATIO) + #if 0 /* Clip sample to arbitrary limits where range > 0 and min + range = max */ static inline long clip_sample(int32_t sample, int32_t min, int32_t range) @@ -260,10 +276,61 @@ void sound_set_pitch(int permille) { pitch_ratio = permille; - dsp_configure(&audio_dsp, DSP_SWITCH_FREQUENCY, - audio_dsp.codec_frequency); + dsp_configure(&AUDIO_DSP, DSP_SWITCH_FREQUENCY, + AUDIO_DSP.codec_frequency); } +void tdspeed_setup(struct dsp_config *dspc) +{ + if(dspc == &AUDIO_DSP) + { + if (!dspc->tdspeed_enabled || + dspc->tdspeed_percent == 0 || + dspc->tdspeed_percent == 100 || + big_sample_buf_count <= 0) + dspc->tdspeed_active = false; + else dspc->tdspeed_active + = tdspeed_init(dspc->codec_frequency == 0 ? NATIVE_FREQUENCY + : dspc->codec_frequency, + dspc->stereo_mode != STEREO_MONO, + dspc->tdspeed_percent); + } +} + +void dsp_timestretch_enable(bool enable) +{ + if (enable) + { + /* Set up timestretch buffers on first enable */ + if (big_sample_buf_count < 0) + { + big_sample_buf_count = BIG_SAMPLE_BUF_COUNT; + big_sample_buf = (int32_t *) buffer_alloc(big_sample_buf_count * sizeof(int32_t)); + big_resample_buf = (int32_t *) buffer_alloc(big_sample_buf_count * RESAMPLE_RATIO * sizeof(int32_t)); + } + } + else + { + /* If not enabled at startup, buffers will never be available */ + if (big_sample_buf_count < 0) + big_sample_buf_count = 0; + } + AUDIO_DSP.tdspeed_enabled = enable; + tdspeed_setup(&AUDIO_DSP); +} + +void dsp_set_timestretch(int percent) +{ + AUDIO_DSP.tdspeed_percent = percent; + tdspeed_setup(&AUDIO_DSP); +} + +bool dsp_timestretch_needs_reboot() +{ + return (AUDIO_DSP.tdspeed_enabled + && big_sample_buf_count <= 0); +} + /* Convert count samples to the internal format, if needed. Updates src * to point past the samples "consumed" and dst is set to point to the * samples to consume. Note that for mono, dst[0] equals dst[1], as there @@ -462,7 +529,7 @@ for (ch = 0; ch < data->num_channels; ch++) { struct dither_data * const dither = &dither_data[ch]; - int32_t *s = src[ch]; + const int32_t *s = src[ch]; int i; for (i = 0, d = &dst[ch]; i < count; i++, s++, d += 2) @@ -540,7 +607,7 @@ int out = dsp->data.num_channels - 1; - if (dsp == &audio_dsp && dither_enabled) + if (dsp == &AUDIO_DSP && dither_enabled) out += 2; dsp->output_samples = sample_output_functions[out]; @@ -567,7 +634,7 @@ */ int32_t *s = src[ch]; int32_t last = data->resample_data.last_sample[ch]; - + data->resample_data.last_sample[ch] = s[count - 1]; d = dst[ch]; phase = data->resample_data.phase; @@ -603,11 +670,10 @@ /* Rolled channel loop actually showed slightly faster. */ do { - /* Should always be able to output a sample for a ratio up to - RESAMPLE_BUF_COUNT / SAMPLE_BUF_COUNT. */ + /* Should always be able to output a sample for a ratio up to RESAMPLE_RATIO */ int32_t *s = src[ch]; int32_t last = data->resample_data.last_sample[ch]; - + data->resample_data.last_sample[ch] = s[count - 1]; d = dst[ch]; phase = data->resample_data.phase; @@ -638,7 +704,7 @@ static void resampler_new_delta(struct dsp_config *dsp) { - dsp->data.resample_data.delta = (unsigned long) + dsp->data.resample_data.delta = (unsigned long) dsp->frequency * 65536LL / NATIVE_FREQUENCY; if (dsp->frequency == NATIVE_FREQUENCY) @@ -686,7 +752,7 @@ void dsp_dither_enable(bool enable) { - struct dsp_config *dsp = &audio_dsp; + struct dsp_config *dsp = &AUDIO_DSP; dither_enabled = enable; sample_output_new_format(dsp); } @@ -705,7 +771,7 @@ int32_t *coefs = &crossfeed_data.coefs[0]; int32_t gain = crossfeed_data.gain; int32_t *di = crossfeed_data.index; - + int32_t acc; int32_t left, right; int i; @@ -734,7 +800,7 @@ /* Now add the attenuated direct sound and write to outputs */ buf[0][i] = FRACMUL(left, gain) + hist_r[1]; buf[1][i] = FRACMUL(right, gain) + hist_l[1]; - + /* Wrap delay line index if bigger than delay line size */ if (di >= delay + 13*2) di = delay; @@ -754,7 +820,7 @@ void dsp_set_crossfeed(bool enable) { crossfeed_enabled = enable; - audio_dsp.apply_crossfeed = (enable && audio_dsp.data.num_channels > 1) + AUDIO_DSP.apply_crossfeed = (enable && AUDIO_DSP.data.num_channels > 1) ? apply_crossfeed : NULL; } @@ -815,17 +881,17 @@ dsp->data.gain = DEFAULT_GAIN; /* Replay gain not relevant to voice */ - if (dsp == &audio_dsp && replaygain) + if (dsp == &AUDIO_DSP && replaygain) { dsp->data.gain = replaygain; } - + if (dsp->eq_process && eq_precut) { dsp->data.gain = (long) (((int64_t) dsp->data.gain * eq_precut) >> 24); } - + if (dsp->data.gain == DEFAULT_GAIN) { dsp->data.gain = 0; @@ -846,7 +912,7 @@ void dsp_set_eq_precut(int precut) { eq_precut = get_replaygain_int(precut * -10); - set_gain(&audio_dsp); + set_gain(&AUDIO_DSP); } /** @@ -867,10 +933,10 @@ cutoff = 0xffffffff / NATIVE_FREQUENCY * (*setting++); q = *setting++; gain = *setting++; - + if (q == 0) q = 1; - + /* NOTE: The coef functions assume the EMAC unit is in fractional mode, which it should be, since we're executed from the main thread. */ @@ -903,7 +969,7 @@ EQ_PEAK_SHIFT, /* peaking */ EQ_SHELF_SHIFT, /* high shelf */ }; - unsigned int channels = audio_dsp.data.num_channels; + unsigned int channels = AUDIO_DSP.data.num_channels; int i; /* filter configuration currently is 1 low shelf filter, 3 band peaking @@ -925,14 +991,14 @@ */ void dsp_set_eq(bool enable) { - audio_dsp.eq_process = enable ? eq_process : NULL; - set_gain(&audio_dsp); + AUDIO_DSP.eq_process = enable ? eq_process : NULL; + set_gain(&AUDIO_DSP); } static void dsp_set_stereo_width(int value) { long width, straight, cross; - + width = value * 0x7fffff / 100; if (value <= 100) @@ -1039,14 +1105,14 @@ }; if ((unsigned)value >= ARRAYLEN(channels_process_functions) || - audio_dsp.stereo_mode == STEREO_MONO) + AUDIO_DSP.stereo_mode == STEREO_MONO) { value = SOUND_CHAN_STEREO; } /* This doesn't apply to voice */ channels_mode = value; - audio_dsp.channels_process = channels_process_functions[value]; + AUDIO_DSP.channels_process = channels_process_functions[value]; } #if CONFIG_CODEC == SWCODEC @@ -1057,10 +1123,10 @@ filter_bishelf_coefs(0xffffffff/NATIVE_FREQUENCY*200, 0xffffffff/NATIVE_FREQUENCY*3500, bass, treble, -prescale, - audio_dsp.tone_filter.coefs); + AUDIO_DSP.tone_filter.coefs); /* Sync the voice dsp coefficients */ - memcpy(&voice_dsp.tone_filter.coefs, audio_dsp.tone_filter.coefs, - sizeof (voice_dsp.tone_filter.coefs)); + memcpy(&VOICE_DSP.tone_filter.coefs, AUDIO_DSP.tone_filter.coefs, + sizeof (VOICE_DSP.tone_filter.coefs)); } #endif @@ -1069,7 +1135,8 @@ */ int dsp_callback(int msg, intptr_t param) { - switch (msg) { + switch (msg) + { #ifdef HAVE_SW_TONE_CONTROLS case DSP_CALLBACK_SET_PRESCALE: prescale = param; @@ -1112,7 +1179,6 @@ static long last_yield; long tick; int written = 0; - int samples; #if defined(CPU_COLDFIRE) /* set emac unit for dsp processing, and save old macsr, we're running in @@ -1132,43 +1198,61 @@ will be preloaded to be used for the call if not. */ while (count > 0) { - samples = MIN(SAMPLE_BUF_COUNT/2, count); + int samples = MIN(sample_buf_count/2, count); count -= samples; dsp->input_samples(samples, src, tmp); + + int chunk; + if (dsp->tdspeed_active) + chunk = tdspeed_doit(tmp, samples); + else + chunk = samples; + int chunk_offset = 0; - if (dsp->apply_gain) - dsp->apply_gain(samples, &dsp->data, tmp); + while (samples > 0 && chunk > 0) + { + int32_t *t2[2]; + t2[0] = tmp[0]+chunk_offset; + t2[1] = tmp[1]+chunk_offset; + + chunk = MIN(sample_buf_count/2, chunk); + chunk_offset += chunk; + samples -= chunk; - if (dsp->resample && (samples = resample(dsp, samples, tmp)) <= 0) - break; /* I'm pretty sure we're downsampling here */ + if (dsp->apply_gain) + dsp->apply_gain(chunk, &dsp->data, t2); - if (dsp->apply_crossfeed) - dsp->apply_crossfeed(samples, tmp); + if (dsp->resample && (chunk = resample(dsp, chunk, t2)) <= 0) + break; /* I'm pretty sure we're downsampling here */ - if (dsp->eq_process) - dsp->eq_process(samples, tmp); + if (dsp->apply_crossfeed) + dsp->apply_crossfeed(chunk, t2); + if (dsp->eq_process) + dsp->eq_process(chunk, t2); + #ifdef HAVE_SW_TONE_CONTROLS - if ((bass | treble) != 0) - eq_filter(tmp, &dsp->tone_filter, samples, + if ((bass | treble) != 0) + eq_filter(t2, &dsp->tone_filter, chunk, dsp->data.num_channels, FILTER_BISHELF_SHIFT); #endif - if (dsp->channels_process) - dsp->channels_process(samples, tmp); + if (dsp->channels_process) + dsp->channels_process(chunk, t2); - dsp->output_samples(samples, &dsp->data, tmp, (int16_t *)dst); + dsp->output_samples(chunk, &dsp->data, t2, (int16_t *)dst); - written += samples; - dst += samples * sizeof (int16_t) * 2; - - /* yield at least once each tick */ - tick = current_tick; - if (TIME_AFTER(tick, last_yield)) - { - last_yield = tick; - yield(); + written += chunk; + dst += chunk * sizeof (int16_t) * 2; + + /* yield at least once each tick */ + tick = current_tick; + if (TIME_AFTER(tick, last_yield)) + { + last_yield = tick; + yield(); + } } } @@ -1188,6 +1272,20 @@ /* dsp_input_size MUST be called afterwards */ int dsp_output_count(struct dsp_config *dsp, int count) { + if(!dsp->tdspeed_active) + { + sample_buf = small_sample_buf; + resample_buf = small_resample_buf; + sample_buf_count = SMALL_SAMPLE_BUF_COUNT; + } + else + { + sample_buf = big_sample_buf; + sample_buf_count = big_sample_buf_count; + resample_buf = big_resample_buf; + } + if(dsp->tdspeed_active) + count = tdspeed_est_output_size(); if (dsp->resample) { count = (int)(((unsigned long)count * NATIVE_FREQUENCY @@ -1195,12 +1293,12 @@ } /* Now we have the resampled sample count which must not exceed - * RESAMPLE_BUF_COUNT/2 to avoid resample buffer overflow. One + * RESAMPLE_BUF_RIGHT_CHANNEL to avoid resample buffer overflow. One * must call dsp_input_count() to get the correct input sample * count. */ - if (count > RESAMPLE_BUF_COUNT/2) - count = RESAMPLE_BUF_COUNT/2; + if (count > RESAMPLE_BUF_RIGHT_CHANNEL) + count = RESAMPLE_BUF_RIGHT_CHANNEL; return count; } @@ -1221,6 +1319,9 @@ dsp->data.resample_data.delta) >> 16); } + if(dsp->tdspeed_active) + count = tdspeed_est_input_size(count); + return count; } @@ -1234,7 +1335,7 @@ { sample_input_new_format(dsp); sample_output_new_format(dsp); - if (dsp == &audio_dsp) + if (dsp == &AUDIO_DSP) dsp_set_crossfeed(crossfeed_enabled); } @@ -1246,9 +1347,9 @@ switch (value) { case CODEC_IDX_AUDIO: - return (intptr_t)&audio_dsp; + return (intptr_t)&AUDIO_DSP; case CODEC_IDX_VOICE: - return (intptr_t)&voice_dsp; + return (intptr_t)&VOICE_DSP; default: return (intptr_t)NULL; } @@ -1262,12 +1363,13 @@ if we're called from the main audio thread. Voice UI thread should not need this feature. */ - if (dsp == &audio_dsp) + if (dsp == &AUDIO_DSP) dsp->frequency = pitch_ratio * dsp->codec_frequency / 1000; else dsp->frequency = dsp->codec_frequency; resampler_new_delta(dsp); + tdspeed_setup(dsp); break; case DSP_SET_SAMPLE_DEPTH: @@ -1290,13 +1392,14 @@ dsp->data.output_scale = dsp->frac_bits + 1 - NATIVE_DEPTH; sample_input_new_format(dsp); - dither_init(dsp); + dither_init(dsp); break; case DSP_SET_STEREO_MODE: dsp->stereo_mode = value; dsp->data.num_channels = value == STEREO_MONO ? 1 : 2; dsp_update_functions(dsp); + tdspeed_setup(dsp); break; case DSP_RESET: @@ -1310,7 +1413,7 @@ dsp->data.clip_min = -((1 << WORD_FRACBITS)); dsp->codec_frequency = dsp->frequency = NATIVE_FREQUENCY; - if (dsp == &audio_dsp) + if (dsp == &AUDIO_DSP) { track_gain = 0; album_gain = 0; @@ -1321,6 +1424,7 @@ dsp_update_functions(dsp); resampler_new_delta(dsp); + tdspeed_setup(dsp); break; case DSP_FLUSH: @@ -1328,25 +1432,26 @@ sizeof (dsp->data.resample_data)); resampler_new_delta(dsp); dither_init(dsp); + tdspeed_setup(dsp); break; case DSP_SET_TRACK_GAIN: - if (dsp == &audio_dsp) + if (dsp == &AUDIO_DSP) dsp_set_gain_var(&track_gain, value); break; case DSP_SET_ALBUM_GAIN: - if (dsp == &audio_dsp) + if (dsp == &AUDIO_DSP) dsp_set_gain_var(&album_gain, value); break; case DSP_SET_TRACK_PEAK: - if (dsp == &audio_dsp) + if (dsp == &AUDIO_DSP) dsp_set_gain_var(&track_peak, value); break; case DSP_SET_ALBUM_PEAK: - if (dsp == &audio_dsp) + if (dsp == &AUDIO_DSP) dsp_set_gain_var(&album_peak, value); break; @@ -1403,5 +1508,5 @@ /* Store in S8.23 format to simplify calculations. */ replaygain = gain; - set_gain(&audio_dsp); + set_gain(&AUDIO_DSP); } Index: apps/dsp.h =================================================================== --- apps/dsp.h (revision 20213) +++ apps/dsp.h (working copy) @@ -164,5 +164,8 @@ int sound_get_pitch(void); int dsp_callback(int msg, intptr_t param); void dsp_dither_enable(bool enable); +void dsp_timestretch_enable(bool enable); +void dsp_set_timestretch(int percent); +bool dsp_timestretch_needs_reboot(void); #endif Index: apps/settings_list.c =================================================================== --- apps/settings_list.c (revision 20213) +++ apps/settings_list.c (working copy) @@ -1181,6 +1181,13 @@ /* dithering */ OFFON_SETTING(F_SOUNDSETTING, dithering_enabled, LANG_DITHERING, false, "dithering enabled", dsp_dither_enable), + + /* timestretch */ + OFFON_SETTING(F_SOUNDSETTING, timestretch_enabled, LANG_TIMESTRETCH, false, + "timestretch enabled", dsp_timestretch_enable), + INT_SETTING(0, timestretch_percent, LANG_TIMESTRETCH_PERCENT, 100, "timestretch percentage", + UNIT_INT, 35, 250, 5, + NULL, NULL, dsp_set_timestretch), #endif #ifdef HAVE_WM8758 SOUND_SETTING(F_NO_WRAP, bass_cutoff, LANG_BASS_CUTOFF, Index: apps/tdspeed.c =================================================================== --- apps/tdspeed.c (revision 0) +++ apps/tdspeed.c (revision 0) @@ -0,0 +1,321 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id: tdspeed.c $ + * + * Copyright (C) 2006 by Nicolas Pitre + * Copyright (C) 2006-2007 by Stéphane Doyon + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include +#include +#include +#include +#include "buffer.h" +#include "debug.h" +#include "system.h" +#include "tdspeed.h" + +#define assert(cond) + +#define MIN_RATE 8000 +#define MAX_RATE 48000 /* double buffer for double rate */ +#define MIN_FACTOR 35 +#define MAX_FACTOR 250 +#define MINFREQ 100 + +#define FIXED_BUFSIZE 3072 /* 48KHz factor 3.0 */ + +struct tdspeed_state_s +{ + bool stereo; + int shift_max; /* maximum displacement on a frame */ + int src_step; /* source window pace */ + int dst_step; /* destination window pace */ + int dst_order; /* power of two for dst_step */ + int ovl_shift; /* overlap buffer frame shift */ + int ovl_size; /* overlap buffer used size */ + int ovl_space; /* overlap buffer size */ + int32_t *ovl_buff[2]; /* overlap buffer */ +}; +static struct tdspeed_state_s tdspeed_state; + +static int32_t *overlap_buffer[2] = { NULL, NULL }; +static int32_t *outbuf[2] = { NULL, NULL }; + +bool tdspeed_init(int samplerate, bool stereo, int factor) +{ + struct tdspeed_state_s *st = &tdspeed_state; + int src_frame_sz; + + /* Check parameters */ + if (factor == 100) + return false; + if (samplerate < MIN_RATE || samplerate > MAX_RATE) + return false; + if (factor < MIN_FACTOR || factor > MAX_FACTOR) + return false; + + /* 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)); + + st->stereo = stereo; + st->dst_step = samplerate / MINFREQ; + + if (factor > 100) + st->dst_step = st->dst_step * 100 / factor; + st->dst_order = 1; + + while (st->dst_step >>= 1) + st->dst_order++; + st->dst_step = (1 << st->dst_order); + st->src_step = st->dst_step * factor / 100; + st->shift_max = (st->dst_step > st->src_step) ? st->dst_step : st->src_step; + + src_frame_sz = st->shift_max + st->dst_step; + if (st->dst_step > st->src_step) + src_frame_sz += st->dst_step - st->src_step; + st->ovl_space = ((src_frame_sz - 2)/st->src_step) * st->src_step + + src_frame_sz; + if (st->src_step > st->dst_step) + st->ovl_space += 2*st->src_step - st->dst_step; + + if (st->ovl_space > FIXED_BUFSIZE) + st->ovl_space = FIXED_BUFSIZE; + + st->ovl_size = 0; + st->ovl_shift = 0; + + st->ovl_buff[0] = overlap_buffer[0]; + if (stereo) + st->ovl_buff[1] = overlap_buffer[1]; + else + st->ovl_buff[1] = st->ovl_buff[0]; + + return true; +} + +static int tdspeed_apply(int32_t *buf_out[2], int32_t *buf_in[2], + int data_len, int last, int out_size) +/* data_len in samples */ +{ + struct tdspeed_state_s *st = &tdspeed_state; + int32_t *curr, *prev, *dest[2], *d; + int i, j, next_frame, prev_frame, shift, src_frame_sz; + bool stereo = buf_in[0] != buf_in[1]; + assert(stereo == st->stereo); + + src_frame_sz = st->shift_max + st->dst_step; + if (st->dst_step > st->src_step) + src_frame_sz += st->dst_step - st->src_step; + + /* deal with overlap data first, if any */ + if (st->ovl_size) + { + int have, copy, steps; + have = st->ovl_size; + if (st->ovl_shift > 0) + have -= st->ovl_shift; + /* append just enough data to have all of the overlap buffer consumed */ + steps = (have - 1) / st->src_step; + copy = steps * st->src_step + src_frame_sz - have; + if (copy < src_frame_sz - st->dst_step) + copy += st->src_step; /* one more step to allow for pregap data */ + if (copy > data_len) copy = data_len; + assert(st->ovl_size +copy <= FIXED_BUFSIZE); + memcpy(st->ovl_buff[0] + st->ovl_size, buf_in[0], + copy * sizeof(int32_t)); + if (stereo) + memcpy(st->ovl_buff[1] + st->ovl_size, buf_in[1], + copy * sizeof(int32_t)); + if (!last && have + copy < src_frame_sz) + { + /* still not enough to process at least one frame */ + st->ovl_size += copy; + return 0; + } + + /* recursively call ourselves to process the overlap buffer */ + have = st->ovl_size; + st->ovl_size = 0; + if (copy == data_len) + { + assert( (have+copy) <= FIXED_BUFSIZE); + return tdspeed_apply(buf_out, st->ovl_buff, have+copy, last, + out_size); + } + assert( (have+copy) <= FIXED_BUFSIZE); + i = tdspeed_apply(buf_out, st->ovl_buff, have+copy, -1, out_size); + dest[0] = buf_out[0] + i; + dest[1] = buf_out[1] + i; + + /* readjust pointers to account for data already consumed */ + next_frame = copy - src_frame_sz + st->src_step; + prev_frame = next_frame - st->ovl_shift; + } + else + { + dest[0] = buf_out[0]; + dest[1] = buf_out[1]; + next_frame = prev_frame = 0; + if (st->ovl_shift > 0) + next_frame += st->ovl_shift; + else + prev_frame += -st->ovl_shift; + } + st->ovl_shift = 0; + + /* process all complete frames */ + while (data_len -next_frame >= src_frame_sz) + { + /* find frame overlap by autocorelation */ + long long min_delta = ~(1ll << 63); /* most positive */ + shift = 0; +#define INC1 8 +#define INC2 32 + /* Power of 2 of a 28bit number requires 56bits, can accumulate + 256times in a 64bit variable. */ + assert(st->dst_step /INC2 <= 256); + assert(next_frame+st->shift_max-1 +st->dst_step-1 < data_len); + assert(prev_frame +st->dst_step-1 < data_len); + for (i = 0; i < st->shift_max; i += INC1) + { + long long delta = 0; + curr = buf_in[0] + next_frame + i; + prev = buf_in[0] + prev_frame; + for (j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2) + { + int32_t diff = *curr - *prev; + delta += (long long)diff * diff; + if (delta >= min_delta) + goto skip; + } + if (stereo) + { + curr = buf_in[1] +next_frame + i; + prev = buf_in[1] +prev_frame; + for (j = 0; j < st->dst_step; j += INC2, curr += INC2, prev += INC2) + { + int32_t diff = *curr - *prev; + delta += (long long)diff * diff; + if (delta >= min_delta) + goto skip; + } + } + min_delta = delta; + shift = i; +skip:; + } + + /* overlap fading-out previous frame with fading-in current frame */ + curr = buf_in[0] +next_frame + shift; + prev = buf_in[0] +prev_frame; + d = dest[0]; + assert(next_frame+shift +st->dst_step-1 < data_len); + assert(prev_frame +st->dst_step-1 < data_len); + assert(dest[0]-buf_out[0] +st->dst_step-1 < out_size); + for (i = 0, j = st->dst_step; j; i++, j--) + { + *d++ = (*curr++ * (long long)i + + *prev++ * (long long)j) >> st->dst_order; + } + dest[0] = d; + if (stereo) + { + curr = buf_in[1] +next_frame + shift; + prev = buf_in[1] +prev_frame; + d = dest[1]; + for (i = 0, j = st->dst_step; j; i++, j--) + { + assert(d < buf_out[1] +out_size); + *d++ = (*curr++ * (long long) i + + *prev++ * (long long) j) >> st->dst_order; + } + dest[1] = d; + } + + /* adjust pointers for next frame */ + prev_frame = next_frame + shift + st->dst_step; + next_frame += st->src_step; + + /* here next_frame - prev_frame = src_step - dst_step - shift */ + assert(next_frame - prev_frame == st->src_step - st->dst_step - shift); + } + + /* now deal with remaining partial frames */ + if (last == -1) + { + /* special overlap buffer processing: remember frame shift only */ + st->ovl_shift = next_frame - prev_frame; + } + else if (last != 0) + { + /* last call: purge all remaining data to output buffer */ + i = data_len -prev_frame; + assert(dest[0] +i <= buf_out[0] +out_size); + memcpy(dest[0], buf_in[0] +prev_frame, i * sizeof(int32_t)); + dest[0] += i; + if (stereo) + { + assert(dest[1] +i <= buf_out[1] +out_size); + memcpy(dest[1], buf_in[1] +prev_frame, i * sizeof(int32_t)); + dest[1] += i; + } + } + else + { + /* preserve remaining data + needed overlap data for next call */ + st->ovl_shift = next_frame - prev_frame; + i = (st->ovl_shift < 0) ? next_frame : prev_frame; + st->ovl_size = data_len - i; + assert(st->ovl_size <= FIXED_BUFSIZE); + memcpy(st->ovl_buff[0], buf_in[0]+i, st->ovl_size * sizeof(int32_t)); + if (stereo) + memcpy(st->ovl_buff[1], buf_in[1]+i, st->ovl_size * sizeof(int32_t)); + } + + return dest[0] - buf_out[0]; +} + +long tdspeed_est_output_size() +{ + return TDSPEED_OUTBUFSIZE; +} + +long tdspeed_est_input_size(long size) +{ + struct tdspeed_state_s *st = &tdspeed_state; + size = (size -st->ovl_size) *st->src_step / st->dst_step; + if (size < 0) + size = 0; + return size; +} + +int tdspeed_doit(int32_t *src[], int count) +{ + count = tdspeed_apply( (int32_t *[2]) { outbuf[0], outbuf[1] }, + src, count, 0, TDSPEED_OUTBUFSIZE); + src[0] = outbuf[0]; + src[1] = outbuf[1]; + return count; +} Index: apps/tdspeed.h =================================================================== --- apps/tdspeed.h (revision 0) +++ apps/tdspeed.h (revision 0) @@ -0,0 +1,33 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id: tdspeed.h $ + * + * Copyright (C) 2006 by Nicolas Pitre + * Copyright (C) 2006-2007 by Stéphane Doyon + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#ifndef _TDSPEED_H +#define _TDSPEED_H + +#define TDSPEED_OUTBUFSIZE 4096 + +bool tdspeed_init(int samplerate, bool stereo, int factor); +long tdspeed_est_output_size(void); +long tdspeed_est_input_size(long size); +int tdspeed_doit(int32_t *src[], int count); + +#endif Index: apps/SOURCES =================================================================== --- apps/SOURCES (revision 20213) +++ apps/SOURCES (working copy) @@ -224,3 +224,4 @@ #elif CONFIG_KEYPAD == ONDAVX747_PAD keymaps/keymap-ondavx747.c #endif +tdspeed.c