File: plotter.c

package info (click to toggle)
libvisual-plugins 1%3A0.4.0%2Bdfsg1-13
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,644 kB
  • sloc: ansic: 9,358; sh: 8,809; cpp: 871; makefile: 221; sed: 16
file content (140 lines) | stat: -rw-r--r-- 3,233 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* 
 *  Jakdaw's XMMS Plugin.
 *  Copyright (C) 1999, 2000, Christopher Wilson.
 *
 *  Email: <Jakdaw@usa.net> 
 *  Project Homepage: <http://www.jakdaw.ucam.org/xmms/>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 *  USA
 */
#include <stdlib.h>
#include <math.h>

#include "plotter.h"

static void vert_line(JakdawPrivate *priv, int x, int a, int b, uint32_t col, uint32_t *vscr);
static int no_of_shifts(int val);
static int p2(int val);

void _jakdaw_plotter_draw(JakdawPrivate *priv, float *pcm_data, float *freq_data, uint32_t *vscr)
{
	int x, y;
	int oy;
	uint32_t col;

	switch (priv->plotter_colortype)
	{
		case PLOTTER_COLOUR_SOLID: col=priv->plotter_scopecolor; break;
		case PLOTTER_COLOUR_RANDOM: col=visual_random_context_int (priv->rcontext); break;
		case PLOTTER_COLOUR_MUSICTRIG:
		default:
					    {
						    float d;
						    int c;
						    d=0;
						    for(c=0;c<16;c++)
							    d=d+freq_data[c];
						    col=(int)((double) d*(256.0 * 16.0));

						    d=0;
						    for(c=16;c<108;c++)
							    d=d+freq_data[c];
						    col|=((int)((double) d*(256.0 * 64.0)))<<8;

						    d=0;
						    for(c=108;c<255;c++)
							    d=d+freq_data[c];
						    col|=((int)((double) d*(256.0 * 128.0)))<<16;

					    }
	}

	oy = (priv->yres / 2) + ((pcm_data[0] * priv->plotter_amplitude) * (priv->yres / 2));

	oy=oy<0 ? 0 : oy>=priv->yres ? priv->yres -1 : oy;

	for(x=0;x<priv->xres;x++)
	{
		y = (priv->yres / 2) + ((pcm_data[x % 512] * priv->plotter_amplitude) * (priv->yres / 2));

		if(y<0) y=0;
		if(y>=priv->yres) y=priv->yres-1;
		if(priv->plotter_scopetype==PLOTTER_SCOPE_LINES)
		{
			vert_line(priv, x, oy, y, col, vscr);
			oy=y;
		}
		else if(priv->plotter_scopetype==PLOTTER_SCOPE_DOTS)
		{
			if(x>0 && x<priv->xres && y>0 && y<priv->yres)
				vscr[(y*priv->xres)+x]=col;
		}
		else if(priv->plotter_scopetype==PLOTTER_SCOPE_SOLID)
		{
			vert_line(priv, x, (priv->yres>>1), y, col, vscr);
		}
	}
}

static void vert_line(JakdawPrivate *priv, int x,int a, int b, uint32_t col, uint32_t *vscr)
{
	int y, ptr;

	if(b<a)
	{
		y=a; a=b; b=y;
	}

	if(a<0 || a>=priv->yres || b<0 || b>=priv->yres)
		return;

	ptr=(a*priv->xres)+x;

	for(y=a;y<=b;y++)
	{
		vscr[ptr]=col;
		ptr+=priv->xres;
	}
}

// Calculate maximum no of right shifts that can be applied to a sample so
// that val can still be represented.

static int no_of_shifts(int val)
{

	int a=1, b=0;

	while(a<val)
	{
		b++;
		a*=2;
	}

	return 16-b;
}

// Calc a power of 2

static int p2(int val)
{
	int a=1;

	for(;val>0;val--)
		a*=2;

	return a;
}