From d023146c6587a60bc5922b9b954731a56d935ebc Mon Sep 17 00:00:00 2001 From: bog Date: Wed, 31 Jan 2024 10:23:03 +0100 Subject: [PATCH] :sparkles: noise command. --- doc/guide/signals.rst | 10 +++++++++ lib/CMakeLists.txt | 5 +++++ lib/Compiler.cpp | 49 +++++++++++++++++++++++++++++-------------- lib/Compiler.hpp | 1 + lib/Noise.cpp | 28 +++++++++++++++++++++++++ lib/Noise.hpp | 27 ++++++++++++++++++++++++ 6 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 lib/Noise.cpp create mode 100644 lib/Noise.hpp diff --git a/doc/guide/signals.rst b/doc/guide/signals.rst index 48b82b0..c024513 100644 --- a/doc/guide/signals.rst +++ b/doc/guide/signals.rst @@ -39,3 +39,13 @@ To generate a sine, we can use the ``sine`` command. # sine signal with a frequency of 440 and an amplitude of 1 [sine 440 1] + + +Noise +^^^^^ + +The noise command generate uniform random frames. + +.. code-block:: + + [noise] diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 5433c03..a5a7da0 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -9,13 +9,18 @@ configure_file( add_library(muz-lib OBJECT # Audio + # ===== Signal.cpp AudioEngine.cpp AudioConf.cpp + # signals + # ------- Constant.cpp Sine.cpp + Noise.cpp # Language + # ======== Node.cpp Lexer.cpp Parser.cpp diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index f0150d7..f1ce4ea 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -1,6 +1,10 @@ #include "Compiler.hpp" + +// signals +// ------- #include "Constant.hpp" #include "Sine.hpp" +#include "Noise.hpp" namespace muz { @@ -38,23 +42,8 @@ namespace muz compile_node(node->child(i)); } - if (name == "sine") - { - check_cmd_arity(*node, 2); + execute_cmd(name, node); - auto one = pop(); - auto signal = std::make_unique(m_conf, - std::move(pop()), - std::move(one)); - push(std::move(signal)); - } - else - { - throw compile_error { - std::string() - + "cannot compile unknown command '" + name + "'." - }; - } } break; case NODE_DIR: { @@ -84,6 +73,34 @@ namespace muz } } + void Compiler::execute_cmd(std::string const& name, + std::shared_ptr node) + { + if (name == "sine") + { + check_cmd_arity(*node, 2); + + auto one = pop(); + auto signal = std::make_unique(m_conf, + pop(), + std::move(one)); + push(std::move(signal)); + } + else if (name == "noise") + { + check_cmd_arity(*node, 0); + auto signal = std::make_unique(m_conf); + push(std::move(signal)); + } + else + { + throw compile_error { + std::string() + + "cannot compile unknown command '" + name + "'." + }; + } + } + void Compiler::push(std::unique_ptr signal) { m_signals.push_back(std::move(signal)); diff --git a/lib/Compiler.hpp b/lib/Compiler.hpp index ed6d5a6..3fc5173 100644 --- a/lib/Compiler.hpp +++ b/lib/Compiler.hpp @@ -24,6 +24,7 @@ namespace muz std::vector> compile(std::shared_ptr node); void compile_node(std::shared_ptr node); + void execute_cmd(std::string const& name, std::shared_ptr node); private: AudioConf const& m_conf; diff --git a/lib/Noise.cpp b/lib/Noise.cpp new file mode 100644 index 0000000..5a31675 --- /dev/null +++ b/lib/Noise.cpp @@ -0,0 +1,28 @@ +#include "Noise.hpp" + +namespace muz +{ + /*explicit*/ Noise::Noise(AudioConf const& conf) + : m_conf { conf } + { + m_rand.seed(std::chrono::steady_clock::now().time_since_epoch().count()); + } + + /*virtual*/ Noise::~Noise() + { + } + + std::vector Noise::next() /*override*/ + { + std::vector out; + + float value = m_distribution(m_rand); + + for (int i=0; i +#include + +#include "commons.hpp" +#include "AudioConf.hpp" +#include "Signal.hpp" + +namespace muz +{ + class Noise: public Signal + { + public: + explicit Noise(AudioConf const& conf); + virtual ~Noise(); + + std::vector next() override; + private: + AudioConf m_conf; + std::mt19937 m_rand; + std::uniform_real_distribution m_distribution {-0.5f, 0.5f}; + }; +} + +#endif