Skip to content

Commit

Permalink
Add amplitude input to Granulator node
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Jul 22, 2024
1 parent 00ab301 commit 3ff4389
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 16 deletions.
6 changes: 3 additions & 3 deletions docs/library/buffer/granulation/granulator/index.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
title: Granulator node documentation
description: Granulator: Granulator. Generates a grain from the given buffer each time a clock signal is received, with the given duration/rate/pan parameters. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.
description: Granulator: Granulator. Generates a grain from the given buffer each time a trigger is received on the `clock` input. Each new grain uses the given `duration`, `amplitude`, `pan` and `rate` values presented at each input at the moment the grain is created. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.

[Reference library](../../index.md) > [Buffer: Granulation](../index.md) > [Granulator](index.md)

# Granulator

```python
Granulator(buffer=None, clock=0, pos=0, duration=0.1, pan=0.0, rate=1.0, max_grains=2048, wrap=false)
Granulator(buffer=None, clock=0, pos=0, duration=0.1, amplitude=0.0, pan=0.0, rate=1.0, max_grains=2048, wrap=false)
```

Granulator. Generates a grain from the given buffer each time a clock signal is received, with the given duration/rate/pan parameters. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.
Granulator. Generates a grain from the given buffer each time a trigger is received on the `clock` input. Each new grain uses the given `duration`, `amplitude`, `pan` and `rate` values presented at each input at the moment the grain is created. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.

2 changes: 1 addition & 1 deletion docs/library/buffer/granulation/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# Buffer: Granulation

- **[SegmentedGranulator](segmentedgranulator/index.md)**: Segmented Granulator.
- **[Granulator](granulator/index.md)**: Granulator. Generates a grain from the given buffer each time a clock signal is received, with the given duration/rate/pan parameters. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.
- **[Granulator](granulator/index.md)**: Granulator. Generates a grain from the given buffer each time a trigger is received on the `clock` input. Each new grain uses the given `duration`, `amplitude`, `pan` and `rate` values presented at each input at the moment the grain is created. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.
2 changes: 1 addition & 1 deletion docs/library/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
## Buffer: Granulation

- **[SegmentedGranulator](buffer/granulation/segmentedgranulator/index.md)**: Segmented Granulator.
- **[Granulator](buffer/granulation/granulator/index.md)**: Granulator. Generates a grain from the given buffer each time a clock signal is received, with the given duration/rate/pan parameters. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.
- **[Granulator](buffer/granulation/granulator/index.md)**: Granulator. Generates a grain from the given buffer each time a trigger is received on the `clock` input. Each new grain uses the given `duration`, `amplitude`, `pan` and `rate` values presented at each input at the moment the grain is created. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.

---

Expand Down
9 changes: 8 additions & 1 deletion source/include/signalflow/node/buffer/granulation/grain.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ namespace signalflow
class Grain
{
public:
Grain(BufferRef buffer, int phase, int length, float rate, float pan = 0, bool wrap = false);
Grain(BufferRef buffer,
int phase,
int length,
float rate,
float amplitude = 1.0f,
float pan = 0.0f,
bool wrap = false);

/**--------------------------------------------------------------------------------*
* Returns true if the grain has completed its requested duration,
Expand All @@ -34,6 +40,7 @@ class Grain
int length_samples;
double samples_processed;
float rate;
float amplitude;
float pan;
bool wrap;
};
Expand Down
11 changes: 7 additions & 4 deletions source/include/signalflow/node/buffer/granulation/granulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ namespace signalflow
{

/**--------------------------------------------------------------------------------*
* Granulator. Generates a grain from the given buffer each time a clock signal
* is received, with the given duration/rate/pan parameters. The input buffer
* can be mono or stereo. If `wrap` is true, grain playback can wrap around the
* end/start of the buffer.
* Granulator. Generates a grain from the given buffer each time a trigger is
* received on the `clock` input. Each new grain uses the given `duration`,
* `amplitude`, `pan` and `rate` values presented at each input at the moment the
* grain is created. The input buffer can be mono or stereo. If `wrap` is true,
* grain playback can wrap around the end/start of the buffer.
*---------------------------------------------------------------------------------*/
class Granulator : public Node
{
Expand All @@ -22,6 +23,7 @@ class Granulator : public Node
NodeRef clock = 0,
NodeRef pos = 0,
NodeRef duration = 0.1,
NodeRef amplitude = 0.0,
NodeRef pan = 0.0,
NodeRef rate = 1.0,
NodeRef max_grains = 2048,
Expand All @@ -37,6 +39,7 @@ class Granulator : public Node
NodeRef pos;
NodeRef clock;
NodeRef duration;
NodeRef amplitude;
NodeRef pan;
NodeRef rate;
NodeRef max_grains;
Expand Down
4 changes: 2 additions & 2 deletions source/src/node/buffer/granulation/grain.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "signalflow/node/buffer/granulation/grain.h"

Grain::Grain(BufferRef buffer, int phase, int length_samples, float rate, float pan, bool wrap)
: buffer(buffer), phase(phase), length_samples(length_samples), rate(rate), pan(pan), wrap(wrap)
Grain::Grain(BufferRef buffer, int phase, int length_samples, float rate, float amplitude, float pan, bool wrap)
: buffer(buffer), phase(phase), length_samples(length_samples), rate(rate), amplitude(amplitude), pan(pan), wrap(wrap)
{
this->samples_processed = 0;
if (rate < 0)
Expand Down
8 changes: 6 additions & 2 deletions source/src/node/buffer/granulation/granulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ Granulator::Granulator(BufferRef buffer,
NodeRef clock,
NodeRef pos,
NodeRef duration,
NodeRef amplitude,
NodeRef pan,
NodeRef rate,
NodeRef max_grains,
bool wrap)
: pos(pos), clock(clock), duration(duration), pan(pan), rate(rate), max_grains(max_grains), wrap(wrap)
: pos(pos), clock(clock), duration(duration), amplitude(amplitude), pan(pan), rate(rate), max_grains(max_grains), wrap(wrap)
{
this->name = "granulator";

this->create_input("pos", this->pos);
this->create_input("clock", this->clock);
this->create_input("duration", this->duration);
this->create_input("amplitude", this->amplitude);
this->create_input("pan", this->pan);
this->create_input("rate", this->rate);
this->create_input("max_grains", this->max_grains);
Expand Down Expand Up @@ -60,6 +62,7 @@ void Granulator::process(Buffer &out, int num_frames)
sample pos = this->pos->out[0][frame];
sample duration = this->duration->out[0][frame];
sample rate = this->rate->out[0][frame];
sample amplitude = this->amplitude->out[0][frame];
sample pan = this->pan->out[0][frame];
sample max_grains = this->max_grains->out[0][frame];

Expand All @@ -71,6 +74,7 @@ void Granulator::process(Buffer &out, int num_frames)
pos * buffer->get_sample_rate(),
duration * buffer->get_sample_rate(),
rate * this->rate_scale_factor,
amplitude,
pan,
this->wrap);
this->grains.push_back(grain);
Expand All @@ -92,7 +96,7 @@ void Granulator::process(Buffer &out, int num_frames)
* obtain the amplitude sample for the correct index (e.g, phase = 0
* for the first sample).
*-----------------------------------------------------------------------*/
float amp = this->envelope->get(0, grain->get_progress());
float amp = this->envelope->get(0, grain->get_progress()) * grain->amplitude;
grain->step();

/*------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions source/src/python/nodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void init_python_nodes(py::module &m)
py::class_<SegmentedGranulator, Node, NodeRefTemplate<SegmentedGranulator>>(m, "SegmentedGranulator", "Segmented Granulator.")
.def(py::init<BufferRef, std::vector<float>, std::vector<float>, NodeRef, NodeRef, NodeRef, NodeRef>(), "buffer"_a = nullptr, "onset_times"_a = 0, "durations"_a = 0, "index"_a = 0.0, "rate"_a = 1.0, "clock"_a = 0, "max_grains"_a = 2048);

py::class_<Granulator, Node, NodeRefTemplate<Granulator>>(m, "Granulator", "Granulator. Generates a grain from the given buffer each time a clock signal is received, with the given duration/rate/pan parameters. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.")
.def(py::init<BufferRef, NodeRef, NodeRef, NodeRef, NodeRef, NodeRef, NodeRef, bool>(), "buffer"_a = nullptr, "clock"_a = 0, "pos"_a = 0, "duration"_a = 0.1, "pan"_a = 0.0, "rate"_a = 1.0, "max_grains"_a = 2048, "wrap"_a = false);
py::class_<Granulator, Node, NodeRefTemplate<Granulator>>(m, "Granulator", "Granulator. Generates a grain from the given buffer each time a trigger is received on the `clock` input. Each new grain uses the given `duration`, `amplitude`, `pan` and `rate` values presented at each input at the moment the grain is created. The input buffer can be mono or stereo. If `wrap` is true, grain playback can wrap around the end/start of the buffer.")
.def(py::init<BufferRef, NodeRef, NodeRef, NodeRef, NodeRef, NodeRef, NodeRef, NodeRef, bool>(), "buffer"_a = nullptr, "clock"_a = 0, "pos"_a = 0, "duration"_a = 0.1, "amplitude"_a = 0.0, "pan"_a = 0.0, "rate"_a = 1.0, "max_grains"_a = 2048, "wrap"_a = false);

#ifdef __APPLE__

Expand Down

0 comments on commit 3ff4389

Please sign in to comment.