Debian Codegraph @

embedded/openframeworks/PyoClass.cpp

raw
1 #include "PyoClass.h"
2
3 /*
4 ** Creates a python interpreter and initialize a pyo server inside it.
5 ** This function must be called, once per Pyo object, before any other
6 ** calls.
7 **
8 ** arguments:
9 ** nChannels : int, number of in/out channels.
10 ** bufferSize : int, number of samples per buffer.
11 ** sampleRate : int, sample rate frequency.
12 **
13 ** All arguments should be equal to the host audio settings.
14 */
15 void Pyo::setup(int _nChannels, int _bufferSize, int _sampleRate) {
16 nChannels = _nChannels;
17 bufferSize = _bufferSize;
18 sampleRate = _sampleRate;
19 interpreter = pyo_new_interpreter(sampleRate, bufferSize, nChannels);
20 pyoInBuffer = reinterpret_cast<float*>(pyo_get_input_buffer_address(interpreter));
21 pyoOutBuffer = reinterpret_cast<float*>(pyo_get_output_buffer_address(interpreter));
22 pyoCallback = reinterpret_cast<callPtr*>(pyo_get_embedded_callback_address(interpreter));
23 pyoId = pyo_get_server_id(interpreter);
24 }
25
26 /*
27 ** Terminates this object's interpreter.
28 */
29 Pyo::~Pyo() {
30 pyo_end_interpreter(interpreter);
31 }
32
33 /*
34 ** This function fills pyo's input buffers with new samples. Should be called
35 ** once per process block, inside the host's audioIn function.
36 **
37 ** arguments:
38 ** *buffer : float *, float pointer pointing to the host's input buffers.
39 */
40 void Pyo::fillin(float *buffer) {
41 for (int i=0; i<(bufferSize*nChannels); i++) pyoInBuffer[i] = buffer[i];
42 }
43
44
45 /*
46 ** This function tells pyo to process a buffer of samples and fills the host's
47 ** output buffer with new samples. Should be called once per process block,
48 ** inside the host's audioOut function.
49 **
50 ** arguments:
51 ** *buffer : float *, float pointer pointing to the host's output buffers.
52 */
53 void Pyo::process(float *buffer) {
54 pyoCallback(pyoId);
55 for (int i=0; i<(bufferSize*nChannels); i++) buffer[i] = pyoOutBuffer[i];
56 }
57
58 /*
59 ** Execute a python script "file" in the objectès thread's interpreter.
60 ** An integer "add" is needed to indicate if the pyo server should be
61 ** reboot or not.
62 **
63 ** arguments:
64 ** file : char *, filename to execute as a python script. The file is first
65 ** searched in the current working directory. If not found,
66 ** the module will try to open it as an absolute path.
67 ** add, int, if positive, the commands in the file will be added to whatever
68 ** is already running in the pyo server. If 0, the server will be
69 ** cleared before executing the file.
70 **
71 ** returns 0 (no error), 1 (failed to open the file) or 2 (bad code in file).
72 */
73 int Pyo::loadfile(const char *file, int add) {
74 return pyo_exec_file(interpreter, file, pyoMsg, add);
75 }
76
77 /*
78 ** Sends a numerical value to an existing Sig or SigTo object.
79 **
80 ** arguments:
81 ** name : const char *, variable name of the object.
82 ** value : float, value to be assigned.
83 **
84 ** returns 0 (no error) or 1 (bad code in file).
85 **
86 ** Example:
87 **
88 ** inside the script file:
89 **
90 ** freq = SigTo(value=440, time=0.1, init=440)
91 **
92 ** Inside OpenFrameworks (for a Pyo object named `pyo`):
93 **
94 ** pyo.value("freq", 880);
95 */
96 int Pyo::value(const char *name, float value) {
97 sprintf(pyoMsg, "%s.value=%f", name, value);
98 return pyo_exec_statement(interpreter, pyoMsg, 0);
99 }
100
101 /*
102 ** Sends an array of numerical values to an existing Sig or SigTo object.
103 **
104 ** arguments:
105 ** name : const char *, variable name of the object.
106 ** value : float *, array of floats.
107 ** len : int, number of elements in the array.
108 **
109 ** returns 0 (no error) or 1 (bad code in file).
110 **
111 ** Example:
112 **
113 ** inside the script file:
114 **
115 ** freq = SigTo(value=[100,200,300,400], time=0.1, init=[100,200,300,400])
116 **
117 ** Inside OpenFrameworks (for a Pyo object named `pyo`):
118 **
119 ** float frequencies[4] = {150, 250, 350, 450};
120 ** pyo.value("freq", frequencies, 4);
121 */
122 int Pyo::value(const char *name, float *value, int len) {
123 char fchar[32];
124 sprintf(pyoMsg, "%s.value=[", name);
125 for (int i=0; i<len; i++) {
126 sprintf(fchar, "%f,", value[i]);
127 strcat(pyoMsg, fchar);
128 }
129 strcat(pyoMsg, "]");
130 return pyo_exec_statement(interpreter, pyoMsg, 0);
131 }
132
133 /*
134 ** Sends a numerical value to a Pyo object's attribute.
135 **
136 ** arguments:
137 ** name : const char *, object name and attribute separated by a dot.
138 ** value : float, value to be assigned.
139 **
140 ** returns 0 (no error) or 1 (bad code in file).
141 **
142 ** Example:
143 **
144 ** inside the script file:
145 **
146 ** filter = Biquad(input=Noise(0.5), freq=1000, q=4, type=2)
147 **
148 ** Inside OpenFrameworks (for a Pyo object named `pyo`):
149 **
150 ** pyo.set("filter.freq", 2000);
151 */
152 int Pyo::set(const char *name, float value) {
153 sprintf(pyoMsg, "%s=%f", name, value);
154 return pyo_exec_statement(interpreter, pyoMsg, 0);
155 }
156
157 /*
158 ** Sends an array of numerical values to a Pyo object's attribute.
159 **
160 ** arguments:
161 ** name : const char *, object name and attribute separated by a dot.
162 ** value : float *, array of floats.
163 ** len : int, number of elements in the array.
164 **
165 ** returns 0 (no error) or 1 (bad code in file).
166 **
167 ** Example:
168 **
169 ** inside the script file:
170 **
171 ** filters = Biquad(input=Noise(0.5), freq=[250, 500, 1000, 2000], q=5, type=2)
172 **
173 ** Inside OpenFrameworks (for a Pyo object named `pyo`):
174 **
175 ** float frequencies[4] = {350, 700, 1400, 2800};
176 ** pyo.set("filters.freq", frequencies, 4);
177 */
178 int Pyo::set(const char *name, float *value, int len) {
179 char fchar[32];
180 sprintf(pyoMsg, "%s=[", name);
181 for (int i=0; i<len; i++) {
182 sprintf(fchar, "%f,", value[i]);
183 strcat(pyoMsg, fchar);
184 }
185 strcat(pyoMsg, "]");
186 return pyo_exec_statement(interpreter, pyoMsg, 0);
187 }
188
189 /*
190 ** Executes any raw valid python statement. With this function, one can dynamically
191 ** creates and manipulates audio objects and algorithms.
192 **
193 ** arguments:
194 ** msg : const char *, pointer to a string containing the statement to execute.
195 **
196 ** returns 0 (no error) or 1 (bad code in file).
197 **
198 ** Example (for a Pyo object named `pyo`):
199 **
200 ** pyo.exec("pits = [0.001, 0.002, 0.003, 0.004]")
201 ** pyo.exec("fr = Rossler(pitch=pits, chaos=0.9, mul=250, add=500)")
202 ** pyo.exec("b = SumOsc(freq=fr, ratio=0.499, index=0.4, mul=0.2).out()")
203 */
204 int Pyo::exec(const char *_msg) {
205 strcpy(pyoMsg, _msg);
206 return pyo_exec_statement(interpreter, pyoMsg, 0);
207 }
208
209 /*
210 ** Shutdown and reboot the pyo server while keeping current in/out buffers.
211 ** This will erase audio objects currently active within the server.
212 **
213 */void Pyo::clear() {
214 pyo_server_reboot(interpreter);
215 }