Skip to content

Commit

Permalink
SampleBundle.at() adapted to use four sample interpolation types, plu…
Browse files Browse the repository at this point in the history
…s added protection against out_of_range exceptions. issue #70
  • Loading branch information
nwolek committed Jan 3, 2016
1 parent c6f849f commit bc29f15
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
39 changes: 33 additions & 6 deletions include/core/JamomaSample.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,43 @@ namespace Jamoma {
template <class InterpolatorType = Jamoma::Interpolator::Linear<Sample>>
Sample at(size_t channel, double interpolatedFrameIndex) {
Sample output = 0.0;
Sample sampleAtIndex0 = 0.0;
Sample sampleAtIndex1 = 0.0;
Sample sampleAtIndex2 = 0.0;
Sample sampleAtIndex3 = 0.0;
InterpolatorType interp;

int frameIndexBefore = (int)interpolatedFrameIndex;
double delta = interpolatedFrameIndex - frameIndexBefore;
int frameIndexAfter = frameIndexBefore + 1;
int frameIndex1 = (int)interpolatedFrameIndex;
double delta = interpolatedFrameIndex - frameIndex1;
int frameIndex0 = frameIndex1 - 1;
int frameIndex2 = frameIndex1 + 1;
int frameIndex3 = frameIndex1 + 2;

Sample sampleAtIndexBefore = mChannels[channel][frameIndexBefore];
Sample sampleAtIndexAfter = mChannels[channel][frameIndexAfter];
try {
sampleAtIndex0 = mChannels[channel].at(frameIndex0);
} catch (const std::out_of_range& oor) {
// stay zero
}

output = interp(sampleAtIndexBefore,sampleAtIndexAfter,delta);
try {
sampleAtIndex1 = mChannels[channel].at(frameIndex1);
} catch (const std::out_of_range& oor) {
// stay zero
}

try {
sampleAtIndex2 = mChannels[channel].at(frameIndex2);
} catch (const std::out_of_range& oor) {
// stay zero
}

try {
sampleAtIndex3 = mChannels[channel].at(frameIndex3);
} catch (const std::out_of_range& oor) {
// stay zero
}

output = interp(sampleAtIndex0, sampleAtIndex1, sampleAtIndex2, sampleAtIndex3, delta);

return output;
}
Expand Down
3 changes: 3 additions & 0 deletions test/SampleBundle/SampleBundle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ namespace Jamoma {
temp = test_bundle.at<Jamoma::Interpolator::Linear<Sample>>(0,d);
std::cout << "linear ( " << d << " ) = " << temp << std::endl;

temp = test_bundle.at<Jamoma::Interpolator::Cubic<Sample>>(0,d);
std::cout << "cubic ( " << d << " ) = " << temp << std::endl;

if (i % 4 == 0) { // every 4th item should be a whole number we can compare with regular access
tempExpected = test_bundle[0][int(d)];
if (temp != tempExpected) badSampleCount++;
Expand Down

0 comments on commit bc29f15

Please sign in to comment.