gain_analysis.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * ReplayGainAnalysis - analyzes input samples and give the recommended dB change
  3. * Copyright (C) 2001 David Robinson and Glen Sawyer
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * concept and filter values by David Robinson (David@Robinson.org)
  20. * -- blame him if you think the idea is flawed
  21. * coding by Glen Sawyer (mp3gain@hotmail.com) 735 W 255 N, Orem, UT 84057-4505 USA
  22. * -- blame him if you think this runs too slowly, or the coding is otherwise flawed
  23. *
  24. * For an explanation of the concepts and the basic algorithms involved, go to:
  25. * http://www.replaygain.org/
  26. */
  27. #ifndef GAIN_ANALYSIS_H
  28. #define GAIN_ANALYSIS_H
  29. #ifdef HAVE_INTTYPES_H
  30. # include <inttypes.h>
  31. #else
  32. # ifdef HAVE_STDINT_H
  33. # include <stdint.h>
  34. # endif
  35. #endif
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. typedef sample_t Float_t; /* Type used for filtering */
  40. #define PINK_REF 64.82 /* 298640883795 */ /* calibration value for 89dB */
  41. #define YULE_ORDER 10
  42. #define BUTTER_ORDER 2
  43. #define YULE_FILTER filterYule
  44. #define BUTTER_FILTER filterButter
  45. #define RMS_PERCENTILE 0.95 /* percentile which is louder than the proposed level */
  46. #define MAX_SAMP_FREQ 48000L /* maximum allowed sample frequency [Hz] */
  47. #define RMS_WINDOW_TIME_NUMERATOR 1L
  48. #define RMS_WINDOW_TIME_DENOMINATOR 20L /* numerator / denominator = time slice size [s] */
  49. #define STEPS_per_dB 100 /* Table entries per dB */
  50. #define MAX_dB 120 /* Table entries for 0...MAX_dB (normal max. values are 70...80 dB) */
  51. enum { GAIN_NOT_ENOUGH_SAMPLES = -24601, GAIN_ANALYSIS_ERROR = 0, GAIN_ANALYSIS_OK =
  52. 1, INIT_GAIN_ANALYSIS_ERROR = 0, INIT_GAIN_ANALYSIS_OK = 1
  53. };
  54. enum { MAX_ORDER = (BUTTER_ORDER > YULE_ORDER ? BUTTER_ORDER : YULE_ORDER)
  55. , MAX_SAMPLES_PER_WINDOW = ((MAX_SAMP_FREQ * RMS_WINDOW_TIME_NUMERATOR) / RMS_WINDOW_TIME_DENOMINATOR + 1) /* max. Samples per Time slice */
  56. };
  57. struct replaygain_data {
  58. Float_t linprebuf[MAX_ORDER * 2];
  59. Float_t *linpre; /* left input samples, with pre-buffer */
  60. Float_t lstepbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER];
  61. Float_t *lstep; /* left "first step" (i.e. post first filter) samples */
  62. Float_t loutbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER];
  63. Float_t *lout; /* left "out" (i.e. post second filter) samples */
  64. Float_t rinprebuf[MAX_ORDER * 2];
  65. Float_t *rinpre; /* right input samples ... */
  66. Float_t rstepbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER];
  67. Float_t *rstep;
  68. Float_t routbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER];
  69. Float_t *rout;
  70. long sampleWindow; /* number of samples required to reach number of milliseconds required for RMS window */
  71. long totsamp;
  72. double lsum;
  73. double rsum;
  74. int freqindex;
  75. int first;
  76. uint32_t A[STEPS_per_dB * MAX_dB];
  77. uint32_t B[STEPS_per_dB * MAX_dB];
  78. };
  79. #ifndef replaygain_data_defined
  80. #define replaygain_data_defined
  81. typedef struct replaygain_data replaygain_t;
  82. #endif
  83. int InitGainAnalysis(replaygain_t * rgData, long samplefreq);
  84. int AnalyzeSamples(replaygain_t * rgData, const Float_t * left_samples,
  85. const Float_t * right_samples, size_t num_samples, int num_channels);
  86. Float_t GetTitleGain(replaygain_t * rgData);
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. #endif /* GAIN_ANALYSIS_H */