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
@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

View File

@ -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

View File

@ -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<Sine>(m_conf,
pop(),
std::move(one));
auto signal = std::make_unique<Sine>(m_conf, pop());
push(std::move(signal));
}
else if (name == "noise")

View File

@ -3,13 +3,10 @@
namespace muz
{
/*explicit*/ Sine::Sine(AudioConf const& conf,
std::unique_ptr<Signal> freq,
std::unique_ptr<Signal> amplitude)
std::unique_ptr<Signal> freq)
: m_conf { conf }
, 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_amplitude { std::move(amplitude) }
{
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*/
{
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<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]
/ static_cast<float>(m_conf.samplerate());

View File

@ -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<Signal> freq,
std::unique_ptr<Signal> amplitude);
std::unique_ptr<Signal> freq);
virtual ~Sine();
void next(std::vector<float>& out) override;
private:
AudioConf const& m_conf;
std::vector<float> m_out_freq;
std::vector<float> m_out_amp;
std::unique_ptr<Signal> m_freq;
std::unique_ptr<Signal> m_amplitude;
std::vector<float> m_phases;
};
}