| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- #include <assert.h>
- #ifdef _WIN32
- #define WIN32_LEAN_AND_MEAN
- #include <Windows.h>
- #include <winsock.h>
- #else
- #endif // _WIN32
- #include "circbuf.h"
- #include "delaybuf.h"
- #include "wsola.h"
- #include "memutil.h"
- /* Operation types of delay buffer */
- enum OP
- {
- OP_PUT,
- OP_GET
- };
- /* Specify time for delaybuf to recalculate effective delay, in ms.
- */
- #define RECALC_TIME 2000
- /* Default value of maximum delay, in ms, this value is used when
- * maximum delay requested is less than ptime (one frame length).
- */
- #define DEFAULT_MAX_DELAY 400
- /* Number of frames to add to learnt level for additional stability.
- */
- #define SAFE_MARGIN 0
- /* This structure describes internal delaybuf settings and states.
- */
- struct delay_buf
- {
- /* Properties and configuration */
- #ifdef _WIN32
- CRITICAL_SECTION lock;
- #else
- pthread_mutex_t lock_mtx;
- #endif
- unsigned samples_per_frame; /**< Number of samples in one frame */
- unsigned ptime; /**< Frame time, in ms */
- unsigned channel_count; /**< Channel count, in ms */
- circ_buf *circ_buf; /**< Circular buffer to store audio
- samples */
- unsigned max_cnt; /**< Maximum samples to be buffered */
- unsigned eff_cnt; /**< Effective count of buffered
- samples to keep the optimum
- balance between delay and
- stability. This is calculated
- based on burst level. */
- /* Learning vars */
- unsigned level; /**< Burst level counter */
- enum OP last_op; /**< Last op (GET or PUT) of learning*/
- int recalc_timer; /**< Timer for recalculating max_level*/
- unsigned max_level; /**< Current max burst level */
- /* Drift handler */
- wsola_t *wsola; /**< Drift handler */
- };
- int delay_buf_create(unsigned clock_rate,
- unsigned samples_per_frame,
- unsigned channel_count,
- unsigned max_delay,
- unsigned options,
- delay_buf **p_b)
- {
- delay_buf *b;
- int status;
- ASSERT_RETURN(samples_per_frame && clock_rate && channel_count && p_b, -1);
- ASSERT_RETURN(options==0, -1);
-
- b = ZALLOC_T(delay_buf);
- b->samples_per_frame = samples_per_frame;
- b->channel_count = channel_count;
- b->ptime = samples_per_frame * 1000 / clock_rate / channel_count;
- if (max_delay < b->ptime)
- max_delay = max(DEFAULT_MAX_DELAY, b->ptime);
- b->max_cnt = samples_per_frame * max_delay / b->ptime;
- b->eff_cnt = b->max_cnt >> 1;
- b->recalc_timer = RECALC_TIME;
- /* Create circular buffer */
- status = circ_buf_create(b->max_cnt, &b->circ_buf);
- if (status != 0)
- return status;
- /* Create WSOLA */
- status = wsola_create(clock_rate, samples_per_frame, 1,WSOLA_NO_FADING, &b->wsola);
- if (status != 0)
- return status;
- /* Finally, create mutex */
- #ifdef _WIN32
- InitializeCriticalSection(&b->lock);
- #else
- status = pthread_mutex_init(&b->lock_mtx, NULL);
- #endif
- *p_b = b;
- return 0;
- }
- int delay_buf_destroy(delay_buf *b)
- {
- int status;
- ASSERT_RETURN(b, -1);
- #ifdef _WIN32
- EnterCriticalSection(&b->lock);
- #else
- pthread_mutex_lock(&b->lock_mtx);
- #endif
- status = wsola_destroy(b->wsola);
- if (status == 0)
- b->wsola = NULL;
- circ_buf_destroy(b->circ_buf);
- b->circ_buf = NULL;
- #ifdef _WIN32
- LeaveCriticalSection(&b->lock);
- DeleteCriticalSection(&b->lock);
- #else
- pthread_mutex_unlock(&b->lock_mtx);
- pthread_mutex_destroy(&b->lock_mtx);
- #endif
- free(b);
- return status;
- }
- /* This function will erase samples from delay buffer.
- * The number of erased samples is guaranteed to be >= erase_cnt.
- */
- static void shrink_buffer(delay_buf *b, unsigned erase_cnt)
- {
- short *buf1, *buf2;
- unsigned buf1len;
- unsigned buf2len;
- int status;
- assert(b && erase_cnt && circ_buf_get_len(b->circ_buf));
- circ_buf_get_read_regions(b->circ_buf, &buf1, &buf1len, &buf2, &buf2len);
- status = wsola_discard(b->wsola, buf1, buf1len, buf2, buf2len,&erase_cnt);
- if ((status == 0) && (erase_cnt > 0)) {
- /* WSOLA discard will manage the first buffer to be full, unless
- * erase_cnt is greater than second buffer length. So it is safe
- * to just set the circular buffer length.
- */
- circ_buf_set_len(b->circ_buf, circ_buf_get_len(b->circ_buf) - erase_cnt);
- }
- }
- /* Fast increase, slow decrease */
- #define AGC_UP(cur, target) cur = (cur + target*3) >> 2
- #define AGC_DOWN(cur, target) cur = (cur*3 + target) >> 2
- #define AGC(cur, target) \
- if (cur < target) AGC_UP(cur, target); \
- else AGC_DOWN(cur, target)
- static void update(delay_buf *b, enum OP op)
- {
- /* Sequential operation */
- if (op == b->last_op) {
- ++b->level;
- return;
- }
- /* Switching operation */
- if (b->level > b->max_level)
- b->max_level = b->level;
- b->recalc_timer -= (b->level * b->ptime) >> 1;
- b->last_op = op;
- b->level = 1;
- /* Recalculate effective count based on max_level */
- if (b->recalc_timer <= 0) {
- unsigned new_eff_cnt = (b->max_level+SAFE_MARGIN)*b->samples_per_frame;
- /* Smoothening effective count transition */
- AGC(b->eff_cnt, new_eff_cnt);
-
- /* Make sure the new effective count is multiplication of
- * channel_count, so let's round it up.
- */
- if (b->eff_cnt % b->channel_count)
- b->eff_cnt += b->channel_count - (b->eff_cnt % b->channel_count);
- b->max_level = 0;
- b->recalc_timer = RECALC_TIME;
- }
- /* See if we need to shrink the buffer to reduce delay */
- if (op == OP_PUT && circ_buf_get_len(b->circ_buf) >
- b->samples_per_frame + b->eff_cnt)
- {
- unsigned erase_cnt = b->samples_per_frame >> 1;
- unsigned old_buf_cnt = circ_buf_get_len(b->circ_buf);
- shrink_buffer(b, erase_cnt);
- }
- }
- int delay_buf_put(delay_buf *b,
- short frame[])
- {
- int status;
- ASSERT_RETURN(b && frame, -1);
- #ifdef _WIN32
- EnterCriticalSection(&b->lock);
- #else
- pthread_mutex_lock(&b->lock_mtx);
- #endif
- update(b, OP_PUT);
-
- status = wsola_save(b->wsola, frame, FALSE);
- if (status != 0) {
- #ifdef _WIN32
- LeaveCriticalSection(&b->lock);
- #else
- pthread_mutex_unlock(&b->lock_mtx);
- #endif
- return status;
- }
- /* Overflow checking */
- if (circ_buf_get_len(b->circ_buf) + b->samples_per_frame >
- b->max_cnt)
- {
- unsigned erase_cnt;
-
- /* shrink one frame or just the diff? */
- //erase_cnt = b->samples_per_frame;
- erase_cnt = circ_buf_get_len(b->circ_buf) +
- b->samples_per_frame - b->max_cnt;
- shrink_buffer(b, erase_cnt);
- /* Check if shrinking failed or erased count is less than requested,
- * delaybuf needs to drop eldest samples, this is bad since the voice
- * samples get rough transition which may produce tick noise.
- */
- if (circ_buf_get_len(b->circ_buf) + b->samples_per_frame >
- b->max_cnt)
- {
- erase_cnt = circ_buf_get_len(b->circ_buf) +
- b->samples_per_frame - b->max_cnt;
- circ_buf_adv_read_ptr(b->circ_buf, erase_cnt);
- }
- }
- circ_buf_write(b->circ_buf, frame, b->samples_per_frame);
- #ifdef _WIN32
- LeaveCriticalSection(&b->lock);
- #else
- pthread_mutex_unlock(&b->lock_mtx);
- #endif
- return 0;
- }
- int delay_buf_get( delay_buf *b,short frame[])
- {
- int status;
- ASSERT_RETURN(b && frame, -1);
- #ifdef _WIN32
- EnterCriticalSection(&b->lock);
- #else
- pthread_mutex_lock(&b->lock_mtx);
- #endif
- update(b, OP_GET);
- /* Starvation checking */
- if (circ_buf_get_len(b->circ_buf) < b->samples_per_frame) {
- //OutputDebugStringA("g");
- status = wsola_generate(b->wsola, frame);
- if (status == 0) {
- if (circ_buf_get_len(b->circ_buf) == 0) {
- #ifdef _WIN32
- LeaveCriticalSection(&b->lock);
- #else
- pthread_mutex_unlock(&b->lock_mtx);
- #endif
- return 0;
- }
- /* Put generated frame into buffer */
- circ_buf_write(b->circ_buf, frame, b->samples_per_frame);
- } else {
- unsigned buf_len = circ_buf_get_len(b->circ_buf);
-
- /* Give all what delay buffer has, then pad with zeroes */
- circ_buf_read(b->circ_buf, frame, buf_len);
- memset(&frame[buf_len], 0,(b->samples_per_frame - buf_len)<<1);
- /* The buffer is empty now, reset it */
- circ_buf_reset(b->circ_buf);
- #ifdef _WIN32
- LeaveCriticalSection(&b->lock);
- #else
- pthread_mutex_unlock(&b->lock_mtx);
- #endif
- return 0;
- }
- }
- circ_buf_read(b->circ_buf, frame, b->samples_per_frame);
- #ifdef _WIN32
- LeaveCriticalSection(&b->lock);
- #else
- pthread_mutex_unlock(&b->lock_mtx);
- #endif
- return 0;
- }
- int delay_buf_reset(delay_buf *b)
- {
- ASSERT_RETURN(b, -1);
- #ifdef _WIN32
- EnterCriticalSection(&b->lock);
- #else
- pthread_mutex_lock(&b->lock_mtx);
- #endif
- b->recalc_timer = RECALC_TIME;
- /* Reset buffer */
- circ_buf_reset(b->circ_buf);
- /* Reset WSOLA */
- wsola_reset(b->wsola, 0);
- #ifdef _WIN32
- LeaveCriticalSection(&b->lock);
- #else
- pthread_mutex_unlock(&b->lock_mtx);
- #endif
- return 0;
- }
|