Jamoma API  0.6.0.a19
Bspline2D.cpp
1 /*
2  * Bspline Function Unit for TTBlue
3  * based on the bspline code by Jasch: http://www.jasch.ch/
4  * ported by Nils Peters
5  *
6  */
7 
8 #include "Bspline2D.h"
9 
10 
11 #define thisTTClass Bspline2D
12 #define thisTTClassName "bspline.2D"
13 #define thisTTClassTags "audio, trajectory, 2D, spline"
14 
15 
16 TT_AUDIO_CONSTRUCTOR
17 { //addAttribute(A, kTypeFloat64);
18  addAttribute(Degree, kTypeUInt8);
19  addAttribute(Steps, kTypeUInt16);
20  addAttribute(Dimen, kTypeUInt8);
21  addAttribute(Resolution, kTypeUInt16);//was size
22  setProcessMethod(processAudio);
23 // setCalculateMethod(calculateValue);
24 }
25 
26 
27 
28 
29 Bspline2D::~Bspline2D()
30 {
31  ;
32 }
33 
34 //TTErr Bspline2D::calculateValue(const TTFloat64& x, TTFloat64& y, TTPtrSizedInt data)
35 //{
36 // y = x;
37 // return kTTErrNone;
38 //}
39 
40 void Bspline2D::calculatePoint()
41 {
42  TTUInt16 i;
43  TTFloat64 temp;
44 
45  point3D[0] = 0;//x
46  point3D[1] = 0;//y
47  point3D[2] = 0;//z
48 
49  for ( i = 0; i <= mResolution; i++) {
50  temp = calculateBlend(i, mDegree); // same blend is used for each dimension coordinate
51  b_op[0] += b_control[i*3] * temp;
52  b_op[1] += b_control[i*3+1] * temp;
53  b_op[2] += b_control[i*3+2] * temp;
54  }
55 }
56 
57 float Bspline2D::calculateBlend(TTUInt16 k, TTUInt16 t)
58 {
59  TTFloat64 value;
60  TTFloat64 v = interval;
61 
62  if (t == 1) { // base case for the recursion
63  if ((b_knots[k] <= v) && (v < b_knots[k+1])) {
64  value=1;
65  } else {
66  value=0;
67  }
68  } else {
69  if ((b_knots[k + t - 1] == b_knots[k]) && (b_knots[k + t] == b_knots[k + 1])) { // check for divide by zero
70  value = 0;
71  } else if (b_knots[k + t - 1] == b_knots[k]) { // if a term's denominator is zero, use just the other
72  value = (b_knots[k + t] - v) / (b_knots[k + t] - b_knots[k + 1]) * calculateBlend(k + 1, t - 1);
73  } else if (b_knots[k + t] == b_knots[k + 1]) {
74  value = (v - b_knots[k]) / (b_knots[k + t - 1] - b_knots[k]) * calculateBlend(k, t - 1);
75  } else {
76  value = (v - b_knots[k]) / (b_knots[k + t - 1] - b_knots[k]) * calculateBlend(k, t - 1) +
77  (b_knots[k + t] - v) / (b_knots[k + t] - b_knots[k + 1]) * calculateBlend(k + 1, t - 1);
78  }
79  }
80  return value;
81 }
82 
83 void Bspline2D::calculateKnots() // generate knot-vector
84 {
85  TTUInt8 i, t, n;
86  n = mResolution;
87  t = mDegree;
88  for (i = 0; i <= (n + t); i++){
89  if (i < t) {
90  b_knots[i] = 0;
91  } else if ((t <= i) && (i <= n)) {
92  b_knots[i] = i - t + 1;
93  } else if (i > n) {
94  b_knots[i] = n - t + 2; // if n - t = -2 then we're screwed, everything goes to 0
95  }
96  }
97 }
98 
99 TTErr Bspline2D::processAudio(TTAudioSignalArrayPtr inputs, TTAudioSignalArrayPtr outputs)
100 {
101  TTAudioSignal& out = outputs->getSignal(0);
102  TTChannelCount numOutputChannels = out.getNumChannelsAsInt();
103 
104  if (numOutputChannels != 2) {
105  TTValue v = 2;
106  out.setMaxNumChannels(v);
107  out.setNumChannels(v);
108  }
109 
110  TTAudioSignal& in0 = inputs->getSignal(0);
111  TTUInt16 vs = in0.getVectorSizeAsInt();
112 
113  TTSampleValuePtr inSampleX = in0.mSampleVectors[0];
114  TTSampleValuePtr outSampleX = out.mSampleVectors[0];
115  TTSampleValuePtr outSampleY = out.mSampleVectors[1];
116  TTFloat64 f;
117 
118  for (int i=0; i<vs; i++) {
119 
120  f = inSampleX[i] * 0.5; //0... 1.
121 
122  if ((f > 0) && (f < 1)){
123  interval = f * (TTFloat64)((mResolution - mDegree) + 2);
124  calculatePoint(); // output intermediate point
125  outSampleX[i] = b_op[0];
126  outSampleY[i] = b_op[1];
127  //outSampleZ[i] = b_op[2];
128  } else if (f <= 0.0){
129  // output the first point
130  } else if (f >= 1.0){
131  // output the last point
132  }
133  }
134 return kTTErrNone;
135 }
std::uint16_t TTUInt16
16 bit unsigned integer
Definition: TTBase.h:176
#define addAttribute(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter...
Definition: TTAttribute.h:29
8-bit unsigned integer, range is 0 through 255.
Definition: TTBase.h:274
#define setProcessMethod(methodName)
A convenience macro to be used by subclasses for setting the process method.
double TTFloat64
64 bit floating point number
Definition: TTBase.h:188
16-bit unsigned integer, range is 0 through 65,535.
Definition: TTBase.h:276
TTErr setMaxNumChannels(const TTValue &newMaxNumChannels)
Attribute accessor.
The TTAudioSignal class represents N vectors of audio samples for M channels.
Definition: TTAudioSignal.h:57
TTSampleValue ** mSampleVectors
An array of pointers to the first sample in each vector. Declared Public for fast access...
Definition: TTAudioSignal.h:74
TTUInt16 TTChannelCount
Data type used when counting the number of channels in multi-channel audio signals and processes...
Definition: TTAudioSignal.h:31
A simple container for an array of TTAudioSignal pointers.
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
No Error.
Definition: TTBase.h:343
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
unsigned char TTUInt8
8 bit unsigned integer (char)
Definition: TTBase.h:174