2 * Copyright (C) 2002 Richard Boulton <richard@tartarus.org>
3 * Copyright (C) 1998-2001 Andy Lo A Foe <andy@alsaplayer.org>
4 * Original code by Tinic Uro
6 * This code is copied from Alsaplayer. The orginal code was by Tinic Uro and under
7 * the BSD license without a advertisig clause. Andy Lo A Foe then relicensed the
8 * code when he used it for Alsaplayer to GPL with Tinic's permission. Richard Boulton
9 * then took this code and made a GPL plugin out of it.
11 * 7th December 2004 Christian Schaller: Richard Boulton and Andy Lo A Foe gave
12 * permission to relicense their changes under BSD license so we where able to restore the
13 * code to Tinic's original BSD license.
15 * This file is under what is known as the BSD license:
17 * Redistribution and use in source and binary forms, with or without modification, i
18 * are permitted provided that the following conditions are met:
20 * 1. Redistributions of source code must retain the above copyright notice, this list of
21 * conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
23 * of conditions and the following disclaimer in the documentation and/or other materials
24 * provided with the distribution.
25 * 3. The name of the author may not be used to endorse or promote products derived from
26 * this software without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
29 * 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,
31 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
33 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * 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.
43 #include "monoscope.h"
49 colors_init (guint32 * colors)
52 int hq = (scope_height / 4);
54 int hh1 = (scope_height / 2) - 1;
55 double scl = (256.0 / (double) hq);
57 for (i = 0; i < hq; i++) {
59 colors[i] = ((int) (i * scl) << 16) + (255 << 8);
61 colors[i + hq1] = (255 << 16) + ((int) ((hq1 - i) * scl) << 8);
63 colors[hh1] = (40 << 16) + (75 << 8);
66 struct monoscope_state *
67 monoscope_init (guint32 resx, guint32 resy)
69 struct monoscope_state *stateptr;
71 /* I didn't program monoscope to only do 256*128, but it works that way */
72 g_return_val_if_fail (resx == scope_width, 0);
73 g_return_val_if_fail (resy == scope_height, 0);
74 stateptr = calloc (1, sizeof (struct monoscope_state));
77 stateptr->cstate = convolve_init (convolver_depth);
78 colors_init (stateptr->colors);
83 monoscope_close (struct monoscope_state *stateptr)
85 convolve_close (stateptr->cstate);
90 monoscope_update (struct monoscope_state *stateptr, gint16 data[convolver_big])
92 /* Really, we want samples evenly spread over the available data.
93 * Just taking a continuous chunk will do for now, though. */
98 int hh = (scope_height / 2);
104 short *thisEq = stateptr->copyEq;
106 memcpy (thisEq, data, sizeof (short) * convolver_big);
107 thisEq += convolve_match (stateptr->avgEq, thisEq, stateptr->cstate);
109 memset (stateptr->display, 0, scope_width * scope_height * sizeof (guint32));
110 for (i = 0; i < convolver_small; i++) {
111 avg = (thisEq[i] + stateptr->avgEq[i]) >> 1;
112 stateptr->avgEq[i] = avg;
114 max = MAX (max, avg);
116 /* running average, 4 values is enough to make it follow volume changes
117 * if this value is too large it will converge slowly
119 stateptr->avgMax += (max / 4) - (stateptr->avgMax / 4);
121 /* input is +/- avgMax, output is +/- hh */
122 if (stateptr->avgMax) {
123 factor = (gdouble) hh / stateptr->avgMax;
128 for (i = 0; i < scope_width; i++) {
129 /* scale 16bit signed audio values to scope_height */
130 foo = stateptr->avgEq[i] * factor;
131 foo = CLAMP (foo, -hh1, hh1);
132 bar = (i + ((foo + hh) * scope_width));
133 if ((bar > 0) && (bar < (scope_width * scope_height))) {
134 loc = stateptr->display + bar;
135 /* draw up / down bars */
137 for (h = 0; h <= (-foo); h++) {
138 *loc = stateptr->colors[h];
142 for (h = 0; h <= foo; h++) {
143 *loc = stateptr->colors[h];
152 guint32 gray = stateptr->colors[hh1];
154 for (i = 16; i < scope_height; i += 16) {
155 for (h = 0; h < scope_width; h += 2) {
156 stateptr->display[(i * scope_width) + h] = gray;
158 stateptr->display[(i * scope_width) + h + 1] = gray;
161 for (i = 16; i < scope_width; i += 16) {
162 for (h = 0; h < scope_height; h += 2) {
163 stateptr->display[i + (h * scope_width)] = gray;
167 return stateptr->display;