aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/source/Median.cpp
blob: 4383a1b74743a9895727f118bbafeb0aad3fd01d (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* sc4pd 
   Median, Median~

   Copyright (c) 2004 Tim Blechmann.                                       

   This code is derived from:
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com


   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.

   Based on:
     PureData by Miller Puckette and others.
         http://www.crca.ucsd.edu/~msp/software.html
     FLEXT by Thomas Grill
         http://www.parasitaere-kapazitaeten.net/ext
     SuperCollider by James McCartney
         http://www.audiosynth.com
     
   Coded while listening to: Morton Feldman: For John Cage
   
*/

#include "sc4pd.hpp"

/* ------------------------ Median(~) ---------------------------------*/


const int kMAXMEDIANSIZE = 32;

class median_shared
{
public:

    inline void Init(long size, float value);
    inline float Insert(float value);
    
private:
    float m_medianValue[kMAXMEDIANSIZE];
    long m_medianAge[kMAXMEDIANSIZE];
    long m_medianSize, m_medianIndex;
};

void median_shared::Init(long size, float value)
{
    // initialize the arrays with the first value
    m_medianSize = size;
    for (int i=0; i<size; ++i) {
	m_medianValue[i] = value;
	m_medianAge[i] = i;
    }
}

inline float median_shared::Insert(float value)
{
    long i, last, pos=-1;
    
    // keeps a sorted list of the previous n=size values
    // the oldest is removed and the newest is inserted.
    // values between the oldest and the newest are shifted over by one.
    
    // values and ages are both arrays that are 'size' long.
    // the median value is always values[size>>1]
    
    last = m_medianSize - 1;
    // find oldest bin and age the other bins.
    for (i=0; i<m_medianSize; ++i) {
	if (m_medianAge[i] == last) { // is it the oldest bin ?
	    pos = i;	
	} else {
	    m_medianAge[i]++;	// age the bin
	}
    }
    // move values to fill in place of the oldest and make a space for the newest
    // search lower if value is too small for the open space
    while (pos != 0 && value < m_medianValue[pos-1]) {
	m_medianValue[pos] = m_medianValue[pos-1];
	m_medianAge[pos] = m_medianAge[pos-1];
	pos--;
    }
    // search higher if value is too big for the open space
    while (pos != last && value > m_medianValue[pos+1]) {
	m_medianValue[pos] = m_medianValue[pos+1];
	m_medianAge[pos] = m_medianAge[pos+1];
	pos++;
    }
    m_medianValue[pos] = value;
    m_medianAge[pos] = 0;		// this is the newest bin, age = 0
    return m_medianValue[m_medianSize>>1];
}


/* ------------------------ Median~ -----------------------------------*/

class Median_ar
    :public sc4pd_dsp
{
    FLEXT_HEADER(Median_ar,sc4pd_dsp);
    
public:
    Median_ar(int argc, t_atom * argv);

protected:
    virtual void m_signal(int n, t_sample *const *in, t_sample *const *out);
    virtual void m_dsp (int n,t_signalvec const * insigs,
			t_signalvec const * outsigs);
    
    void m_set(float f)
    {
	m_size=(int)f;
	Median.Init(m_size,Median.Insert(0)); /* this is not beautiful, but i'm not
						 aware of a nicer way */
    }

private:
    FLEXT_CALLBACK_F(m_set);

    median_shared Median; //median
    int m_size;           //median size
};

FLEXT_LIB_DSP_V("Median~",Median_ar);

Median_ar::Median_ar(int argc,t_atom * argv)
{
    FLEXT_ADDMETHOD_(0,"set",m_set);

    AtomList Args(argc,argv);
    m_size=sc_getfloatarg(Args,0);
    
    AddOutSignal();
}

void Median_ar::m_dsp(int n,t_signalvec const * insigs,
		      t_signalvec const * outsigs)
{
    Median.Init(m_size,0); //how is this done in SuperCollider?
}

void Median_ar::m_signal(int n, t_sample *const *in, t_sample *const *out)
{
    t_sample *nout = *out;
    t_sample *nin = *in;
    
    for (int i = 0; i!= n;++i)
    {
	(*(nout)++) = Median.Insert(*(nin)++);
    }
}


/* ------------------------ Median ------------------------------------*/

class Median_kr
    :public sc4pd_dsp
{
    FLEXT_HEADER(Median_kr,sc4pd_dsp);
    
public:
    Median_kr(int argc, t_atom * argv);

protected:
    void m_set(float f)
    {
	m_size=(int)f;
	Median.Init(m_size,Median.Insert(0)); /* this is not beautiful, but i'm not
						 aware of a nicer way */
    }
    void m_perform(float f);

private:
    FLEXT_CALLBACK_F(m_set);
    FLEXT_CALLBACK_F(m_perform);

    median_shared Median; //median
    int m_size;           //median size
};

FLEXT_LIB_V("Median",Median_kr);

Median_kr::Median_kr(int argc,t_atom * argv)
{
    FLEXT_ADDMETHOD_(0,"set",m_set);
    FLEXT_ADDMETHOD(0,m_perform);


    AtomList Args(argc,argv);
    m_size=(int)sc_getfloatarg(Args,0);

    float m_set=sc_getfloatarg(Args,1);

    Median.Init(m_size,m_set);
    AddOutFloat();
}

void Median_kr::m_perform(float f)
{
    ToOutFloat(0,Median.Insert(f));
}