Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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. The orginal 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
49 colors_init (guint32 * colors)
50 {
51   int i;
52
53   for (i = 0; i < 32; i++) {
54     colors[i] = (i * 8 << 16) + (255 << 8);
55     colors[i + 31] = (255 << 16) + (((31 - i) * 8) << 8);
56   }
57   colors[63] = (40 << 16) + (75 << 8);
58 }
59
60 struct monoscope_state *
61 monoscope_init (guint32 resx, guint32 resy)
62 {
63   struct monoscope_state *stateptr;
64
65   /* I didn't program monoscope to only do 256*128, but it works that way */
66   g_return_val_if_fail (resx == 256, 0);
67   g_return_val_if_fail (resy == 128, 0);
68   stateptr = calloc (1, sizeof (struct monoscope_state));
69   if (stateptr == 0)
70     return 0;
71   stateptr->cstate = convolve_init ();
72   colors_init (stateptr->colors);
73   return stateptr;
74 }
75
76 void
77 monoscope_close (struct monoscope_state *stateptr)
78 {
79   convolve_close (stateptr->cstate);
80   free (stateptr);
81 }
82
83 guint32 *
84 monoscope_update (struct monoscope_state *stateptr, gint16 data[512])
85 {
86   /* Note that CONVOLVE_BIG must == data size here, ie 512. */
87   /* Really, we want samples evenly spread over the available data.
88    * Just taking a continuous chunk will do for now, though. */
89   int i;
90   int foo;
91   int bar;
92   int h;
93   guint32 *loc;
94
95   int factor;
96   int val;
97   int max = 1;
98   short *thisEq;
99
100   memcpy (stateptr->copyEq, data, sizeof (short) * CONVOLVE_BIG);
101   thisEq = stateptr->copyEq;
102 #if 1
103   val = convolve_match (stateptr->avgEq, stateptr->copyEq, stateptr->cstate);
104   thisEq += val;
105 #endif
106   memset (stateptr->display, 0, 256 * 128 * sizeof (guint32));
107   for (i = 0; i < 256; i++) {
108     foo = thisEq[i] + (stateptr->avgEq[i] >> 1);
109     stateptr->avgEq[i] = foo;
110     if (foo < 0)
111       foo = -foo;
112     if (foo > max)
113       max = foo;
114   }
115   stateptr->avgMax += max - (stateptr->avgMax >> 8);
116   if (stateptr->avgMax < max)
117     stateptr->avgMax = max;     /* Avoid overflow */
118   factor = 0x7fffffff / stateptr->avgMax;
119   /* Keep the scaling sensible. */
120   if (factor > (1 << 18))
121     factor = 1 << 18;
122   if (factor < (1 << 8))
123     factor = 1 << 8;
124   for (i = 0; i < 256; i++) {
125     foo = stateptr->avgEq[i] * factor;
126     foo >>= 18;
127     if (foo > 63)
128       foo = 63;
129     if (foo < -63)
130       foo = -63;
131     val = (i + ((foo + 64) << 8));
132     bar = val;
133     if ((bar > 0) && (bar < (256 * 128))) {
134       loc = stateptr->display + bar;
135       if (foo < 0) {
136         for (h = 0; h <= (-foo); h++) {
137           *loc = stateptr->colors[h];
138           loc += 256;
139         }
140       } else {
141         for (h = 0; h <= foo; h++) {
142           *loc = stateptr->colors[h];
143           loc -= 256;
144         }
145       }
146     }
147   }
148
149   /* Draw grid. */
150   for (i = 16; i < 128; i += 16) {
151     for (h = 0; h < 256; h += 2) {
152       stateptr->display[(i << 8) + h] = stateptr->colors[63];
153       if (i == 64)
154         stateptr->display[(i << 8) + h + 1] = stateptr->colors[63];
155     }
156   }
157   for (i = 16; i < 256; i += 16) {
158     for (h = 0; h < 128; h += 2) {
159       stateptr->display[i + (h << 8)] = stateptr->colors[63];
160     }
161   }
162
163   return stateptr->display;
164 }