I'm too lazy to comment this
[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 #include "convolve.h"
24
25 #define scope_width 256
26 #define scope_height 128
27
28 static gint16 newEq[CONVOLVE_BIG];      // latest block of 512 samples.
29 static gint16 copyEq[CONVOLVE_BIG];
30 static int avgEq[CONVOLVE_SMALL];      // a running average of the last few.
31 static int avgMax;                     // running average of max sample.
32 static guint32 display[(scope_width + 1) * (scope_height + 1)];
33
34 static convolve_state *state = NULL;
35 static guint32 colors[64];
36
37 static void colors_init(guint32 * colors)
38 {
39     int i;
40     for (i = 0; i < 32; i++) {
41         colors[i] = (i*8 << 16) + (255 << 8);
42         colors[i+31] = (255 << 16) + (((31 - i) * 8) << 8);
43     }
44     colors[63] = (40 << 16) + (75 << 8);
45 }
46
47 void monoscope_init (guint32 resx, guint32 resy)
48 {
49     state = convolve_init();
50     colors_init(colors);
51 }
52
53 guint32 * monoscope_update (gint16 data [2][512])
54 {
55     /* Note that CONVOLVE_BIG must == data size here, ie 512. */
56     /* Really, we want samples evenly spread over the available data.
57      * Just taking a continuous chunk will do for now, though. */
58     int i;
59     for (i = 0; i < CONVOLVE_BIG; i++) {
60         /* Average the two channels. */
61         newEq[i] = (((int) data[0][i]) + (int) data[1][i]) >> 1;
62     }
63
64     int foo;
65     int bar;  
66     int h;
67     guchar bits[ 257 * 129];
68     guint32 *loc;
69
70         int factor;
71         int val;
72         int max = 1;
73         short * thisEq;
74         memcpy (copyEq, newEq, sizeof (short) * CONVOLVE_BIG);
75         thisEq = copyEq;
76 #if 1                                   
77         val = convolve_match (avgEq, copyEq, state);
78         thisEq += val;
79 #endif                                  
80         memset(display, 0, 256 * 128 * sizeof(guint32));
81         for (i=0; i < 256; i++) {
82             foo = thisEq[i] + (avgEq[i] >> 1);
83             avgEq[i] = foo;
84             if (foo < 0)
85                 foo = -foo;
86             if (foo > max)
87                 max = foo;
88         }
89         avgMax += max - (avgMax >> 8);
90         if (avgMax < max)
91             avgMax = max; /* Avoid overflow */
92         factor = 0x7fffffff / avgMax;
93         /* Keep the scaling sensible. */
94         if (factor > (1 << 18))
95             factor = 1 << 18;
96         if (factor < (1 << 8))
97             factor = 1 << 8;
98         for (i=0; i < 256; i++) {
99             foo = avgEq[i] * factor;
100             foo >>= 18;
101             if (foo > 63)
102                 foo = 63;
103             if (foo < -64)
104                 foo = -64;
105             val = (i + ((foo+64) << 8));
106             bar = val;
107             if ((bar > 0) && (bar < (256 * 128))) {
108                 loc = display + bar;
109                 if (foo < 0) {
110                     for (h = 0; h <= (-foo); h++) {
111                         *loc = colors[h];
112                         loc+=256; 
113                     }
114                 } else {
115                     for (h = 0; h <= foo; h++) {
116                         *loc = colors[h];
117                         loc-=256;
118                     }
119                 }
120             }
121         }
122
123         /* Draw grid. */
124         for (i=16;i < 128; i+=16) {
125             for (h = 0; h < 256; h+=2) {
126                 display[(i << 8) + h] = colors[63];
127                 if (i == 64)
128                     display[(i << 8) + h + 1] = colors[63];
129             }
130         }
131         for (i = 16; i < 256; i+=16) {
132             for (h = 0; h < 128; h+=2) {
133                 display[i + (h << 8)] = colors[63];
134             }
135         }
136
137     return display;
138 }
139
140 void monoscope_close ()
141 {
142 }
143