vu.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include "main.h"
  2. #include "../nu/AutoLock.h"
  3. #include <math.h>
  4. #include "WinampAttributes.h"
  5. #include "../nsutil/stats.h"
  6. using namespace Nullsoft::Utility;
  7. struct VU_list
  8. {
  9. int timestamp;
  10. unsigned char data[2]; // two channels for now, we'll worry about more later
  11. };
  12. static VU_list *vu_bufs;
  13. static int vu_position, vu_length, vu_size;
  14. static int last_pos;
  15. LockGuard VU_guard GUARDNAME("VU Guard");
  16. void vu_init(int numframes, int srate)
  17. {
  18. AutoLock lock (VU_guard LOCKNAME("vu_init"));
  19. vu_length = 0;
  20. if (numframes < 1) numframes = 1;
  21. if (numframes > vu_size)
  22. {
  23. free(vu_bufs);
  24. vu_bufs = (VU_list *)calloc(numframes, sizeof(VU_list));
  25. vu_size = numframes;
  26. }
  27. vu_position = 0;
  28. vu_length = numframes;
  29. last_pos = 0;
  30. }
  31. void vu_deinit(void)
  32. {
  33. AutoLock lock (VU_guard LOCKNAME("vu_deinit"));
  34. vu_length = 0;
  35. }
  36. void VU_Create()
  37. {
  38. vu_length = vu_size = 0;
  39. }
  40. void VU_Destroy()
  41. {
  42. free(vu_bufs);
  43. vu_bufs = 0;
  44. vu_size = 0;
  45. }
  46. int vu_add(char *values, int timestamp)
  47. {
  48. AutoLock lock (VU_guard LOCKNAME("vu_add"));
  49. if (!vu_bufs || vu_length == 0)
  50. return 1;
  51. if (vu_length < 2)
  52. {
  53. vu_position = 0;
  54. }
  55. vu_bufs[vu_position].timestamp = timestamp;//+600;
  56. memcpy(vu_bufs[vu_position].data, values, 2);
  57. vu_position++;
  58. if (vu_position >= vu_length)
  59. vu_position -= vu_length;
  60. return 0;
  61. }
  62. bool vu_get(int timestamp, unsigned char data[2])
  63. {
  64. int x, closest = 1000000, closest_v = -1;
  65. int i;
  66. AutoLock lock (VU_guard LOCKNAME("vu_get"));
  67. if (!vu_bufs || vu_length==0)
  68. return false;
  69. if (vu_length < 2)
  70. {
  71. memcpy(data, vu_bufs[0].data, 2);
  72. return true;
  73. }
  74. i = last_pos;
  75. for (x = 0; x < vu_length; x ++)
  76. {
  77. int d;
  78. if (i >= vu_length) i = 0;
  79. d = timestamp - vu_bufs[i].timestamp;
  80. if (d < 0) d = -d;
  81. if (d < closest)
  82. {
  83. closest = d;
  84. closest_v = i;
  85. }
  86. else if (closest < 200) break;
  87. i++;
  88. }
  89. if (closest < 200 && closest_v >= 0)
  90. {
  91. last_pos = closest_v;
  92. memcpy(data, vu_bufs[closest_v].data, 2);
  93. return true;
  94. }
  95. return false;
  96. }
  97. int export_vu_get(int channel)
  98. {
  99. if (channel>2 || channel<0)
  100. return -1;
  101. unsigned char data[2];
  102. int now = in_getouttime();
  103. if (vu_get(now, data))
  104. return data[channel];
  105. else
  106. return -1;
  107. }
  108. float historyL=0, historyR=0;
  109. static void FillFloat(float *floatBuf, void *samples, size_t bps, size_t numSamples, size_t numChannels, float preamp)
  110. {
  111. switch(bps)
  112. {
  113. case 8:
  114. {
  115. preamp /= 256.0f;
  116. unsigned __int8 *samples8 = (unsigned __int8 *)samples;
  117. for (size_t x = 0; x != numSamples; x ++)
  118. {
  119. floatBuf[x] = (float)(samples8[x*numChannels]-128) * preamp;
  120. }
  121. }
  122. break;
  123. case 16:
  124. {
  125. preamp/=32768.0f;
  126. short *samples16 = (short *)samples;
  127. for (size_t x = 0; x != numSamples; x ++)
  128. {
  129. floatBuf[x] = (float)samples16[x*numChannels] * preamp;
  130. }
  131. }
  132. break;
  133. case 24:
  134. {
  135. preamp/=2147483648.0f;
  136. unsigned __int8 *samples8 = (unsigned __int8 *)samples;
  137. for (size_t x = 0; x != numSamples; x ++)
  138. {
  139. long temp = (((long)samples8[0]) << 8);
  140. temp = temp | (((long)samples8[1]) << 16);
  141. temp = temp | (((long)samples8[2]) << 24);
  142. floatBuf[x] = (float)temp * preamp;
  143. samples8+=3*numChannels;
  144. }
  145. }
  146. break;
  147. case 32:
  148. {
  149. preamp /= 2147483648.0f;
  150. int32_t *samples32 = (int32_t *)samples;
  151. for (size_t x = 0; x != numSamples; x ++)
  152. {
  153. floatBuf[x] = (float)samples32[x*numChannels] * preamp;
  154. }
  155. }
  156. break;
  157. }
  158. }
  159. void calcVuData(unsigned char *out, char *data, const int channels, const int bits)
  160. {
  161. static float left_peak=10.0f, left_history=0.0f;
  162. static float right_peak=10.0f, right_history=0.0f;
  163. float left[576] = {0}, right[576] = {0};
  164. float left_rms, right_rms;
  165. float scale = 1.0;
  166. if (config_replaygain)
  167. scale= 1.0f/pow(10.0f, config_replaygain_non_rg_gain.GetFloat() / 20.0f);
  168. FillFloat(left, data, bits, 576, channels, scale);
  169. if (channels > 1)
  170. FillFloat(right, data+(bits/8), bits, 576, channels, scale);
  171. else
  172. memcpy(right, left, 576*sizeof(float));
  173. nsutil_stats_RMS_F32(left, 576, &left_rms);
  174. nsutil_stats_RMS_F32(right, 576, &right_rms);
  175. // use a simple one-pole IIR to 'adjust' the vu meter to the songs loudness level
  176. // the small constant
  177. left_peak = 0.855f*left_peak + 0.095f*left_rms + 0.5f;
  178. left_rms = 10.0f*left_rms /left_peak;
  179. right_peak = 0.855f*right_peak + 0.095f*right_rms + 0.5f;
  180. right_rms = 10.0f*right_rms /right_peak;
  181. float left_db=left_rms*left_rms;
  182. float right_db=right_rms*right_rms;
  183. if (left_db < left_history)
  184. left_db = left_db*0.2f + left_history*0.8f;
  185. else
  186. left_db = left_db*0.75f + left_history*0.25f;
  187. if (right_db < right_history)
  188. right_db = right_db*0.2f + right_history*0.8f;
  189. else
  190. right_db = right_db*0.75f + right_history*0.25f;
  191. left_history = left_db;
  192. right_history = right_db;
  193. left_db = min(left_db, 255);
  194. left_db = max(left_db, 0);
  195. right_db = min(right_db, 255);
  196. right_db = max(right_db, 0);
  197. out[0] = (unsigned char)(left_db );
  198. out[1] = (unsigned char)(right_db);
  199. }