Skip to content

Commit

Permalink
Buffer: Standardise on using interpolation_mode naming throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Dec 17, 2023
1 parent 661063f commit 0e5fc54
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
2 changes: 1 addition & 1 deletion source/include/signalflow/buffer/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class Buffer
unsigned long num_frames;
float duration;

signalflow_interpolation_mode_t interpolate;
signalflow_interpolation_mode_t interpolation_mode;
};

/**-------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions source/include/signalflow/core/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ typedef RingBuffer<sample> SampleRingBuffer;
*------------------------------------------------------------------------*/
enum signalflow_interpolation_mode_t : unsigned int
{
SIGNALFLOW_INTERPOLATION_NONE,
SIGNALFLOW_INTERPOLATION_LINEAR,
SIGNALFLOW_INTERPOLATION_COSINE
SIGNALFLOW_INTERPOLATION_MODE_NONE,
SIGNALFLOW_INTERPOLATION_MODE_LINEAR,
SIGNALFLOW_INTERPOLATION_MODE_COSINE
};

enum signalflow_event_distribution_t : unsigned int
Expand Down
16 changes: 8 additions & 8 deletions source/src/buffer/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Buffer::Buffer(int num_channels, int num_frames)
{
this->num_channels = num_channels;
this->num_frames = num_frames;
this->interpolate = SIGNALFLOW_INTERPOLATION_LINEAR;
this->interpolation_mode = SIGNALFLOW_INTERPOLATION_MODE_LINEAR;

/*--------------------------------------------------------------------------------
* If the AudioGraph has been instantiated, populate the buffer's sample
Expand Down Expand Up @@ -81,7 +81,7 @@ Buffer::Buffer(std::vector<sample> data)

Buffer::Buffer(std::string filename)
{
this->interpolate = SIGNALFLOW_INTERPOLATION_LINEAR;
this->interpolation_mode = SIGNALFLOW_INTERPOLATION_MODE_LINEAR;
this->load(filename);
}

Expand Down Expand Up @@ -294,7 +294,7 @@ std::vector<BufferRef> Buffer::split(int num_frames_per_part)
BufferRef buf = new Buffer(1, num_frames_per_part, &ptr);

// Is there a better way to initialise all properties of the buffer?
buf->interpolate = this->interpolate;
buf->interpolation_mode = this->interpolation_mode;

bufs[i] = buf;
}
Expand Down Expand Up @@ -342,19 +342,19 @@ sample Buffer::get_frame(int channel, double frame)
frame = 0;
}

if (this->interpolate == SIGNALFLOW_INTERPOLATION_LINEAR)
if (this->interpolation_mode == SIGNALFLOW_INTERPOLATION_MODE_LINEAR)
{
double frame_frac = (frame - (int) frame);
sample rv = ((1.0 - frame_frac) * this->data[channel][(int) frame]) + (frame_frac * this->data[channel][(int) ceil(frame)]);
return rv;
}
else if (this->interpolate == SIGNALFLOW_INTERPOLATION_NONE)
else if (this->interpolation_mode == SIGNALFLOW_INTERPOLATION_MODE_NONE)
{
return this->data[channel][(int) frame];
}
else
{
throw std::runtime_error("Buffer: Unsupported interpolation mode: " + std::to_string(this->interpolate));
throw std::runtime_error("Buffer: Unsupported interpolation mode: " + std::to_string(this->interpolation_mode));
}
}

Expand Down Expand Up @@ -419,12 +419,12 @@ std::string Buffer::get_filename()

void Buffer::set_interpolation_mode(signalflow_interpolation_mode_t mode)
{
this->interpolate = mode;
this->interpolation_mode = mode;
}

signalflow_interpolation_mode_t Buffer::get_interpolation_mode()
{
return this->interpolate;
return this->interpolation_mode;
}

sample **Buffer::get_data()
Expand Down
8 changes: 4 additions & 4 deletions source/src/buffer/buffer2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Buffer2D::Buffer2D(std::vector<BufferRef> buffers)

this->num_buffers = buffers.size();
this->duration = this->num_frames / this->sample_rate;
this->interpolate = SIGNALFLOW_INTERPOLATION_LINEAR;
this->interpolation_mode = SIGNALFLOW_INTERPOLATION_MODE_LINEAR;

/*------------------------------------------------------------------------
* Data is always a square matrix.
Expand All @@ -56,7 +56,7 @@ Buffer2D::~Buffer2D()
sample Buffer2D::get2D(double offset_x, double offset_z)
{
offset_z *= (this->num_buffers - 1);
if (this->interpolate == SIGNALFLOW_INTERPOLATION_LINEAR)
if (this->interpolation_mode == SIGNALFLOW_INTERPOLATION_MODE_LINEAR)
{
int offset_x_int = int(offset_x);
int offset_z_int = int(offset_z);
Expand All @@ -74,7 +74,7 @@ sample Buffer2D::get2D(double offset_x, double offset_z)

return rv;
}
else if (this->interpolate == SIGNALFLOW_INTERPOLATION_NONE)
else if (this->interpolation_mode == SIGNALFLOW_INTERPOLATION_MODE_NONE)
{
int offset_x_int = int(round(offset_x)) % this->num_frames;
int offset_z_int = int(round(offset_z)) % this->num_buffers;
Expand All @@ -83,7 +83,7 @@ sample Buffer2D::get2D(double offset_x, double offset_z)
}
else
{
throw std::runtime_error("Buffer2D: Unsupported interpolation mode: " + std::to_string(this->interpolate));
throw std::runtime_error("Buffer2D: Unsupported interpolation mode: " + std::to_string(this->interpolation_mode));
}
}

Expand Down
17 changes: 10 additions & 7 deletions source/src/python/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ void init_python_buffer(py::module &m)
R"pbdoc(Allocate a buffer with `num_channels` channels and `num_frames` frames.)pbdoc")
.def(
py::init<int, int, std::vector<std::vector<float>>>(), "num_channels"_a, "num_frames"_a,
"data"_a
","
"data"_a,
R"pbdoc(Allocate a buffer with `num_channels` channels and `num_frames` frames, containing the floating-point samples in `data`.)pbdoc")
.def(
py::init<std::vector<std::vector<float>>>(),
Expand Down Expand Up @@ -53,10 +52,12 @@ void init_python_buffer(py::module &m)
}
})
.def(
"__mul__", [](BufferRef a, float b) { return a * b; }, "value"_a,
"__mul__", [](BufferRef a, float b) { return a * b; },
"value"_a,
R"pbdoc(Returns a new Buffer containing the samples in `self` multiplied by the scaling factor `value`.)pbdoc")
.def(
"__rmul__", [](BufferRef a, float b) { return a * b; }, "value_"_a,
"__rmul__", [](BufferRef a, float b) { return a * b; },
"value_"_a,
R"pbdoc(Returns a new Buffer containing the samples in `self` multiplied by the scaling factor `value`.)pbdoc")
.def(
"__len__", [](Buffer &buf) { return buf.get_num_frames(); },
Expand All @@ -75,7 +76,7 @@ void init_python_buffer(py::module &m)
R"pbdoc(Returns the buffer's duration, in seconds.)pbdoc")
.def_property_readonly("filename", &Buffer::get_filename,
R"pbdoc(Returns the buffer's filename, if the buffer has been loaded from/saved to file.)pbdoc")
.def_property("interpolate", &Buffer::get_interpolation_mode, &Buffer::set_interpolation_mode,
.def_property("interpolation_mode", &Buffer::get_interpolation_mode, &Buffer::set_interpolation_mode,
R"pbdoc(Get/set the buffer's interpolation mode.)pbdoc")

#if !(defined(__linux__) && defined(__arm__))
Expand All @@ -102,9 +103,11 @@ void init_python_buffer(py::module &m)
.def("get", &Buffer::get, "channel"_a, "frame"_a)
.def("get_frame", &Buffer::get_frame, "channel"_a, "frame"_a)
.def(
"fill", [](Buffer &buf, float sample) { buf.fill(sample); }, "sample"_a)
"fill", [](Buffer &buf, float sample) { buf.fill(sample); },
"sample"_a)
.def(
"fill", [](Buffer &buf, const std::function<float(float)> f) { buf.fill(f); }, "function"_a)
"fill", [](Buffer &buf, const std::function<float(float)> f) { buf.fill(f); },
"function"_a)
.def("split", &Buffer::split, "num_frames_per_part"_a)
.def("load", &Buffer::load, "filename"_a)
.def("save", &Buffer::save, "filename"_a);
Expand Down
6 changes: 3 additions & 3 deletions source/src/python/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ void init_python_constants(py::module &m)
* Constants
*-------------------------------------------------------------------------------*/
py::enum_<signalflow_interpolation_mode_t>(m, "signalflow_interpolation_mode_t", py::arithmetic(), "signalflow_interpolation_mode_t")
.value("SIGNALFLOW_INTERPOLATION_NONE", SIGNALFLOW_INTERPOLATION_NONE, "No interpolation")
.value("SIGNALFLOW_INTERPOLATION_LINEAR", SIGNALFLOW_INTERPOLATION_LINEAR, "Linear interpolation")
.value("SIGNALFLOW_INTERPOLATION_COSINE", SIGNALFLOW_INTERPOLATION_COSINE, "Cosine interpolation")
.value("SIGNALFLOW_INTERPOLATION_MODE_NONE", SIGNALFLOW_INTERPOLATION_MODE_NONE, "No interpolation")
.value("SIGNALFLOW_INTERPOLATION_MODE_LINEAR", SIGNALFLOW_INTERPOLATION_MODE_LINEAR, "Linear interpolation")
.value("SIGNALFLOW_INTERPOLATION_MODE_COSINE", SIGNALFLOW_INTERPOLATION_MODE_COSINE, "Cosine interpolation")
.export_values();

py::enum_<signalflow_event_distribution_t>(m, "signalflow_event_distribution_t", py::arithmetic(), "signalflow_event_distribution_t")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_buffer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from signalflow import Buffer, Buffer2D
from signalflow import SIGNALFLOW_INTERPOLATION_NONE, SIGNALFLOW_INTERPOLATION_LINEAR
from signalflow import SIGNALFLOW_INTERPOLATION_MODE_NONE, SIGNALFLOW_INTERPOLATION_MODE_LINEAR
from signalflow import GraphNotCreatedException
import numpy as np
import pytest
Expand Down Expand Up @@ -85,7 +85,7 @@ def test_buffer_interpolate(graph):
assert b.get(0, -0.5) == 1
assert b.get(0, 1.5) == 2

b.interpolate = SIGNALFLOW_INTERPOLATION_NONE
b.interpolation_mode = SIGNALFLOW_INTERPOLATION_MODE_NONE
assert b.get(0, 0) == 1
assert b.get(0, 1) == 2
assert b.get(0, 0.5) == 1
Expand Down

0 comments on commit 0e5fc54

Please sign in to comment.