Skip to content

Commit

Permalink
SpatialPanner: Add initial WFS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed May 24, 2024
1 parent 115d955 commit a373f0d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "signalflow/buffer/ringbuffer.h"
#include "signalflow/core/constants.h"
#include "signalflow/node/node.h"

Expand Down Expand Up @@ -50,6 +51,7 @@ class SpatialPanner : public Node
NodeRef y = 0.0,
NodeRef z = 0.0,
NodeRef radius = 1.0,
NodeRef use_delays = 1.0,
std::string algorithm = "dbap");

virtual void process(Buffer &out, int num_frames) override;
Expand All @@ -60,7 +62,9 @@ class SpatialPanner : public Node
NodeRef y;
NodeRef z;
NodeRef radius;
NodeRef use_delays;
std::string algorithm;
SampleRingBuffer *buffer;
};

REGISTER(SpatialPanner, "spatial-panner")
Expand Down
51 changes: 49 additions & 2 deletions source/src/node/processors/panning/spatial-environment.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "signalflow/core/graph.h"
#include "signalflow/node/processors/panning/spatial-environment.h"
#include <limits>

Expand Down Expand Up @@ -48,8 +49,9 @@ std::vector<std::shared_ptr<SpatialSpeaker>> SpatialEnvironment::get_channels()

SpatialPanner::SpatialPanner(std::shared_ptr<SpatialEnvironment> env,
NodeRef input, NodeRef x, NodeRef y, NodeRef z, NodeRef radius,
NodeRef use_delays,
std::string algorithm)
: env(env), input(input), x(x), y(y), z(z), radius(radius), algorithm(algorithm)
: env(env), input(input), x(x), y(y), z(z), radius(radius), use_delays(use_delays), algorithm(algorithm)
{
this->name = "spatial-panner";

Expand All @@ -59,13 +61,17 @@ SpatialPanner::SpatialPanner(std::shared_ptr<SpatialEnvironment> env,
this->set_channels(1, channels.size());
}

// 1.0 = max_delay_time
this->buffer = new SampleRingBuffer(1.0 * this->graph->get_sample_rate());

this->create_input("input", this->input);
this->create_input("x", this->x);
this->create_input("y", this->y);
this->create_input("z", this->z);
this->create_input("radius", this->radius);
this->create_input("use_delays", this->use_delays);

if (algorithm != "dbap" && algorithm != "nearest")
if (algorithm != "dbap" && algorithm != "nearest" && algorithm != "beamformer")
{
throw std::runtime_error("Invalid spatialisation algorithm: " + algorithm);
}
Expand All @@ -89,6 +95,7 @@ void SpatialPanner::process(Buffer &out, int num_frames)
float amp = (this->radius->out[0][frame] - distance) / this->radius->out[0][frame];
if (amp < 0)
amp = 0;

out[channel][frame] = amp * this->input->out[0][frame];
}
}
Expand Down Expand Up @@ -118,6 +125,46 @@ void SpatialPanner::process(Buffer &out, int num_frames)
out[nearest_index][frame] = this->input->out[0][frame];
}
}
else if (algorithm == "beamformer")
{
for (int frame = 0; frame < num_frames; frame++)
{
this->buffer->append(this->input->out[0][frame]);
for (int channel = 0; channel < this->get_num_output_channels(); channel++)
{
auto speaker = speakers[channel];
if (speaker)
{
// m/s
float SPEED_OF_SOUND = 343.0;
float distance = sqrtf(powf(speaker->x - this->x->out[0][frame], 2) + powf(speaker->y - this->y->out[0][frame], 2) + powf(speaker->z - this->z->out[0][frame], 2));

// float amp = (this->radius->out[0][frame] - distance) / this->radius->out[0][frame];
// if (amp < 0)
// amp = 0;
float distance_m = distance * 0.001;
float attenuation = 1.0 / (distance_m * distance_m);
// float attenuation = 1.0;
if (frame == 0 && channel == 0)
{
// printf("distance %.4f, attenuation: %.4f\n", distance, attenuation);
// printf("use_delays: %f\n", use_delays->out[0][frame]);
}

if (use_delays->out[0][frame])
{
float delay_seconds = distance_m / SPEED_OF_SOUND;
float delay_samples = delay_seconds * this->graph->get_sample_rate();
out[channel][frame] = this->buffer->get(-delay_samples) * attenuation;
}
else
{
out[channel][frame] = this->buffer->get(-1) * attenuation;
}
}
}
}
}
}

}

0 comments on commit a373f0d

Please sign in to comment.