summaryrefslogtreecommitdiffstats
path: root/gst/level/filter.func
blob: fd82c0383841999f803af3e88c56e8ebafdafbff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
static void inline
gst_level_fast_16bit_chain (gint16 * in, guint num, gint channels,
    gint resolution, double *CS, double *peak)
*/
/* process one (interleaved) channel of incoming samples
 * calculate square sum of samples
 * normalize and return normalized Cumulative Square
 * caller must assure num is a multiple of channels
 * this filter only accepts signed audio data, so mid level is always 0
 */
{
  register int j;
  double squaresum = 0.0;	/* square sum of the integer samples */
  register double square = 0.0;		/* Square */
  register double PSS = 0.0;		/* Peak Square Sample */
  gdouble normalizer;

  *CS = 0.0;      /* Cumulative Square for this block */

  normalizer = (double) (1 << resolution);

  /*
   * process data here  
   * input sample data enters in *in_data as 8 or 16 bit data
   * samples for left and right channel are interleaved
   * returns the Mean Square of the samples as a double between 0 and 1
   */

  for (j = 0; j < num; j += channels)
  {
    //g_print ("ch %d -> smp %d\n", j, in[j]);
    square = (double) (in[j] * in[j]);
    if (square > PSS) PSS = square;
    squaresum += square;
  }
  *peak = PSS / ((double) normalizer * (double) normalizer);
  
  /* return normalized cumulative square */
  *CS = squaresum / ((double) normalizer * (double) normalizer);
}