| 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 ([email protected]) |
| 20 | * -- blame him if you think the idea is flawed |
| 21 | * coding by Glen Sawyer ([email protected]) 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 | |
| 28 | #ifndef GAIN_ANALYSIS_H |
| 29 | #define GAIN_ANALYSIS_H |
| 30 | |
| 31 | #ifdef HAVE_INTTYPES_H |
| 32 | # include <inttypes.h> |
| 33 | #else |
| 34 | # ifdef HAVE_STDINT_H |
| 35 | # include <stdint.h> |
| 36 | # endif |
| 37 | #endif |
| 38 | |
| 39 | #ifdef __cplusplus |
| 40 | extern "C" { |
| 41 | #endif |
| 42 | |
| 43 | |
| 44 | typedef sample_t Float_t; /* Type used for filtering */ |
| 45 | |
| 46 | |
| 47 | #define PINK_REF 64.82 /* 298640883795 */ /* calibration value for 89dB */ |
| 48 | |
| 49 | |
| 50 | #define YULE_ORDER 10 |
| 51 | #define BUTTER_ORDER 2 |
| 52 | #define YULE_FILTER filterYule |
| 53 | #define BUTTER_FILTER filterButter |
| 54 | #define RMS_PERCENTILE 0.95 /* percentile which is louder than the proposed level */ |
| 55 | #define MAX_SAMP_FREQ 48000L /* maximum allowed sample frequency [Hz] */ |
| 56 | #define RMS_WINDOW_TIME_NUMERATOR 1L |
| 57 | #define RMS_WINDOW_TIME_DENOMINATOR 20L /* numerator / denominator = time slice size [s] */ |
| 58 | #define STEPS_per_dB 100 /* Table entries per dB */ |
| 59 | #define MAX_dB 120 /* Table entries for 0...MAX_dB (normal max. values are 70...80 dB) */ |
| 60 | |
| 61 | enum { GAIN_NOT_ENOUGH_SAMPLES = -24601, GAIN_ANALYSIS_ERROR = 0, GAIN_ANALYSIS_OK = |
| 62 | 1, INIT_GAIN_ANALYSIS_ERROR = 0, INIT_GAIN_ANALYSIS_OK = 1 |
| 63 | }; |
| 64 | |
| 65 | enum { MAX_ORDER = (BUTTER_ORDER > YULE_ORDER ? BUTTER_ORDER : YULE_ORDER) |
| 66 | , MAX_SAMPLES_PER_WINDOW = ((MAX_SAMP_FREQ * RMS_WINDOW_TIME_NUMERATOR) / RMS_WINDOW_TIME_DENOMINATOR + 1) /* max. Samples per Time slice */ |
| 67 | }; |
| 68 | |
| 69 | struct replaygain_data { |
| 70 | Float_t linprebuf[MAX_ORDER * 2]; |
| 71 | Float_t *linpre; /* left input samples, with pre-buffer */ |
| 72 | Float_t lstepbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER]; |
| 73 | Float_t *lstep; /* left "first step" (i.e. post first filter) samples */ |
| 74 | Float_t loutbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER]; |
| 75 | Float_t *lout; /* left "out" (i.e. post second filter) samples */ |
| 76 | Float_t rinprebuf[MAX_ORDER * 2]; |
| 77 | Float_t *rinpre; /* right input samples ... */ |
| 78 | Float_t rstepbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER]; |
| 79 | Float_t *rstep; |
| 80 | Float_t routbuf[MAX_SAMPLES_PER_WINDOW + MAX_ORDER]; |
| 81 | Float_t *rout; |
| 82 | long sampleWindow; /* number of samples required to reach number of milliseconds required for RMS window */ |
| 83 | long totsamp; |
| 84 | double lsum; |
| 85 | double rsum; |
| 86 | int freqindex; |
| 87 | int first; |
| 88 | uint32_t A[STEPS_per_dB * MAX_dB]; |
| 89 | uint32_t B[STEPS_per_dB * MAX_dB]; |
| 90 | |
| 91 | }; |
| 92 | #ifndef replaygain_data_defined |
| 93 | #define replaygain_data_defined |
| 94 | typedef struct replaygain_data replaygain_t; |
| 95 | #endif |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | int InitGainAnalysis(replaygain_t * rgData, long samplefreq); |
| 101 | int AnalyzeSamples(replaygain_t * rgData, const Float_t * left_samples, |
| 102 | const Float_t * right_samples, size_t num_samples, int num_channels); |
| 103 | Float_t GetTitleGain(replaygain_t * rgData); |
| 104 | |
| 105 | |
| 106 | #ifdef __cplusplus |
| 107 | } |
| 108 | #endif |
| 109 | #endif /* GAIN_ANALYSIS_H */ |