From 3fa209f9aba0d5709ef5632852f665655b820f23 Mon Sep 17 00:00:00 2001 From: bog Date: Thu, 1 Feb 2024 20:43:41 +0100 Subject: [PATCH] :sparkles: sine only takes frequency as argument (amplitude is no more specified). --- doc/guide/quickstart.rst | 12 ++++++------ doc/guide/signals.rst | 12 ++++++------ lib/Compiler.cpp | 7 ++----- lib/Sine.cpp | 12 +++--------- lib/Sine.hpp | 7 ++----- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/doc/guide/quickstart.rst b/doc/guide/quickstart.rst index 782c0c6..f2bea7f 100644 --- a/doc/guide/quickstart.rst +++ b/doc/guide/quickstart.rst @@ -7,20 +7,20 @@ start with a simple example. .. code-block:: bash - @out [sine 440 1] + @out [sine 440] Here, ``@out`` is a *directive* and ``sine`` is a *command*. Let start with commands. A command is kind of a function. It has a name followed by its parameters if any. -Here, the command ``sine`` takes two parameters: a frequency and an -amplitude. Like its name suggests, ``sine`` is a signal composed of -one sine wave with the given frequency and amplitude. +Here, the command ``sine`` takes one parameter: the signal +frequency. Like its name suggests, ``sine`` is a signal composed of +one sine wave with the given frequency and an amplitude of 1. .. note:: - The parameters ``440`` and ``1`` are not simple numbers. In fact, - their are signals too ! We call them constant signal. + The parameter ``440`` is not a simple number. In fact, + it's a signals too ! We call this kind of number **constant signal**. In order to hear our sine wave, we have to specify how we want it to be played. That is what the directive ``@out`` does. It will play the diff --git a/doc/guide/signals.rst b/doc/guide/signals.rst index 4d6fa81..d5e9c62 100644 --- a/doc/guide/signals.rst +++ b/doc/guide/signals.rst @@ -24,9 +24,9 @@ commands. In MuzScript, every numbers are constant signals. Sine ^^^^ -The sine wave signal is the fundamental signal in sound theory. -It takes two arguments: a frequency and an amplitude. -Mathematically we can define our sine using the following formula: +The sine wave signal is the fundamental signal in sound theory. It +takes the frequency of the signal as argument. Mathematically we can +define our sine using the following formula: .. math:: @@ -38,7 +38,7 @@ To generate a sine, we can use the ``sine`` command. .. code-block:: # sine signal with a frequency of 440 and an amplitude of 1 - [sine 440 1] + [sine 440] Noise @@ -60,11 +60,11 @@ different ways. Add ^^^ -The ``sum`` command takes two signals as input and outputs their sum. +The ``add`` command takes two signals as input and outputs their sum. .. code-block:: - [sum [sine 220] [sine 220]] + [add [sine 220] [sine 220]] Sub diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp index 165ff72..ea0a3fb 100644 --- a/lib/Compiler.cpp +++ b/lib/Compiler.cpp @@ -82,12 +82,9 @@ namespace muz { if (name == "sine") { - check_cmd_arity(*node, 2); + check_cmd_arity(*node, 1); - auto one = pop(); - auto signal = std::make_unique(m_conf, - pop(), - std::move(one)); + auto signal = std::make_unique(m_conf, pop()); push(std::move(signal)); } else if (name == "noise") diff --git a/lib/Sine.cpp b/lib/Sine.cpp index 7fd842f..008817f 100644 --- a/lib/Sine.cpp +++ b/lib/Sine.cpp @@ -3,13 +3,10 @@ namespace muz { /*explicit*/ Sine::Sine(AudioConf const& conf, - std::unique_ptr freq, - std::unique_ptr amplitude) + std::unique_ptr freq) : m_conf { conf } , m_out_freq {std::vector (m_conf.channels(), 0.0f)} - , m_out_amp {std::vector (m_conf.channels(), 0.0f)} , m_freq { std::move(freq) } - , m_amplitude { std::move(amplitude) } { for (size_t i=0; i(m_conf.channels()); i++) { @@ -24,20 +21,17 @@ namespace muz void Sine::next(std::vector& out) /*override*/ { assert(m_freq); - assert(m_amplitude); m_freq->next(m_out_freq); - m_amplitude->next(m_out_amp); - if (m_out_freq.size() != m_out_amp.size() - || m_out_freq.size() != m_phases.size()) + if (m_out_freq.size() != m_phases.size()) { throw signal_error {"cannot generate sine: channel number mismatch"}; } for (size_t i=0; i(m_conf.channels()); i++) { - float const value = m_out_amp[i] * std::sin(m_phases[i]); + float const value = std::sin(m_phases[i]); m_phases[i] += 2 * M_PI * m_out_freq[i] / static_cast(m_conf.samplerate()); diff --git a/lib/Sine.hpp b/lib/Sine.hpp index 9416076..38dca7b 100644 --- a/lib/Sine.hpp +++ b/lib/Sine.hpp @@ -8,24 +8,21 @@ namespace muz { /** - * Sinusoid signal with an amplitude and a frequency. + * Sinusoid signal with an amplitude of 1 at a given frequency. **/ class Sine: public Signal { public: explicit Sine(AudioConf const& conf, - std::unique_ptr freq, - std::unique_ptr amplitude); + std::unique_ptr freq); virtual ~Sine(); void next(std::vector& out) override; private: AudioConf const& m_conf; std::vector m_out_freq; - std::vector m_out_amp; std::unique_ptr m_freq; - std::unique_ptr m_amplitude; std::vector m_phases; }; }