File: Neuromodules.cpp

package info (click to toggle)
supercollider-sc3-plugins 3.13.0~repack-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 20,104 kB
  • sloc: cpp: 303,352; lisp: 9,589; ansic: 3,547; sh: 96; makefile: 87; haskell: 21
file content (203 lines) | stat: -rw-r--r-- 4,594 bytes parent folder | download | duplicates (5)
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
/*
	SuperCollider real time audio synthesis system
 Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com
 
 
	Neuromodules
	by Frank Pasemann and Julian Rohrhuber
 
 
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 */

#include "SC_PlugIn.h"
#include <stdio.h>

#ifndef MAXFLOAT
# include <float.h>
# define MAXFLOAT FLT_MAX
#endif

static InterfaceTable *ft;

#define MAXCHANNELS 32


struct Dneuromodule : public Unit
{
	int m_size;
	
	double *m_theta;
	double *m_x;
	double *m_weights;
	double *m_x_temp;
};

extern "C"
{
	void Dneuromodule_Ctor(Dneuromodule *unit);
	void Dneuromodule_Dtor(Dneuromodule *unit);
	void Dneuromodule_reset(Dneuromodule *unit, int inNumSamples);
	void Dneuromodule_end(Dneuromodule *unit);
	void Dneuromodule_next(Dneuromodule *unit, int inNumSamples);
	void Neuromodule_initInputs(Dneuromodule *unit, int size);
}


void Neuromodule_initInputs(Dneuromodule *unit, int size)
{
	int memsize = size * sizeof(double);
	int numWeights = size * size;
	
	
	unit->m_theta = (double*)RTAlloc(unit->mWorld, memsize);
	memset(unit->m_theta, 0, memsize);
	
	unit->m_x = (double*)RTAlloc(unit->mWorld, memsize);
	memset(unit->m_x, 0, memsize);
	
	unit->m_weights = (double*)RTAlloc(unit->mWorld, numWeights);
	memset(unit->m_weights, 0, numWeights);
	
	unit->m_x_temp = (double*)RTAlloc(unit->mWorld, memsize);
	memset(unit->m_x_temp, 0, memsize);

	
}

void Dneuromodule_next(Dneuromodule *unit, int inNumSamples)
{
	
	
	if(inNumSamples) {
		
		int size = unit->m_size;
		int numWeights = size * size;
		
		
		// printf("theta: ");
		
		// THETA
		for(int i=0; i < size; i++) {
			double val = (double) DEMANDINPUT_A(i + 1, inNumSamples);
			if(sc_isnan(val)) { Dneuromodule_end(unit); return; }
			//printf("%f ", val);
			unit->m_theta[i] = val;
		}
		
		// printf("weights: ");
		
		// WEIGHTS
		for(int i=0; i < numWeights; i++) {
			double val = (double) DEMANDINPUT_A(i + 1 + size + size, inNumSamples);
			if(sc_isnan(val)) { Dneuromodule_end(unit); return; }
		// 	printf("%f ", val);
			unit->m_weights[i] = val;
		}
		
		
		// keep normalized old values in a temporary array
		// so that we can overwrite the original directly
		
		for(int i=0; i < size; i++) {
			unit->m_x_temp[i] = std::tanh(unit->m_x[i]);
		}
		
		// iterate over all other values for each values and calcuate their contribution by weights.
		
		// printf("outputs: \n");
		for(int i=0; i < size; i++) {
			double xval;
			xval = unit->m_theta[i];
			for(int j=0; j < size; j++) {
				// printf("x = %f + %f * %f\n", xval, unit->m_weights[i * size + j], unit->m_x_temp[j]);
				xval += unit->m_weights[i * size + j] * unit->m_x_temp[j];
			}
			xval = zapgremlins(xval);
			unit->m_x[i] = xval;
			// printf("-> %f ", xval);
			OUT0(i) = (float) xval;
		}
		
		//printf("\n");

		
	} else {
		
		Dneuromodule_reset(unit, inNumSamples);
		
	}
	
	
}

void Dneuromodule_reset(Dneuromodule *unit, int inNumSamples)
{
	int size = unit->m_size;
	int size_end = size * size + 2 * size + 1;
	
	for(int i =	1; i < size_end; i++) {
		RESETINPUT(i);
	}
	// initialize x
	for(int i=0; i < size; i++) {
		double val = (double) IN(i + 1 + size)[inNumSamples];
		if(sc_isnan(val)) { Dneuromodule_end(unit); return; }
		// printf("%f ", val);
		unit->m_x[i] = val;
		OUT0(i) = val;
	}
}

void Dneuromodule_end(Dneuromodule *unit)
{
	
	for(int i =	0; i < unit->m_size; i++) {
		OUT0(i) = NAN;
	}
	
}


void Dneuromodule_Ctor(Dneuromodule *unit)
{
	
	SETCALC(Dneuromodule_next);
	unit->m_size = sc_max((int) IN0(0), 0);
	Neuromodule_initInputs(unit, unit->m_size);
	
	Dneuromodule_reset(unit, 0);
}

void Dneuromodule_Dtor(Dneuromodule *unit)
{
	RTFree(unit->mWorld, unit->m_theta);
	RTFree(unit->mWorld, unit->m_x);
	RTFree(unit->mWorld, unit->m_weights);
	RTFree(unit->mWorld, unit->m_x_temp);
}


//////////////////////////////////////////////////////



PluginLoad(TagSystem)
{
	ft = inTable;
	DefineDtorUnit(Dneuromodule);
	
}