1
0

gain_analysis.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /*
  2. * ReplayGainAnalysis - analyzes input samples and give the recommended dB change
  3. * Copyright (C) 2001 David Robinson and Glen Sawyer
  4. * Improvements and optimizations added by Frank Klemm, and by Marcel Müller
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * concept and filter values by David Robinson ([email protected])
  21. * -- blame him if you think the idea is flawed
  22. * original coding by Glen Sawyer ([email protected])
  23. * -- blame him if you think this runs too slowly, or the coding is otherwise flawed
  24. *
  25. * lots of code improvements by Frank Klemm ( http://www.uni-jena.de/~pfk/mpp/ )
  26. * -- credit him for all the _good_ programming ;)
  27. *
  28. *
  29. * For an explanation of the concepts and the basic algorithms involved, go to:
  30. * http://www.replaygain.org/
  31. */
  32. /*
  33. * Here's the deal. Call
  34. *
  35. * InitGainAnalysis ( long samplefreq );
  36. *
  37. * to initialize everything. Call
  38. *
  39. * AnalyzeSamples ( const Float_t* left_samples,
  40. * const Float_t* right_samples,
  41. * size_t num_samples,
  42. * int num_channels );
  43. *
  44. * as many times as you want, with as many or as few samples as you want.
  45. * If mono, pass the sample buffer in through left_samples, leave
  46. * right_samples NULL, and make sure num_channels = 1.
  47. *
  48. * GetTitleGain()
  49. *
  50. * will return the recommended dB level change for all samples analyzed
  51. * SINCE THE LAST TIME you called GetTitleGain() OR InitGainAnalysis().
  52. *
  53. * GetAlbumGain()
  54. *
  55. * will return the recommended dB level change for all samples analyzed
  56. * since InitGainAnalysis() was called and finalized with GetTitleGain().
  57. *
  58. * Pseudo-code to process an album:
  59. *
  60. * Float_t l_samples [4096];
  61. * Float_t r_samples [4096];
  62. * size_t num_samples;
  63. * unsigned int num_songs;
  64. * unsigned int i;
  65. *
  66. * InitGainAnalysis ( 44100 );
  67. * for ( i = 1; i <= num_songs; i++ ) {
  68. * while ( ( num_samples = getSongSamples ( song[i], left_samples, right_samples ) ) > 0 )
  69. * AnalyzeSamples ( left_samples, right_samples, num_samples, 2 );
  70. * fprintf ("Recommended dB change for song %2d: %+6.2f dB\n", i, GetTitleGain() );
  71. * }
  72. * fprintf ("Recommended dB change for whole album: %+6.2f dB\n", GetAlbumGain() );
  73. */
  74. /*
  75. * So here's the main source of potential code confusion:
  76. *
  77. * The filters applied to the incoming samples are IIR filters,
  78. * meaning they rely on up to <filter order> number of previous samples
  79. * AND up to <filter order> number of previous filtered samples.
  80. *
  81. * I set up the AnalyzeSamples routine to minimize memory usage and interface
  82. * complexity. The speed isn't compromised too much (I don't think), but the
  83. * internal complexity is higher than it should be for such a relatively
  84. * simple routine.
  85. *
  86. * Optimization/clarity suggestions are welcome.
  87. */
  88. /*
  89. * 30 Aug 2006 - Ben Allison (benski[]nullsoft.com)
  90. * Modification to allow for multiple instances to be run simtulaneously (via context pointer)
  91. * 03 July 2007 - Marc Lerch (marc.lerch[]gmail.com) and Ben Allison (benski[]nullsoft.com)
  92. * Coefficients for 64000, 88200 and 96000 sampling rates
  93. */
  94. #include <stdio.h>
  95. #include <stdlib.h>
  96. #include <string.h>
  97. #include <math.h>
  98. #include "gain_analysis.h"
  99. #include "../nsutil/iir.h"
  100. #include "../nsutil/stats.h"
  101. typedef unsigned short Uint16_t;
  102. typedef signed short Int16_t;
  103. typedef unsigned int Uint32_t;
  104. typedef signed int Int32_t;
  105. #define YULE_ORDER 10
  106. #define BUTTER_ORDER 2
  107. #define RMS_PERCENTILE 0.95 // percentile which is louder than the proposed level
  108. #define MAX_SAMP_FREQ 192000. // maximum allowed sample frequency [Hz]
  109. #define RMS_WINDOW_TIME 0.050 // Time slice size [s]
  110. #define STEPS_per_dB 100. // Table entries per dB
  111. #define MAX_dB 120. // Table entries for 0...MAX_dB (normal max. values are 70...80 dB)
  112. #define MAX_ORDER (BUTTER_ORDER > YULE_ORDER ? BUTTER_ORDER : YULE_ORDER)
  113. #define MAX_SAMPLES_PER_WINDOW (size_t) (MAX_SAMP_FREQ * RMS_WINDOW_TIME) // max. Samples per Time slice
  114. #define PINK_REF 64.82 //298640883795 // calibration value
  115. typedef struct
  116. {
  117. Float_t linprebuf [MAX_ORDER * 2];
  118. Float_t* linpre; // left input samples, with pre-buffer
  119. Float_t loutbuf [MAX_SAMPLES_PER_WINDOW + MAX_ORDER];
  120. Float_t* lout; // left "out" (i.e. post second filter) samples
  121. Float_t rinprebuf [MAX_ORDER * 2];
  122. Float_t* rinpre; // right input samples ...
  123. Float_t routbuf [MAX_SAMPLES_PER_WINDOW + MAX_ORDER];
  124. Float_t* rout;
  125. size_t sampleWindow; // number of samples required to reach number of milliseconds required for RMS window
  126. size_t totsamp;
  127. double lsum;
  128. double rsum;
  129. int freqindex;
  130. int first;
  131. Uint32_t A [(size_t)(STEPS_per_dB * MAX_dB)];
  132. Uint32_t B [(size_t)(STEPS_per_dB * MAX_dB)];
  133. nsutil_iir_t butter_iir;
  134. nsutil_iir_t yule_iir;
  135. } ReplayGainContext;
  136. #ifdef WIN32
  137. #ifndef __GNUC__
  138. #pragma warning ( disable : 4305 )
  139. #endif
  140. #endif
  141. typedef const float yule_coefficients[2*(YULE_ORDER + 1)];
  142. static yule_coefficients ABYule[] =
  143. {
  144. { 0.038575994352, -0.02160367184185, -0.00123395316851, -9.291677959e-005, -0.01655260341619, 0.02161526843274, -0.02074045215285, 0.00594298065125, 0.00306428023191, 0.00012025322027, 0.00288463683916,
  145. 1, -3.8466461711807, 7.8150165300554, -11.34170355132, 13.055042193275, -12.287598951453, 9.4829380631979, -5.87257861776, 2.7546586187461, -0.86984376593551, 0.13919314567432 },
  146. { 0.0541865640643, -0.02911007808948, -0.00848709379851, -0.00851165645469, -0.00834990904936, 0.02245293253339, -0.02596338512915, 0.01624864962975, -0.00240879051584, 0.00674613682247, -0.00187763777362,
  147. 1, -3.4784594855007, 6.3631777756615, -8.5475152747187, 9.4769360780128, -8.8149868137015, 6.85401540937, -4.3947099607956, 2.1961168489077, -0.75104302451432, 0.13149317958808 },
  148. { 0.15457299681924, -0.09331049056315, -0.06247880153653, 0.02163541888798, -0.05588393329856, 0.04781476674921, 0.00222312597743, 0.03174092540049, -0.01390589421898, 0.00651420667831, -0.00881362733839,
  149. 1, -2.3789883497308, 2.8486815115633, -2.6457717022983, 2.2369765745171, -1.671481533676, 1.0059595480855, -0.45953458054983, 0.16378164858596, -0.05032077717131, 0.0234789740702 },
  150. { 0.30296907319327, -0.22613988682123, -0.08587323730772, 0.03282930172664, -0.00915702933434, -0.02364141202522, -0.00584456039913, 0.06276101321749, -8.28086748e-006, 0.00205861885564, -0.02950134983287,
  151. 1, -1.6127316513725, 1.0797749225997, -0.2565625775407, -0.1627671912044, -0.22638893773906, 0.39120800788284, -0.22138138954925, 0.04500235387352, 0.02005851806501, 0.00302439095741 },
  152. { 0.33642304856132, -0.2557224142557, -0.11828570177555, 0.11921148675203, -0.07834489609479, -0.0046997791438, -0.0058950022444, 0.05724228140351, 0.00832043980773, -0.0163538138454, -0.0176017656815,
  153. 1, -1.498589793678, 0.87350271418188, 0.12205022308084, -0.80774944671438, 0.47854794562326, -0.12453458140019, -0.04067510197014, 0.08333755284107, -0.04237348025746, 0.02977207319925 },
  154. { 0.4491525660845, -0.14351757464547, -0.22784394429749, -0.01419140100551, 0.04078262797139, -0.12398163381748, 0.04097565135648, 0.10478503600251, -0.01863887810927, -0.03193428438915, 0.00541907748707,
  155. 1, -0.62820619233671, 0.29661783706366, -0.372563729424, 0.00213767857124, -0.42029820170918, 0.22199650564824, 0.00613424350682, 0.06747620744683, 0.05784820375801, 0.03222754072173 },
  156. { 0.56619470757641, -0.75464456939302, 0.1624213774223, 0.16744243493672, -0.18901604199609, 0.3093178284183, -0.27562961986224, 0.00647310677246, 0.08647503780351, -0.0378898455484, -0.00588215443421,
  157. 1, -1.0480033512635, 0.29156311971249, -0.26806001042947, 0.00819999645858, 0.45054734505008, -0.33032403314006, 0.0673936833311, -0.04784254229033, 0.01639907836189, 0.01807364323573 },
  158. { 0.58100494960553, -0.53174909058578, -0.14289799034253, 0.17520704835522, 0.02377945217615, 0.15558449135573, -0.25344790059353, 0.01628462406333, 0.06920467763959, -0.03721611395801, -0.00749618797172,
  159. 1, -0.51035327095184, -0.31863563325245, -0.20256413484477, 0.1472815413433, 0.38952639978999, -0.23313271880868, -0.05246019024463, -0.02505961724053, 0.02442357316099, 0.01818801111503 },
  160. { 0.53648789255105, -0.42163034350696, -0.00275953611929, 0.04267842219415, -0.10214864179676, 0.14590772289388, -0.02459864859345, -0.11202315195388, -0.04060034127, 0.0478866554818, -0.02217936801134,
  161. 1, -0.2504987195602, -0.43193942311114, -0.03424681017675, -0.04678328784242, 0.26408300200955, 0.15113130533216, -0.17556493366449, -0.18823009262115, 0.05477720428674, 0.0470440968812 },
  162. { 0.38524531015142, -0.27682212062067, -0.09980181488805, 0.09951486755646, -0.08934020156622, -0.00322369330199, -0.00110329090689, 0.03784509844682, 0.01683906213303, -0.01147039862572, -0.01941767987192,
  163. 1, -1.2970891840453, 0.90399339674203, -0.29613799017877, -0.42326645916207, 0.379348874022, -0.37919795944938, 0.23410283284785, -0.03892971758879, 0.00403009552351, 0.03640166626278 },
  164. { 0.08717879977844, -0.01000374016172, -0.06265852122368, -0.0111932880095, -0.0011427937296, 0.02081333954769, -0.01603261863207, 0.01936763028546, 0.00760044736442, -0.00303979112271, -0.00075088605788,
  165. 1, -2.6281631147215, 3.5373453581799, -3.8100344867892, 3.9129163673013, -3.5351860589629, 2.7135686615787, -1.8672331184659, 1.1207538236766, -0.4857408688689, 0.11330544663849 },
  166. { 0.03144914734085, -0.06151729206963, 0.08066788708145, -0.09737939921516, 0.08943210803999, -0.0698998467201, 0.04926972841044, -0.03161257848451, 0.01456837493506, -0.00316015108496, 0.00132807215875,
  167. 1, -4.8737731309003, 12.039221601402, -20.101511183814, 25.103885344152, -24.290655608159, 18.271584690907, -10.452495525606, 4.30319491872, -1.1371699207019, 0.14510733527035 },
  168. { 0.02613056568174, -0.08128786488109, 0.14937282347325, -0.21695711675126, 0.25010286673402, -0.23162283619278, 0.17424041833052, -0.1029959921668, 0.04258696481981, -0.00977952936493, 0.00105325558889,
  169. 1, -5.7362547709212, 16.15249794355, -29.686549124645, 39.557061556741, -39.825245562463, 30.50605345013, -17.430517728212, 7.0515457390802, -1.8078383972051, 0.22127840210813 },
  170. { 0.02667482047416, -0.11377479336097, 0.23063167910965, -0.30726477945593, 0.33188520686529, -0.33862680249063, 0.3180716153134, -0.2373079692988, 0.12273894790371, -0.03840017967282, 0.00549673387936,
  171. 1, -6.318364516573, 18.313513108018, -31.882100148159, 36.537921469767, -28.233930364676, 14.247252582272, -4.0467098001285, 0.18865757280515, 0.25420333563908, -0.06012333531065 },
  172. { 0.00588138296683, -0.01613559730421, 0.02184798954216, -0.01742490405317, 0.0046463564378, 0.01117772513205, -0.02123865824368, 0.0195935441335, -0.01079720643523, 0.00352183686289, -0.00063124341421,
  173. 1, -5.9780882364201, 16.213625079641, -25.729237306526, 25.404706631395, -14.661662877711, 2.8159748435975, 2.5144712596973, -2.2357530698529, 0.75788151036791, -0.10078025199029 },
  174. { 0.00528778718259, -0.01893240907245, 0.03185982561867, -0.02926260297838, 0.00715743034072, 0.01985743355827, -0.03222614850941, 0.02565681978192, -0.01210662313473, 0.00325436284541, -0.00044173593001,
  175. 1, -6.2493210845629, 17.423443205385, -27.868197090549, 26.790873446813, -13.437110814851, -0.66023612948173, 6.0365809181494, -4.2492657703031, 1.4082926870919, -0.19480852628112 },
  176. { 0.00553120584305, -0.02112620545016, 0.03549076243117, -0.03362498312306, 0.01425867248183, 0.01344686928787, -0.03392770787836, 0.0346413645953, -0.02039116051549, 0.00667420794705, -0.00093763762995,
  177. 1, -6.1458171083992, 16.047859036758, -22.190891314077, 15.247564715803, -0.52001440400238, -8.0048864169994, 6.6091609476886, -2.3785602281092, 0.33106947986101, 0.00459820832036 },
  178. { 0.0063968235945, -0.02556437970955, 0.04230854400938, -0.03722462201267, 0.01718514827295, 0.00610592243009, -0.03065965747365, 0.04345745003539, -0.03298592681309, 0.01320937236809, -0.00220304127757,
  179. 1, -6.1481462352343, 15.800024571416, -20.784875876869, 11.988485523103, 3.3646201506261, -10.224198683595, 6.6559970214647, -1.6714186111048, -0.05417956536718, 0.07374767867406 },
  180. { 0.00268568524529, -0.0085237942608, 0.00852704191347, 0.00146116310295, -0.00950855828762, 0.00625449515499, 0.00116183868722, -0.00362461417136, 0.00203961000134, -0.00050664587933, 4.327455427e-005,
  181. 1, -5.5751278276304, 12.442910560658, -12.874627996812, 3.0855484696158, 6.6249345988069, -7.0766276631325, 2.5117554273644, 0.06731510802735, -0.24567753819213, 0.03961404162376 },
  182. { 0.01184742123123, -0.04631092400086, 0.06584226961238, -0.02165588522478, -0.05656260778952, 0.0860749359276, -0.03375544339786, -0.04216579932754, 0.06416711490648, -0.03444708260844, 0.00697275872241,
  183. 1, -5.2472731834817, 10.608215851922, -8.7412766581041, -1.3390607137168, 8.0797288209661, -5.4617991895085, 0.54318070652536, 0.8745096922428, -0.34656083539754, 0.03034796843589 }
  184. };
  185. typedef __declspec(align(32)) const float butter_coefficients[2*(BUTTER_ORDER + 1)];
  186. static butter_coefficients ABButter[] =
  187. {
  188. { 0.98621192462708, -1.9724238492542, 0.98621192462708, 1, -1.9722337291953, 0.97261396931306 },
  189. { 0.98500175787242, -1.9700035157448, 0.98500175787242, 1, -1.9697785558262, 0.9702284756635 },
  190. { 0.97938932735214, -1.9587786547043, 0.97938932735214, 1, -1.958353809754, 0.95920349965459 },
  191. { 0.97531843204928, -1.9506368640986, 0.97531843204928, 1, -1.9500275914988, 0.95124613669835 },
  192. { 0.97316523498161, -1.9463304699632, 0.97316523498161, 1, -1.9456102356653, 0.94705070426118 },
  193. { 0.96454515552826, -1.9290903110565, 0.96454515552826, 1, -1.9278328697704, 0.93034775234268 },
  194. { 0.96009142950541, -1.9201828590108, 0.96009142950541, 1, -1.9185895303378, 0.92177618768381 },
  195. { 0.95856916599601, -1.917138331992, 0.95856916599601, 1, -1.9154210807478, 0.91885558323625 },
  196. { 0.94597685600279, -1.8919537120056, 0.94597685600279, 1, -1.8890330793945, 0.89487434461664 },
  197. { 0.96535326815829, -1.9307065363166, 0.96535326815829, 1, -1.9295057798352, 0.93190729279793 },
  198. { 0.98252400815195, -1.9650480163039, 0.98252400815195, 1, -1.9647425826904, 0.9653534499174 },
  199. { 0.98816995007392, -1.9763399001478, 0.98816995007392, 1, -1.9761999451697, 0.97647985512594 },
  200. { 0.98964101933472, -1.9792820386694, 0.98964101933472, 1, -1.9791747273101, 0.9793893500288 },
  201. { 0.99247255046129, -1.9849451009226, 0.99247255046129, 1, -1.9848884376234, 0.98500176422183 },
  202. { 0.99308203517541, -1.9861640703508, 0.99308203517541, 1, -1.9861162115409, 0.98621192916075 },
  203. { 0.99406737810867, -1.9881347562173, 0.99406737810867, 1, -1.9880995599051, 0.98816995252954 },
  204. { 0.99480702681278, -1.9896140536256, 0.99480702681278, 1, -1.9895870864732, 0.9896410207779 },
  205. { 0.99538268958706, -1.9907653791741, 0.99538268958706, 1, -1.9907440595051, 0.99078669884321 },
  206. { 0.99622916581118, -1.9924583316224, 0.99622916581118, 1, -1.9924441123813, 0.99247255086339 },
  207. { 0.99653501465135, -1.9930700293027, 0.99653501465135, 1, -1.9930580231432, 0.99308203546221 },
  208. };
  209. #ifdef WIN32
  210. #ifndef __GNUC__
  211. #pragma warning ( default : 4305 )
  212. #endif
  213. #endif
  214. // When calling these filter procedures, make sure that ip[-order] and op[-order] point to real data!
  215. // If your compiler complains that "'operation on 'output' may be undefined", you can
  216. // either ignore the warnings or uncomment the three "y" lines (and comment out the indicated line)
  217. static void YULE_FILTER(nsutil_iir_t *iir, const Float_t* input, Float_t* output, size_t nSamples)
  218. {
  219. nsutil_iir_Filter_F32(iir, input, output, (int)nSamples);
  220. }
  221. static void BUTTER_FILTER(nsutil_iir_t *iir, Float_t* samples, size_t nSamples)
  222. {
  223. nsutil_iir_Filter_F32_IP(iir, samples, (int)nSamples);
  224. }
  225. // returns a INIT_GAIN_ANALYSIS_OK if successful, INIT_GAIN_ANALYSIS_ERROR if not
  226. DLLEXPORT int WAResetSampleFrequency(void *context, long samplefreq )
  227. {
  228. int i;
  229. ReplayGainContext *rg=context;
  230. // zero out initial values
  231. for ( i = 0; i < MAX_ORDER; i++ )
  232. rg->linprebuf[i] = rg->loutbuf[i] = rg->rinprebuf[i] = rg->routbuf[i] = 0.;
  233. switch ( (int)(samplefreq) )
  234. {
  235. case 48000: rg->freqindex = 0; break;
  236. case 44100: rg->freqindex = 1; break;
  237. case 32000: rg->freqindex = 2; break;
  238. case 24000: rg->freqindex = 3; break;
  239. case 22050: rg->freqindex = 4; break;
  240. case 16000: rg->freqindex = 5; break;
  241. case 12000: rg->freqindex = 6; break;
  242. case 11025: rg->freqindex = 7; break;
  243. case 8000: rg->freqindex = 8; break;
  244. case 18900: rg->freqindex = 9; break;
  245. case 37800: rg->freqindex = 10; break;
  246. case 56000: rg->freqindex = 11; break;
  247. case 64000: rg->freqindex = 12; break;
  248. case 88200: rg->freqindex = 13; break;
  249. case 96000: rg->freqindex = 14; break;
  250. case 112000: rg->freqindex = 15; break;
  251. case 128000: rg->freqindex = 16; break;
  252. case 144000: rg->freqindex = 17; break;
  253. case 176400: rg->freqindex = 18; break;
  254. case 192000: rg->freqindex = 19; break;
  255. default: return INIT_GAIN_ANALYSIS_ERROR;
  256. }
  257. rg->sampleWindow = (int) ceil (samplefreq * RMS_WINDOW_TIME);
  258. rg->lsum = 0.;
  259. rg->rsum = 0.;
  260. rg->totsamp = 0;
  261. // TODO: ippsZero_32f
  262. memset ( rg->A, 0, sizeof(rg->A) );
  263. if (rg->butter_iir)
  264. nsutil_iir_Destroy_F32(rg->butter_iir);
  265. if (rg->yule_iir)
  266. nsutil_iir_Destroy_F32(rg->yule_iir);
  267. nsutil_iir_Create_F32(ABButter[rg->freqindex], 2, &rg->butter_iir);
  268. nsutil_iir_Create_F32(ABYule[rg->freqindex], 10, &rg->yule_iir);
  269. return INIT_GAIN_ANALYSIS_OK;
  270. }
  271. DLLEXPORT int WAInitGainAnalysis(void *context, long samplefreq)
  272. {
  273. ReplayGainContext *rg=context;
  274. if (WAResetSampleFrequency(context, samplefreq) != INIT_GAIN_ANALYSIS_OK)
  275. {
  276. return INIT_GAIN_ANALYSIS_ERROR;
  277. }
  278. rg->linpre = rg->linprebuf + MAX_ORDER;
  279. rg->rinpre = rg->rinprebuf + MAX_ORDER;
  280. rg->lout = rg->loutbuf + MAX_ORDER;
  281. rg->rout = rg->routbuf + MAX_ORDER;
  282. // TODO: ippsZero_32f
  283. memset ( rg->B, 0, sizeof(rg->B) );
  284. return INIT_GAIN_ANALYSIS_OK;
  285. }
  286. // returns GAIN_ANALYSIS_OK if successful, GAIN_ANALYSIS_ERROR if not
  287. static __inline double fsqr(const double d)
  288. {
  289. return d*d;
  290. }
  291. DLLEXPORT
  292. int WAAnalyzeSamples(void *context, const Float_t* left_samples, const Float_t* right_samples, size_t num_samples, int num_channels)
  293. {
  294. ReplayGainContext *rg=context;
  295. const Float_t* curleft;
  296. const Float_t* curright;
  297. float left_rms, right_rms;
  298. size_t batchsamples;
  299. size_t cursamplepos;
  300. if ( num_samples == 0 )
  301. return GAIN_ANALYSIS_OK;
  302. cursamplepos = 0;
  303. batchsamples = num_samples;
  304. switch ( num_channels)
  305. {
  306. case 1: right_samples = left_samples;
  307. case 2: break;
  308. default: return GAIN_ANALYSIS_ERROR;
  309. }
  310. if ( num_samples < MAX_ORDER )
  311. {
  312. memcpy ( rg->linprebuf + MAX_ORDER, left_samples , num_samples * sizeof(Float_t) );
  313. memcpy ( rg->rinprebuf + MAX_ORDER, right_samples, num_samples * sizeof(Float_t) );
  314. }
  315. else
  316. {
  317. memcpy ( rg->linprebuf + MAX_ORDER, left_samples, MAX_ORDER * sizeof(Float_t) );
  318. memcpy ( rg->rinprebuf + MAX_ORDER, right_samples, MAX_ORDER * sizeof(Float_t) );
  319. }
  320. while ( batchsamples > 0 )
  321. {
  322. size_t cursamples = batchsamples > rg->sampleWindow - rg->totsamp ? rg->sampleWindow - rg->totsamp : batchsamples;
  323. if ( cursamplepos < MAX_ORDER )
  324. {
  325. curleft = rg->linpre + cursamplepos;
  326. curright = rg->rinpre + cursamplepos;
  327. if (cursamples > MAX_ORDER - cursamplepos )
  328. cursamples = MAX_ORDER - cursamplepos;
  329. }
  330. else
  331. {
  332. curleft = left_samples + cursamplepos;
  333. curright = right_samples + cursamplepos;
  334. }
  335. YULE_FILTER( rg->yule_iir, curleft , rg->lout + rg->totsamp, cursamples);
  336. YULE_FILTER( rg->yule_iir, curright, rg->rout + rg->totsamp, cursamples);
  337. BUTTER_FILTER(rg->butter_iir, rg->lout + rg->totsamp, cursamples);
  338. BUTTER_FILTER(rg->butter_iir, rg->rout + rg->totsamp, cursamples);
  339. curleft = rg->lout + rg->totsamp; // Get the squared values
  340. curright = rg->rout + rg->totsamp;
  341. nsutil_stats_RMS_F32(curleft, cursamples, &left_rms);
  342. nsutil_stats_RMS_F32(curright, cursamples, &right_rms);
  343. rg->lsum += left_rms*left_rms;
  344. rg->rsum += right_rms*right_rms;
  345. batchsamples -= cursamples;
  346. cursamplepos += cursamples;
  347. rg->totsamp += cursamples;
  348. if ( rg->totsamp == rg->sampleWindow )
  349. { // Get the Root Mean Square (RMS) for this set of samples
  350. double val = STEPS_per_dB * 10. * log10 ( (rg->lsum + rg->rsum) / rg->totsamp * 0.5 + 1.e-37 );
  351. int ival = (int) val;
  352. if ( ival < 0 ) ival = 0;
  353. if ( ival >= (int)(sizeof(rg->A) / sizeof(*(rg->A))) ) ival = sizeof(rg->A) / sizeof(*(rg->A)) - 1;
  354. rg->A [ival]++;
  355. rg->lsum = rg->rsum = 0.;
  356. memmove ( rg->loutbuf , rg->loutbuf + rg->totsamp, MAX_ORDER * sizeof(Float_t) );
  357. memmove ( rg->routbuf , rg->routbuf + rg->totsamp, MAX_ORDER * sizeof(Float_t) );
  358. rg->totsamp = 0;
  359. }
  360. if ( rg->totsamp > rg->sampleWindow ) // somehow I really screwed up: Error in programming! Contact author about totsamp > sampleWindow
  361. return GAIN_ANALYSIS_ERROR;
  362. }
  363. if ( num_samples < MAX_ORDER )
  364. {
  365. memmove ( rg->linprebuf, rg->linprebuf + num_samples, (MAX_ORDER - num_samples) * sizeof(Float_t) );
  366. memmove ( rg->rinprebuf, rg->rinprebuf + num_samples, (MAX_ORDER - num_samples) * sizeof(Float_t) );
  367. memcpy ( rg->linprebuf + MAX_ORDER - num_samples, left_samples, num_samples * sizeof(Float_t) );
  368. memcpy ( rg->rinprebuf + MAX_ORDER - num_samples, right_samples, num_samples * sizeof(Float_t) );
  369. }
  370. else
  371. {
  372. memcpy ( rg->linprebuf, left_samples + num_samples - MAX_ORDER, MAX_ORDER * sizeof(Float_t) );
  373. memcpy ( rg->rinprebuf, right_samples + num_samples - MAX_ORDER, MAX_ORDER * sizeof(Float_t) );
  374. }
  375. return GAIN_ANALYSIS_OK;
  376. }
  377. static Float_t analyzeResult(Uint32_t* Array, size_t len)
  378. {
  379. Uint32_t elems;
  380. Int32_t upper;
  381. size_t i;
  382. elems = 0;
  383. for ( i = 0; i < len; i++ )
  384. elems += Array[i];
  385. if ( elems == 0 )
  386. return GAIN_NOT_ENOUGH_SAMPLES;
  387. upper = (Int32_t) ceil (elems * (1. - RMS_PERCENTILE));
  388. for ( i = len; i-- > 0; )
  389. {
  390. if ( (upper -= Array[i]) <= 0 )
  391. break;
  392. }
  393. return (Float_t) ((Float_t)PINK_REF - (Float_t)i / (Float_t)STEPS_per_dB);
  394. }
  395. DLLEXPORT Float_t WAGetTitleGain(void *context)
  396. {
  397. ReplayGainContext *rg=context;
  398. Float_t retval;
  399. int i;
  400. retval = analyzeResult ( rg->A, sizeof(rg->A) / sizeof(*(rg->A)) );
  401. for ( i = 0; i < (int)(sizeof(rg->A) / sizeof(*(rg->A))); i++ )
  402. {
  403. // TODO: ippsAdd_32f_I
  404. rg->B[i] += rg->A[i];
  405. rg->A[i] = 0;
  406. }
  407. for ( i = 0; i < MAX_ORDER; i++ )
  408. rg->linprebuf[i] = rg->loutbuf[i] = rg->rinprebuf[i] = rg->routbuf[i] = 0.f;
  409. nsutil_iir_Reset_F32(rg->yule_iir);
  410. nsutil_iir_Reset_F32(rg->butter_iir);
  411. rg->totsamp = 0;
  412. rg->lsum = rg->rsum = 0.;
  413. return retval;
  414. }
  415. DLLEXPORT Float_t WAGetAlbumGain(void *context)
  416. {
  417. ReplayGainContext *rg=context;
  418. return analyzeResult(rg->B, sizeof(rg->B) / sizeof(*(rg->B)) );
  419. }
  420. DLLEXPORT void *WACreateRGContext()
  421. {
  422. return calloc(1, sizeof(ReplayGainContext));
  423. }
  424. DLLEXPORT void WAFreeRGContext(void *context)
  425. {
  426. free(context);
  427. }
  428. /* end of gain_analysis.c */