aboutsummaryrefslogtreecommitdiff
path: root/externals/grill/fftease/src/dentist~.cpp
blob: 99d111bb6d90824c77172df5f9169786e0aeb237 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*

FFTease - A set of Live Spectral Processors
Originally written by Eric Lyon and Christopher Penrose for the Max/MSP platform

Copyright (c)Thomas Grill (xovo@gmx.net)
For information on usage and redistribution, and for a DISCLAIMER OF ALL
WARRANTIES, see the file, "license.txt," in this distribution.  

*/

#include "main.h"
#include <stdlib.h>

class dentist:
	public fftease
{
	FLEXT_HEADER_S(dentist,fftease,setup)
	
public:
	dentist(I argc,const t_atom *argv);

protected:

	virtual V Transform(I _N2,S *const *in);

	BL *_bin_selection;
	I _teeth;  
	F _knee;
	I _max_bin; // determined by _knee and fundamental frequency

	V reset_shuffle();

private:

	virtual V Set();
	virtual V Clear();
	virtual V Delete();

	V ms_knee(F knee);
	V ms_teeth(I teeth) { _teeth = teeth;	reset_shuffle(); }

	
	static V setup(t_classid c);

	FLEXT_CALLBACK(reset_shuffle)
	FLEXT_ATTRGET_F(_knee)
	FLEXT_CALLSET_F(ms_knee)
	FLEXT_ATTRGET_I(_teeth)
	FLEXT_CALLSET_I(ms_teeth)
};

FLEXT_LIB_DSP_V("dentist~",dentist)


V dentist::setup(t_classid c)
{
	FLEXT_CADDBANG(c,0,reset_shuffle);

	FLEXT_CADDATTR_VAR(c,"knee",_knee,ms_knee);
	FLEXT_CADDATTR_VAR(c,"teeth",_teeth,ms_teeth);
}


dentist::dentist(I argc,const t_atom *argv):
	fftease(4,F_BALANCED|F_BITSHUFFLE),	
	_teeth(10),_knee(500),_max_bin(0)
{
	/* parse and set object's options given */
	if(argc >= 1) {
		if(CanbeFloat(argv[0]))
			_knee = GetAFloat(argv[0]);
		else
			post("%s - Knee must be a float value - set to %0f",thisName(),_knee);
	}
	if(argc >= 2) {
		if(CanbeInt(argv[1]))
			_teeth = GetAInt(argv[1]);
		else
			post("%s - Teeth must be an integer value - set to %0i",thisName(),_teeth);
	}

	AddInSignal("Messages and input signal");
	AddOutSignal("Transformed signal");
}

V dentist::Clear()
{
	_bin_selection = NULL;
	fftease::Clear();
}

V dentist::Delete() 
{
	fftease::Delete();
	if(_bin_selection) delete[] _bin_selection;
}


V dentist::ms_knee(F f)
{
	_knee = f;	// store original

	const F funda = get_Fund();

	// TG: This is a different, but steady correction than in original fftease
	if( f < funda ) f = funda;
	else if(f > Samplerate()/2) f = Samplerate()/2;

	_max_bin = (I)(f/funda+0.5);

	reset_shuffle();
}


V dentist::Set()
{
	fftease::Set();
	
	_bin_selection = new BL[get_N()/2];

	// calculation of _max_bin
	ms_knee(_knee); 
}

V dentist::Transform(I _N,S *const *)
{
	const BL *bs = _bin_selection;
	for(I i = 0; i < _N ; i += 2)
		if(!*(bs++)) _channel1[i] = 0;
}


V dentist::reset_shuffle()
{
	const I _N2 = get_N()/2;
	I t = _teeth;

	// check number of teeth
	if( t < 0 ) t = 0;
	else if( t > _N2 ) t = _N2;

	// clear and set random bins
	I i;
	for( i = 0; i < _N2; i++ )
		_bin_selection[i] = false;
	for( i = 0; i < t; i++ )
		_bin_selection[rand()%_max_bin] = true;
}