delaybuf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. #include <assert.h>
  2. #ifdef _WIN32
  3. #define WIN32_LEAN_AND_MEAN
  4. #include <Windows.h>
  5. #include <winsock.h>
  6. #else
  7. #endif // _WIN32
  8. #include "circbuf.h"
  9. #include "delaybuf.h"
  10. #include "wsola.h"
  11. #include "memutil.h"
  12. /* Operation types of delay buffer */
  13. enum OP
  14. {
  15. OP_PUT,
  16. OP_GET
  17. };
  18. /* Specify time for delaybuf to recalculate effective delay, in ms.
  19. */
  20. #define RECALC_TIME 2000
  21. /* Default value of maximum delay, in ms, this value is used when
  22. * maximum delay requested is less than ptime (one frame length).
  23. */
  24. #define DEFAULT_MAX_DELAY 400
  25. /* Number of frames to add to learnt level for additional stability.
  26. */
  27. #define SAFE_MARGIN 0
  28. /* This structure describes internal delaybuf settings and states.
  29. */
  30. struct delay_buf
  31. {
  32. /* Properties and configuration */
  33. #ifdef _WIN32
  34. CRITICAL_SECTION lock;
  35. #else
  36. pthread_mutex_t lock_mtx;
  37. #endif
  38. unsigned samples_per_frame; /**< Number of samples in one frame */
  39. unsigned ptime; /**< Frame time, in ms */
  40. unsigned channel_count; /**< Channel count, in ms */
  41. circ_buf *circ_buf; /**< Circular buffer to store audio
  42. samples */
  43. unsigned max_cnt; /**< Maximum samples to be buffered */
  44. unsigned eff_cnt; /**< Effective count of buffered
  45. samples to keep the optimum
  46. balance between delay and
  47. stability. This is calculated
  48. based on burst level. */
  49. /* Learning vars */
  50. unsigned level; /**< Burst level counter */
  51. enum OP last_op; /**< Last op (GET or PUT) of learning*/
  52. int recalc_timer; /**< Timer for recalculating max_level*/
  53. unsigned max_level; /**< Current max burst level */
  54. /* Drift handler */
  55. wsola_t *wsola; /**< Drift handler */
  56. };
  57. int delay_buf_create(unsigned clock_rate,
  58. unsigned samples_per_frame,
  59. unsigned channel_count,
  60. unsigned max_delay,
  61. unsigned options,
  62. delay_buf **p_b)
  63. {
  64. delay_buf *b;
  65. int status;
  66. ASSERT_RETURN(samples_per_frame && clock_rate && channel_count && p_b, -1);
  67. ASSERT_RETURN(options==0, -1);
  68. b = ZALLOC_T(delay_buf);
  69. b->samples_per_frame = samples_per_frame;
  70. b->channel_count = channel_count;
  71. b->ptime = samples_per_frame * 1000 / clock_rate / channel_count;
  72. if (max_delay < b->ptime)
  73. max_delay = max(DEFAULT_MAX_DELAY, b->ptime);
  74. b->max_cnt = samples_per_frame * max_delay / b->ptime;
  75. b->eff_cnt = b->max_cnt >> 1;
  76. b->recalc_timer = RECALC_TIME;
  77. /* Create circular buffer */
  78. status = circ_buf_create(b->max_cnt, &b->circ_buf);
  79. if (status != 0)
  80. return status;
  81. /* Create WSOLA */
  82. status = wsola_create(clock_rate, samples_per_frame, 1,WSOLA_NO_FADING, &b->wsola);
  83. if (status != 0)
  84. return status;
  85. /* Finally, create mutex */
  86. #ifdef _WIN32
  87. InitializeCriticalSection(&b->lock);
  88. #else
  89. status = pthread_mutex_init(&b->lock_mtx, NULL);
  90. #endif
  91. *p_b = b;
  92. return 0;
  93. }
  94. int delay_buf_destroy(delay_buf *b)
  95. {
  96. int status;
  97. ASSERT_RETURN(b, -1);
  98. #ifdef _WIN32
  99. EnterCriticalSection(&b->lock);
  100. #else
  101. pthread_mutex_lock(&b->lock_mtx);
  102. #endif
  103. status = wsola_destroy(b->wsola);
  104. if (status == 0)
  105. b->wsola = NULL;
  106. circ_buf_destroy(b->circ_buf);
  107. b->circ_buf = NULL;
  108. #ifdef _WIN32
  109. LeaveCriticalSection(&b->lock);
  110. DeleteCriticalSection(&b->lock);
  111. #else
  112. pthread_mutex_unlock(&b->lock_mtx);
  113. pthread_mutex_destroy(&b->lock_mtx);
  114. #endif
  115. free(b);
  116. return status;
  117. }
  118. /* This function will erase samples from delay buffer.
  119. * The number of erased samples is guaranteed to be >= erase_cnt.
  120. */
  121. static void shrink_buffer(delay_buf *b, unsigned erase_cnt)
  122. {
  123. short *buf1, *buf2;
  124. unsigned buf1len;
  125. unsigned buf2len;
  126. int status;
  127. assert(b && erase_cnt && circ_buf_get_len(b->circ_buf));
  128. circ_buf_get_read_regions(b->circ_buf, &buf1, &buf1len, &buf2, &buf2len);
  129. status = wsola_discard(b->wsola, buf1, buf1len, buf2, buf2len,&erase_cnt);
  130. if ((status == 0) && (erase_cnt > 0)) {
  131. /* WSOLA discard will manage the first buffer to be full, unless
  132. * erase_cnt is greater than second buffer length. So it is safe
  133. * to just set the circular buffer length.
  134. */
  135. circ_buf_set_len(b->circ_buf, circ_buf_get_len(b->circ_buf) - erase_cnt);
  136. }
  137. }
  138. /* Fast increase, slow decrease */
  139. #define AGC_UP(cur, target) cur = (cur + target*3) >> 2
  140. #define AGC_DOWN(cur, target) cur = (cur*3 + target) >> 2
  141. #define AGC(cur, target) \
  142. if (cur < target) AGC_UP(cur, target); \
  143. else AGC_DOWN(cur, target)
  144. static void update(delay_buf *b, enum OP op)
  145. {
  146. /* Sequential operation */
  147. if (op == b->last_op) {
  148. ++b->level;
  149. return;
  150. }
  151. /* Switching operation */
  152. if (b->level > b->max_level)
  153. b->max_level = b->level;
  154. b->recalc_timer -= (b->level * b->ptime) >> 1;
  155. b->last_op = op;
  156. b->level = 1;
  157. /* Recalculate effective count based on max_level */
  158. if (b->recalc_timer <= 0) {
  159. unsigned new_eff_cnt = (b->max_level+SAFE_MARGIN)*b->samples_per_frame;
  160. /* Smoothening effective count transition */
  161. AGC(b->eff_cnt, new_eff_cnt);
  162. /* Make sure the new effective count is multiplication of
  163. * channel_count, so let's round it up.
  164. */
  165. if (b->eff_cnt % b->channel_count)
  166. b->eff_cnt += b->channel_count - (b->eff_cnt % b->channel_count);
  167. b->max_level = 0;
  168. b->recalc_timer = RECALC_TIME;
  169. }
  170. /* See if we need to shrink the buffer to reduce delay */
  171. if (op == OP_PUT && circ_buf_get_len(b->circ_buf) >
  172. b->samples_per_frame + b->eff_cnt)
  173. {
  174. unsigned erase_cnt = b->samples_per_frame >> 1;
  175. unsigned old_buf_cnt = circ_buf_get_len(b->circ_buf);
  176. shrink_buffer(b, erase_cnt);
  177. }
  178. }
  179. int delay_buf_put(delay_buf *b,
  180. short frame[])
  181. {
  182. int status;
  183. ASSERT_RETURN(b && frame, -1);
  184. #ifdef _WIN32
  185. EnterCriticalSection(&b->lock);
  186. #else
  187. pthread_mutex_lock(&b->lock_mtx);
  188. #endif
  189. update(b, OP_PUT);
  190. status = wsola_save(b->wsola, frame, FALSE);
  191. if (status != 0) {
  192. #ifdef _WIN32
  193. LeaveCriticalSection(&b->lock);
  194. #else
  195. pthread_mutex_unlock(&b->lock_mtx);
  196. #endif
  197. return status;
  198. }
  199. /* Overflow checking */
  200. if (circ_buf_get_len(b->circ_buf) + b->samples_per_frame >
  201. b->max_cnt)
  202. {
  203. unsigned erase_cnt;
  204. /* shrink one frame or just the diff? */
  205. //erase_cnt = b->samples_per_frame;
  206. erase_cnt = circ_buf_get_len(b->circ_buf) +
  207. b->samples_per_frame - b->max_cnt;
  208. shrink_buffer(b, erase_cnt);
  209. /* Check if shrinking failed or erased count is less than requested,
  210. * delaybuf needs to drop eldest samples, this is bad since the voice
  211. * samples get rough transition which may produce tick noise.
  212. */
  213. if (circ_buf_get_len(b->circ_buf) + b->samples_per_frame >
  214. b->max_cnt)
  215. {
  216. erase_cnt = circ_buf_get_len(b->circ_buf) +
  217. b->samples_per_frame - b->max_cnt;
  218. circ_buf_adv_read_ptr(b->circ_buf, erase_cnt);
  219. }
  220. }
  221. circ_buf_write(b->circ_buf, frame, b->samples_per_frame);
  222. #ifdef _WIN32
  223. LeaveCriticalSection(&b->lock);
  224. #else
  225. pthread_mutex_unlock(&b->lock_mtx);
  226. #endif
  227. return 0;
  228. }
  229. int delay_buf_get( delay_buf *b,short frame[])
  230. {
  231. int status;
  232. ASSERT_RETURN(b && frame, -1);
  233. #ifdef _WIN32
  234. EnterCriticalSection(&b->lock);
  235. #else
  236. pthread_mutex_lock(&b->lock_mtx);
  237. #endif
  238. update(b, OP_GET);
  239. /* Starvation checking */
  240. if (circ_buf_get_len(b->circ_buf) < b->samples_per_frame) {
  241. //OutputDebugStringA("g");
  242. status = wsola_generate(b->wsola, frame);
  243. if (status == 0) {
  244. if (circ_buf_get_len(b->circ_buf) == 0) {
  245. #ifdef _WIN32
  246. LeaveCriticalSection(&b->lock);
  247. #else
  248. pthread_mutex_unlock(&b->lock_mtx);
  249. #endif
  250. return 0;
  251. }
  252. /* Put generated frame into buffer */
  253. circ_buf_write(b->circ_buf, frame, b->samples_per_frame);
  254. } else {
  255. unsigned buf_len = circ_buf_get_len(b->circ_buf);
  256. /* Give all what delay buffer has, then pad with zeroes */
  257. circ_buf_read(b->circ_buf, frame, buf_len);
  258. memset(&frame[buf_len], 0,(b->samples_per_frame - buf_len)<<1);
  259. /* The buffer is empty now, reset it */
  260. circ_buf_reset(b->circ_buf);
  261. #ifdef _WIN32
  262. LeaveCriticalSection(&b->lock);
  263. #else
  264. pthread_mutex_unlock(&b->lock_mtx);
  265. #endif
  266. return 0;
  267. }
  268. }
  269. circ_buf_read(b->circ_buf, frame, b->samples_per_frame);
  270. #ifdef _WIN32
  271. LeaveCriticalSection(&b->lock);
  272. #else
  273. pthread_mutex_unlock(&b->lock_mtx);
  274. #endif
  275. return 0;
  276. }
  277. int delay_buf_reset(delay_buf *b)
  278. {
  279. ASSERT_RETURN(b, -1);
  280. #ifdef _WIN32
  281. EnterCriticalSection(&b->lock);
  282. #else
  283. pthread_mutex_lock(&b->lock_mtx);
  284. #endif
  285. b->recalc_timer = RECALC_TIME;
  286. /* Reset buffer */
  287. circ_buf_reset(b->circ_buf);
  288. /* Reset WSOLA */
  289. wsola_reset(b->wsola, 0);
  290. #ifdef _WIN32
  291. LeaveCriticalSection(&b->lock);
  292. #else
  293. pthread_mutex_unlock(&b->lock_mtx);
  294. #endif
  295. return 0;
  296. }