File: logistic.cpp

package info (click to toggle)
cmt 1.16-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 548 kB
  • sloc: cpp: 6,852; makefile: 95
file content (156 lines) | stat: -rw-r--r-- 4,041 bytes parent folder | download | duplicates (11)
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
/* logistic.cpp

   A sample-and-hold logistic map control generator

   (c) 2002 Nathaniel Virgo

   Part of the Computer Music Toolkit - a library of LADSPA plugins. 
   The Computer Music Toolkit is Copyright (C) 2000-2002 
   Richard W.E. Furse. The author may be contacted at
   richard@muse.demon.co.uk.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public Licence as
   published by the Free Software Foundation; either version 2 of the
   Licence, 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
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307, USA. */

/*****************************************************************************/

#include <stdlib.h>

/*****************************************************************************/

#include "cmt.h"

#include "pinknoise.h"
#include "utils.h"

/*****************************************************************************/

namespace logistic {

    enum {
	port_r           = 0,
	port_frequency   = 1,
	port_output      = 2,
	n_ports          = 3
    };
    
    /** This plugin uses the logistic map to generate periodic or
	chaotic control signals. */
    class Plugin : public CMT_PluginInstance {
    private:
	LADSPA_Data sample_rate;
	LADSPA_Data x;
	unsigned counter;
    public:
	
        Plugin(const LADSPA_Descriptor *,
	       unsigned long s_rate) : 
	    CMT_PluginInstance(n_ports), 
	    sample_rate(s_rate) {
	}
	
	~Plugin() {
	}
	
	friend void activate(LADSPA_Handle instance);
	
	friend void run(LADSPA_Handle instance,
			unsigned long sample_count);	
    };
    
    void activate(LADSPA_Handle instance) {
	Plugin *pp = (Plugin *) instance;
	Plugin &p  = *pp;
	
	p.x = 0.3; // arbitrary non-zero value.
    }

    void run(LADSPA_Handle instance,
	     unsigned long sample_count) {

	Plugin *pp = (Plugin *) instance;
	Plugin &p  = *pp;

	LADSPA_Data   r         = *pp->m_ppfPorts[port_r];
	LADSPA_Data   frequency = *pp->m_ppfPorts[port_frequency];
	LADSPA_Data * out       =  pp->m_ppfPorts[port_output];

	frequency = BOUNDED_ABOVE(frequency,p.sample_rate);
	r = BOUNDED_ABOVE(r,4);
	unsigned remain = sample_count;

	if (frequency > 0) {
	    while (remain) {
		unsigned jump_samples = (remain<p.counter) ? remain : p.counter;
		for (unsigned j=0; j<jump_samples; ++j) {
		    *(out++) = p.x*2-1;
		}
		p.counter -= jump_samples;
		remain -= jump_samples;
		if (p.counter==0) {
		    p.x = r*p.x*(1.0f - p.x);
		    p.counter = (unsigned)(p.sample_rate/frequency);
		}
	    }
	}
	else
	{
	    for (unsigned long i=0; i<sample_count; ++i)
		*(out++) = p.x;
	}
    }

    void initialise() {
	CMT_Descriptor * d = new CMT_Descriptor
	    (1849,
	     "logistic",
	     0,
	     "Logistic Map Control Generator",
	     CMT_MAKER("Nathaniel Virgo"),
	     CMT_COPYRIGHT("2002", "Nathaniel Virgo"),
	     NULL,
	     CMT_Instantiate<Plugin>,
	     activate,
	     run,
	     NULL,
	     NULL,
	     NULL);
	d->addPort
	    (LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
	     "\"r\" parameter",
	     (LADSPA_HINT_BOUNDED_BELOW
	      | LADSPA_HINT_BOUNDED_ABOVE
	      | LADSPA_HINT_DEFAULT_MAXIMUM),
	     2.9, 3.9999);
	d->addPort
	    (LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL,
	     "Step frequency",
	     (LADSPA_HINT_BOUNDED_BELOW
	      | LADSPA_HINT_BOUNDED_ABOVE
	      | LADSPA_HINT_SAMPLE_RATE
	      | LADSPA_HINT_DEFAULT_MIDDLE),
	     0, 0.001);
	d->addPort
	    (LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO,
	     "Output");
	registerNewPluginDescriptor(d);
    }

} // end of namespace

/*****************************************************************************/

/* EOF */