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