-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathDspDelayRead.cpp
80 lines (70 loc) · 3.05 KB
/
DspDelayRead.cpp
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
/*
* Copyright 2009,2010 Reality Jockey, Ltd.
* http://rjdj.me/
*
* This file is part of ZenGarden.
*
* ZenGarden 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.
*
* ZenGarden 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 Lesser General Public License
* along with ZenGarden. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ArrayArithmetic.h"
#include "DspDelayRead.h"
#include "DspDelayWrite.h"
#include "PdGraph.h"
MessageObject *DspDelayRead::newObject(PdMessage *initMessage, PdGraph *graph) {
return new DspDelayRead(initMessage, graph);
}
DspDelayRead::DspDelayRead(PdMessage *initMessage, PdGraph *graph) : DelayReceiver(1, 0, 0, 1, graph) {
if (initMessage->isSymbol(0) && initMessage->isFloat(1)) {
name = StaticUtils::copyString(initMessage->getSymbol(0));
delayInSamples = StaticUtils::millisecondsToSamples(initMessage->getFloat(1), graph->getSampleRate());
} else {
graph->printErr("delread~ must be initialised in the format [delread~ name delay].");
delayInSamples = 0.0f;
}
processFunction = &processSignal;
// TODO(mhroth): implement process function for case of receiving messages
// processFunctionNoMessage = &processScalar;
}
DspDelayRead::~DspDelayRead() {
// nothing to do
}
void DspDelayRead::onInletConnectionUpdate(unsigned int inletIndex) {
// processFunction = (incomingMessageConnections[0].size() > 0) ? &processScalar : &processSignal;
}
void DspDelayRead::processMessage(int inletIndex, PdMessage *message) {
if (inletIndex == 0 && message->isFloat(0)) {
// update the delay time
delayInSamples = StaticUtils::millisecondsToSamples(message->getFloat(0), graph->getSampleRate());
}
}
void DspDelayRead::processSignal(DspObject *dspObject, int fromIndex, int toIndex) {
DspDelayRead *d = reinterpret_cast<DspDelayRead *>(dspObject);
int headIndex = 0;
int bufferLength = 0;
float *buffer = d->delayline->getBuffer(&headIndex, &bufferLength);
// this handles the most common case. Messages are rarely sent to delread~.
int delayIndex = headIndex - toIndex - ((int) d->delayInSamples);
if (delayIndex < 0) {
delayIndex += bufferLength;
}
if (delayIndex > bufferLength - toIndex) {
int samplesInBuffer = bufferLength - delayIndex; // samples remaining in the buffer that belong in this block
memcpy(d->dspBufferAtOutlet[0], buffer + delayIndex, samplesInBuffer * sizeof(float));
memcpy(d->dspBufferAtOutlet[0] + samplesInBuffer, buffer, (toIndex - samplesInBuffer) * sizeof(float));
} else {
memcpy(d->dspBufferAtOutlet[0], buffer + delayIndex, toIndex*sizeof(float));
}
}