Remove static structures: you can now have multiple monoscopes, and they'll actually...
[platform/upstream/gst-plugins-good.git] / gst / monoscope / monoscope.c
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.
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 #include "monoscope.h"
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 static void colors_init(guint32 * colors)
28 {
29     int i;
30     for (i = 0; i < 32; i++) {
31         colors[i] = (i*8 << 16) + (255 << 8);
32         colors[i+31] = (255 << 16) + (((31 - i) * 8) << 8);
33     }
34     colors[63] = (40 << 16) + (75 << 8);
35 }
36
37 struct monoscope_state * monoscope_init (guint32 resx, guint32 resy)
38 {
39     struct monoscope_state * stateptr;
40     stateptr = calloc(1, sizeof(struct monoscope_state));
41     if (stateptr == 0) return 0;
42     stateptr->cstate = convolve_init();
43     colors_init(stateptr->colors);
44     return stateptr;
45 }
46
47 guint32 * monoscope_update (struct monoscope_state * stateptr, 
48                             gint16 data [512])
49 {
50     /* Note that CONVOLVE_BIG must == data size here, ie 512. */
51     /* Really, we want samples evenly spread over the available data.
52      * Just taking a continuous chunk will do for now, though. */
53     int i;
54     int foo;
55     int bar;  
56     int h;
57     guint32 *loc;
58
59         int factor;
60         int val;
61         int max = 1;
62         short * thisEq;
63
64         memcpy (stateptr->copyEq, data, sizeof (short) * CONVOLVE_BIG);
65         thisEq = stateptr->copyEq;
66 #if 1                                   
67         val = convolve_match (stateptr->avgEq, stateptr->copyEq, stateptr->cstate);
68         thisEq += val;
69 #endif                                  
70         memset(stateptr->display, 0, 256 * 128 * sizeof(guint32));
71         for (i=0; i < 256; i++) {
72             foo = thisEq[i] + (stateptr->avgEq[i] >> 1);
73             stateptr->avgEq[i] = foo;
74             if (foo < 0)
75                 foo = -foo;
76             if (foo > max)
77                 max = foo;
78         }
79         stateptr->avgMax += max - (stateptr->avgMax >> 8);
80         if (stateptr->avgMax < max)
81             stateptr->avgMax = max; /* Avoid overflow */
82         factor = 0x7fffffff / stateptr->avgMax;
83         /* Keep the scaling sensible. */
84         if (factor > (1 << 18))
85             factor = 1 << 18;
86         if (factor < (1 << 8))
87             factor = 1 << 8;
88         for (i=0; i < 256; i++) {
89             foo = stateptr->avgEq[i] * factor;
90             foo >>= 18;
91             if (foo > 63)
92                 foo = 63;
93             if (foo < -64)
94                 foo = -64;
95             val = (i + ((foo+64) << 8));
96             bar = val;
97             if ((bar > 0) && (bar < (256 * 128))) {
98                 loc = stateptr->display + bar;
99                 if (foo < 0) {
100                     for (h = 0; h <= (-foo); h++) {
101                         *loc = stateptr->colors[h];
102                         loc+=256; 
103                     }
104                 } else {
105                     for (h = 0; h <= foo; h++) {
106                         *loc = stateptr->colors[h];
107                         loc-=256;
108                     }
109                 }
110             }
111         }
112
113         /* Draw grid. */
114         for (i=16;i < 128; i+=16) {
115             for (h = 0; h < 256; h+=2) {
116                 stateptr->display[(i << 8) + h] = stateptr->colors[63];
117                 if (i == 64)
118                     stateptr->display[(i << 8) + h + 1] = stateptr->colors[63];
119             }
120         }
121         for (i = 16; i < 256; i+=16) {
122             for (h = 0; h < 128; h+=2) {
123                 stateptr->display[i + (h << 8)] = stateptr->colors[63];
124             }
125         }
126
127     return stateptr->display;
128 }
129
130 void monoscope_close (struct monoscope_state * stateptr)
131 {
132 }
133