diff --git a/apps/SOURCES b/apps/SOURCES index 45eb0768a3..e20f1c961b 100644 --- a/apps/SOURCES +++ b/apps/SOURCES @@ -26,6 +26,7 @@ menus/audiohw_eq_menu.c menus/eq_menu.c buffering.c voice_thread.c +rbcodec_helpers.c #else /* !SWCODEC */ mpeg.c #endif diff --git a/apps/rbcodec_helpers.c b/apps/rbcodec_helpers.c new file mode 100644 index 0000000000..a7c592c62c --- /dev/null +++ b/apps/rbcodec_helpers.c @@ -0,0 +1,102 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2006 by Nicolas Pitre + * Copyright (C) 2006-2007 by Stéphane Doyon + * Copyright (C) 2012 Michael Sevakis + * + * 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 "platform.h" +#include "dsp_core.h" +#include "core_alloc.h" +#include "tdspeed.h" + +static int handles[4] = { 0, 0, 0, 0 }; + +static int move_callback(int handle, void *current, void *new) +{ +#if 0 + /* Should not currently need to block this since DSP loop completes an + iteration before yielding and begins again at its input buffer */ + if (dsp_is_busy(tdspeed_state.dsp)) + return BUFLIB_CB_CANNOT_MOVE; /* DSP processing in progress */ +#endif + + for (unsigned int i = 0; i < ARRAYLEN(handles); i++) + { + if (handle != handles[i]) + continue; + + tdspeed_move(i, current, new); + break; + } + + return BUFLIB_CB_OK; +} + +static struct buflib_callbacks ops = +{ + .move_callback = move_callback, + .shrink_callback = NULL, +}; + +/* Allocate timestretch buffers */ +bool tdspeed_alloc_buffers(int32_t **buffers, const int *buf_s, int nbuf) +{ + static const char *buffer_names[4] = { + "tdspeed ovl L", + "tdspeed ovl R", + "tdspeed out L", + "tdspeed out R" + }; + + for (int i = 0; i < nbuf; i++) + { + if (handles[i] <= 0) + { + handles[i] = core_alloc_ex(buffer_names[i], buf_s[i], &ops); + + if (handles[i] <= 0) + return false; + } + + if (buffers[i] == NULL) + { + buffers[i] = core_get_data(handles[i]); + + if (buffers[i] == NULL) + return false; + } + } + + return true; +} + +/* Free timestretch buffers */ +void tdspeed_free_buffers(int32_t **buffers, int nbuf) +{ + for (int i = 0; i < nbuf; i++) + { + if (handles[i] > 0) + core_free(handles[i]); + + handles[i] = 0; + buffers[i] = NULL; + } +} + diff --git a/apps/rbcodecplatform.h b/apps/rbcodecplatform.h index 1dc72ac4e0..d644b7c47a 100644 --- a/apps/rbcodecplatform.h +++ b/apps/rbcodecplatform.h @@ -31,5 +31,9 @@ #include "dsp-util.h" #define HAVE_CLIP_SAMPLE_16 #endif + +bool tdspeed_alloc_buffers(int32_t **buffers, const int *buf_s, int nbuf); +void tdspeed_free_buffers(int32_t **buffers, int nbuf); + #endif diff --git a/lib/rbcodec/dsp/tdspeed.c b/lib/rbcodec/dsp/tdspeed.c index 82b6b0ea95..ea1013e049 100644 --- a/lib/rbcodec/dsp/tdspeed.c +++ b/lib/rbcodec/dsp/tdspeed.c @@ -20,15 +20,17 @@ * KIND, either express or implied. * ****************************************************************************/ +#include "platform.h" #include "config.h" -#include "system.h" #include "sound.h" #include "core_alloc.h" #include "dsp-util.h" #include "dsp_proc_entry.h" #include "tdspeed.h" +#ifndef assert #define assert(cond) +#endif #define TIMESTRETCH_SET_FACTOR (DSP_PROC_SETTING+DSP_PROC_TIMESTRETCH) @@ -39,6 +41,7 @@ #define MAX_INPUTCOUNT 512 /* Max input count so dst doesn't overflow */ #define FIXED_BUFCOUNT 3072 /* 48KHz factor 3.0 */ #define FIXED_OUTBUFCOUNT 4096 +#define NBUFFERS 4 enum tdspeed_ops { @@ -64,8 +67,13 @@ static struct tdspeed_state_s int32_t *ovl_buff[2]; /* overlap buffer (L+R) */ } tdspeed_state; -static int handles[4] = { 0, 0, 0, 0 }; -static int32_t *buffers[4] = { NULL, NULL, NULL, NULL }; +static int32_t *buffers[NBUFFERS] = { NULL, NULL, NULL, NULL }; +static const int buffer_sizes[NBUFFERS] = { + FIXED_BUFCOUNT * sizeof(int32_t), + FIXED_BUFCOUNT * sizeof(int32_t), + FIXED_OUTBUFCOUNT * sizeof(int32_t), + FIXED_OUTBUFCOUNT * sizeof(int32_t) +}; #define overlap_buffer (&buffers[0]) #define outbuf (&buffers[2]) @@ -74,107 +82,6 @@ static int32_t *buffers[4] = { NULL, NULL, NULL, NULL }; /* Processed buffer passed out to later stages */ static struct dsp_buffer dsp_outbuf; -static int move_callback(int handle, void *current, void *new) -{ -#if 0 - /* Should not currently need to block this since DSP loop completes an - iteration before yielding and begins again at its input buffer */ - if (dsp_is_busy(tdspeed_state.dsp)) - return BUFLIB_CB_CANNOT_MOVE; /* DSP processing in progress */ -#endif - - ptrdiff_t shift = (int32_t *)new - (int32_t *)current; - int32_t **p32 = dsp_outbuf.p32; - - for (unsigned int i = 0; i < ARRAYLEN(handles); i++) - { - if (handle != handles[i]) - continue; - - switch (i) - { - case 0: case 1: - /* moving overlap (input) buffers */ - tdspeed_state.ovl_buff[i] = new; - break; - - case 2: - /* moving outbuf left channel and dsp_outbuf.p32[0] */ - if (p32[0] == p32[1]) - p32[1] += shift; /* mono mode */ - - p32[0] += shift; - break; - - case 3: - /* moving outbuf right channel and dsp_outbuf.p32[1] */ - p32[1] += shift; - break; - } - - buffers[i] = new; - break; - } - - return BUFLIB_CB_OK; -} - -static struct buflib_callbacks ops = -{ - .move_callback = move_callback, - .shrink_callback = NULL, -}; - -/* Allocate timestretch buffers */ -static bool tdspeed_alloc_buffers(void) -{ - static const struct - { - const char *name; - size_t size; - } bufdefs[4] = - { - { "tdspeed ovl L", FIXED_BUFCOUNT * sizeof(int32_t) }, - { "tdspeed ovl R", FIXED_BUFCOUNT * sizeof(int32_t) }, - { "tdspeed out L", FIXED_OUTBUFCOUNT * sizeof(int32_t) }, - { "tdspeed out R", FIXED_OUTBUFCOUNT * sizeof(int32_t) }, - }; - - for (unsigned int i = 0; i < ARRAYLEN(bufdefs); i++) - { - if (handles[i] <= 0) - { - handles[i] = core_alloc_ex(bufdefs[i].name, bufdefs[i].size, &ops); - - if (handles[i] <= 0) - return false; - } - - if (buffers[i] == NULL) - { - buffers[i] = core_get_data(handles[i]); - - if (buffers[i] == NULL) - return false; - } - } - - return true; -} - -/* Free timestretch buffers */ -static void tdspeed_free_buffers(void) -{ - for (unsigned int i = 0; i < ARRAYLEN(handles); i++) - { - if (handles[i] > 0) - core_free(handles[i]); - - handles[i] = 0; - buffers[i] = NULL; - } -} - /* Discard all data */ static void tdspeed_flush(void) { @@ -650,7 +557,7 @@ static intptr_t tdspeed_configure(struct dsp_proc_entry *this, break; case DSP_PROC_INIT: - if (!tdspeed_alloc_buffers()) + if (!tdspeed_alloc_buffers(buffers, buffer_sizes, NBUFFERS)) return -1; /* fail the init */ st->this = this; @@ -664,7 +571,7 @@ static intptr_t tdspeed_configure(struct dsp_proc_entry *this, st->this = NULL; st->factor = PITCH_SPEED_100; dsp_outbuf.remcount = 0; - tdspeed_free_buffers(); + tdspeed_free_buffers(buffers, NBUFFERS); break; case TIMESTRETCH_SET_FACTOR: @@ -680,6 +587,35 @@ static intptr_t tdspeed_configure(struct dsp_proc_entry *this, (void)value; } +void tdspeed_move(int i, void* current, void* new) +{ + ptrdiff_t shift = (int32_t *)new - (int32_t *)current; + int32_t **p32 = dsp_outbuf.p32; + + switch (i) + { + case 0: case 1: + /* moving overlap (input) buffers */ + tdspeed_state.ovl_buff[i] = new; + break; + + case 2: + /* moving outbuf left channel and dsp_outbuf.p32[0] */ + if (p32[0] == p32[1]) + p32[1] += shift; /* mono mode */ + + p32[0] += shift; + break; + + case 3: + /* moving outbuf right channel and dsp_outbuf.p32[1] */ + p32[1] += shift; + break; + } + + buffers[i] = new; +} + /* Database entry */ DSP_PROC_DB_ENTRY(TIMESTRETCH, tdspeed_configure); diff --git a/lib/rbcodec/dsp/tdspeed.h b/lib/rbcodec/dsp/tdspeed.h index ca8a7846a4..2949c1bee9 100644 --- a/lib/rbcodec/dsp/tdspeed.h +++ b/lib/rbcodec/dsp/tdspeed.h @@ -39,5 +39,6 @@ void dsp_timestretch_enable(bool enable); void dsp_set_timestretch(int32_t percent); int32_t dsp_get_timestretch(void); bool dsp_timestretch_available(void); +void tdspeed_move(int i, void* current, void* new); #endif /* _TDSPEED_H */ diff --git a/lib/rbcodec/rbcodecplatform-unix.h b/lib/rbcodec/rbcodecplatform-unix.h index c9c8a29642..5e65be3ae4 100644 --- a/lib/rbcodec/rbcodecplatform-unix.h +++ b/lib/rbcodec/rbcodecplatform-unix.h @@ -74,4 +74,27 @@ static inline off_t filesize(int fd) { #endif #endif */ + +static inline bool tdspeed_alloc_buffers(int32_t **buffers, + const int *buf_s, int nbuf) +{ + int i; + for (i = 0; i < nbuf; i++) + { + buffers[i] = malloc(buf_s[i]); + if (!buffers[i]) + return false; + } + return true; +} + +static inline void tdspeed_free_buffers(int32_t **buffers, int nbuf) +{ + int i; + for (i = 0; i < nbuf; i++) + { + free(buffers[i]); + } +} + #endif diff --git a/lib/rbcodec/test/SOURCES b/lib/rbcodec/test/SOURCES index 4c0d906dc5..a89af238ca 100644 --- a/lib/rbcodec/test/SOURCES +++ b/lib/rbcodec/test/SOURCES @@ -1,6 +1,4 @@ warble.c -../../../firmware/buflib.c -../../../firmware/core_alloc.c ../../../firmware/common/strlcpy.c ../../../firmware/common/unicode.c ../../../firmware/common/structec.c diff --git a/lib/rbcodec/test/warble.c b/lib/rbcodec/test/warble.c index ea8efcffca..d5bc1c5aea 100644 --- a/lib/rbcodec/test/warble.c +++ b/lib/rbcodec/test/warble.c @@ -33,7 +33,6 @@ #include #include "buffering.h" /* TYPE_PACKET_AUDIO */ #include "codecs.h" -#include "core_alloc.h" /* core_allocator_init */ #include "dsp_core.h" #include "metadata.h" #include "settings.h" @@ -851,7 +850,6 @@ int main(int argc, char **argv) } } - core_allocator_init(); if (argc == optind + 2) { write_init(argv[optind + 1]); } else if (argc == optind + 1) {