#include "ArrayArithmetic.h"
#include "DspDivide.h"
#include "PdGraph.h"
message::Object *DspDivide::new_object(pd::Message *init_message, PdGraph *graph) {
return new DspDivide(init_message, graph);
}
DspDivide::DspDivide(pd::Message *init_message, PdGraph *graph) : DspObject(2, 2, 0, 1, graph) {
constant = init_message->is_float(0) ? init_message->get_float(0) : 0.0f;
process_function = &processScalar;
process_functionNoMessage = &processScalar;
}
DspDivide::~DspDivide() {
}
void DspDivide::onInletConnectionUpdate(unsigned int inlet_index) {
process_function = (incomingDspConnections[0].size() > 0 && incomingDspConnections[1].size() > 0)
? &processSignal : &processScalar;
}
string DspDivide::toString() {
const char *fmt = (constant == 0.0f) ? "%s" : "%s %g";
char str[snprintf(NULL, 0, fmt, get_object_label(), constant)+1];
snprintf(str, sizeof(str), fmt, get_object_label(), constant);
return string(str);
}
void DspDivide::process_message(int inlet_index, pd::Message *message) {
if (inlet_index == 1) {
if (message->is_float(0)) {
if (message->get_float(0) == 0.0f) {
constant = 1.0f;
graph->print_err("%s received message which set divisor to zero. Divisor set to 1.0f.", get_object_label());
} else {
constant = message->get_float(0);
}
}
}
}
void DspDivide::processSignal(DspObject *dspObject, int fromIndex, int toIndex) {
DspDivide *d = reinterpret_cast<DspDivide *>(dspObject);
ArrayArithmetic::divide(d->dspBufferAtInlet[0], d->dspBufferAtInlet[1],
d->dspBufferAtOutlet[0], 0, toIndex);
}
void DspDivide::processScalar(DspObject *dspObject, int fromIndex, int toIndex) {
DspDivide *d = reinterpret_cast<DspDivide *>(dspObject);
ArrayArithmetic::divide(d->dspBufferAtInlet[0], d->constant, d->dspBufferAtOutlet[0], fromIndex, toIndex);
}