File: PyoClass.h

package info (click to toggle)
python-pyo 1.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,332 kB
  • sloc: python: 135,133; ansic: 127,822; javascript: 16,116; sh: 395; makefile: 388; cpp: 242
file content (228 lines) | stat: -rw-r--r-- 8,690 bytes parent folder | download | duplicates (4)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**************************************************************************
 * Copyright 2018 Olivier Belanger                                        *
 *                                                                        *
 * This file is part of pyo-plug, an audio plugin using the python        *
 * module pyo to create the dsp.                                          *
 *                                                                        *
 * pyo-plug is free software: you can redistribute it and/or modify       *
 * it under the terms of the GNU Lesser General Public License as         *
 * published by the Free Software Foundation, either version 3 of the     *
 * License, or (at your option) any later version.                        *
 *                                                                        *
 * pyo-plug 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 Lesser General Public License for more details.                    *
 *                                                                        *
 * You should have received a copy of the GNU LGPL along with pyo-plug.   *
 * If not, see <http://www.gnu.org/licenses/>.                            *
 *                                                                        *
 *************************************************************************/
#ifndef PYOCLASS_H_INCLUDED
#define PYOCLASS_H_INCLUDED

#include "m_pyo.h"
#include "../JuceLibraryCode/JuceHeader.h"

typedef int callPtr(int);

class Pyo {
    public:
        Pyo();

        /*
        ** Terminates this object's interpreter.
        */
        ~Pyo();

        /*
        ** Creates a python interpreter and initialize a pyo server inside it.
        ** This function must be called, once per Pyo object, before any other
        ** calls.
        **
        ** arguments:
        **   nChannels : int, number of in/out channels.
        **   bufferSize : int, number of samples per buffer.
        **   sampleRate : int, sample rate frequency.
        **
        ** All arguments should be equal to the host audio settings.
        */
        void setup(int nChannels, int bufferSize, int sampleRate);

        /*
        ** This function can be used to pass the DAW's bpm value to the 
        ** python interpreter. Changes the value of the BPM variable 
        ** (which defaults to 60).
        **
        ** arguments:
        **   bpm : double, new BPM.
        */
        void setbpm(double bpm);

        /*
        ** This function fills pyo's input buffers with new samples, tells pyo to
        ** process a buffer of samples and fills the host's output buffer with new
        ** samples. Should be called once per process block, inside the host's
        ** processBlock function.
        **
        ** arguments:
        **   buffer : AudioBuffer<float>&, Juce's audio buffer object.
        */
        void process(AudioSampleBuffer& buffer);

        /*
        ** Execute a python script "file" in the objectès thread's interpreter.
        ** An integer "add" is needed to indicate if the pyo server should be
        ** reboot or not.
        **
        ** arguments:
        **   file : const char * or const String &,
        **             filename to execute as a python script. The file is first
        **             searched in the current working directory. If not found,
        **             the module will try to open it as an absolute path.
        **   add, int, if positive, the commands in the file will be added to whatever
        **             is already running in the pyo server. If 0, the server will be
        **             cleared before executing the file.
        **
        ** returns 0 (no error), 1 (failed to open the file) or 2 (bad code in file).
        */
        int loadfile(const char *file, int add);
        int loadfile(const String &file, int add);

        /*
        ** Executes any raw valid python statement. With this function, one can
        ** dynamically creates and manipulates audio objects and algorithms.
        **
        ** arguments:
        **   msg : const char * or const String &
        **         pointer to a string containing the statement to execute.
        **
        ** returns 0 (no error) or 1 (bad code in file).
        **
        ** Example (for a Pyo object named `pyo`):
        **
        ** pyo.exec("pits = [0.001, 0.002, 0.003, 0.004]")
        ** pyo.exec("fr = Rossler(pitch=pits, chaos=0.9, mul=250, add=500)")
        ** pyo.exec("b = SumOsc(freq=fr, ratio=0.499, index=0.4, mul=0.2).out()")
        */
        int exec(const char *msg);
        int exec(const String &msg);

        /*
        ** Sends a numerical value to an existing Sig or SigTo object.
        **
        ** arguments:
        **   name : const char * or const String &,
        **          variable name of the object.
        **   value : float, value to be assigned.
        **
        ** Example:
        **
        ** inside the script file:
        **
        ** freq = SigTo(value=440, time=0.1, init=440)
        **
        ** Inside Juce (for a Pyo object named `pyo`):
        **
        ** pyo.value("freq", 880);
        */
        int value(const char *name, float value);
        int value(const String &name, float value);

        /*
        ** Sends an array of numerical values to an existing Sig or SigTo object.
        **
        ** arguments:
        **   name : const char * or const String &,
        **          variable name of the object.
        **   value : float *, array of floats.
        **   len : int, number of elements in the array.
        **
        ** Example:
        **
        ** inside the script file:
        **
        ** freq = SigTo(value=[100,200,300,400], time=0.1, init=[100,200,300,400])
        **
        ** Inside Juce (for a Pyo object named `pyo`):
        **
        ** float frequencies[4] = {150, 250, 350, 450};
        ** pyo.value("freq", frequencies, 4);
        */
        int value(const char *name, float *value, int len);
        int value(const String &name, float *value, int len);

        /*
        ** Sends a numerical value to a Pyo object's attribute.
        **
        ** arguments:
        **   name : const char * or const String &,
        **          object name and attribute separated by a dot.
        **   value : float, value to be assigned.
        **
        ** Example:
        **
        ** inside the script file:
        **
        ** filter = Biquad(input=Noise(0.5), freq=1000, q=4, type=2)
        **
        ** Inside Juce (for a Pyo object named `pyo`):
        **
        ** pyo.set("filter.freq", 2000);
        */
        int set(const char *name, float value);
        int set(const String &name, float value);

        /*
        ** Sends an array of numerical values to a Pyo object's attribute.
        **
        ** arguments:
        **   name : const char * or const String &,
        **          object name and attribute separated by a dot.
        **   value : float *, array of floats.
        **   len : int, number of elements in the array.
        **
        ** Example:
        **
        ** inside the script file:
        **
        ** filters = Biquad(input=Noise(0.5), freq=[250, 500, 1000, 2000], q=5, type=2)
        **
        ** Inside Juce (for a Pyo object named `pyo`):
        **
        ** float frequencies[4] = {350, 700, 1400, 2800};
        ** pyo.set("filters.freq", frequencies, 4);
        */
        int set(const char *name, float *value, int len);
        int set(const String &name, float *value, int len);

        /*
        ** Sends a MIDI messges to the Server.
        **
        ** Example (for a Pyo object named `pyo`):
        **
        ** pyo.midi(144, 60, 127) //Send a Note on message on channel 1 for note # 60
        ** pyo.midi(128, 60, 0)   //Send a Note off messge on channel 1 for note # 60
        */
        void midi(int status, int data1, int data2);

        /*
        ** Shutdown and reboot the pyo server while keeping current in/out buffers.
        ** This will erase audio objects currently active within the server.
        **
        */
        void clear();

    private:
        int nChannels;
        int bufferSize;
        int sampleRate;
        PyThreadState *interpreter;
        float *pyoInBuffer;
        float *pyoOutBuffer;
        callPtr *pyoCallback;
        int pyoId;
        char pyoMsg[262144];
};

#endif  // PYOCLASS_H_INCLUDED