sine only takes frequency as argument (amplitude is no more specified).

main
bog 2024-02-01 20:43:41 +01:00
parent f27d1434da
commit 3fa209f9ab
5 changed files with 19 additions and 31 deletions

View File

@ -7,20 +7,20 @@ start with a simple example.
.. code-block:: bash .. code-block:: bash
@out [sine 440 1] @out [sine 440]
Here, ``@out`` is a *directive* and ``sine`` is a *command*. Here, ``@out`` is a *directive* and ``sine`` is a *command*.
Let start with commands. A command is kind of a function. It has a Let start with commands. A command is kind of a function. It has a
name followed by its parameters if any. name followed by its parameters if any.
Here, the command ``sine`` takes two parameters: a frequency and an Here, the command ``sine`` takes one parameter: the signal
amplitude. Like its name suggests, ``sine`` is a signal composed of frequency. Like its name suggests, ``sine`` is a signal composed of
one sine wave with the given frequency and amplitude. one sine wave with the given frequency and an amplitude of 1.
.. note:: .. note::
The parameters ``440`` and ``1`` are not simple numbers. In fact, The parameter ``440`` is not a simple number. In fact,
their are signals too ! We call them constant signal. 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 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 be played. That is what the directive ``@out`` does. It will play the

View File

@ -24,9 +24,9 @@ commands. In MuzScript, every numbers are constant signals.
Sine Sine
^^^^ ^^^^
The sine wave signal is the fundamental signal in sound theory. The sine wave signal is the fundamental signal in sound theory. It
It takes two arguments: a frequency and an amplitude. takes the frequency of the signal as argument. Mathematically we can
Mathematically we can define our sine using the following formula: define our sine using the following formula:
.. math:: .. math::
@ -38,7 +38,7 @@ To generate a sine, we can use the ``sine`` command.
.. code-block:: .. code-block::
# sine signal with a frequency of 440 and an amplitude of 1 # sine signal with a frequency of 440 and an amplitude of 1
[sine 440 1] [sine 440]
Noise Noise
@ -60,11 +60,11 @@ different ways.
Add 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:: .. code-block::
[sum [sine 220] [sine 220]] [add [sine 220] [sine 220]]
Sub Sub

View File

@ -82,12 +82,9 @@ namespace muz
{ {
if (name == "sine") if (name == "sine")
{ {
check_cmd_arity(*node, 2); check_cmd_arity(*node, 1);
auto one = pop(); auto signal = std::make_unique<Sine>(m_conf, pop());
auto signal = std::make_unique<Sine>(m_conf,
pop(),
std::move(one));
push(std::move(signal)); push(std::move(signal));
} }
else if (name == "noise") else if (name == "noise")

View File

@ -3,13 +3,10 @@
namespace muz namespace muz
{ {
/*explicit*/ Sine::Sine(AudioConf const& conf, /*explicit*/ Sine::Sine(AudioConf const& conf,
std::unique_ptr<Signal> freq, std::unique_ptr<Signal> freq)
std::unique_ptr<Signal> amplitude)
: m_conf { conf } : m_conf { conf }
, m_out_freq {std::vector<float> (m_conf.channels(), 0.0f)} , m_out_freq {std::vector<float> (m_conf.channels(), 0.0f)}
, m_out_amp {std::vector<float> (m_conf.channels(), 0.0f)}
, m_freq { std::move(freq) } , m_freq { std::move(freq) }
, m_amplitude { std::move(amplitude) }
{ {
for (size_t i=0; i<static_cast<size_t>(m_conf.channels()); i++) for (size_t i=0; i<static_cast<size_t>(m_conf.channels()); i++)
{ {
@ -24,20 +21,17 @@ namespace muz
void Sine::next(std::vector<float>& out) /*override*/ void Sine::next(std::vector<float>& out) /*override*/
{ {
assert(m_freq); assert(m_freq);
assert(m_amplitude);
m_freq->next(m_out_freq); m_freq->next(m_out_freq);
m_amplitude->next(m_out_amp);
if (m_out_freq.size() != m_out_amp.size() if (m_out_freq.size() != m_phases.size())
|| m_out_freq.size() != m_phases.size())
{ {
throw signal_error {"cannot generate sine: channel number mismatch"}; throw signal_error {"cannot generate sine: channel number mismatch"};
} }
for (size_t i=0; i<static_cast<size_t>(m_conf.channels()); i++) for (size_t i=0; i<static_cast<size_t>(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] m_phases[i] += 2 * M_PI * m_out_freq[i]
/ static_cast<float>(m_conf.samplerate()); / static_cast<float>(m_conf.samplerate());

View File

@ -8,24 +8,21 @@
namespace muz 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 class Sine: public Signal
{ {
public: public:
explicit Sine(AudioConf const& conf, explicit Sine(AudioConf const& conf,
std::unique_ptr<Signal> freq, std::unique_ptr<Signal> freq);
std::unique_ptr<Signal> amplitude);
virtual ~Sine(); virtual ~Sine();
void next(std::vector<float>& out) override; void next(std::vector<float>& out) override;
private: private:
AudioConf const& m_conf; AudioConf const& m_conf;
std::vector<float> m_out_freq; std::vector<float> m_out_freq;
std::vector<float> m_out_amp;
std::unique_ptr<Signal> m_freq; std::unique_ptr<Signal> m_freq;
std::unique_ptr<Signal> m_amplitude;
std::vector<float> m_phases; std::vector<float> m_phases;
}; };
} }