score-plugin-fx/Fx/Envelope.hpp
1 #pragma once
2 #include <Engine/Node/SimpleApi.hpp>
3 
4 #include <numeric>
5 namespace Nodes
6 {
7 namespace Envelope
8 {
9 struct Node
10 {
12  {
13  static const constexpr auto prettyName = "Envelope";
14  static const constexpr auto objectKey = "Envelope";
15  static const constexpr auto category = "Audio";
16  static const constexpr auto author = "ossia score";
17  static const constexpr auto kind
18  = Process::ProcessCategory::Analyzer | Process::ProcessCategory::Deprecated;
19  static const constexpr auto description
20  = "Converts an audio signal into RMS and peak values";
21  static const constexpr auto tags = std::array<const char*, 0>{};
22  static const uuid_constexpr auto uuid
23  = make_uuid("95F44151-13EF-4537-8189-0CC243341269");
24 
25  static const constexpr audio_in audio_ins[]{"in"};
26  static const constexpr value_out value_outs[]{"rms", "peak"};
27  };
28 
29  using control_policy = ossia::safe_nodes::default_tick;
30  static auto get(const ossia::audio_channel& chan)
31  {
32  if(chan.size() > 0)
33  {
34  auto max = chan[0];
35  auto rms = 0.;
36  for(auto sample : chan)
37  {
38  max = std::max(max, std::abs(sample));
39  rms += sample * sample;
40  }
41  rms = std::sqrt(rms);
42  rms /= chan.size();
43 
44  return std::make_pair(rms, max);
45  }
46  else
47  {
48  using val_t = ossia::audio_channel::value_type;
49  return std::make_pair(val_t{}, val_t{});
50  }
51  }
52 
53  static void
54  run(const ossia::audio_port& audio, ossia::value_port& rms_port,
55  ossia::value_port& peak_port, ossia::token_request tk, ossia::exec_state_facade e)
56  {
57  const auto [tick_start, d] = e.timings(tk);
58  switch(audio.channels())
59  {
60  case 0:
61  return;
62  case 1: {
63  auto [rms, peak] = get(audio.channel(0));
64 
65  rms_port.write_value(rms, tick_start);
66  peak_port.write_value(peak, tick_start);
67  break;
68  }
69  default: {
70  std::vector<ossia::value> peak_vec;
71  peak_vec.reserve(audio.channels());
72  std::vector<ossia::value> rms_vec;
73  rms_vec.reserve(audio.channels());
74  for(auto& c : audio)
75  {
76  auto [rms, peak] = get(c);
77  rms_vec.push_back(rms);
78  peak_vec.push_back(peak);
79  }
80  rms_port.write_value(rms_vec, tick_start);
81  peak_port.write_value(peak_vec, tick_start);
82  }
83  break;
84  }
85  }
86 };
87 }
88 }
Definition: SimpleApi.hpp:32
Definition: score-plugin-fx/Fx/Envelope.hpp:12
Definition: score-plugin-fx/Fx/Envelope.hpp:10