summaryrefslogtreecommitdiffstats
path: root/flow/stereofftscope_impl.cpp
blob: 1b280a526861438e3d91f15a195a77a4b1f33dbb (plain)
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
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       [email protected]

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library 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
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    */

#include "artsflow.h"
#include "fft.h"
#include "stdsynthmodule.h"
#include <math.h>
#include <iostream>

using namespace std;
using namespace Arts;

namespace Arts {

class StereoFFTScope_impl : public StereoFFTScope_skel, public StdSynthModule {
protected:
	static const unsigned long SAMPLES = 4096;
	vector<float> _scope;
	/*
	 * some gcc versions expose ugly behaviour with virtual inheritance:
	 * putting window[4096] & inbuffer[4096] here bloats the vtable then,
	 * and tells you to recompile with -fhuge-objects ... so allocate them
	 * dynamically
	 */
	float *window;
	float *inbuffer;
	unsigned long inbufferpos;
public:
	void do_fft()
	{
		float out_real[SAMPLES],out_img[SAMPLES];
		arts_fft_float(SAMPLES,0,inbuffer,0,out_real,out_img);

		_scope.clear();
    	unsigned int i = 3;
    	unsigned int j = 0;
    	for(;;) {
        	float xrange = 0.0;
        	while(j != i)
        	{
            	xrange += (fabs(out_img[j]) + fabs(out_real[j]))/(float)SAMPLES;
            	j++;
        	}
			_scope.push_back(xrange);

        	if(i == SAMPLES/2) return;

            i += i/2;
            if(i > SAMPLES/2) i = SAMPLES/2;
    	}
	}
	void streamInit()
	{
		unsigned long i;
		for(i=0;i<SAMPLES;i++)
		{
			float x = (float)i/(float)SAMPLES; 
			window[i] = sin(x*M_PI)*sin(x*M_PI);
			inbuffer[i] = 0;
		}
		do_fft();	// initialize so that we never return an empty scope
	}
	void streamStart()
	{
		inbufferpos = 0;
	}
	vector<float> *scope()
	{
		return new vector<float>(_scope);
	}
	/*
	  in audio stream inleft, inright;
	  out audio stream outleft, outright;
	*/
	void calculateBlock(unsigned long samples)
	{
		unsigned long i;
		for(i=0;i<samples;i++)
		{
			inbuffer[inbufferpos] =
				(inleft[i] + inright[i])*window[inbufferpos];
			if(++inbufferpos == SAMPLES)
			{
				do_fft();
				inbufferpos = 0;
			}
			/*
			   monitoring only tasks can't be done with that StereoEffect
			   interface nicely - copy input to output until there is
			   something better
			 */
			outleft[i] = inleft[i];
			outright[i] = inright[i];
		}
	}

	/* prevent vtable overflows (see above) */
	StereoFFTScope_impl() {
		window = new float[SAMPLES];
		inbuffer = new float[SAMPLES];
	}
	~StereoFFTScope_impl() {
		delete [] window;
		delete [] inbuffer;
	}
};

const unsigned long StereoFFTScope_impl::SAMPLES;

REGISTER_IMPLEMENTATION(StereoFFTScope_impl);

}