Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
saturation.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "ase/processor.hh"
3#include "ase/internal.hh"
4#include "saturationdsp.hh"
5
6namespace {
7
8using namespace Ase;
9
10class Saturation : public AudioProcessor {
11 IBusId stereoin;
12 OBusId stereout;
13 SaturationDSP saturation;
14public:
15 Saturation (const ProcessorSetup &psetup) :
16 AudioProcessor (psetup)
17 {}
18 static void
19 static_info (AudioProcessorInfo &info)
20 {
21 info.version = "1";
22 info.label = "Saturation";
23 info.category = "Distortion";
24 info.creator_name = "Stefan Westerfeld";
25 info.website_url = "https://anklang.testbit.eu";
26 }
27 enum Params { MODE = 1, MIX, DRIVE };
28 void
29 initialize (SpeakerArrangement busses) override
30 {
31 stereoin = add_input_bus ("Stereo In", SpeakerArrangement::STEREO);
32 stereout = add_output_bus ("Stereo Out", SpeakerArrangement::STEREO);
33
34 ParameterMap pmap;
35 pmap.group = "Settings";
36
37 ChoiceS centries;
38 centries += { "Soft/tanh", "Soft Saturation using the tanh function" };
39 centries += { "Hard", "Hard clipping" };
40 pmap[MODE] = Param { "mode", "Mode", "Mode", 0, "", std::move (centries), "", { String ("blurb=") + _("Saturation Function"), } };
41 pmap[MIX] = Param { "mix", "Mix dry/wet", "Mix", 100, "%", { 0, 100 } };
42 pmap[DRIVE] = Param { "drive", "Drive", "Drive", 0, "dB", { -6, 36 } };
43
44 install_params (pmap);
45
47 }
48 SaturationDSP::Mode
49 map_mode (int m)
50 {
51 if (m == 1)
52 return SaturationDSP::Mode::HARD_CLIP;
53 return SaturationDSP::Mode::TANH_CHEAP;
54 }
55 void
56 adjust_param (uint32_t tag) override
57 {
58 switch (Params (tag))
59 {
60 case DRIVE: saturation.set_drive (get_param (DRIVE), false);
61 return;
62 case MIX: saturation.set_mix (get_param (MIX), false);
63 return;
64 case MODE: saturation.set_mode (map_mode (get_param (MODE)));
65 return;
66 }
67 }
68 void
69 reset (uint64 target_stamp) override
70 {
71 saturation.reset (sample_rate());
72 adjust_all_params();
73 }
74 void
75 render_audio (float *left_in, float *right_in, float *left_out, float *right_out, uint n_frames)
76 {
77 if (!n_frames)
78 return;
79
80 saturation.process<true> (left_in, right_in, left_out, right_out, n_frames);
81 }
82 void
83 render (uint n_frames) override
84 {
85 float *left_in = const_cast<float*> (ifloats (stereoin, 0));
86 float *right_in = const_cast<float*> (ifloats (stereoin, 1));
87 float *left_out = oblock (stereout, 0);
88 float *right_out = oblock (stereout, 1);
89
90 uint offset = 0;
91 MidiEventInput evinput = midi_event_input();
92 for (const auto &ev : evinput)
93 {
94 // process any audio that is before the event
95 render_audio (left_in + offset, right_in + offset, left_out + offset, right_out + offset, ev.frame - offset);
96 offset = ev.frame;
97
98 switch (ev.message())
99 {
100 case MidiMessage::PARAM_VALUE:
101 apply_event (ev);
102 adjust_param (ev.param);
103 break;
104 default: ;
105 }
106 }
107 // process frames after last event
108 render_audio (left_in + offset, right_in + offset, left_out + offset, right_out + offset, n_frames - offset);
109 }
110};
111static auto saturation = register_audio_processor<Saturation> ("Ase::Devices::Saturation");
112
113}
Audio signal AudioProcessor base class, implemented by all effects and instruments.
Definition processor.hh:76
const float * ifloats(IBusId b, uint c) const
Access readonly float buffer of input bus b, channel c, see also ofloats().
Definition processor.hh:546
MidiEventInput midi_event_input()
Access the current MidiEvent inputs during render(), needs prepare_event_input().
Definition processor.cc:844
OBusId add_output_bus(CString uilabel, SpeakerArrangement speakerarrangement, const String &hints="", const String &blurb="")
Add an output bus with uilabel and channels configured via speakerarrangement.
Definition processor.cc:530
virtual void initialize(SpeakerArrangement busses)=0
Definition processor.cc:432
void apply_event(const MidiEvent &event)
Assign MidiEvent::PARAM_VALUE event values to parameters.
Definition processor.hh:387
IBusId add_input_bus(CString uilabel, SpeakerArrangement speakerarrangement, const String &hints="", const String &blurb="")
Add an input bus with uilabel and channels configured via speakerarrangement.
Definition processor.cc:510
void install_params(const AudioParams::Map &params)
Reset list of parameters, enqueues parameter value initializaiton events.
Definition processor.cc:213
float * oblock(OBusId b, uint c)
Definition processor.hh:561
#define _(...)
Retrieve the translation of a C or C++ string.
Definition internal.hh:18
The Anklang C++ API namespace.
Definition api.hh:9
CString website_url
Website of/about this AudioProcessor.
Definition processor.hh:35
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:25
IBusId
ID type for AudioProcessor input buses, buses are numbered with increasing index.
Definition processor.hh:21
SpeakerArrangement
Definition transport.hh:11
OBusId
ID type for AudioProcessor output buses, buses are numbered with increasing index.
Definition processor.hh:25
CString version
Version identifier.
Definition processor.hh:31
CString creator_name
Name of the creator.
Definition processor.hh:36
CString label
Preferred user interface name.
Definition processor.hh:30
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:35
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
CString category
Category to allow grouping for processors of similar function.
Definition processor.hh:32
Detailed information and common properties of AudioProcessor subclasses.
Definition processor.hh:29
Structured initializer for Parameter.
Definition parameter.hh:31
Parameter list construction helper.
Definition parameter.hh:93
String group
Group to be applied to all newly inserted Parameter objects.
Definition parameter.hh:95