Anklang-0.3.0.dev797+g4e3241f3 anklang-0.3.0.dev797+g4e3241f3
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
46 prepare_event_input();
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}
#define _(...)
Retrieve the translation of a C or C++ string.
Definition internal.hh:18
The Anklang C++ API namespace.
Definition api.hh:9
uint64_t uint64
A 64-bit unsigned integer, use PRI*64 in format strings.
Definition cxxaux.hh:25
SpeakerArrangement
Flags to indicate channel arrangements of a bus.
Definition transport.hh:11
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:35
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
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