Debian Codegraph @

embedded/juceplugin/PyoClass.h

raw
1 /**************************************************************************
2 * Copyright 2018 Olivier Belanger *
3 * *
4 * This file is part of pyo-plug, an audio plugin using the python *
5 * module pyo to create the dsp. *
6 * *
7 * pyo-plug is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU Lesser General Public License as *
9 * published by the Free Software Foundation, either version 3 of the *
10 * License, or (at your option) any later version. *
11 * *
12 * pyo-plug is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU Lesser General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU LGPL along with pyo-plug. *
18 * If not, see <http://www.gnu.org/licenses/>. *
19 * *
20 *************************************************************************/
21 #ifndef PYOCLASS_H_INCLUDED
22 #define PYOCLASS_H_INCLUDED
23
24 #include "m_pyo.h"
25 #include "../JuceLibraryCode/JuceHeader.h"
26
27 typedef int callPtr(int);
28
29 class Pyo {
30 public:
31 Pyo();
32
33 /*
34 ** Terminates this object's interpreter.
35 */
36 ~Pyo();
37
38 /*
39 ** Creates a python interpreter and initialize a pyo server inside it.
40 ** This function must be called, once per Pyo object, before any other
41 ** calls.
42 **
43 ** arguments:
44 ** nChannels : int, number of in/out channels.
45 ** bufferSize : int, number of samples per buffer.
46 ** sampleRate : int, sample rate frequency.
47 **
48 ** All arguments should be equal to the host audio settings.
49 */
50 void setup(int nChannels, int bufferSize, int sampleRate);
51
52 /*
53 ** This function can be used to pass the DAW's bpm value to the
54 ** python interpreter. Changes the value of the BPM variable
55 ** (which defaults to 60).
56 **
57 ** arguments:
58 ** bpm : double, new BPM.
59 */
60 void setbpm(double bpm);
61
62 /*
63 ** This function fills pyo's input buffers with new samples, tells pyo to
64 ** process a buffer of samples and fills the host's output buffer with new
65 ** samples. Should be called once per process block, inside the host's
66 ** processBlock function.
67 **
68 ** arguments:
69 ** buffer : AudioBuffer<float>&, Juce's audio buffer object.
70 */
71 void process(AudioSampleBuffer& buffer);
72
73 /*
74 ** Execute a python script "file" in the objectès thread's interpreter.
75 ** An integer "add" is needed to indicate if the pyo server should be
76 ** reboot or not.
77 **
78 ** arguments:
79 ** file : const char * or const String &,
80 ** filename to execute as a python script. The file is first
81 ** searched in the current working directory. If not found,
82 ** the module will try to open it as an absolute path.
83 ** add, int, if positive, the commands in the file will be added to whatever
84 ** is already running in the pyo server. If 0, the server will be
85 ** cleared before executing the file.
86 **
87 ** returns 0 (no error), 1 (failed to open the file) or 2 (bad code in file).
88 */
89 int loadfile(const char *file, int add);
90 int loadfile(const String &file, int add);
91
92 /*
93 ** Executes any raw valid python statement. With this function, one can
94 ** dynamically creates and manipulates audio objects and algorithms.
95 **
96 ** arguments:
97 ** msg : const char * or const String &
98 ** pointer to a string containing the statement to execute.
99 **
100 ** returns 0 (no error) or 1 (bad code in file).
101 **
102 ** Example (for a Pyo object named `pyo`):
103 **
104 ** pyo.exec("pits = [0.001, 0.002, 0.003, 0.004]")
105 ** pyo.exec("fr = Rossler(pitch=pits, chaos=0.9, mul=250, add=500)")
106 ** pyo.exec("b = SumOsc(freq=fr, ratio=0.499, index=0.4, mul=0.2).out()")
107 */
108 int exec(const char *msg);
109 int exec(const String &msg);
110
111 /*
112 ** Sends a numerical value to an existing Sig or SigTo object.
113 **
114 ** arguments:
115 ** name : const char * or const String &,
116 ** variable name of the object.
117 ** value : float, value to be assigned.
118 **
119 ** Example:
120 **
121 ** inside the script file:
122 **
123 ** freq = SigTo(value=440, time=0.1, init=440)
124 **
125 ** Inside Juce (for a Pyo object named `pyo`):
126 **
127 ** pyo.value("freq", 880);
128 */
129 int value(const char *name, float value);
130 int value(const String &name, float value);
131
132 /*
133 ** Sends an array of numerical values to an existing Sig or SigTo object.
134 **
135 ** arguments:
136 ** name : const char * or const String &,
137 ** variable name of the object.
138 ** value : float *, array of floats.
139 ** len : int, number of elements in the array.
140 **
141 ** Example:
142 **
143 ** inside the script file:
144 **
145 ** freq = SigTo(value=[100,200,300,400], time=0.1, init=[100,200,300,400])
146 **
147 ** Inside Juce (for a Pyo object named `pyo`):
148 **
149 ** float frequencies[4] = {150, 250, 350, 450};
150 ** pyo.value("freq", frequencies, 4);
151 */
152 int value(const char *name, float *value, int len);
153 int value(const String &name, float *value, int len);
154
155 /*
156 ** Sends a numerical value to a Pyo object's attribute.
157 **
158 ** arguments:
159 ** name : const char * or const String &,
160 ** object name and attribute separated by a dot.
161 ** value : float, value to be assigned.
162 **
163 ** Example:
164 **
165 ** inside the script file:
166 **
167 ** filter = Biquad(input=Noise(0.5), freq=1000, q=4, type=2)
168 **
169 ** Inside Juce (for a Pyo object named `pyo`):
170 **
171 ** pyo.set("filter.freq", 2000);
172 */
173 int set(const char *name, float value);
174 int set(const String &name, float value);
175
176 /*
177 ** Sends an array of numerical values to a Pyo object's attribute.
178 **
179 ** arguments:
180 ** name : const char * or const String &,
181 ** object name and attribute separated by a dot.
182 ** value : float *, array of floats.
183 ** len : int, number of elements in the array.
184 **
185 ** Example:
186 **
187 ** inside the script file:
188 **
189 ** filters = Biquad(input=Noise(0.5), freq=[250, 500, 1000, 2000], q=5, type=2)
190 **
191 ** Inside Juce (for a Pyo object named `pyo`):
192 **
193 ** float frequencies[4] = {350, 700, 1400, 2800};
194 ** pyo.set("filters.freq", frequencies, 4);
195 */
196 int set(const char *name, float *value, int len);
197 int set(const String &name, float *value, int len);
198
199 /*
200 ** Sends a MIDI messges to the Server.
201 **
202 ** Example (for a Pyo object named `pyo`):
203 **
204 ** pyo.midi(144, 60, 127) //Send a Note on message on channel 1 for note # 60
205 ** pyo.midi(128, 60, 0) //Send a Note off messge on channel 1 for note # 60
206 */
207 void midi(int status, int data1, int data2);
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 */
214 void clear();
215
216 private:
217 int nChannels;
218 int bufferSize;
219 int sampleRate;
220 PyThreadState *interpreter;
221 float *pyoInBuffer;
222 float *pyoOutBuffer;
223 callPtr *pyoCallback;
224 int pyoId;
225 char pyoMsg[262144];
226 };
227
228 #endif // PYOCLASS_H_INCLUDED