'Clock' node to generate pulses in BPM units.

main
Ben Niemann 1 year ago
parent a6831d6569
commit 3ccbc3a7a5

@ -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,71 @@
/*
* @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 <cmath>
#include <string>
#include <vector>
#include <flatbuffers/flatbuffers.h>
#include "noisicaa/core/flatbuffers.h"
#include "noisicaa/engine/engine_impl.h"
#include "noisicaa/engine/buffers.h"
#include "noisicaa/engine/block_context.h"
#include "noisicaa/engine/node_db.h"
#include "noisicaa/node_lib/clock/audio.h"
#include "noisicaa/node_lib/clock/node_message_generated.h"
namespace noisicaa::engine::clock {
Node::Node(EngineImpl* engine, uint64_t id)
: ProcessorNode(engine, id, "noisicaa.node_lib.clock") {}
Status Node::run(BlockContext* ctxt, const std::vector<Buffer*>* buffers) {
float bpm = ((float*)buffers->at(0)->data())[0];
float* out = (float*)buffers->at(1)->data();
uint32_t next_click = _engine->sample_rate() / (std::max(1.0f, std::min(bpm, 1000.0f)) / 60.0);
for (uint32_t smpl = 0 ; smpl < ctxt->block_size ; ++smpl) {
if (_counter >= next_click) {
*out++ = 1.0;
_counter = 0;
FlatBufferAllocator allocator;
flatbuffers::FlatBufferBuilder fbs(allocator.buf_size, &allocator);
NodeMessageBuilder msg_builder(fbs);
fbs.Finish(msg_builder.Finish());
push_node_message(ctxt, fbs.GetBufferPointer(), fbs.GetSize());
} else {
*out++ = 0.0;
++_counter;
}
}
return Status::Ok();
}
NOISICAA_REGISTER_NODE_CLASS(Node, "node://clock")
} // namespace noisicaa::engine::clock

@ -0,0 +1,54 @@
// -*- 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_CLOCK_H
#define _NOISICAA_NODE_LIB_CLOCK_H
#include <memory>
#include <vector>
#include "noisicaa/core/status.h"
#include "noisicaa/engine/processor_node.h"
namespace noisicaa::engine {
class EngineImpl;
class Program;
namespace clock {
class Node : public ProcessorNode {
public:
Node(EngineImpl* engine, uint64_t id);
Status run(BlockContext* ctxt, const std::vector<Buffer*>* buffers) override;
private:
uint32_t _counter = 0x7fffffff;
};
} // namespace clocl
} // namespace noisicaa::engine
#endif

@ -0,0 +1,17 @@
uri: builtin://clock
audio_class_uri: node://clock
display_name: Clock
ui:
uri: builtin://clock
body_qml: noisicaa/node_lib/clock/ui.qml
ports:
- name: bpm
direction: INPUT
type: CONTROL
float_value:
default: 120.0
min: 1.0
max: 1000.0
- name: out
direction: OUTPUT
type: AUDIO

@ -0,0 +1,33 @@
# @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 noisicaa import model
logger = logging.getLogger(__name__)
class Node(model.GraphNode):
pass
URI = 'builtin://clock'
CLASS = Node

@ -0,0 +1,27 @@
// @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.clock;
table NodeMessage {
// The message itself means that that click was produced, so no content is needed.
}
root_type NodeMessage;

@ -0,0 +1,45 @@
# @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
from PySide2 import QtCore
from noisicaa.ui.GraphNode import GraphNode
logger = logging.getLogger(__name__)
class Node(GraphNode):
click = QtCore.Signal()
def __init__(self, **kwargs: Any) -> None:
super().__init__(**kwargs)
self.setNodeMessageHandler(self.__handleNodeMessage)
def __handleNodeMessage(self, msg_serialized_unused: bytes) -> None:
# Don't care what the messasge it, because it just means 'click'.
self.click.emit()
URI = 'builtin://clock'
CLASS = Node

@ -0,0 +1,72 @@
/*
* @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
ColumnLayout {
property QtObject d
anchors.centerIn: parent
Rectangle {
Layout.fillWidth: true
Layout.preferredHeight: 32
color: "#000000"
ColorAnimation on color {
id: clickAnimation;
from: "#FF0000"
to: "#000000"
duration: 200
}
}
RowLayout {
Layout.alignment: Qt.AlignHCenter
Label {
text: "BPM"
}
TextInput {
id: bpmInput
Layout.preferredWidth: 80
property var p: d.ports[0]
enabled: p.numConnections == 0
text: "%1".arg(p.port.controlValue)
validator: RegExpValidator { regExp : /[.0-9]{0,7}/ }
onEditingFinished: function() {
text = Math.max(1.0, Math.min(parseFloat(text), 1000.0));
}
onTextEdited: function() {
if (text) {
p.setControlValue(parseFloat(text));
}
}
}
}
Component.onCompleted: function() {
d.click.connect(clickAnimation.restart);
}
}

@ -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_audio=True,
has_model=True,
has_ui=True,
has_qml=True,
has_node_message=True,
)
Loading…
Cancel
Save