2002-03-02 12:36:07 +00:00
|
|
|
/* monoscope.cpp
|
|
|
|
* Copyright (C) 2002 Richard Boulton <richard@tartarus.org>
|
|
|
|
* Copyright (C) 1998-2001 Andy Lo A Foe <andy@alsaplayer.org>
|
|
|
|
* Original code by Tinic Uro
|
|
|
|
*
|
2004-12-07 17:22:19 +00:00
|
|
|
* This code is copied from Alsaplayer. The orginal code was by Tinic Uro and under
|
|
|
|
* the BSD license without a advertisig clause. Andy Lo A Foe then relicensed the
|
|
|
|
* code when he used it for Alsaplayer to GPL with Tinic's permission. Richard Boulton
|
|
|
|
* then took this code and made a GPL plugin out of it.
|
2016-01-28 07:53:35 +00:00
|
|
|
*
|
2004-12-07 17:22:19 +00:00
|
|
|
* 7th December 2004 Christian Schaller: Richard Boulton and Andy Lo A Foe gave
|
2016-01-28 07:53:35 +00:00
|
|
|
* permission to relicense their changes under BSD license so we where able to restore the
|
2004-12-07 17:22:19 +00:00
|
|
|
* code to Tinic's original BSD license.
|
2002-03-02 12:36:07 +00:00
|
|
|
*
|
2004-12-07 17:22:19 +00:00
|
|
|
* This file is under what is known as the BSD license:
|
2002-03-02 12:36:07 +00:00
|
|
|
*
|
2004-12-07 17:22:19 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without modification, i
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
2016-01-28 07:53:35 +00:00
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of
|
2004-12-07 17:22:19 +00:00
|
|
|
* conditions and the following disclaimer.
|
2016-01-28 07:53:35 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
* of conditions and the following disclaimer in the documentation and/or other materials
|
2004-12-07 17:22:19 +00:00
|
|
|
* provided with the distribution.
|
2016-01-28 07:53:35 +00:00
|
|
|
* 3. The name of the author may not be used to endorse or promote products derived from
|
2004-12-07 17:22:19 +00:00
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
2016-01-28 07:53:35 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2004-12-07 17:22:19 +00:00
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
2016-01-28 07:53:35 +00:00
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
2004-12-07 17:22:19 +00:00
|
|
|
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2002-03-02 12:36:07 +00:00
|
|
|
*
|
|
|
|
*/
|
2003-11-07 12:47:01 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2002-03-02 12:36:07 +00:00
|
|
|
#include "monoscope.h"
|
|
|
|
|
2002-03-08 16:43:08 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2002-03-02 12:36:07 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
static void
|
|
|
|
colors_init (guint32 * colors)
|
2002-03-02 12:36:07 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
int i;
|
2016-01-28 16:00:55 +00:00
|
|
|
int hq = (scope_height / 4);
|
|
|
|
int hq1 = hq - 1;
|
|
|
|
int hh1 = (scope_height / 2) - 1;
|
|
|
|
double scl = (256.0 / (double) hq);
|
2004-03-14 22:34:33 +00:00
|
|
|
|
2016-01-28 16:00:55 +00:00
|
|
|
for (i = 0; i < hq; i++) {
|
|
|
|
/* green to yellow */
|
|
|
|
colors[i] = ((int) (i * scl) << 16) + (255 << 8);
|
|
|
|
/* yellow to red */
|
|
|
|
colors[i + hq1] = (255 << 16) + ((int) ((hq1 - i) * scl) << 8);
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
2016-01-28 16:00:55 +00:00
|
|
|
colors[hh1] = (40 << 16) + (75 << 8);
|
2002-03-02 12:36:07 +00:00
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
struct monoscope_state *
|
|
|
|
monoscope_init (guint32 resx, guint32 resy)
|
2002-03-02 12:36:07 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
struct monoscope_state *stateptr;
|
2004-04-27 15:59:57 +00:00
|
|
|
|
|
|
|
/* I didn't program monoscope to only do 256*128, but it works that way */
|
2016-01-28 08:43:12 +00:00
|
|
|
g_return_val_if_fail (resx == scope_width, 0);
|
|
|
|
g_return_val_if_fail (resy == scope_height, 0);
|
2004-03-14 22:34:33 +00:00
|
|
|
stateptr = calloc (1, sizeof (struct monoscope_state));
|
|
|
|
if (stateptr == 0)
|
|
|
|
return 0;
|
2016-01-28 08:43:12 +00:00
|
|
|
stateptr->cstate = convolve_init (convolver_depth);
|
2004-03-14 22:34:33 +00:00
|
|
|
colors_init (stateptr->colors);
|
|
|
|
return stateptr;
|
2002-03-02 12:36:07 +00:00
|
|
|
}
|
|
|
|
|
2011-03-02 11:00:31 +00:00
|
|
|
void
|
|
|
|
monoscope_close (struct monoscope_state *stateptr)
|
|
|
|
{
|
|
|
|
convolve_close (stateptr->cstate);
|
|
|
|
free (stateptr);
|
|
|
|
}
|
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
guint32 *
|
2016-01-28 08:43:12 +00:00
|
|
|
monoscope_update (struct monoscope_state *stateptr, gint16 data[convolver_big])
|
2002-03-02 12:36:07 +00:00
|
|
|
{
|
2004-03-14 22:34:33 +00:00
|
|
|
/* Really, we want samples evenly spread over the available data.
|
|
|
|
* Just taking a continuous chunk will do for now, though. */
|
|
|
|
int i;
|
2016-01-28 16:00:55 +00:00
|
|
|
int foo, bar;
|
|
|
|
int avg;
|
2004-03-14 22:34:33 +00:00
|
|
|
int h;
|
2016-01-28 16:00:55 +00:00
|
|
|
int hh = (scope_height / 2);
|
|
|
|
int hh1 = hh - 1;
|
2004-03-14 22:34:33 +00:00
|
|
|
guint32 *loc;
|
2002-03-02 12:36:07 +00:00
|
|
|
|
2016-02-12 19:57:29 +00:00
|
|
|
double factor;
|
2004-03-14 22:34:33 +00:00
|
|
|
int max = 1;
|
2016-01-28 16:00:55 +00:00
|
|
|
short *thisEq = stateptr->copyEq;
|
2002-03-02 20:15:00 +00:00
|
|
|
|
2016-01-28 08:43:12 +00:00
|
|
|
memcpy (thisEq, data, sizeof (short) * convolver_big);
|
2016-01-28 16:00:55 +00:00
|
|
|
thisEq += convolve_match (stateptr->avgEq, thisEq, stateptr->cstate);
|
2016-01-28 07:53:35 +00:00
|
|
|
|
2016-01-28 08:43:12 +00:00
|
|
|
memset (stateptr->display, 0, scope_width * scope_height * sizeof (guint32));
|
|
|
|
for (i = 0; i < convolver_small; i++) {
|
2016-02-12 19:57:29 +00:00
|
|
|
avg = (thisEq[i] + stateptr->avgEq[i]) >> 1;
|
2016-01-28 16:00:55 +00:00
|
|
|
stateptr->avgEq[i] = avg;
|
|
|
|
avg = abs (avg);
|
|
|
|
max = MAX (max, avg);
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
2016-02-12 19:57:29 +00:00
|
|
|
/* running average, 4 values is enough to make it follow volume changes
|
|
|
|
* if this value is too large it will converge slowly
|
|
|
|
*/
|
|
|
|
stateptr->avgMax += (max / 4) - (stateptr->avgMax / 4);
|
|
|
|
|
|
|
|
/* input is +/- avgMax, output is +/- hh */
|
|
|
|
if (stateptr->avgMax) {
|
|
|
|
factor = (gdouble) hh / stateptr->avgMax;
|
|
|
|
} else {
|
|
|
|
factor = 1.0;
|
|
|
|
}
|
2016-01-28 16:00:55 +00:00
|
|
|
|
|
|
|
for (i = 0; i < scope_width; i++) {
|
2016-02-12 19:57:29 +00:00
|
|
|
/* scale 16bit signed audio values to scope_height */
|
2004-03-14 22:34:33 +00:00
|
|
|
foo = stateptr->avgEq[i] * factor;
|
2016-01-28 16:00:55 +00:00
|
|
|
foo = CLAMP (foo, -hh1, hh1);
|
|
|
|
bar = (i + ((foo + hh) * scope_width));
|
|
|
|
if ((bar > 0) && (bar < (scope_width * scope_height))) {
|
2004-03-14 22:34:33 +00:00
|
|
|
loc = stateptr->display + bar;
|
2016-01-28 16:00:55 +00:00
|
|
|
/* draw up / down bars */
|
2004-03-14 22:34:33 +00:00
|
|
|
if (foo < 0) {
|
2004-03-15 19:32:27 +00:00
|
|
|
for (h = 0; h <= (-foo); h++) {
|
|
|
|
*loc = stateptr->colors[h];
|
2016-01-28 16:00:55 +00:00
|
|
|
loc += scope_width;
|
2004-03-15 19:32:27 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
} else {
|
2004-03-15 19:32:27 +00:00
|
|
|
for (h = 0; h <= foo; h++) {
|
|
|
|
*loc = stateptr->colors[h];
|
2016-01-28 16:00:55 +00:00
|
|
|
loc -= scope_width;
|
2004-03-15 19:32:27 +00:00
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-02 12:36:07 +00:00
|
|
|
|
2004-03-14 22:34:33 +00:00
|
|
|
/* Draw grid. */
|
2016-01-28 16:00:55 +00:00
|
|
|
{
|
|
|
|
guint32 gray = stateptr->colors[hh1];
|
|
|
|
|
|
|
|
for (i = 16; i < scope_height; i += 16) {
|
|
|
|
for (h = 0; h < scope_width; h += 2) {
|
|
|
|
stateptr->display[(i * scope_width) + h] = gray;
|
|
|
|
if (i == hh)
|
|
|
|
stateptr->display[(i * scope_width) + h + 1] = gray;
|
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
2016-01-28 16:00:55 +00:00
|
|
|
for (i = 16; i < scope_width; i += 16) {
|
|
|
|
for (h = 0; h < scope_height; h += 2) {
|
|
|
|
stateptr->display[i + (h * scope_width)] = gray;
|
|
|
|
}
|
2004-03-14 22:34:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return stateptr->display;
|
2002-03-02 12:36:07 +00:00
|
|
|
}
|