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)
53 for (i = 0; i < 32; i++) {
54 colors[i] = (i * 8 << 16) + (255 << 8);
55 colors[i + 31] = (255 << 16) + (((31 - i) * 8) << 8);
57 colors[63] = (40 << 16) + (75 << 8);
60 struct monoscope_state *
61 monoscope_init (guint32 resx, guint32 resy)
63 struct monoscope_state *stateptr;
65 /* I didn't program monoscope to only do 256*128, but it works that way */
66 g_return_val_if_fail (resx == 256, 0);
67 g_return_val_if_fail (resy == 128, 0);
68 stateptr = calloc (1, sizeof (struct monoscope_state));
71 stateptr->cstate = convolve_init ();
72 colors_init (stateptr->colors);
77 monoscope_close (struct monoscope_state *stateptr)
79 convolve_close (stateptr->cstate);
84 monoscope_update (struct monoscope_state *stateptr, gint16 data[512])
86 /* Note that CONVOLVE_BIG must == data size here, ie 512. */
87 /* Really, we want samples evenly spread over the available data.
88 * Just taking a continuous chunk will do for now, though. */
100 memcpy (stateptr->copyEq, data, sizeof (short) * CONVOLVE_BIG);
101 thisEq = stateptr->copyEq;
103 val = convolve_match (stateptr->avgEq, stateptr->copyEq, stateptr->cstate);
106 memset (stateptr->display, 0, 256 * 128 * sizeof (guint32));
107 for (i = 0; i < 256; i++) {
108 foo = thisEq[i] + (stateptr->avgEq[i] >> 1);
109 stateptr->avgEq[i] = foo;
115 stateptr->avgMax += max - (stateptr->avgMax >> 8);
116 if (stateptr->avgMax < max)
117 stateptr->avgMax = max; /* Avoid overflow */
118 factor = 0x7fffffff / stateptr->avgMax;
119 /* Keep the scaling sensible. */
120 if (factor > (1 << 18))
122 if (factor < (1 << 8))
124 for (i = 0; i < 256; i++) {
125 foo = stateptr->avgEq[i] * factor;
131 val = (i + ((foo + 64) << 8));
133 if ((bar > 0) && (bar < (256 * 128))) {
134 loc = stateptr->display + bar;
136 for (h = 0; h <= (-foo); h++) {
137 *loc = stateptr->colors[h];
141 for (h = 0; h <= foo; h++) {
142 *loc = stateptr->colors[h];
150 for (i = 16; i < 128; i += 16) {
151 for (h = 0; h < 256; h += 2) {
152 stateptr->display[(i << 8) + h] = stateptr->colors[63];
154 stateptr->display[(i << 8) + h + 1] = stateptr->colors[63];
157 for (i = 16; i < 256; i += 16) {
158 for (h = 0; h < 128; h += 2) {
159 stateptr->display[i + (h << 8)] = stateptr->colors[63];
163 return stateptr->display;