analysis.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. #define ANALYSIS_C
  28. #ifdef MLP_TRAINING
  29. #include <stdio.h>
  30. #endif
  31. #include "mathops.h"
  32. #include "kiss_fft.h"
  33. #include "celt.h"
  34. #include "modes.h"
  35. #include "arch.h"
  36. #include "quant_bands.h"
  37. #include "analysis.h"
  38. #include "mlp.h"
  39. #include "stack_alloc.h"
  40. #include "float_cast.h"
  41. #ifndef M_PI
  42. #define M_PI 3.141592653
  43. #endif
  44. #ifndef DISABLE_FLOAT_API
  45. #define TRANSITION_PENALTY 10
  46. static const float dct_table[128] = {
  47. 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f,
  48. 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f, 0.250000f,
  49. 0.351851f, 0.338330f, 0.311806f, 0.273300f, 0.224292f, 0.166664f, 0.102631f, 0.034654f,
  50. -0.034654f,-0.102631f,-0.166664f,-0.224292f,-0.273300f,-0.311806f,-0.338330f,-0.351851f,
  51. 0.346760f, 0.293969f, 0.196424f, 0.068975f,-0.068975f,-0.196424f,-0.293969f,-0.346760f,
  52. -0.346760f,-0.293969f,-0.196424f,-0.068975f, 0.068975f, 0.196424f, 0.293969f, 0.346760f,
  53. 0.338330f, 0.224292f, 0.034654f,-0.166664f,-0.311806f,-0.351851f,-0.273300f,-0.102631f,
  54. 0.102631f, 0.273300f, 0.351851f, 0.311806f, 0.166664f,-0.034654f,-0.224292f,-0.338330f,
  55. 0.326641f, 0.135299f,-0.135299f,-0.326641f,-0.326641f,-0.135299f, 0.135299f, 0.326641f,
  56. 0.326641f, 0.135299f,-0.135299f,-0.326641f,-0.326641f,-0.135299f, 0.135299f, 0.326641f,
  57. 0.311806f, 0.034654f,-0.273300f,-0.338330f,-0.102631f, 0.224292f, 0.351851f, 0.166664f,
  58. -0.166664f,-0.351851f,-0.224292f, 0.102631f, 0.338330f, 0.273300f,-0.034654f,-0.311806f,
  59. 0.293969f,-0.068975f,-0.346760f,-0.196424f, 0.196424f, 0.346760f, 0.068975f,-0.293969f,
  60. -0.293969f, 0.068975f, 0.346760f, 0.196424f,-0.196424f,-0.346760f,-0.068975f, 0.293969f,
  61. 0.273300f,-0.166664f,-0.338330f, 0.034654f, 0.351851f, 0.102631f,-0.311806f,-0.224292f,
  62. 0.224292f, 0.311806f,-0.102631f,-0.351851f,-0.034654f, 0.338330f, 0.166664f,-0.273300f,
  63. };
  64. static const float analysis_window[240] = {
  65. 0.000043f, 0.000171f, 0.000385f, 0.000685f, 0.001071f, 0.001541f, 0.002098f, 0.002739f,
  66. 0.003466f, 0.004278f, 0.005174f, 0.006156f, 0.007222f, 0.008373f, 0.009607f, 0.010926f,
  67. 0.012329f, 0.013815f, 0.015385f, 0.017037f, 0.018772f, 0.020590f, 0.022490f, 0.024472f,
  68. 0.026535f, 0.028679f, 0.030904f, 0.033210f, 0.035595f, 0.038060f, 0.040604f, 0.043227f,
  69. 0.045928f, 0.048707f, 0.051564f, 0.054497f, 0.057506f, 0.060591f, 0.063752f, 0.066987f,
  70. 0.070297f, 0.073680f, 0.077136f, 0.080665f, 0.084265f, 0.087937f, 0.091679f, 0.095492f,
  71. 0.099373f, 0.103323f, 0.107342f, 0.111427f, 0.115579f, 0.119797f, 0.124080f, 0.128428f,
  72. 0.132839f, 0.137313f, 0.141849f, 0.146447f, 0.151105f, 0.155823f, 0.160600f, 0.165435f,
  73. 0.170327f, 0.175276f, 0.180280f, 0.185340f, 0.190453f, 0.195619f, 0.200838f, 0.206107f,
  74. 0.211427f, 0.216797f, 0.222215f, 0.227680f, 0.233193f, 0.238751f, 0.244353f, 0.250000f,
  75. 0.255689f, 0.261421f, 0.267193f, 0.273005f, 0.278856f, 0.284744f, 0.290670f, 0.296632f,
  76. 0.302628f, 0.308658f, 0.314721f, 0.320816f, 0.326941f, 0.333097f, 0.339280f, 0.345492f,
  77. 0.351729f, 0.357992f, 0.364280f, 0.370590f, 0.376923f, 0.383277f, 0.389651f, 0.396044f,
  78. 0.402455f, 0.408882f, 0.415325f, 0.421783f, 0.428254f, 0.434737f, 0.441231f, 0.447736f,
  79. 0.454249f, 0.460770f, 0.467298f, 0.473832f, 0.480370f, 0.486912f, 0.493455f, 0.500000f,
  80. 0.506545f, 0.513088f, 0.519630f, 0.526168f, 0.532702f, 0.539230f, 0.545751f, 0.552264f,
  81. 0.558769f, 0.565263f, 0.571746f, 0.578217f, 0.584675f, 0.591118f, 0.597545f, 0.603956f,
  82. 0.610349f, 0.616723f, 0.623077f, 0.629410f, 0.635720f, 0.642008f, 0.648271f, 0.654508f,
  83. 0.660720f, 0.666903f, 0.673059f, 0.679184f, 0.685279f, 0.691342f, 0.697372f, 0.703368f,
  84. 0.709330f, 0.715256f, 0.721144f, 0.726995f, 0.732807f, 0.738579f, 0.744311f, 0.750000f,
  85. 0.755647f, 0.761249f, 0.766807f, 0.772320f, 0.777785f, 0.783203f, 0.788573f, 0.793893f,
  86. 0.799162f, 0.804381f, 0.809547f, 0.814660f, 0.819720f, 0.824724f, 0.829673f, 0.834565f,
  87. 0.839400f, 0.844177f, 0.848895f, 0.853553f, 0.858151f, 0.862687f, 0.867161f, 0.871572f,
  88. 0.875920f, 0.880203f, 0.884421f, 0.888573f, 0.892658f, 0.896677f, 0.900627f, 0.904508f,
  89. 0.908321f, 0.912063f, 0.915735f, 0.919335f, 0.922864f, 0.926320f, 0.929703f, 0.933013f,
  90. 0.936248f, 0.939409f, 0.942494f, 0.945503f, 0.948436f, 0.951293f, 0.954072f, 0.956773f,
  91. 0.959396f, 0.961940f, 0.964405f, 0.966790f, 0.969096f, 0.971321f, 0.973465f, 0.975528f,
  92. 0.977510f, 0.979410f, 0.981228f, 0.982963f, 0.984615f, 0.986185f, 0.987671f, 0.989074f,
  93. 0.990393f, 0.991627f, 0.992778f, 0.993844f, 0.994826f, 0.995722f, 0.996534f, 0.997261f,
  94. 0.997902f, 0.998459f, 0.998929f, 0.999315f, 0.999615f, 0.999829f, 0.999957f, 1.000000f,
  95. };
  96. static const int tbands[NB_TBANDS+1] = {
  97. 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 136, 160, 192, 240
  98. };
  99. #define NB_TONAL_SKIP_BANDS 9
  100. static opus_val32 silk_resampler_down2_hp(
  101. opus_val32 *S, /* I/O State vector [ 2 ] */
  102. opus_val32 *out, /* O Output signal [ floor(len/2) ] */
  103. const opus_val32 *in, /* I Input signal [ len ] */
  104. int inLen /* I Number of input samples */
  105. )
  106. {
  107. int k, len2 = inLen/2;
  108. opus_val32 in32, out32, out32_hp, Y, X;
  109. opus_val64 hp_ener = 0;
  110. /* Internal variables and state are in Q10 format */
  111. for( k = 0; k < len2; k++ ) {
  112. /* Convert to Q10 */
  113. in32 = in[ 2 * k ];
  114. /* All-pass section for even input sample */
  115. Y = SUB32( in32, S[ 0 ] );
  116. X = MULT16_32_Q15(QCONST16(0.6074371f, 15), Y);
  117. out32 = ADD32( S[ 0 ], X );
  118. S[ 0 ] = ADD32( in32, X );
  119. out32_hp = out32;
  120. /* Convert to Q10 */
  121. in32 = in[ 2 * k + 1 ];
  122. /* All-pass section for odd input sample, and add to output of previous section */
  123. Y = SUB32( in32, S[ 1 ] );
  124. X = MULT16_32_Q15(QCONST16(0.15063f, 15), Y);
  125. out32 = ADD32( out32, S[ 1 ] );
  126. out32 = ADD32( out32, X );
  127. S[ 1 ] = ADD32( in32, X );
  128. Y = SUB32( -in32, S[ 2 ] );
  129. X = MULT16_32_Q15(QCONST16(0.15063f, 15), Y);
  130. out32_hp = ADD32( out32_hp, S[ 2 ] );
  131. out32_hp = ADD32( out32_hp, X );
  132. S[ 2 ] = ADD32( -in32, X );
  133. hp_ener += out32_hp*(opus_val64)out32_hp;
  134. /* Add, convert back to int16 and store to output */
  135. out[ k ] = HALF32(out32);
  136. }
  137. #ifdef FIXED_POINT
  138. /* len2 can be up to 480, so we shift by 8 more to make it fit. */
  139. hp_ener = hp_ener >> (2*SIG_SHIFT + 8);
  140. #endif
  141. return (opus_val32)hp_ener;
  142. }
  143. static opus_val32 downmix_and_resample(downmix_func downmix, const void *_x, opus_val32 *y, opus_val32 S[3], int subframe, int offset, int c1, int c2, int C, int Fs)
  144. {
  145. VARDECL(opus_val32, tmp);
  146. opus_val32 scale;
  147. int j;
  148. opus_val32 ret = 0;
  149. SAVE_STACK;
  150. if (subframe==0) return 0;
  151. if (Fs == 48000)
  152. {
  153. subframe *= 2;
  154. offset *= 2;
  155. } else if (Fs == 16000) {
  156. subframe = subframe*2/3;
  157. offset = offset*2/3;
  158. }
  159. ALLOC(tmp, subframe, opus_val32);
  160. downmix(_x, tmp, subframe, offset, c1, c2, C);
  161. #ifdef FIXED_POINT
  162. scale = (1<<SIG_SHIFT);
  163. #else
  164. scale = 1.f/32768;
  165. #endif
  166. if (c2==-2)
  167. scale /= C;
  168. else if (c2>-1)
  169. scale /= 2;
  170. for (j=0;j<subframe;j++)
  171. tmp[j] *= scale;
  172. if (Fs == 48000)
  173. {
  174. ret = silk_resampler_down2_hp(S, y, tmp, subframe);
  175. } else if (Fs == 24000) {
  176. OPUS_COPY(y, tmp, subframe);
  177. } else if (Fs == 16000) {
  178. VARDECL(opus_val32, tmp3x);
  179. ALLOC(tmp3x, 3*subframe, opus_val32);
  180. /* Don't do this at home! This resampler is horrible and it's only (barely)
  181. usable for the purpose of the analysis because we don't care about all
  182. the aliasing between 8 kHz and 12 kHz. */
  183. for (j=0;j<subframe;j++)
  184. {
  185. tmp3x[3*j] = tmp[j];
  186. tmp3x[3*j+1] = tmp[j];
  187. tmp3x[3*j+2] = tmp[j];
  188. }
  189. silk_resampler_down2_hp(S, y, tmp3x, 3*subframe);
  190. }
  191. RESTORE_STACK;
  192. return ret;
  193. }
  194. void tonality_analysis_init(TonalityAnalysisState *tonal, opus_int32 Fs)
  195. {
  196. /* Initialize reusable fields. */
  197. tonal->arch = opus_select_arch();
  198. tonal->Fs = Fs;
  199. /* Clear remaining fields. */
  200. tonality_analysis_reset(tonal);
  201. }
  202. void tonality_analysis_reset(TonalityAnalysisState *tonal)
  203. {
  204. /* Clear non-reusable fields. */
  205. char *start = (char*)&tonal->TONALITY_ANALYSIS_RESET_START;
  206. OPUS_CLEAR(start, sizeof(TonalityAnalysisState) - (start - (char*)tonal));
  207. }
  208. void tonality_get_info(TonalityAnalysisState *tonal, AnalysisInfo *info_out, int len)
  209. {
  210. int pos;
  211. int curr_lookahead;
  212. float tonality_max;
  213. float tonality_avg;
  214. int tonality_count;
  215. int i;
  216. int pos0;
  217. float prob_avg;
  218. float prob_count;
  219. float prob_min, prob_max;
  220. float vad_prob;
  221. int mpos, vpos;
  222. int bandwidth_span;
  223. pos = tonal->read_pos;
  224. curr_lookahead = tonal->write_pos-tonal->read_pos;
  225. if (curr_lookahead<0)
  226. curr_lookahead += DETECT_SIZE;
  227. tonal->read_subframe += len/(tonal->Fs/400);
  228. while (tonal->read_subframe>=8)
  229. {
  230. tonal->read_subframe -= 8;
  231. tonal->read_pos++;
  232. }
  233. if (tonal->read_pos>=DETECT_SIZE)
  234. tonal->read_pos-=DETECT_SIZE;
  235. /* On long frames, look at the second analysis window rather than the first. */
  236. if (len > tonal->Fs/50 && pos != tonal->write_pos)
  237. {
  238. pos++;
  239. if (pos==DETECT_SIZE)
  240. pos=0;
  241. }
  242. if (pos == tonal->write_pos)
  243. pos--;
  244. if (pos<0)
  245. pos = DETECT_SIZE-1;
  246. pos0 = pos;
  247. OPUS_COPY(info_out, &tonal->info[pos], 1);
  248. if (!info_out->valid)
  249. return;
  250. tonality_max = tonality_avg = info_out->tonality;
  251. tonality_count = 1;
  252. /* Look at the neighbouring frames and pick largest bandwidth found (to be safe). */
  253. bandwidth_span = 6;
  254. /* If possible, look ahead for a tone to compensate for the delay in the tone detector. */
  255. for (i=0;i<3;i++)
  256. {
  257. pos++;
  258. if (pos==DETECT_SIZE)
  259. pos = 0;
  260. if (pos == tonal->write_pos)
  261. break;
  262. tonality_max = MAX32(tonality_max, tonal->info[pos].tonality);
  263. tonality_avg += tonal->info[pos].tonality;
  264. tonality_count++;
  265. info_out->bandwidth = IMAX(info_out->bandwidth, tonal->info[pos].bandwidth);
  266. bandwidth_span--;
  267. }
  268. pos = pos0;
  269. /* Look back in time to see if any has a wider bandwidth than the current frame. */
  270. for (i=0;i<bandwidth_span;i++)
  271. {
  272. pos--;
  273. if (pos < 0)
  274. pos = DETECT_SIZE-1;
  275. if (pos == tonal->write_pos)
  276. break;
  277. info_out->bandwidth = IMAX(info_out->bandwidth, tonal->info[pos].bandwidth);
  278. }
  279. info_out->tonality = MAX32(tonality_avg/tonality_count, tonality_max-.2f);
  280. mpos = vpos = pos0;
  281. /* If we have enough look-ahead, compensate for the ~5-frame delay in the music prob and
  282. ~1 frame delay in the VAD prob. */
  283. if (curr_lookahead > 15)
  284. {
  285. mpos += 5;
  286. if (mpos>=DETECT_SIZE)
  287. mpos -= DETECT_SIZE;
  288. vpos += 1;
  289. if (vpos>=DETECT_SIZE)
  290. vpos -= DETECT_SIZE;
  291. }
  292. /* The following calculations attempt to minimize a "badness function"
  293. for the transition. When switching from speech to music, the badness
  294. of switching at frame k is
  295. b_k = S*v_k + \sum_{i=0}^{k-1} v_i*(p_i - T)
  296. where
  297. v_i is the activity probability (VAD) at frame i,
  298. p_i is the music probability at frame i
  299. T is the probability threshold for switching
  300. S is the penalty for switching during active audio rather than silence
  301. the current frame has index i=0
  302. Rather than apply badness to directly decide when to switch, what we compute
  303. instead is the threshold for which the optimal switching point is now. When
  304. considering whether to switch now (frame 0) or at frame k, we have:
  305. S*v_0 = S*v_k + \sum_{i=0}^{k-1} v_i*(p_i - T)
  306. which gives us:
  307. T = ( \sum_{i=0}^{k-1} v_i*p_i + S*(v_k-v_0) ) / ( \sum_{i=0}^{k-1} v_i )
  308. We take the min threshold across all positive values of k (up to the maximum
  309. amount of lookahead we have) to give us the threshold for which the current
  310. frame is the optimal switch point.
  311. The last step is that we need to consider whether we want to switch at all.
  312. For that we use the average of the music probability over the entire window.
  313. If the threshold is higher than that average we're not going to
  314. switch, so we compute a min with the average as well. The result of all these
  315. min operations is music_prob_min, which gives the threshold for switching to music
  316. if we're currently encoding for speech.
  317. We do the exact opposite to compute music_prob_max which is used for switching
  318. from music to speech.
  319. */
  320. prob_min = 1.f;
  321. prob_max = 0.f;
  322. vad_prob = tonal->info[vpos].activity_probability;
  323. prob_count = MAX16(.1f, vad_prob);
  324. prob_avg = MAX16(.1f, vad_prob)*tonal->info[mpos].music_prob;
  325. while (1)
  326. {
  327. float pos_vad;
  328. mpos++;
  329. if (mpos==DETECT_SIZE)
  330. mpos = 0;
  331. if (mpos == tonal->write_pos)
  332. break;
  333. vpos++;
  334. if (vpos==DETECT_SIZE)
  335. vpos = 0;
  336. if (vpos == tonal->write_pos)
  337. break;
  338. pos_vad = tonal->info[vpos].activity_probability;
  339. prob_min = MIN16((prob_avg - TRANSITION_PENALTY*(vad_prob - pos_vad))/prob_count, prob_min);
  340. prob_max = MAX16((prob_avg + TRANSITION_PENALTY*(vad_prob - pos_vad))/prob_count, prob_max);
  341. prob_count += MAX16(.1f, pos_vad);
  342. prob_avg += MAX16(.1f, pos_vad)*tonal->info[mpos].music_prob;
  343. }
  344. info_out->music_prob = prob_avg/prob_count;
  345. prob_min = MIN16(prob_avg/prob_count, prob_min);
  346. prob_max = MAX16(prob_avg/prob_count, prob_max);
  347. prob_min = MAX16(prob_min, 0.f);
  348. prob_max = MIN16(prob_max, 1.f);
  349. /* If we don't have enough look-ahead, do our best to make a decent decision. */
  350. if (curr_lookahead < 10)
  351. {
  352. float pmin, pmax;
  353. pmin = prob_min;
  354. pmax = prob_max;
  355. pos = pos0;
  356. /* Look for min/max in the past. */
  357. for (i=0;i<IMIN(tonal->count-1, 15);i++)
  358. {
  359. pos--;
  360. if (pos < 0)
  361. pos = DETECT_SIZE-1;
  362. pmin = MIN16(pmin, tonal->info[pos].music_prob);
  363. pmax = MAX16(pmax, tonal->info[pos].music_prob);
  364. }
  365. /* Bias against switching on active audio. */
  366. pmin = MAX16(0.f, pmin - .1f*vad_prob);
  367. pmax = MIN16(1.f, pmax + .1f*vad_prob);
  368. prob_min += (1.f-.1f*curr_lookahead)*(pmin - prob_min);
  369. prob_max += (1.f-.1f*curr_lookahead)*(pmax - prob_max);
  370. }
  371. info_out->music_prob_min = prob_min;
  372. info_out->music_prob_max = prob_max;
  373. /* printf("%f %f %f %f %f\n", prob_min, prob_max, prob_avg/prob_count, vad_prob, info_out->music_prob); */
  374. }
  375. static const float std_feature_bias[9] = {
  376. 5.684947f, 3.475288f, 1.770634f, 1.599784f, 3.773215f,
  377. 2.163313f, 1.260756f, 1.116868f, 1.918795f
  378. };
  379. #define LEAKAGE_OFFSET 2.5f
  380. #define LEAKAGE_SLOPE 2.f
  381. #ifdef FIXED_POINT
  382. /* For fixed-point, the input is +/-2^15 shifted up by SIG_SHIFT, so we need to
  383. compensate for that in the energy. */
  384. #define SCALE_COMPENS (1.f/((opus_int32)1<<(15+SIG_SHIFT)))
  385. #define SCALE_ENER(e) ((SCALE_COMPENS*SCALE_COMPENS)*(e))
  386. #else
  387. #define SCALE_ENER(e) (e)
  388. #endif
  389. #ifdef FIXED_POINT
  390. static int is_digital_silence32(const opus_val32* pcm, int frame_size, int channels, int lsb_depth)
  391. {
  392. int silence = 0;
  393. opus_val32 sample_max = 0;
  394. #ifdef MLP_TRAINING
  395. return 0;
  396. #endif
  397. sample_max = celt_maxabs32(pcm, frame_size*channels);
  398. silence = (sample_max == 0);
  399. (void)lsb_depth;
  400. return silence;
  401. }
  402. #else
  403. #define is_digital_silence32(pcm, frame_size, channels, lsb_depth) is_digital_silence(pcm, frame_size, channels, lsb_depth)
  404. #endif
  405. 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)
  406. {
  407. int i, b;
  408. const kiss_fft_state *kfft;
  409. VARDECL(kiss_fft_cpx, in);
  410. VARDECL(kiss_fft_cpx, out);
  411. int N = 480, N2=240;
  412. float * OPUS_RESTRICT A = tonal->angle;
  413. float * OPUS_RESTRICT dA = tonal->d_angle;
  414. float * OPUS_RESTRICT d2A = tonal->d2_angle;
  415. VARDECL(float, tonality);
  416. VARDECL(float, noisiness);
  417. float band_tonality[NB_TBANDS];
  418. float logE[NB_TBANDS];
  419. float BFCC[8];
  420. float features[25];
  421. float frame_tonality;
  422. float max_frame_tonality;
  423. /*float tw_sum=0;*/
  424. float frame_noisiness;
  425. const float pi4 = (float)(M_PI*M_PI*M_PI*M_PI);
  426. float slope=0;
  427. float frame_stationarity;
  428. float relativeE;
  429. float frame_probs[2];
  430. float alpha, alphaE, alphaE2;
  431. float frame_loudness;
  432. float bandwidth_mask;
  433. int is_masked[NB_TBANDS+1];
  434. int bandwidth=0;
  435. float maxE = 0;
  436. float noise_floor;
  437. int remaining;
  438. AnalysisInfo *info;
  439. float hp_ener;
  440. float tonality2[240];
  441. float midE[8];
  442. float spec_variability=0;
  443. float band_log2[NB_TBANDS+1];
  444. float leakage_from[NB_TBANDS+1];
  445. float leakage_to[NB_TBANDS+1];
  446. float layer_out[MAX_NEURONS];
  447. float below_max_pitch;
  448. float above_max_pitch;
  449. int is_silence;
  450. SAVE_STACK;
  451. if (!tonal->initialized)
  452. {
  453. tonal->mem_fill = 240;
  454. tonal->initialized = 1;
  455. }
  456. alpha = 1.f/IMIN(10, 1+tonal->count);
  457. alphaE = 1.f/IMIN(25, 1+tonal->count);
  458. /* Noise floor related decay for bandwidth detection: -2.2 dB/second */
  459. alphaE2 = 1.f/IMIN(100, 1+tonal->count);
  460. if (tonal->count <= 1) alphaE2 = 1;
  461. if (tonal->Fs == 48000)
  462. {
  463. /* len and offset are now at 24 kHz. */
  464. len/= 2;
  465. offset /= 2;
  466. } else if (tonal->Fs == 16000) {
  467. len = 3*len/2;
  468. offset = 3*offset/2;
  469. }
  470. kfft = celt_mode->mdct.kfft[0];
  471. tonal->hp_ener_accum += (float)downmix_and_resample(downmix, x,
  472. &tonal->inmem[tonal->mem_fill], tonal->downmix_state,
  473. IMIN(len, ANALYSIS_BUF_SIZE-tonal->mem_fill), offset, c1, c2, C, tonal->Fs);
  474. if (tonal->mem_fill+len < ANALYSIS_BUF_SIZE)
  475. {
  476. tonal->mem_fill += len;
  477. /* Don't have enough to update the analysis */
  478. RESTORE_STACK;
  479. return;
  480. }
  481. hp_ener = tonal->hp_ener_accum;
  482. info = &tonal->info[tonal->write_pos++];
  483. if (tonal->write_pos>=DETECT_SIZE)
  484. tonal->write_pos-=DETECT_SIZE;
  485. is_silence = is_digital_silence32(tonal->inmem, ANALYSIS_BUF_SIZE, 1, lsb_depth);
  486. ALLOC(in, 480, kiss_fft_cpx);
  487. ALLOC(out, 480, kiss_fft_cpx);
  488. ALLOC(tonality, 240, float);
  489. ALLOC(noisiness, 240, float);
  490. for (i=0;i<N2;i++)
  491. {
  492. float w = analysis_window[i];
  493. in[i].r = (kiss_fft_scalar)(w*tonal->inmem[i]);
  494. in[i].i = (kiss_fft_scalar)(w*tonal->inmem[N2+i]);
  495. in[N-i-1].r = (kiss_fft_scalar)(w*tonal->inmem[N-i-1]);
  496. in[N-i-1].i = (kiss_fft_scalar)(w*tonal->inmem[N+N2-i-1]);
  497. }
  498. OPUS_MOVE(tonal->inmem, tonal->inmem+ANALYSIS_BUF_SIZE-240, 240);
  499. remaining = len - (ANALYSIS_BUF_SIZE-tonal->mem_fill);
  500. tonal->hp_ener_accum = (float)downmix_and_resample(downmix, x,
  501. &tonal->inmem[240], tonal->downmix_state, remaining,
  502. offset+ANALYSIS_BUF_SIZE-tonal->mem_fill, c1, c2, C, tonal->Fs);
  503. tonal->mem_fill = 240 + remaining;
  504. if (is_silence)
  505. {
  506. /* On silence, copy the previous analysis. */
  507. int prev_pos = tonal->write_pos-2;
  508. if (prev_pos < 0)
  509. prev_pos += DETECT_SIZE;
  510. OPUS_COPY(info, &tonal->info[prev_pos], 1);
  511. RESTORE_STACK;
  512. return;
  513. }
  514. opus_fft(kfft, in, out, tonal->arch);
  515. #ifndef FIXED_POINT
  516. /* If there's any NaN on the input, the entire output will be NaN, so we only need to check one value. */
  517. if (celt_isnan(out[0].r))
  518. {
  519. info->valid = 0;
  520. RESTORE_STACK;
  521. return;
  522. }
  523. #endif
  524. for (i=1;i<N2;i++)
  525. {
  526. float X1r, X2r, X1i, X2i;
  527. float angle, d_angle, d2_angle;
  528. float angle2, d_angle2, d2_angle2;
  529. float mod1, mod2, avg_mod;
  530. X1r = (float)out[i].r+out[N-i].r;
  531. X1i = (float)out[i].i-out[N-i].i;
  532. X2r = (float)out[i].i+out[N-i].i;
  533. X2i = (float)out[N-i].r-out[i].r;
  534. angle = (float)(.5f/M_PI)*fast_atan2f(X1i, X1r);
  535. d_angle = angle - A[i];
  536. d2_angle = d_angle - dA[i];
  537. angle2 = (float)(.5f/M_PI)*fast_atan2f(X2i, X2r);
  538. d_angle2 = angle2 - angle;
  539. d2_angle2 = d_angle2 - d_angle;
  540. mod1 = d2_angle - (float)float2int(d2_angle);
  541. noisiness[i] = ABS16(mod1);
  542. mod1 *= mod1;
  543. mod1 *= mod1;
  544. mod2 = d2_angle2 - (float)float2int(d2_angle2);
  545. noisiness[i] += ABS16(mod2);
  546. mod2 *= mod2;
  547. mod2 *= mod2;
  548. avg_mod = .25f*(d2A[i]+mod1+2*mod2);
  549. /* This introduces an extra delay of 2 frames in the detection. */
  550. tonality[i] = 1.f/(1.f+40.f*16.f*pi4*avg_mod)-.015f;
  551. /* No delay on this detection, but it's less reliable. */
  552. tonality2[i] = 1.f/(1.f+40.f*16.f*pi4*mod2)-.015f;
  553. A[i] = angle2;
  554. dA[i] = d_angle2;
  555. d2A[i] = mod2;
  556. }
  557. for (i=2;i<N2-1;i++)
  558. {
  559. float tt = MIN32(tonality2[i], MAX32(tonality2[i-1], tonality2[i+1]));
  560. tonality[i] = .9f*MAX32(tonality[i], tt-.1f);
  561. }
  562. frame_tonality = 0;
  563. max_frame_tonality = 0;
  564. /*tw_sum = 0;*/
  565. info->activity = 0;
  566. frame_noisiness = 0;
  567. frame_stationarity = 0;
  568. if (!tonal->count)
  569. {
  570. for (b=0;b<NB_TBANDS;b++)
  571. {
  572. tonal->lowE[b] = 1e10;
  573. tonal->highE[b] = -1e10;
  574. }
  575. }
  576. relativeE = 0;
  577. frame_loudness = 0;
  578. /* The energy of the very first band is special because of DC. */
  579. {
  580. float E = 0;
  581. float X1r, X2r;
  582. X1r = 2*(float)out[0].r;
  583. X2r = 2*(float)out[0].i;
  584. E = X1r*X1r + X2r*X2r;
  585. for (i=1;i<4;i++)
  586. {
  587. float binE = out[i].r*(float)out[i].r + out[N-i].r*(float)out[N-i].r
  588. + out[i].i*(float)out[i].i + out[N-i].i*(float)out[N-i].i;
  589. E += binE;
  590. }
  591. E = SCALE_ENER(E);
  592. band_log2[0] = .5f*1.442695f*(float)log(E+1e-10f);
  593. }
  594. for (b=0;b<NB_TBANDS;b++)
  595. {
  596. float E=0, tE=0, nE=0;
  597. float L1, L2;
  598. float stationarity;
  599. for (i=tbands[b];i<tbands[b+1];i++)
  600. {
  601. float binE = out[i].r*(float)out[i].r + out[N-i].r*(float)out[N-i].r
  602. + out[i].i*(float)out[i].i + out[N-i].i*(float)out[N-i].i;
  603. binE = SCALE_ENER(binE);
  604. E += binE;
  605. tE += binE*MAX32(0, tonality[i]);
  606. nE += binE*2.f*(.5f-noisiness[i]);
  607. }
  608. #ifndef FIXED_POINT
  609. /* Check for extreme band energies that could cause NaNs later. */
  610. if (!(E<1e9f) || celt_isnan(E))
  611. {
  612. info->valid = 0;
  613. RESTORE_STACK;
  614. return;
  615. }
  616. #endif
  617. tonal->E[tonal->E_count][b] = E;
  618. frame_noisiness += nE/(1e-15f+E);
  619. frame_loudness += (float)sqrt(E+1e-10f);
  620. logE[b] = (float)log(E+1e-10f);
  621. band_log2[b+1] = .5f*1.442695f*(float)log(E+1e-10f);
  622. tonal->logE[tonal->E_count][b] = logE[b];
  623. if (tonal->count==0)
  624. tonal->highE[b] = tonal->lowE[b] = logE[b];
  625. if (tonal->highE[b] > tonal->lowE[b] + 7.5)
  626. {
  627. if (tonal->highE[b] - logE[b] > logE[b] - tonal->lowE[b])
  628. tonal->highE[b] -= .01f;
  629. else
  630. tonal->lowE[b] += .01f;
  631. }
  632. if (logE[b] > tonal->highE[b])
  633. {
  634. tonal->highE[b] = logE[b];
  635. tonal->lowE[b] = MAX32(tonal->highE[b]-15, tonal->lowE[b]);
  636. } else if (logE[b] < tonal->lowE[b])
  637. {
  638. tonal->lowE[b] = logE[b];
  639. tonal->highE[b] = MIN32(tonal->lowE[b]+15, tonal->highE[b]);
  640. }
  641. relativeE += (logE[b]-tonal->lowE[b])/(1e-5f + (tonal->highE[b]-tonal->lowE[b]));
  642. L1=L2=0;
  643. for (i=0;i<NB_FRAMES;i++)
  644. {
  645. L1 += (float)sqrt(tonal->E[i][b]);
  646. L2 += tonal->E[i][b];
  647. }
  648. stationarity = MIN16(0.99f,L1/(float)sqrt(1e-15+NB_FRAMES*L2));
  649. stationarity *= stationarity;
  650. stationarity *= stationarity;
  651. frame_stationarity += stationarity;
  652. /*band_tonality[b] = tE/(1e-15+E)*/;
  653. band_tonality[b] = MAX16(tE/(1e-15f+E), stationarity*tonal->prev_band_tonality[b]);
  654. #if 0
  655. if (b>=NB_TONAL_SKIP_BANDS)
  656. {
  657. frame_tonality += tweight[b]*band_tonality[b];
  658. tw_sum += tweight[b];
  659. }
  660. #else
  661. frame_tonality += band_tonality[b];
  662. if (b>=NB_TBANDS-NB_TONAL_SKIP_BANDS)
  663. frame_tonality -= band_tonality[b-NB_TBANDS+NB_TONAL_SKIP_BANDS];
  664. #endif
  665. max_frame_tonality = MAX16(max_frame_tonality, (1.f+.03f*(b-NB_TBANDS))*frame_tonality);
  666. slope += band_tonality[b]*(b-8);
  667. /*printf("%f %f ", band_tonality[b], stationarity);*/
  668. tonal->prev_band_tonality[b] = band_tonality[b];
  669. }
  670. leakage_from[0] = band_log2[0];
  671. leakage_to[0] = band_log2[0] - LEAKAGE_OFFSET;
  672. for (b=1;b<NB_TBANDS+1;b++)
  673. {
  674. float leak_slope = LEAKAGE_SLOPE*(tbands[b]-tbands[b-1])/4;
  675. leakage_from[b] = MIN16(leakage_from[b-1]+leak_slope, band_log2[b]);
  676. leakage_to[b] = MAX16(leakage_to[b-1]-leak_slope, band_log2[b]-LEAKAGE_OFFSET);
  677. }
  678. for (b=NB_TBANDS-2;b>=0;b--)
  679. {
  680. float leak_slope = LEAKAGE_SLOPE*(tbands[b+1]-tbands[b])/4;
  681. leakage_from[b] = MIN16(leakage_from[b+1]+leak_slope, leakage_from[b]);
  682. leakage_to[b] = MAX16(leakage_to[b+1]-leak_slope, leakage_to[b]);
  683. }
  684. celt_assert(NB_TBANDS+1 <= LEAK_BANDS);
  685. for (b=0;b<NB_TBANDS+1;b++)
  686. {
  687. /* leak_boost[] is made up of two terms. The first, based on leakage_to[],
  688. represents the boost needed to overcome the amount of analysis leakage
  689. cause in a weaker band b by louder neighbouring bands.
  690. The second, based on leakage_from[], applies to a loud band b for
  691. which the quantization noise causes synthesis leakage to the weaker
  692. neighbouring bands. */
  693. float boost = MAX16(0, leakage_to[b] - band_log2[b]) +
  694. MAX16(0, band_log2[b] - (leakage_from[b]+LEAKAGE_OFFSET));
  695. info->leak_boost[b] = IMIN(255, (int)floor(.5 + 64.f*boost));
  696. }
  697. for (;b<LEAK_BANDS;b++) info->leak_boost[b] = 0;
  698. for (i=0;i<NB_FRAMES;i++)
  699. {
  700. int j;
  701. float mindist = 1e15f;
  702. for (j=0;j<NB_FRAMES;j++)
  703. {
  704. int k;
  705. float dist=0;
  706. for (k=0;k<NB_TBANDS;k++)
  707. {
  708. float tmp;
  709. tmp = tonal->logE[i][k] - tonal->logE[j][k];
  710. dist += tmp*tmp;
  711. }
  712. if (j!=i)
  713. mindist = MIN32(mindist, dist);
  714. }
  715. spec_variability += mindist;
  716. }
  717. spec_variability = (float)sqrt(spec_variability/NB_FRAMES/NB_TBANDS);
  718. bandwidth_mask = 0;
  719. bandwidth = 0;
  720. maxE = 0;
  721. noise_floor = 5.7e-4f/(1<<(IMAX(0,lsb_depth-8)));
  722. noise_floor *= noise_floor;
  723. below_max_pitch=0;
  724. above_max_pitch=0;
  725. for (b=0;b<NB_TBANDS;b++)
  726. {
  727. float E=0;
  728. float Em;
  729. int band_start, band_end;
  730. /* Keep a margin of 300 Hz for aliasing */
  731. band_start = tbands[b];
  732. band_end = tbands[b+1];
  733. for (i=band_start;i<band_end;i++)
  734. {
  735. float binE = out[i].r*(float)out[i].r + out[N-i].r*(float)out[N-i].r
  736. + out[i].i*(float)out[i].i + out[N-i].i*(float)out[N-i].i;
  737. E += binE;
  738. }
  739. E = SCALE_ENER(E);
  740. maxE = MAX32(maxE, E);
  741. if (band_start < 64)
  742. {
  743. below_max_pitch += E;
  744. } else {
  745. above_max_pitch += E;
  746. }
  747. tonal->meanE[b] = MAX32((1-alphaE2)*tonal->meanE[b], E);
  748. Em = MAX32(E, tonal->meanE[b]);
  749. /* Consider the band "active" only if all these conditions are met:
  750. 1) less than 90 dB below the peak band (maximal masking possible considering
  751. both the ATH and the loudness-dependent slope of the spreading function)
  752. 2) above the PCM quantization noise floor
  753. We use b+1 because the first CELT band isn't included in tbands[]
  754. */
  755. if (E*1e9f > maxE && (Em > 3*noise_floor*(band_end-band_start) || E > noise_floor*(band_end-band_start)))
  756. bandwidth = b+1;
  757. /* Check if the band is masked (see below). */
  758. is_masked[b] = E < (tonal->prev_bandwidth >= b+1 ? .01f : .05f)*bandwidth_mask;
  759. /* Use a simple follower with 13 dB/Bark slope for spreading function. */
  760. bandwidth_mask = MAX32(.05f*bandwidth_mask, E);
  761. }
  762. /* Special case for the last two bands, for which we don't have spectrum but only
  763. the energy above 12 kHz. The difficulty here is that the high-pass we use
  764. leaks some LF energy, so we need to increase the threshold without accidentally cutting
  765. off the band. */
  766. if (tonal->Fs == 48000) {
  767. float noise_ratio;
  768. float Em;
  769. float E = hp_ener*(1.f/(60*60));
  770. noise_ratio = tonal->prev_bandwidth==20 ? 10.f : 30.f;
  771. #ifdef FIXED_POINT
  772. /* silk_resampler_down2_hp() shifted right by an extra 8 bits. */
  773. E *= 256.f*(1.f/Q15ONE)*(1.f/Q15ONE);
  774. #endif
  775. above_max_pitch += E;
  776. tonal->meanE[b] = MAX32((1-alphaE2)*tonal->meanE[b], E);
  777. Em = MAX32(E, tonal->meanE[b]);
  778. if (Em > 3*noise_ratio*noise_floor*160 || E > noise_ratio*noise_floor*160)
  779. bandwidth = 20;
  780. /* Check if the band is masked (see below). */
  781. is_masked[b] = E < (tonal->prev_bandwidth == 20 ? .01f : .05f)*bandwidth_mask;
  782. }
  783. if (above_max_pitch > below_max_pitch)
  784. info->max_pitch_ratio = below_max_pitch/above_max_pitch;
  785. else
  786. info->max_pitch_ratio = 1;
  787. /* In some cases, resampling aliasing can create a small amount of energy in the first band
  788. being cut. So if the last band is masked, we don't include it. */
  789. if (bandwidth == 20 && is_masked[NB_TBANDS])
  790. bandwidth-=2;
  791. else if (bandwidth > 0 && bandwidth <= NB_TBANDS && is_masked[bandwidth-1])
  792. bandwidth--;
  793. if (tonal->count<=2)
  794. bandwidth = 20;
  795. frame_loudness = 20*(float)log10(frame_loudness);
  796. tonal->Etracker = MAX32(tonal->Etracker-.003f, frame_loudness);
  797. tonal->lowECount *= (1-alphaE);
  798. if (frame_loudness < tonal->Etracker-30)
  799. tonal->lowECount += alphaE;
  800. for (i=0;i<8;i++)
  801. {
  802. float sum=0;
  803. for (b=0;b<16;b++)
  804. sum += dct_table[i*16+b]*logE[b];
  805. BFCC[i] = sum;
  806. }
  807. for (i=0;i<8;i++)
  808. {
  809. float sum=0;
  810. for (b=0;b<16;b++)
  811. sum += dct_table[i*16+b]*.5f*(tonal->highE[b]+tonal->lowE[b]);
  812. midE[i] = sum;
  813. }
  814. frame_stationarity /= NB_TBANDS;
  815. relativeE /= NB_TBANDS;
  816. if (tonal->count<10)
  817. relativeE = .5f;
  818. frame_noisiness /= NB_TBANDS;
  819. #if 1
  820. info->activity = frame_noisiness + (1-frame_noisiness)*relativeE;
  821. #else
  822. info->activity = .5*(1+frame_noisiness-frame_stationarity);
  823. #endif
  824. frame_tonality = (max_frame_tonality/(NB_TBANDS-NB_TONAL_SKIP_BANDS));
  825. frame_tonality = MAX16(frame_tonality, tonal->prev_tonality*.8f);
  826. tonal->prev_tonality = frame_tonality;
  827. slope /= 8*8;
  828. info->tonality_slope = slope;
  829. tonal->E_count = (tonal->E_count+1)%NB_FRAMES;
  830. tonal->count = IMIN(tonal->count+1, ANALYSIS_COUNT_MAX);
  831. info->tonality = frame_tonality;
  832. for (i=0;i<4;i++)
  833. 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];
  834. for (i=0;i<4;i++)
  835. tonal->cmean[i] = (1-alpha)*tonal->cmean[i] + alpha*BFCC[i];
  836. for (i=0;i<4;i++)
  837. features[4+i] = 0.63246f*(BFCC[i]-tonal->mem[i+24]) + 0.31623f*(tonal->mem[i]-tonal->mem[i+16]);
  838. for (i=0;i<3;i++)
  839. 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];
  840. if (tonal->count > 5)
  841. {
  842. for (i=0;i<9;i++)
  843. tonal->std[i] = (1-alpha)*tonal->std[i] + alpha*features[i]*features[i];
  844. }
  845. for (i=0;i<4;i++)
  846. features[i] = BFCC[i]-midE[i];
  847. for (i=0;i<8;i++)
  848. {
  849. tonal->mem[i+24] = tonal->mem[i+16];
  850. tonal->mem[i+16] = tonal->mem[i+8];
  851. tonal->mem[i+8] = tonal->mem[i];
  852. tonal->mem[i] = BFCC[i];
  853. }
  854. for (i=0;i<9;i++)
  855. features[11+i] = (float)sqrt(tonal->std[i]) - std_feature_bias[i];
  856. features[18] = spec_variability - 0.78f;
  857. features[20] = info->tonality - 0.154723f;
  858. features[21] = info->activity - 0.724643f;
  859. features[22] = frame_stationarity - 0.743717f;
  860. features[23] = info->tonality_slope + 0.069216f;
  861. features[24] = tonal->lowECount - 0.067930f;
  862. compute_dense(&layer0, layer_out, features);
  863. compute_gru(&layer1, tonal->rnn_state, layer_out);
  864. compute_dense(&layer2, frame_probs, tonal->rnn_state);
  865. /* Probability of speech or music vs noise */
  866. info->activity_probability = frame_probs[1];
  867. info->music_prob = frame_probs[0];
  868. /*printf("%f %f %f\n", frame_probs[0], frame_probs[1], info->music_prob);*/
  869. #ifdef MLP_TRAINING
  870. for (i=0;i<25;i++)
  871. printf("%f ", features[i]);
  872. printf("\n");
  873. #endif
  874. info->bandwidth = bandwidth;
  875. tonal->prev_bandwidth = bandwidth;
  876. /*printf("%d %d\n", info->bandwidth, info->opus_bandwidth);*/
  877. info->noisiness = frame_noisiness;
  878. info->valid = 1;
  879. RESTORE_STACK;
  880. }
  881. void run_analysis(TonalityAnalysisState *analysis, const CELTMode *celt_mode, const void *analysis_pcm,
  882. int analysis_frame_size, int frame_size, int c1, int c2, int C, opus_int32 Fs,
  883. int lsb_depth, downmix_func downmix, AnalysisInfo *analysis_info)
  884. {
  885. int offset;
  886. int pcm_len;
  887. analysis_frame_size -= analysis_frame_size&1;
  888. if (analysis_pcm != NULL)
  889. {
  890. /* Avoid overflow/wrap-around of the analysis buffer */
  891. analysis_frame_size = IMIN((DETECT_SIZE-5)*Fs/50, analysis_frame_size);
  892. pcm_len = analysis_frame_size - analysis->analysis_offset;
  893. offset = analysis->analysis_offset;
  894. while (pcm_len>0) {
  895. tonality_analysis(analysis, celt_mode, analysis_pcm, IMIN(Fs/50, pcm_len), offset, c1, c2, C, lsb_depth, downmix);
  896. offset += Fs/50;
  897. pcm_len -= Fs/50;
  898. }
  899. analysis->analysis_offset = analysis_frame_size;
  900. analysis->analysis_offset -= frame_size;
  901. }
  902. tonality_get_info(analysis, analysis_info, frame_size);
  903. }
  904. #endif /* DISABLE_FLOAT_API */