1 /* monoscope.cpp
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
5 *
6 * This code is copied from Alsaplayer. The original 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.
10 *
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.
14 *
15 * This file is under what is known as the BSD license:
16 *
17 * Redistribution and use in source and binary forms, with or without modification, i
18 * are permitted provided that the following conditions are met:
19 *
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.
27 *
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.
36 *
37 */
38
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif
42
43 #include "monoscope.h"
44
45 #include <string.h>
46 #include <stdlib.h>
47
48 static void
colors_init(guint32 * colors)49 colors_init (guint32 * colors)
50 {
51 int i;
52 int hq = (scope_height / 4);
53 int hq1 = hq - 1;
54 int hh1 = (scope_height / 2) - 1;
55 double scl = (256.0 / (double) hq);
56
57 for (i = 0; i < hq; i++) {
58 /* green to yellow */
59 colors[i] = ((int) (i * scl) << 16) + (255 << 8);
60 /* yellow to red */
61 colors[i + hq1] = (255 << 16) + ((int) ((hq1 - i) * scl) << 8);
62 }
63 colors[hh1] = (40 << 16) + (75 << 8);
64 }
65
66 struct monoscope_state *
monoscope_init(guint32 resx,guint32 resy)67 monoscope_init (guint32 resx, guint32 resy)
68 {
69 struct monoscope_state *stateptr;
70
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));
75 if (stateptr == 0)
76 return 0;
77 stateptr->cstate = convolve_init (convolver_depth);
78 colors_init (stateptr->colors);
79 return stateptr;
80 }
81
82 void
monoscope_close(struct monoscope_state * stateptr)83 monoscope_close (struct monoscope_state *stateptr)
84 {
85 convolve_close (stateptr->cstate);
86 free (stateptr);
87 }
88
89 guint32 *
monoscope_update(struct monoscope_state * stateptr,gint16 data[convolver_big])90 monoscope_update (struct monoscope_state *stateptr, gint16 data[convolver_big])
91 {
92 /* Really, we want samples evenly spread over the available data.
93 * Just taking a continuous chunk will do for now, though. */
94 int i;
95 int foo, bar;
96 int avg;
97 int h;
98 int hh = (scope_height / 2);
99 int hh1 = hh - 1;
100 guint32 *loc;
101
102 double factor;
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);
108
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;
113 avg = abs (avg);
114 max = MAX (max, avg);
115 }
116 /* running average, 4 values is enough to make it follow volume changes
117 * if this value is too large it will converge slowly
118 */
119 stateptr->avgMax += (max / 4) - (stateptr->avgMax / 4);
120
121 /* input is +/- avgMax, output is +/- hh */
122 if (stateptr->avgMax) {
123 factor = (gdouble) hh / stateptr->avgMax;
124 } else {
125 factor = 1.0;
126 }
127
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 */
136 if (foo < 0) {
137 for (h = 0; h <= (-foo); h++) {
138 *loc = stateptr->colors[h];
139 loc += scope_width;
140 }
141 } else {
142 for (h = 0; h <= foo; h++) {
143 *loc = stateptr->colors[h];
144 loc -= scope_width;
145 }
146 }
147 }
148 }
149
150 /* Draw grid. */
151 {
152 guint32 gray = stateptr->colors[hh1];
153
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;
157 if (i == hh)
158 stateptr->display[(i * scope_width) + h + 1] = gray;
159 }
160 }
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;
164 }
165 }
166 }
167 return stateptr->display;
168 }
169