2#include <ossia/dataflow/graph_node.hpp>
3#include <ossia/dataflow/port.hpp>
6#include <boost/predef.h>
10struct sine final :
public ossia::nonowning_graph_node
12 ossia::value_inlet freq_in;
13 ossia::audio_outlet audio_out;
20 double amplitude = 0.8;
23 m_inlets.push_back(&freq_in);
24 m_outlets.push_back(&audio_out);
27 std::string label() const noexcept
override {
return "sine"; }
28 void run(
const ossia::token_request& t, ossia::exec_state_facade st)
noexcept override
30 auto& vals = freq_in.target<ossia::value_port>()->get_data();
33 freq =
ossia::clamp(ossia::convert<float>(vals.back().value), 0.f, 20000.f);
36 ossia::audio_port& audio = *audio_out;
37 const auto [tick_start, N] = st.timings(t);
41 audio.set_channels(1);
42 auto& c = audio.channel(0);
43 c.resize(tick_start + N);
47 const auto fs = st.sampleRate();
48 auto frequ_cos = std::cos(ossia::two_pi * freq / fs);
49 auto frequ_sin = std::sin(ossia::two_pi * freq / fs);
50 for(int64_t i = tick_start; i < tick_start + N; i++)
52 auto new_cos = m_cos * frequ_cos - m_sin * frequ_sin;
53 auto new_sin = m_cos * frequ_sin + m_sin * frequ_cos;
54 auto norm = (new_cos * new_cos + new_sin * new_sin);
56 m_cos = new_cos / norm;
57 m_sin = new_sin / norm;
59 c[i] = amplitude * m_sin;
65struct sine_value final :
public ossia::nonowning_graph_node
67 ossia::value_inlet freq_in;
68 ossia::value_outlet sin_out;
75 double amplitude = 0.8;
78 m_inlets.push_back(&freq_in);
79 m_outlets.push_back(&sin_out);
82 std::string label() const noexcept
override {
return "sine"; }
83 void run(
const ossia::token_request& t, ossia::exec_state_facade st)
noexcept override
85 auto& vals = freq_in.target<ossia::value_port>()->get_data();
88 freq =
ossia::clamp(ossia::convert<float>(vals.back().value), 0.f, 20000.f);
91 ossia::value_port& out = *sin_out;
92 const auto [tick_start, N] = st.timings(t);
94 const auto fs = st.sampleRate();
95 auto frequ_cos = std::cos(ossia::two_pi * freq / fs);
96 auto frequ_sin = std::sin(ossia::two_pi * freq / fs);
97 auto new_cos = m_cos * frequ_cos - m_sin * frequ_sin;
98 auto new_sin = m_cos * frequ_sin + m_sin * frequ_cos;
99 auto norm = (new_cos * new_cos + new_sin * new_sin);
100 m_cos = new_cos / norm;
101 m_sin = new_sin / norm;
103 out.write_value(amplitude * m_sin, tick_start);
OSSIA_INLINE constexpr T clamp(T d, const T min, const T max) noexcept
clamp Returns the value bounded by a min and a max
Definition math.hpp:154