eb0e728a78e49d0db3233c0ade89e1a3febd753d
[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
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "monoscope.h"
28
29 #include <string.h>
30 #include <stdlib.h>
31
32 static void colors_init(guint32 * colors)
33 {
34     int i;
35     for (i = 0; i < 32; i++) {
36         colors[i] = (i*8 << 16) + (255 << 8);
37         colors[i+31] = (255 << 16) + (((31 - i) * 8) << 8);
38     }
39     colors[63] = (40 << 16) + (75 << 8);
40 }
41
42 struct monoscope_state * monoscope_init (guint32 resx, guint32 resy)
43 {
44     struct monoscope_state * stateptr;
45     stateptr = calloc(1, sizeof(struct monoscope_state));
46     if (stateptr == 0) return 0;
47     stateptr->cstate = convolve_init();
48     colors_init(stateptr->colors);
49     return stateptr;
50 }
51
52 guint32 * monoscope_update (struct monoscope_state * stateptr, 
53                             gint16 data [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     int foo;
60     int bar;  
61     int h;
62     guint32 *loc;
63
64         int factor;
65         int val;
66         int max = 1;
67         short * thisEq;
68
69         memcpy (stateptr->copyEq, data, sizeof (short) * CONVOLVE_BIG);
70         thisEq = stateptr->copyEq;
71 #if 1                                   
72         val = convolve_match (stateptr->avgEq, stateptr->copyEq, stateptr->cstate);
73         thisEq += val;
74 #endif                                  
75         memset(stateptr->display, 0, 256 * 128 * sizeof(guint32));
76         for (i=0; i < 256; i++) {
77             foo = thisEq[i] + (stateptr->avgEq[i] >> 1);
78             stateptr->avgEq[i] = foo;
79             if (foo < 0)
80                 foo = -foo;
81             if (foo > max)
82                 max = foo;
83         }
84         stateptr->avgMax += max - (stateptr->avgMax >> 8);
85         if (stateptr->avgMax < max)
86             stateptr->avgMax = max; /* Avoid overflow */
87         factor = 0x7fffffff / stateptr->avgMax;
88         /* Keep the scaling sensible. */
89         if (factor > (1 << 18))
90             factor = 1 << 18;
91         if (factor < (1 << 8))
92             factor = 1 << 8;
93         for (i=0; i < 256; i++) {
94             foo = stateptr->avgEq[i] * factor;
95             foo >>= 18;
96             if (foo > 63)
97                 foo = 63;
98             if (foo < -64)
99                 foo = -64;
100             val = (i + ((foo+64) << 8));
101             bar = val;
102             if ((bar > 0) && (bar < (256 * 128))) {
103                 loc = stateptr->display + bar;
104                 if (foo < 0) {
105                     for (h = 0; h <= (-foo); h++) {
106                         *loc = stateptr->colors[h];
107                         loc+=256; 
108                     }
109                 } else {
110                     for (h = 0; h <= foo; h++) {
111                         *loc = stateptr->colors[h];
112                         loc-=256;
113                     }
114                 }
115             }
116         }
117
118         /* Draw grid. */
119         for (i=16;i < 128; i+=16) {
120             for (h = 0; h < 256; h+=2) {
121                 stateptr->display[(i << 8) + h] = stateptr->colors[63];
122                 if (i == 64)
123                     stateptr->display[(i << 8) + h + 1] = stateptr->colors[63];
124             }
125         }
126         for (i = 16; i < 256; i+=16) {
127             for (h = 0; h < 128; h+=2) {
128                 stateptr->display[i + (h << 8)] = stateptr->colors[63];
129             }
130         }
131
132     return stateptr->display;
133 }
134
135 void monoscope_close (struct monoscope_state * stateptr)
136 {
137 }
138