monoscope.c 5.45 KB
Newer Older
Richard Boulton's avatar
Richard Boulton committed
1 2 3 4 5
/*  monoscope.cpp
 *  Copyright (C) 2002 Richard Boulton <richard@tartarus.org>
 *  Copyright (C) 1998-2001 Andy Lo A Foe <andy@alsaplayer.org>
 *  Original code by Tinic Uro
 *
6 7 8 9
 *  This code is copied from Alsaplayer. The orginal code was by Tinic Uro and under
 *  the BSD license without a advertisig clause. Andy Lo A Foe then relicensed the
 *  code when he used it for Alsaplayer to GPL with Tinic's permission. Richard Boulton
 *  then took this code and made a GPL plugin out of it.
Stefan Sauer's avatar
Stefan Sauer committed
10
 *
11
 *  7th December 2004 Christian Schaller: Richard Boulton and Andy Lo A Foe gave
Stefan Sauer's avatar
Stefan Sauer committed
12
 *  permission to relicense their changes under BSD license so we where able to restore the
13
 *  code to Tinic's original BSD license.
Richard Boulton's avatar
Richard Boulton committed
14
 *
15
 * This file is under what is known as the BSD license:
Richard Boulton's avatar
Richard Boulton committed
16
 *
17 18 19
 * Redistribution and use in source and binary forms, with or without modification, i
 * are permitted provided that the following conditions are met:
 *
Stefan Sauer's avatar
Stefan Sauer committed
20
 * 1. Redistributions of source code must retain the above copyright notice, this list of
21
 * conditions and the following disclaimer.
Stefan Sauer's avatar
Stefan Sauer committed
22 23
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 * of conditions and the following disclaimer in the documentation and/or other materials
24
 * provided with the distribution.
Stefan Sauer's avatar
Stefan Sauer committed
25
 * 3. The name of the author may not be used to endorse or promote products derived from
26 27
 * this software without specific prior written permission.
 *
Stefan Sauer's avatar
Stefan Sauer committed
28 29
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
Stefan Sauer's avatar
Stefan Sauer committed
31 32 33 34
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Richard Boulton's avatar
Richard Boulton committed
36 37
 *
 */
38 39 40 41 42

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

Richard Boulton's avatar
Richard Boulton committed
43 44
#include "monoscope.h"

45 46
#include <string.h>
#include <stdlib.h>
Richard Boulton's avatar
Richard Boulton committed
47

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
48 49
static void
colors_init (guint32 * colors)
Richard Boulton's avatar
Richard Boulton committed
50
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
51
  int i;
52 53 54 55
  int hq = (scope_height / 4);
  int hq1 = hq - 1;
  int hh1 = (scope_height / 2) - 1;
  double scl = (256.0 / (double) hq);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
56

57 58 59 60 61
  for (i = 0; i < hq; i++) {
    /* green to yellow */
    colors[i] = ((int) (i * scl) << 16) + (255 << 8);
    /* yellow to red */
    colors[i + hq1] = (255 << 16) + ((int) ((hq1 - i) * scl) << 8);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
62
  }
63
  colors[hh1] = (40 << 16) + (75 << 8);
Richard Boulton's avatar
Richard Boulton committed
64 65
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
66 67
struct monoscope_state *
monoscope_init (guint32 resx, guint32 resy)
Richard Boulton's avatar
Richard Boulton committed
68
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
69
  struct monoscope_state *stateptr;
70 71

  /* I didn't program monoscope to only do 256*128, but it works that way */
72 73
  g_return_val_if_fail (resx == scope_width, 0);
  g_return_val_if_fail (resy == scope_height, 0);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
74 75 76
  stateptr = calloc (1, sizeof (struct monoscope_state));
  if (stateptr == 0)
    return 0;
77
  stateptr->cstate = convolve_init (convolver_depth);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78 79
  colors_init (stateptr->colors);
  return stateptr;
Richard Boulton's avatar
Richard Boulton committed
80 81
}

82 83 84 85 86 87 88
void
monoscope_close (struct monoscope_state *stateptr)
{
  convolve_close (stateptr->cstate);
  free (stateptr);
}

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
89
guint32 *
90
monoscope_update (struct monoscope_state *stateptr, gint16 data[convolver_big])
Richard Boulton's avatar
Richard Boulton committed
91
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
92 93 94
  /* Really, we want samples evenly spread over the available data.
   * Just taking a continuous chunk will do for now, though. */
  int i;
95 96
  int foo, bar;
  int avg;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
97
  int h;
98 99
  int hh = (scope_height / 2);
  int hh1 = hh - 1;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
100
  guint32 *loc;
Richard Boulton's avatar
Richard Boulton committed
101

102
  double factor;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
103
  int max = 1;
104
  short *thisEq = stateptr->copyEq;
105

106
  memcpy (thisEq, data, sizeof (short) * convolver_big);
107
  thisEq += convolve_match (stateptr->avgEq, thisEq, stateptr->cstate);
Stefan Sauer's avatar
Stefan Sauer committed
108

109 110
  memset (stateptr->display, 0, scope_width * scope_height * sizeof (guint32));
  for (i = 0; i < convolver_small; i++) {
111
    avg = (thisEq[i] + stateptr->avgEq[i]) >> 1;
112 113 114
    stateptr->avgEq[i] = avg;
    avg = abs (avg);
    max = MAX (max, avg);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
115
  }
116 117 118 119 120 121 122 123 124 125 126
  /* running average, 4 values is enough to make it follow volume changes
   * if this value is too large it will converge slowly
   */
  stateptr->avgMax += (max / 4) - (stateptr->avgMax / 4);

  /* input is +/- avgMax, output is +/- hh */
  if (stateptr->avgMax) {
    factor = (gdouble) hh / stateptr->avgMax;
  } else {
    factor = 1.0;
  }
127 128

  for (i = 0; i < scope_width; i++) {
129
    /* scale 16bit signed audio values to scope_height */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
130
    foo = stateptr->avgEq[i] * factor;
131 132 133
    foo = CLAMP (foo, -hh1, hh1);
    bar = (i + ((foo + hh) * scope_width));
    if ((bar > 0) && (bar < (scope_width * scope_height))) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
134
      loc = stateptr->display + bar;
135
      /* draw up / down bars */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
136
      if (foo < 0) {
137 138
        for (h = 0; h <= (-foo); h++) {
          *loc = stateptr->colors[h];
139
          loc += scope_width;
140
        }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
141
      } else {
142 143
        for (h = 0; h <= foo; h++) {
          *loc = stateptr->colors[h];
144
          loc -= scope_width;
145
        }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
146 147 148
      }
    }
  }
Richard Boulton's avatar
Richard Boulton committed
149

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
150
  /* Draw grid. */
151 152 153 154 155 156 157 158 159
  {
    guint32 gray = stateptr->colors[hh1];

    for (i = 16; i < scope_height; i += 16) {
      for (h = 0; h < scope_width; h += 2) {
        stateptr->display[(i * scope_width) + h] = gray;
        if (i == hh)
          stateptr->display[(i * scope_width) + h + 1] = gray;
      }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
160
    }
161 162 163 164
    for (i = 16; i < scope_width; i += 16) {
      for (h = 0; h < scope_height; h += 2) {
        stateptr->display[i + (h * scope_width)] = gray;
      }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
165 166 167
    }
  }
  return stateptr->display;
Richard Boulton's avatar
Richard Boulton committed
168
}