| 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 |
#include "PyoClass.h" |
| 22 |
|
| 23 |
Pyo::Pyo() { |
| 24 |
interpreter = nullptr; |
| 25 |
} |
| 26 |
|
| 27 |
Pyo::~Pyo() { |
| 28 |
if (interpreter != NULL) { |
| 29 |
pyo_end_interpreter(interpreter); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
void Pyo::setup(int _nChannels, int _bufferSize, int _sampleRate) { |
| 34 |
nChannels = _nChannels; |
| 35 |
bufferSize = _bufferSize; |
| 36 |
sampleRate = _sampleRate; |
| 37 |
interpreter = pyo_new_interpreter(sampleRate, bufferSize, nChannels); |
| 38 |
pyoInBuffer = reinterpret_cast<float*>(pyo_get_input_buffer_address(interpreter)); |
| 39 |
pyoOutBuffer = reinterpret_cast<float*>(pyo_get_output_buffer_address(interpreter)); |
| 40 |
pyoCallback = reinterpret_cast<callPtr*>(pyo_get_embedded_callback_address(interpreter)); |
| 41 |
pyoId = pyo_get_server_id(interpreter); |
| 42 |
pyo_set_server_params(interpreter, sampleRate, bufferSize); |
| 43 |
} |
| 44 |
|
| 45 |
void Pyo::setbpm(double bpm) { |
| 46 |
pyo_set_bpm(interpreter, bpm); |
| 47 |
} |
| 48 |
|
| 49 |
void Pyo::process(AudioBuffer<float>& buffer) { |
| 50 |
for (int channel = 0; channel < nChannels; ++channel) { |
| 51 |
float *channelData = buffer.getWritePointer(channel); |
| 52 |
for (int sample = 0; sample < bufferSize; sample++) { |
| 53 |
pyoInBuffer[sample*nChannels+channel] = channelData[sample]; |
| 54 |
} |
| 55 |
} |
| 56 |
pyoCallback(pyoId); |
| 57 |
for (int channel = 0; channel < nChannels; ++channel) { |
| 58 |
float *channelData = buffer.getWritePointer(channel); |
| 59 |
for (int sample = 0; sample < bufferSize; sample++) { |
| 60 |
channelData[sample] = pyoOutBuffer[sample*nChannels+channel]; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
int Pyo::loadfile(const char *file, int add) { |
| 66 |
return pyo_exec_file(interpreter, file, pyoMsg, add); |
| 67 |
} |
| 68 |
|
| 69 |
int Pyo::loadfile(const String &file, int add) { |
| 70 |
return pyo_exec_file(interpreter, file.getCharPointer(), pyoMsg, add); |
| 71 |
} |
| 72 |
|
| 73 |
int Pyo::exec(const char *_msg) { |
| 74 |
strcpy(pyoMsg, _msg); |
| 75 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 76 |
} |
| 77 |
|
| 78 |
int Pyo::exec(const String &_msg) { |
| 79 |
strcpy(pyoMsg, _msg.getCharPointer()); |
| 80 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 81 |
} |
| 82 |
|
| 83 |
int Pyo::value(const char *name, float value) { |
| 84 |
sprintf(pyoMsg, "%s.value=%f", name, value); |
| 85 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 86 |
} |
| 87 |
|
| 88 |
int Pyo::value(const String &name, float value) { |
| 89 |
const char * _name = name.getCharPointer(); |
| 90 |
sprintf(pyoMsg, "%s.value=%f", _name, value); |
| 91 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 92 |
} |
| 93 |
|
| 94 |
int Pyo::value(const char *name, float *value, int len) { |
| 95 |
char fchar[32]; |
| 96 |
sprintf(pyoMsg, "%s.value=[", name); |
| 97 |
for (int i=0; i<len; i++) { |
| 98 |
sprintf(fchar, "%f,", value[i]); |
| 99 |
strcat(pyoMsg, fchar); |
| 100 |
} |
| 101 |
strcat(pyoMsg, "]"); |
| 102 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 103 |
} |
| 104 |
|
| 105 |
int Pyo::value(const String &name, float *value, int len) { |
| 106 |
char fchar[32]; |
| 107 |
const char * _name = name.getCharPointer(); |
| 108 |
sprintf(pyoMsg, "%s.value=[", _name); |
| 109 |
for (int i=0; i<len; i++) { |
| 110 |
sprintf(fchar, "%f,", value[i]); |
| 111 |
strcat(pyoMsg, fchar); |
| 112 |
} |
| 113 |
strcat(pyoMsg, "]"); |
| 114 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 115 |
} |
| 116 |
|
| 117 |
int Pyo::set(const char *name, float value) { |
| 118 |
sprintf(pyoMsg, "%s=%f", name, value); |
| 119 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 120 |
} |
| 121 |
|
| 122 |
int Pyo::set(const String &name, float value) { |
| 123 |
const char * _name = name.getCharPointer(); |
| 124 |
sprintf(pyoMsg, "%s=%f", _name, value); |
| 125 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 126 |
} |
| 127 |
|
| 128 |
int Pyo::set(const char *name, float *value, int len) { |
| 129 |
char fchar[32]; |
| 130 |
sprintf(pyoMsg, "%s=[", name); |
| 131 |
for (int i=0; i<len; i++) { |
| 132 |
sprintf(fchar, "%f,", value[i]); |
| 133 |
strcat(pyoMsg, fchar); |
| 134 |
} |
| 135 |
strcat(pyoMsg, "]"); |
| 136 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 137 |
} |
| 138 |
|
| 139 |
int Pyo::set(const String &name, float *value, int len) { |
| 140 |
char fchar[32]; |
| 141 |
const char * _name = name.getCharPointer(); |
| 142 |
sprintf(pyoMsg, "%s=[", _name); |
| 143 |
for (int i=0; i<len; i++) { |
| 144 |
sprintf(fchar, "%f,", value[i]); |
| 145 |
strcat(pyoMsg, fchar); |
| 146 |
} |
| 147 |
strcat(pyoMsg, "]"); |
| 148 |
return pyo_exec_statement(interpreter, pyoMsg, 0); |
| 149 |
} |
| 150 |
|
| 151 |
void Pyo::midi(int status, int data1, int data2) { |
| 152 |
pyo_add_midi_event(interpreter, status, data1, data2); |
| 153 |
} |
| 154 |
|
| 155 |
void Pyo::clear() { |
| 156 |
pyo_server_reboot(interpreter); |
| 157 |
} |