Add 'Step Sequencer' node.
parent
e0ad86f894
commit
daea30ac22
|
@ -0,0 +1,19 @@
|
|||
# @begin:license
|
||||
#
|
||||
# Copyright (c) 2015-2019, Benjamin Niemann <pink@odahoda.de>
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# @end:license
|
|
@ -0,0 +1,167 @@
|
|||
/*
|
||||
* @begin:license
|
||||
*
|
||||
* Copyright (c) 2015-2021, Ben Niemann <pink@odahoda.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* @end:license
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <flatbuffers/flatbuffers.h>
|
||||
|
||||
#include "noisicaa/core/flatbuffers.h"
|
||||
#include "noisicaa/engine/buffers.h"
|
||||
#include "noisicaa/engine/program.h"
|
||||
#include "noisicaa/engine/opcodes.h"
|
||||
#include "noisicaa/engine/engine_impl.h"
|
||||
#include "noisicaa/engine/block_context.h"
|
||||
#include "noisicaa/node_lib/step_sequencer/audio.h"
|
||||
#include "noisicaa/node_lib/step_sequencer/node_message_generated.h"
|
||||
|
||||
namespace noisicaa::engine::step_sequencer {
|
||||
|
||||
struct Spec {
|
||||
struct Step {
|
||||
Step(float v, bool g) : value(v), gate(g) {}
|
||||
|
||||
float value;
|
||||
bool gate;
|
||||
};
|
||||
|
||||
std::vector<Step> steps;
|
||||
};
|
||||
|
||||
Node::Node(EngineImpl* engine, uint64_t id)
|
||||
: ProcessorNode(engine, id, "noisicaa.node_lib.step_sequencer"),
|
||||
_next_spec(nullptr),
|
||||
_current_spec(nullptr),
|
||||
_old_spec(nullptr) {}
|
||||
|
||||
Node::~Node() {
|
||||
Spec* spec = _next_spec.exchange(nullptr);
|
||||
if (spec != nullptr) {
|
||||
delete spec;
|
||||
}
|
||||
spec = _current_spec.exchange(nullptr);
|
||||
if (spec != nullptr) {
|
||||
delete spec;
|
||||
}
|
||||
spec = _old_spec.exchange(nullptr);
|
||||
if (spec != nullptr) {
|
||||
delete spec;
|
||||
}
|
||||
}
|
||||
|
||||
Status Node::init(const NodeDescription* description) {
|
||||
RETURN_IF_ERROR(ProcessorNode::init(description));
|
||||
|
||||
return Status::Ok();
|
||||
}
|
||||
|
||||
Status Node::handle_message(const std::string& msg) {
|
||||
const SetSpec* set_spec = flatbuffers::GetRoot<SetSpec>(msg.c_str());
|
||||
|
||||
std::unique_ptr<Spec> new_spec(new Spec());
|
||||
const auto steps = set_spec->steps();
|
||||
for (unsigned int i = 0 ; i < steps->size() ; ++i) {
|
||||
const auto step = steps->Get(i);
|
||||
new_spec->steps.emplace_back(step->value(), step->gate());
|
||||
}
|
||||
|
||||
// Discard any next spec, which hasn't been picked up by the audio thread.
|
||||
Spec* prev_next_spec = _next_spec.exchange(nullptr);
|
||||
if (prev_next_spec != nullptr) {
|
||||
delete prev_next_spec;
|
||||
}
|
||||
|
||||
// Discard spec, which the audio thread doesn't use anymore.
|
||||
Spec* old_spec = _old_spec.exchange(nullptr);
|
||||
if (old_spec != nullptr) {
|
||||
delete old_spec;
|
||||
}
|
||||
|
||||
// Make the new spec the next one for the audio thread.
|
||||
prev_next_spec = _next_spec.exchange(new_spec.release());
|
||||
assert(prev_next_spec == nullptr);
|
||||
|
||||
return Status::Ok();
|
||||
}
|
||||
|
||||
|
||||
Status Node::run(BlockContext* ctxt, const std::vector<Buffer*>* buffers) {
|
||||
float* clock = (float*)buffers->at(0)->data();
|
||||
float* out = (float*)buffers->at(1)->data();
|
||||
float* gate = (float*)buffers->at(2)->data();
|
||||
|
||||
// If there is a next step, make it the current. The current step becomes
|
||||
// the old step, which will eventually be destroyed in the main thread.
|
||||
// It must not happen that a next step is available, before an old one has
|
||||
// been disposed of.
|
||||
Spec* spec = _next_spec.exchange(nullptr);
|
||||
if (spec != nullptr) {
|
||||
Spec* old_spec = _current_spec.exchange(spec);
|
||||
old_spec = _old_spec.exchange(old_spec);
|
||||
assert(old_spec == nullptr);
|
||||
}
|
||||
|
||||
spec = _current_spec.load();
|
||||
if (spec == nullptr) {
|
||||
// No step yet, just clear my output ports.
|
||||
for (uint32_t smpl = 0 ; smpl < ctxt->block_size ; ++smpl) {
|
||||
*out++ = 0.0;
|
||||
*gate++ = 0.0;
|
||||
}
|
||||
return Status::Ok();
|
||||
}
|
||||
|
||||
for (uint32_t smpl = 0 ; smpl < ctxt->block_size ; ++smpl, ++clock, ++out, ++gate) {
|
||||
if (*clock >= 0.55 && !_clock) {
|
||||
_clock = true;
|
||||
|
||||
_current_step = (_current_step + 1) % spec->steps.size();
|
||||
|
||||
FlatBufferAllocator allocator;
|
||||
flatbuffers::FlatBufferBuilder fbs(allocator.buf_size, &allocator);
|
||||
|
||||
NodeMessageBuilder msg_builder(fbs);
|
||||
msg_builder.add_step(_current_step);
|
||||
fbs.Finish(msg_builder.Finish());
|
||||
|
||||
push_node_message(ctxt, fbs.GetBufferPointer(), fbs.GetSize());
|
||||
} else if (*clock < 0.45 && _clock) {
|
||||
_clock = false;
|
||||
}
|
||||
|
||||
if (_current_step >= 0) {
|
||||
const auto& step = spec->steps[_current_step % spec->steps.size()];
|
||||
*out = step.value;
|
||||
*gate = step.gate;
|
||||
} else {
|
||||
*out = 0.0;
|
||||
*gate = 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
return Status::Ok();
|
||||
}
|
||||
|
||||
NOISICAA_REGISTER_NODE_CLASS(Node, "node://step-sequencer")
|
||||
|
||||
} // namespace noisicaa::engine::step_sequencer
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
// -*- mode: c++ -*-
|
||||
|
||||
/*
|
||||
* @begin:license
|
||||
*
|
||||
* Copyright (c) 2015-2021, Ben Niemann <pink@odahoda.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* @end:license
|
||||
*/
|
||||
|
||||
#ifndef _NOISICAA_NODE_LIB_STEP_SEQUENCER_H
|
||||
#define _NOISICAA_NODE_LIB_STEP_SEQUENCER_H
|
||||
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "noisicaa/core/status.h"
|
||||
#include "noisicaa/engine/processor_node.h"
|
||||
|
||||
namespace noisicaa::engine {
|
||||
|
||||
class EngineImpl;
|
||||
class Program;
|
||||
|
||||
namespace step_sequencer {
|
||||
|
||||
struct Spec;
|
||||
|
||||
class Node : public ProcessorNode {
|
||||
public:
|
||||
Node(EngineImpl* engine, uint64_t id);
|
||||
~Node() override;
|
||||
|
||||
Status init(const NodeDescription* description) override;
|
||||
Status run(BlockContext* ctxt, const std::vector<Buffer*>* buffers) override;
|
||||
Status handle_message(const std::string& msg) override;
|
||||
|
||||
private:
|
||||
std::atomic<Spec*> _next_spec;
|
||||
std::atomic<Spec*> _current_spec;
|
||||
std::atomic<Spec*> _old_spec;
|
||||
|
||||
int32_t _current_step = -1;
|
||||
bool _clock = false;
|
||||
};
|
||||
|
||||
} // namespace step_sequencer
|
||||
} // namespace noisicaa::engine
|
||||
|
||||
#endif
|
|
@ -0,0 +1,17 @@
|
|||
uri: builtin://step-sequencer
|
||||
audio_class_uri: node://step-sequencer
|
||||
model_class_uri: builtin://step-sequencer
|
||||
display_name: Step Sequencer
|
||||
ui:
|
||||
uri: builtin://step-sequencer
|
||||
body_qml: noisicaa/node_lib/step_sequencer/ui.qml
|
||||
ports:
|
||||
- name: clock
|
||||
direction: INPUT
|
||||
type: AUDIO
|
||||
- name: out
|
||||
direction: OUTPUT
|
||||
type: AUDIO
|
||||
- name: gate
|
||||
direction: OUTPUT
|
||||
type: AUDIO
|
|
@ -0,0 +1,78 @@
|
|||
# @begin:license
|
||||
#
|
||||
# Copyright (c) 2015-2019, Benjamin Niemann <pink@odahoda.de>
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# @end:license
|
||||
|
||||
import logging
|
||||
from typing import Any, Iterator
|
||||
|
||||
import flatbuffers
|
||||
|
||||
from noisicaa import model
|
||||
from . import node_message_generated as node_message_fb
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Step(model.BaseObject):
|
||||
value, valueChanged = model.ScalarProperty('value', float, default=0.0)
|
||||
gate, gateChanged = model.ScalarProperty('gate', bool, default=False)
|
||||
|
||||
def setup(self) -> None:
|
||||
super().setup()
|
||||
self.valueChanged.connect(lambda _: self.parent().updateSpec())
|
||||
self.gateChanged.connect(lambda _: self.parent().updateSpec())
|
||||
|
||||
|
||||
class Node(model.GraphNode):
|
||||
steps, stepsChanged = model.ListProperty('steps', Step)
|
||||
|
||||
def created(self, **kwargs: Any) -> None:
|
||||
super().created(**kwargs)
|
||||
for _ in range(8):
|
||||
self.steps.append(Step.create(parent=self))
|
||||
|
||||
def setup(self) -> None:
|
||||
super().setup()
|
||||
self.stepsChanged.connect(lambda _: self.updateSpec())
|
||||
|
||||
def updateSpec(self) -> None:
|
||||
self.project.engineChanges.call(model.SendNodeMessage(self.id, self.__setSpecMessage()))
|
||||
|
||||
def __setSpecMessage(self) -> bytes:
|
||||
builder = flatbuffers.Builder(1024)
|
||||
builder.Finish(node_message_fb.CreateSetSpec(
|
||||
builder,
|
||||
steps=[
|
||||
node_message_fb.CreateStep(
|
||||
builder,
|
||||
gate=step.gate,
|
||||
value=step.value,
|
||||
)
|
||||
for step in self.steps
|
||||
]))
|
||||
return builder.Output()
|
||||
|
||||
def initialEngineChanges(self) -> Iterator[model.EngineChange]:
|
||||
yield from super().initialEngineChanges()
|
||||
yield model.SendNodeMessage(self.id, self.__setSpecMessage())
|
||||
|
||||
|
||||
URI = 'builtin://step-sequencer'
|
||||
CLASS = Node
|
||||
EXTRA_CLASSES = [ Step ]
|
|
@ -0,0 +1,36 @@
|
|||
// @begin:license
|
||||
//
|
||||
// Copyright (c) 2015-2021, Ben Niemann <pink@odahoda.de>
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation; either version 2 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
//
|
||||
// @end:license
|
||||
|
||||
namespace noisicaa.engine.step_sequencer;
|
||||
|
||||
table Step {
|
||||
value : float;
|
||||
gate : bool;
|
||||
}
|
||||
|
||||
table SetSpec {
|
||||
steps : [Step];
|
||||
}
|
||||
|
||||
table NodeMessage {
|
||||
step : int;
|
||||
}
|
||||
|
||||
root_type NodeMessage;
|
|
@ -0,0 +1,69 @@
|
|||
# @begin:license
|
||||
#
|
||||
# Copyright (c) 2015-2019, Benjamin Niemann <pink@odahoda.de>
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# @end:license
|
||||
|
||||
import logging
|
||||
from typing import Any, Dict, List
|
||||
|
||||
from PySide2.QtCore import Qt
|
||||
from PySide2 import QtCore
|
||||
|
||||
from noisicaa import engine
|
||||
from noisicaa.ui import ui_base
|
||||
from noisicaa.ui.GraphNode import GraphNode
|
||||
from . import node_message_generated as node_message_fb
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class Node(GraphNode):
|
||||
currentStep, currentStepChanged = ui_base.Property('currentStep', int)
|
||||
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.setNodeMessageHandler(self.__handleNodeMessage)
|
||||
|
||||
def __handleNodeMessage(self, msg_serialized: bytes) -> None:
|
||||
msg = node_message_fb.NodeMessage(msg_serialized)
|
||||
self.currentStep = msg.step
|
||||
|
||||
@QtCore.Slot(int, bool)
|
||||
def setStepGate(self, index: int, value: bool) -> None:
|
||||
assert 0 <= index < len(self.node.steps)
|
||||
|
||||
if value == self.node.steps[index].gate:
|
||||
return
|
||||
|
||||
with self.project.captureChanges("{}: Toggle step".format(self.node.title)):
|
||||
self.node.steps[index].gate = value
|
||||
|
||||
@QtCore.Slot(int, float)
|
||||
def setStepValue(self, index: int, value: float) -> None:
|
||||
assert 0 <= index < len(self.node.steps)
|
||||
|
||||
if value == self.node.steps[index].value:
|
||||
return
|
||||
|
||||
with self.project.captureChanges("{}: Change step value".format(self.node.title)):
|
||||
self.node.steps[index].value = value
|
||||
|
||||
|
||||
URI = 'builtin://step-sequencer'
|
||||
CLASS = Node
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
* @begin:license
|
||||
*
|
||||
* Copyright (c) 2015-2019, Benjamin Niemann <pink@odahoda.de>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* @end:license
|
||||
*/
|
||||
|
||||
import QtQuick 2.12
|
||||
import QtQuick.Controls 2.15
|
||||
import QtQuick.Layouts 1.15
|
||||
import noisicaa.ui.components 1.0
|
||||
|
||||
ColumnLayout {
|
||||
property QtObject d
|
||||
anchors.centerIn: parent
|
||||
|
||||
RowLayout {
|
||||
Repeater {
|
||||
model: d.node.steps
|
||||
|
||||
ColumnLayout {
|
||||
property QtObject s: d.node.steps[index]
|
||||
|
||||
Rectangle {
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: 32
|
||||
|
||||
color: index == d.currentStep ? "#aaaaff" : "transparent"
|
||||
|
||||
Text {
|
||||
anchors.centerIn: parent
|
||||
text: index
|
||||
}
|
||||
}
|
||||
|
||||
CheckBox {
|
||||
id: gateButton
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
checked: s.gate
|
||||
onToggled: d.setStepGate(index, checked);
|
||||
}
|
||||
|
||||
Slider {
|
||||
id: valueSlider
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
orientation: Qt.Vertical
|
||||
from: 0.0
|
||||
to: 1.0
|
||||
value: s.value
|
||||
onValueChanged: d.setStepValue(index, value);
|
||||
}
|
||||
|
||||
Text {
|
||||
Layout.alignment: Qt.AlignHCenter
|
||||
text: valueSlider.value.toFixed(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
# @begin:license
|
||||
#
|
||||
# Copyright (c) 2015-2021, Ben Niemann <pink@odahoda.de>
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# @end:license
|
||||
|
||||
def build(ctx):
|
||||
ctx.noise_node(
|
||||
has_node_message=True,
|
||||
has_audio=True,
|
||||
has_model=True,
|
||||
has_ui=True,
|
||||
has_qml=True,
|
||||
)
|
Loading…
Reference in New Issue