analysis.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. /* Copyright (c) 2011 Xiph.Org Foundation
  2. Written by Jean-Marc Valin */
  3. /*
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. - Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. - Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  13. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  14. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  15. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
  16. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  17. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  18. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  19. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include "kiss_fft.h"
  28. #include "celt.h"
  29. #include "modes.h"
  30. #include "arch.h"
  31. #include "quant_bands.h"
  32. #include <stdio.h>
  33. #include "analysis.h"
  34. #include "mlp.h"
  35. #include "stack_alloc.h"
  36. #ifndef M_PI
  37. #define M_PI 3.141592653
  38. #endif
  39. static const float dct_table[128] = {
  40. 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f,
  41. 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f,
  42. 0.351851f, 0.338330f, 0.311806f, 0.273300f, 0.224292f, 0.166664f, 0.102631f, 0.034654f,
  43. -0.034654f,-0.102631f,-0.166664f,-0.224292f,-0.273300f,-0.311806f,-0.338330f,-0.351851f,
  44. 0.346760f, 0.293969f, 0.196424f, 0.068975f,-0.068975f,-0.196424f,-0.293969f,-0.346760f,
  45. -0.346760f,-0.293969f,-0.196424f,-0.068975f, 0.068975f, 0.196424f, 0.293969f, 0.346760f,
  46. 0.338330f, 0.224292f, 0.034654f,-0.166664f,-0.311806f,-0.351851f,-0.273300f,-0.102631f,
  47. 0.102631f, 0.273300f, 0.351851f, 0.311806f, 0.166664f,-0.034654f,-0.224292f,-0.338330f,
  48. 0.326641f, 0.135299f,-0.135299f,-0.326641f,-0.326641f,-0.135299f, 0.135299f, 0.326641f,
  49. 0.326641f, 0.135299f,-0.135299f,-0.326641f,-0.326641f,-0.135299f, 0.135299f, 0.326641f,
  50. 0.311806f, 0.034654f,-0.273300f,-0.338330f,-0.102631f, 0.224292f, 0.351851f, 0.166664f,
  51. -0.166664f,-0.351851f,-0.224292f, 0.102631f, 0.338330f, 0.273300f,-0.034654f,-0.311806f,
  52. 0.293969f,-0.068975f,-0.346760f,-0.196424f, 0.196424f, 0.346760f, 0.068975f,-0.293969f,
  53. -0.293969f, 0.068975f, 0.346760f, 0.196424f,-0.196424f,-0.346760f,-0.068975f, 0.293969f,
  54. 0.273300f,-0.166664f,-0.338330f, 0.034654f, 0.351851f, 0.102631f,-0.311806f,-0.224292f,
  55. 0.224292f, 0.311806f,-0.102631f,-0.351851f,-0.034654f, 0.338330f, 0.166664f,-0.273300f,
  56. };
  57. static const float analysis_window[240] = {
  58. 0.000043f, 0.000171f, 0.000385f, 0.000685f, 0.001071f, 0.001541f, 0.002098f, 0.002739f,
  59. 0.003466f, 0.004278f, 0.005174f, 0.006156f, 0.007222f, 0.008373f, 0.009607f, 0.010926f,
  60. 0.012329f, 0.013815f, 0.015385f, 0.017037f, 0.018772f, 0.020590f, 0.022490f, 0.024472f,
  61. 0.026535f, 0.028679f, 0.030904f, 0.033210f, 0.035595f, 0.038060f, 0.040604f, 0.043227f,
  62. 0.045928f, 0.048707f, 0.051564f, 0.054497f, 0.057506f, 0.060591f, 0.063752f, 0.066987f,
  63. 0.070297f, 0.073680f, 0.077136f, 0.080665f, 0.084265f, 0.087937f, 0.091679f, 0.095492f,
  64. 0.099373f, 0.103323f, 0.107342f, 0.111427f, 0.115579f, 0.119797f, 0.124080f, 0.128428f,
  65. 0.132839f, 0.137313f, 0.141849f, 0.146447f, 0.151105f, 0.155823f, 0.160600f, 0.165435f,
  66. 0.170327f, 0.175276f, 0.180280f, 0.185340f, 0.190453f, 0.195619f, 0.200838f, 0.206107f,
  67. 0.211427f, 0.216797f, 0.222215f, 0.227680f, 0.233193f, 0.238751f, 0.244353f, 0.250000f,
  68. 0.255689f, 0.261421f, 0.267193f, 0.273005f, 0.278856f, 0.284744f, 0.290670f, 0.296632f,
  69. 0.302628f, 0.308658f, 0.314721f, 0.320816f, 0.326941f, 0.333097f, 0.339280f, 0.345492f,
  70. 0.351729f, 0.357992f, 0.364280f, 0.370590f, 0.376923f, 0.383277f, 0.389651f, 0.396044f,
  71. 0.402455f, 0.408882f, 0.415325f, 0.421783f, 0.428254f, 0.434737f, 0.441231f, 0.447736f,
  72. 0.454249f, 0.460770f, 0.467298f, 0.473832f, 0.480370f, 0.486912f, 0.493455f, 0.500000f,
  73. 0.506545f, 0.513088f, 0.519630f, 0.526168f, 0.532702f, 0.539230f, 0.545751f, 0.552264f,
  74. 0.558769f, 0.565263f, 0.571746f, 0.578217f, 0.584675f, 0.591118f, 0.597545f, 0.603956f,
  75. 0.610349f, 0.616723f, 0.623077f, 0.629410f, 0.635720f, 0.642008f, 0.648271f, 0.654508f,
  76. 0.660720f, 0.666903f, 0.673059f, 0.679184f, 0.685279f, 0.691342f, 0.697372f, 0.703368f,
  77. 0.709330f, 0.715256f, 0.721144f, 0.726995f, 0.732807f, 0.738579f, 0.744311f, 0.750000f,
  78. 0.755647f, 0.761249f, 0.766807f, 0.772320f, 0.777785f, 0.783203f, 0.788573f, 0.793893f,
  79. 0.799162f, 0.804381f, 0.809547f, 0.814660f, 0.819720f, 0.824724f, 0.829673f, 0.834565f,
  80. 0.839400f, 0.844177f, 0.848895f, 0.853553f, 0.858151f, 0.862687f, 0.867161f, 0.871572f,
  81. 0.875920f, 0.880203f, 0.884421f, 0.888573f, 0.892658f, 0.896677f, 0.900627f, 0.904508f,
  82. 0.908321f, 0.912063f, 0.915735f, 0.919335f, 0.922864f, 0.926320f, 0.929703f, 0.933013f,
  83. 0.936248f, 0.939409f, 0.942494f, 0.945503f, 0.948436f, 0.951293f, 0.954072f, 0.956773f,
  84. 0.959396f, 0.961940f, 0.964405f, 0.966790f, 0.969096f, 0.971321f, 0.973465f, 0.975528f,
  85. 0.977510f, 0.979410f, 0.981228f, 0.982963f, 0.984615f, 0.986185f, 0.987671f, 0.989074f,
  86. 0.990393f, 0.991627f, 0.992778f, 0.993844f, 0.994826f, 0.995722f, 0.996534f, 0.997261f,
  87. 0.997902f, 0.998459f, 0.998929f, 0.999315f, 0.999615f, 0.999829f, 0.999957f, 1.000000f,
  88. };
  89. static const int tbands[NB_TBANDS+1] = {
  90. 2, 4, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 68, 80, 96, 120
  91. };
  92. static const int extra_bands[NB_TOT_BANDS+1] = {
  93. 1, 2, 4, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, 68, 80, 96, 120, 160, 200
  94. };
  95. /*static const float tweight[NB_TBANDS+1] = {
  96. .3, .4, .5, .6, .7, .8, .9, 1., 1., 1., 1., 1., 1., 1., .8, .7, .6, .5
  97. };*/
  98. #define NB_TONAL_SKIP_BANDS 9
  99. #define cA 0.43157974f
  100. #define cB 0.67848403f
  101. #define cC 0.08595542f
  102. #define cE ((float)M_PI/2)
  103. static OPUS_INLINE float fast_atan2f(float y, float x) {
  104. float x2, y2;
  105. /* Should avoid underflow on the values we'll get */
  106. if (ABS16(x)+ABS16(y)<1e-9f)
  107. {
  108. x*=1e12f;
  109. y*=1e12f;
  110. }
  111. x2 = x*x;
  112. y2 = y*y;
  113. if(x2<y2){
  114. float den = (y2 + cB*x2) * (y2 + cC*x2);
  115. if (den!=0)
  116. return -x*y*(y2 + cA*x2) / den + (y<0 ? -cE : cE);
  117. else
  118. return (y<0 ? -cE : cE);
  119. }else{
  120. float den = (x2 + cB*y2) * (x2 + cC*y2);
  121. if (den!=0)
  122. return x*y*(x2 + cA*y2) / den + (y<0 ? -cE : cE) - (x*y<0 ? -cE : cE);
  123. else
  124. return (y<0 ? -cE : cE) - (x*y<0 ? -cE : cE);
  125. }
  126. }
  127. void tonality_analysis_init(TonalityAnalysisState *tonal)
  128. {
  129. /* Initialize reusable fields. */
  130. tonal->arch = opus_select_arch();
  131. /* Clear remaining fields. */
  132. tonality_analysis_reset(tonal);
  133. }
  134. void tonality_analysis_reset(TonalityAnalysisState *tonal)
  135. {
  136. /* Clear non-reusable fields. */
  137. char *start = (char*)&tonal->TONALITY_ANALYSIS_RESET_START;
  138. OPUS_CLEAR(start, sizeof(TonalityAnalysisState) - (start - (char*)tonal));
  139. }
  140. void tonality_get_info(TonalityAnalysisState *tonal, AnalysisInfo *info_out, int len)
  141. {
  142. int pos;
  143. int curr_lookahead;
  144. float psum;
  145. int i;
  146. pos = tonal->read_pos;
  147. curr_lookahead = tonal->write_pos-tonal->read_pos;
  148. if (curr_lookahead<0)
  149. curr_lookahead += DETECT_SIZE;
  150. if (len > 480 && pos != tonal->write_pos)
  151. {
  152. pos++;
  153. if (pos==DETECT_SIZE)
  154. pos=0;
  155. }
  156. if (pos == tonal->write_pos)
  157. pos--;
  158. if (pos<0)
  159. pos = DETECT_SIZE-1;
  160. OPUS_COPY(info_out, &tonal->info[pos], 1);
  161. tonal->read_subframe += len/120;
  162. while (tonal->read_subframe>=4)
  163. {
  164. tonal->read_subframe -= 4;
  165. tonal->read_pos++;
  166. }
  167. if (tonal->read_pos>=DETECT_SIZE)
  168. tonal->read_pos-=DETECT_SIZE;
  169. /* Compensate for the delay in the features themselves.
  170. FIXME: Need a better estimate the 10 I just made up */
  171. curr_lookahead = IMAX(curr_lookahead-10, 0);
  172. psum=0;
  173. /* Summing the probability of transition patterns that involve music at
  174. time (DETECT_SIZE-curr_lookahead-1) */
  175. for (i=0;i<DETECT_SIZE-curr_lookahead;i++)
  176. psum += tonal->pmusic[i];
  177. for (;i<DETECT_SIZE;i++)
  178. psum += tonal->pspeech[i];
  179. psum = psum*tonal->music_confidence + (1-psum)*tonal->speech_confidence;
  180. /*printf("%f %f %f\n", psum, info_out->music_prob, info_out->tonality);*/
  181. info_out->music_prob = psum;
  182. }
  183. static void tonality_analysis(TonalityAnalysisState *tonal, const CELTMode *celt_mode, const void *x, int len, int offset, int c1, int c2, int C, int lsb_depth, downmix_func downmix)
  184. {
  185. int i, b;
  186. const kiss_fft_state *kfft;
  187. VARDECL(kiss_fft_cpx, in);
  188. VARDECL(kiss_fft_cpx, out);
  189. int N = 480, N2=240;
  190. float * OPUS_RESTRICT A = tonal->angle;
  191. float * OPUS_RESTRICT dA = tonal->d_angle;
  192. float * OPUS_RESTRICT d2A = tonal->d2_angle;
  193. VARDECL(float, tonality);
  194. VARDECL(float, noisiness);
  195. float band_tonality[NB_TBANDS];
  196. float logE[NB_TBANDS];
  197. float BFCC[8];
  198. float features[25];
  199. float frame_tonality;
  200. float max_frame_tonality;
  201. /*float tw_sum=0;*/
  202. float frame_noisiness;
  203. const float pi4 = (float)(M_PI*M_PI*M_PI*M_PI);
  204. float slope=0;
  205. float frame_stationarity;
  206. float relativeE;
  207. float frame_probs[2];
  208. float alpha, alphaE, alphaE2;
  209. float frame_loudness;
  210. float bandwidth_mask;
  211. int bandwidth=0;
  212. float maxE = 0;
  213. float noise_floor;
  214. int remaining;
  215. AnalysisInfo *info;
  216. SAVE_STACK;
  217. tonal->last_transition++;
  218. alpha = 1.f/IMIN(20, 1+tonal->count);
  219. alphaE = 1.f/IMIN(50, 1+tonal->count);
  220. alphaE2 = 1.f/IMIN(1000, 1+tonal->count);
  221. if (tonal->count<4)
  222. tonal->music_prob = .5;
  223. kfft = celt_mode->mdct.kfft[0];
  224. if (tonal->count==0)
  225. tonal->mem_fill = 240;
  226. downmix(x, &tonal->inmem[tonal->mem_fill], IMIN(len, ANALYSIS_BUF_SIZE-tonal->mem_fill), offset, c1, c2, C);
  227. if (tonal->mem_fill+len < ANALYSIS_BUF_SIZE)
  228. {
  229. tonal->mem_fill += len;
  230. /* Don't have enough to update the analysis */
  231. RESTORE_STACK;
  232. return;
  233. }
  234. info = &tonal->info[tonal->write_pos++];
  235. if (tonal->write_pos>=DETECT_SIZE)
  236. tonal->write_pos-=DETECT_SIZE;
  237. ALLOC(in, 480, kiss_fft_cpx);
  238. ALLOC(out, 480, kiss_fft_cpx);
  239. ALLOC(tonality, 240, float);
  240. ALLOC(noisiness, 240, float);
  241. for (i=0;i<N2;i++)
  242. {
  243. float w = analysis_window[i];
  244. in[i].r = (kiss_fft_scalar)(w*tonal->inmem[i]);
  245. in[i].i = (kiss_fft_scalar)(w*tonal->inmem[N2+i]);
  246. in[N-i-1].r = (kiss_fft_scalar)(w*tonal->inmem[N-i-1]);
  247. in[N-i-1].i = (kiss_fft_scalar)(w*tonal->inmem[N+N2-i-1]);
  248. }
  249. OPUS_MOVE(tonal->inmem, tonal->inmem+ANALYSIS_BUF_SIZE-240, 240);
  250. remaining = len - (ANALYSIS_BUF_SIZE-tonal->mem_fill);
  251. downmix(x, &tonal->inmem[240], remaining, offset+ANALYSIS_BUF_SIZE-tonal->mem_fill, c1, c2, C);
  252. tonal->mem_fill = 240 + remaining;
  253. opus_fft(kfft, in, out, tonal->arch);
  254. #ifndef FIXED_POINT
  255. /* If there's any NaN on the input, the entire output will be NaN, so we only need to check one value. */
  256. if (celt_isnan(out[0].r))
  257. {
  258. info->valid = 0;
  259. RESTORE_STACK;
  260. return;
  261. }
  262. #endif
  263. for (i=1;i<N2;i++)
  264. {
  265. float X1r, X2r, X1i, X2i;
  266. float angle, d_angle, d2_angle;
  267. float angle2, d_angle2, d2_angle2;
  268. float mod1, mod2, avg_mod;
  269. X1r = (float)out[i].r+out[N-i].r;
  270. X1i = (float)out[i].i-out[N-i].i;
  271. X2r = (float)out[i].i+out[N-i].i;
  272. X2i = (float)out[N-i].r-out[i].r;
  273. angle = (float)(.5f/M_PI)*fast_atan2f(X1i, X1r);
  274. d_angle = angle - A[i];
  275. d2_angle = d_angle - dA[i];
  276. angle2 = (float)(.5f/M_PI)*fast_atan2f(X2i, X2r);
  277. d_angle2 = angle2 - angle;
  278. d2_angle2 = d_angle2 - d_angle;
  279. mod1 = d2_angle - (float)floor(.5+d2_angle);
  280. noisiness[i] = ABS16(mod1);
  281. mod1 *= mod1;
  282. mod1 *= mod1;
  283. mod2 = d2_angle2 - (float)floor(.5+d2_angle2);
  284. noisiness[i] += ABS16(mod2);
  285. mod2 *= mod2;
  286. mod2 *= mod2;
  287. avg_mod = .25f*(d2A[i]+2.f*mod1+mod2);
  288. tonality[i] = 1.f/(1.f+40.f*16.f*pi4*avg_mod)-.015f;
  289. A[i] = angle2;
  290. dA[i] = d_angle2;
  291. d2A[i] = mod2;
  292. }
  293. frame_tonality = 0;
  294. max_frame_tonality = 0;
  295. /*tw_sum = 0;*/
  296. info->activity = 0;
  297. frame_noisiness = 0;
  298. frame_stationarity = 0;
  299. if (!tonal->count)
  300. {
  301. for (b=0;b<NB_TBANDS;b++)
  302. {
  303. tonal->lowE[b] = 1e10;
  304. tonal->highE[b] = -1e10;
  305. }
  306. }
  307. relativeE = 0;
  308. frame_loudness = 0;
  309. for (b=0;b<NB_TBANDS;b++)
  310. {
  311. float E=0, tE=0, nE=0;
  312. float L1, L2;
  313. float stationarity;
  314. for (i=tbands[b];i<tbands[b+1];i++)
  315. {
  316. float binE = out[i].r*(float)out[i].r + out[N-i].r*(float)out[N-i].r
  317. + out[i].i*(float)out[i].i + out[N-i].i*(float)out[N-i].i;
  318. #ifdef FIXED_POINT
  319. /* FIXME: It's probably best to change the BFCC filter initial state instead */
  320. binE *= 5.55e-17f;
  321. #endif
  322. E += binE;
  323. tE += binE*tonality[i];
  324. nE += binE*2.f*(.5f-noisiness[i]);
  325. }
  326. #ifndef FIXED_POINT
  327. /* Check for extreme band energies that could cause NaNs later. */
  328. if (!(E<1e9f) || celt_isnan(E))
  329. {
  330. info->valid = 0;
  331. RESTORE_STACK;
  332. return;
  333. }
  334. #endif
  335. tonal->E[tonal->E_count][b] = E;
  336. frame_noisiness += nE/(1e-15f+E);
  337. frame_loudness += (float)sqrt(E+1e-10f);
  338. logE[b] = (float)log(E+1e-10f);
  339. tonal->lowE[b] = MIN32(logE[b], tonal->lowE[b]+.01f);
  340. tonal->highE[b] = MAX32(logE[b], tonal->highE[b]-.1f);
  341. if (tonal->highE[b] < tonal->lowE[b]+1.f)
  342. {
  343. tonal->highE[b]+=.5f;
  344. tonal->lowE[b]-=.5f;
  345. }
  346. relativeE += (logE[b]-tonal->lowE[b])/(1e-15f+tonal->highE[b]-tonal->lowE[b]);
  347. L1=L2=0;
  348. for (i=0;i<NB_FRAMES;i++)
  349. {
  350. L1 += (float)sqrt(tonal->E[i][b]);
  351. L2 += tonal->E[i][b];
  352. }
  353. stationarity = MIN16(0.99f,L1/(float)sqrt(1e-15+NB_FRAMES*L2));
  354. stationarity *= stationarity;
  355. stationarity *= stationarity;
  356. frame_stationarity += stationarity;
  357. /*band_tonality[b] = tE/(1e-15+E)*/;
  358. band_tonality[b] = MAX16(tE/(1e-15f+E), stationarity*tonal->prev_band_tonality[b]);
  359. #if 0
  360. if (b>=NB_TONAL_SKIP_BANDS)
  361. {
  362. frame_tonality += tweight[b]*band_tonality[b];
  363. tw_sum += tweight[b];
  364. }
  365. #else
  366. frame_tonality += band_tonality[b];
  367. if (b>=NB_TBANDS-NB_TONAL_SKIP_BANDS)
  368. frame_tonality -= band_tonality[b-NB_TBANDS+NB_TONAL_SKIP_BANDS];
  369. #endif
  370. max_frame_tonality = MAX16(max_frame_tonality, (1.f+.03f*(b-NB_TBANDS))*frame_tonality);
  371. slope += band_tonality[b]*(b-8);
  372. /*printf("%f %f ", band_tonality[b], stationarity);*/
  373. tonal->prev_band_tonality[b] = band_tonality[b];
  374. }
  375. bandwidth_mask = 0;
  376. bandwidth = 0;
  377. maxE = 0;
  378. noise_floor = 5.7e-4f/(1<<(IMAX(0,lsb_depth-8)));
  379. #ifdef FIXED_POINT
  380. noise_floor *= 1<<(15+SIG_SHIFT);
  381. #endif
  382. noise_floor *= noise_floor;
  383. for (b=0;b<NB_TOT_BANDS;b++)
  384. {
  385. float E=0;
  386. int band_start, band_end;
  387. /* Keep a margin of 300 Hz for aliasing */
  388. band_start = extra_bands[b];
  389. band_end = extra_bands[b+1];
  390. for (i=band_start;i<band_end;i++)
  391. {
  392. float binE = out[i].r*(float)out[i].r + out[N-i].r*(float)out[N-i].r
  393. + out[i].i*(float)out[i].i + out[N-i].i*(float)out[N-i].i;
  394. E += binE;
  395. }
  396. maxE = MAX32(maxE, E);
  397. tonal->meanE[b] = MAX32((1-alphaE2)*tonal->meanE[b], E);
  398. E = MAX32(E, tonal->meanE[b]);
  399. /* Use a simple follower with 13 dB/Bark slope for spreading function */
  400. bandwidth_mask = MAX32(.05f*bandwidth_mask, E);
  401. /* Consider the band "active" only if all these conditions are met:
  402. 1) less than 10 dB below the simple follower
  403. 2) less than 90 dB below the peak band (maximal masking possible considering
  404. both the ATH and the loudness-dependent slope of the spreading function)
  405. 3) above the PCM quantization noise floor
  406. */
  407. if (E>.1*bandwidth_mask && E*1e9f > maxE && E > noise_floor*(band_end-band_start))
  408. bandwidth = b;
  409. }
  410. if (tonal->count<=2)
  411. bandwidth = 20;
  412. frame_loudness = 20*(float)log10(frame_loudness);
  413. tonal->Etracker = MAX32(tonal->Etracker-.03f, frame_loudness);
  414. tonal->lowECount *= (1-alphaE);
  415. if (frame_loudness < tonal->Etracker-30)
  416. tonal->lowECount += alphaE;
  417. for (i=0;i<8;i++)
  418. {
  419. float sum=0;
  420. for (b=0;b<16;b++)
  421. sum += dct_table[i*16+b]*logE[b];
  422. BFCC[i] = sum;
  423. }
  424. frame_stationarity /= NB_TBANDS;
  425. relativeE /= NB_TBANDS;
  426. if (tonal->count<10)
  427. relativeE = .5;
  428. frame_noisiness /= NB_TBANDS;
  429. #if 1
  430. info->activity = frame_noisiness + (1-frame_noisiness)*relativeE;
  431. #else
  432. info->activity = .5*(1+frame_noisiness-frame_stationarity);
  433. #endif
  434. frame_tonality = (max_frame_tonality/(NB_TBANDS-NB_TONAL_SKIP_BANDS));
  435. frame_tonality = MAX16(frame_tonality, tonal->prev_tonality*.8f);
  436. tonal->prev_tonality = frame_tonality;
  437. slope /= 8*8;
  438. info->tonality_slope = slope;
  439. tonal->E_count = (tonal->E_count+1)%NB_FRAMES;
  440. tonal->count++;
  441. info->tonality = frame_tonality;
  442. for (i=0;i<4;i++)
  443. features[i] = -0.12299f*(BFCC[i]+tonal->mem[i+24]) + 0.49195f*(tonal->mem[i]+tonal->mem[i+16]) + 0.69693f*tonal->mem[i+8] - 1.4349f*tonal->cmean[i];
  444. for (i=0;i<4;i++)
  445. tonal->cmean[i] = (1-alpha)*tonal->cmean[i] + alpha*BFCC[i];
  446. for (i=0;i<4;i++)
  447. features[4+i] = 0.63246f*(BFCC[i]-tonal->mem[i+24]) + 0.31623f*(tonal->mem[i]-tonal->mem[i+16]);
  448. for (i=0;i<3;i++)
  449. features[8+i] = 0.53452f*(BFCC[i]+tonal->mem[i+24]) - 0.26726f*(tonal->mem[i]+tonal->mem[i+16]) -0.53452f*tonal->mem[i+8];
  450. if (tonal->count > 5)
  451. {
  452. for (i=0;i<9;i++)
  453. tonal->std[i] = (1-alpha)*tonal->std[i] + alpha*features[i]*features[i];
  454. }
  455. for (i=0;i<8;i++)
  456. {
  457. tonal->mem[i+24] = tonal->mem[i+16];
  458. tonal->mem[i+16] = tonal->mem[i+8];
  459. tonal->mem[i+8] = tonal->mem[i];
  460. tonal->mem[i] = BFCC[i];
  461. }
  462. for (i=0;i<9;i++)
  463. features[11+i] = (float)sqrt(tonal->std[i]);
  464. features[20] = info->tonality;
  465. features[21] = info->activity;
  466. features[22] = frame_stationarity;
  467. features[23] = info->tonality_slope;
  468. features[24] = tonal->lowECount;
  469. #ifndef DISABLE_FLOAT_API
  470. mlp_process(&net, features, frame_probs);
  471. frame_probs[0] = .5f*(frame_probs[0]+1);
  472. /* Curve fitting between the MLP probability and the actual probability */
  473. frame_probs[0] = .01f + 1.21f*frame_probs[0]*frame_probs[0] - .23f*(float)pow(frame_probs[0], 10);
  474. /* Probability of active audio (as opposed to silence) */
  475. frame_probs[1] = .5f*frame_probs[1]+.5f;
  476. /* Consider that silence has a 50-50 probability. */
  477. frame_probs[0] = frame_probs[1]*frame_probs[0] + (1-frame_probs[1])*.5f;
  478. /*printf("%f %f ", frame_probs[0], frame_probs[1]);*/
  479. {
  480. /* Probability of state transition */
  481. float tau;
  482. /* Represents independence of the MLP probabilities, where
  483. beta=1 means fully independent. */
  484. float beta;
  485. /* Denormalized probability of speech (p0) and music (p1) after update */
  486. float p0, p1;
  487. /* Probabilities for "all speech" and "all music" */
  488. float s0, m0;
  489. /* Probability sum for renormalisation */
  490. float psum;
  491. /* Instantaneous probability of speech and music, with beta pre-applied. */
  492. float speech0;
  493. float music0;
  494. float p, q;
  495. /* One transition every 3 minutes of active audio */
  496. tau = .00005f*frame_probs[1];
  497. /* Adapt beta based on how "unexpected" the new prob is */
  498. p = MAX16(.05f,MIN16(.95f,frame_probs[0]));
  499. q = MAX16(.05f,MIN16(.95f,tonal->music_prob));
  500. beta = .01f+.05f*ABS16(p-q)/(p*(1-q)+q*(1-p));
  501. /* p0 and p1 are the probabilities of speech and music at this frame
  502. using only information from previous frame and applying the
  503. state transition model */
  504. p0 = (1-tonal->music_prob)*(1-tau) + tonal->music_prob *tau;
  505. p1 = tonal->music_prob *(1-tau) + (1-tonal->music_prob)*tau;
  506. /* We apply the current probability with exponent beta to work around
  507. the fact that the probability estimates aren't independent. */
  508. p0 *= (float)pow(1-frame_probs[0], beta);
  509. p1 *= (float)pow(frame_probs[0], beta);
  510. /* Normalise the probabilities to get the Marokv probability of music. */
  511. tonal->music_prob = p1/(p0+p1);
  512. info->music_prob = tonal->music_prob;
  513. /* This chunk of code deals with delayed decision. */
  514. psum=1e-20f;
  515. /* Instantaneous probability of speech and music, with beta pre-applied. */
  516. speech0 = (float)pow(1-frame_probs[0], beta);
  517. music0 = (float)pow(frame_probs[0], beta);
  518. if (tonal->count==1)
  519. {
  520. tonal->pspeech[0]=.5;
  521. tonal->pmusic [0]=.5;
  522. }
  523. /* Updated probability of having only speech (s0) or only music (m0),
  524. before considering the new observation. */
  525. s0 = tonal->pspeech[0] + tonal->pspeech[1];
  526. m0 = tonal->pmusic [0] + tonal->pmusic [1];
  527. /* Updates s0 and m0 with instantaneous probability. */
  528. tonal->pspeech[0] = s0*(1-tau)*speech0;
  529. tonal->pmusic [0] = m0*(1-tau)*music0;
  530. /* Propagate the transition probabilities */
  531. for (i=1;i<DETECT_SIZE-1;i++)
  532. {
  533. tonal->pspeech[i] = tonal->pspeech[i+1]*speech0;
  534. tonal->pmusic [i] = tonal->pmusic [i+1]*music0;
  535. }
  536. /* Probability that the latest frame is speech, when all the previous ones were music. */
  537. tonal->pspeech[DETECT_SIZE-1] = m0*tau*speech0;
  538. /* Probability that the latest frame is music, when all the previous ones were speech. */
  539. tonal->pmusic [DETECT_SIZE-1] = s0*tau*music0;
  540. /* Renormalise probabilities to 1 */
  541. for (i=0;i<DETECT_SIZE;i++)
  542. psum += tonal->pspeech[i] + tonal->pmusic[i];
  543. psum = 1.f/psum;
  544. for (i=0;i<DETECT_SIZE;i++)
  545. {
  546. tonal->pspeech[i] *= psum;
  547. tonal->pmusic [i] *= psum;
  548. }
  549. psum = tonal->pmusic[0];
  550. for (i=1;i<DETECT_SIZE;i++)
  551. psum += tonal->pspeech[i];
  552. /* Estimate our confidence in the speech/music decisions */
  553. if (frame_probs[1]>.75)
  554. {
  555. if (tonal->music_prob>.9)
  556. {
  557. float adapt;
  558. adapt = 1.f/(++tonal->music_confidence_count);
  559. tonal->music_confidence_count = IMIN(tonal->music_confidence_count, 500);
  560. tonal->music_confidence += adapt*MAX16(-.2f,frame_probs[0]-tonal->music_confidence);
  561. }
  562. if (tonal->music_prob<.1)
  563. {
  564. float adapt;
  565. adapt = 1.f/(++tonal->speech_confidence_count);
  566. tonal->speech_confidence_count = IMIN(tonal->speech_confidence_count, 500);
  567. tonal->speech_confidence += adapt*MIN16(.2f,frame_probs[0]-tonal->speech_confidence);
  568. }
  569. } else {
  570. if (tonal->music_confidence_count==0)
  571. tonal->music_confidence = .9f;
  572. if (tonal->speech_confidence_count==0)
  573. tonal->speech_confidence = .1f;
  574. }
  575. }
  576. if (tonal->last_music != (tonal->music_prob>.5f))
  577. tonal->last_transition=0;
  578. tonal->last_music = tonal->music_prob>.5f;
  579. #else
  580. info->music_prob = 0;
  581. #endif
  582. /*for (i=0;i<25;i++)
  583. printf("%f ", features[i]);
  584. printf("\n");*/
  585. info->bandwidth = bandwidth;
  586. /*printf("%d %d\n", info->bandwidth, info->opus_bandwidth);*/
  587. info->noisiness = frame_noisiness;
  588. info->valid = 1;
  589. RESTORE_STACK;
  590. }
  591. void run_analysis(TonalityAnalysisState *analysis, const CELTMode *celt_mode, const void *analysis_pcm,
  592. int analysis_frame_size, int frame_size, int c1, int c2, int C, opus_int32 Fs,
  593. int lsb_depth, downmix_func downmix, AnalysisInfo *analysis_info)
  594. {
  595. int offset;
  596. int pcm_len;
  597. if (analysis_pcm != NULL)
  598. {
  599. /* Avoid overflow/wrap-around of the analysis buffer */
  600. analysis_frame_size = IMIN((DETECT_SIZE-5)*Fs/100, analysis_frame_size);
  601. pcm_len = analysis_frame_size - analysis->analysis_offset;
  602. offset = analysis->analysis_offset;
  603. do {
  604. tonality_analysis(analysis, celt_mode, analysis_pcm, IMIN(480, pcm_len), offset, c1, c2, C, lsb_depth, downmix);
  605. offset += 480;
  606. pcm_len -= 480;
  607. } while (pcm_len>0);
  608. analysis->analysis_offset = analysis_frame_size;
  609. analysis->analysis_offset -= frame_size;
  610. }
  611. analysis_info->valid = 0;
  612. tonality_get_info(analysis, analysis_info, frame_size);
  613. }