Vital
Loading...
Searching...
No Matches
synth_types.cpp
Go to the documentation of this file.
1/*
2Summary:
3In these files, we define fundamental types and structures that are central to the internal state and logic of the Vital synthesizer engine. The ModulationConnection and ModulationConnectionBank manage the links between modulation sources (like LFOs) and destinations (like filter cutoff). The StringLayout helps map a computer keyboard to musical notes. The modulation_change, control_map, input_map, and output_map typedefs provide convenient, strongly-typed ways to manage changes to controls, handle various inputs, and track outputs throughout the synthesizer’s audio graph.
4*/
5
6#include "synth_types.h"
7
8#include "synth_constants.h"
10
11namespace vital {
12 namespace {
13 // Delimiter used in modulation source names to separate prefixes.
14 const std::string kModulationSourceDelimiter = "_";
15
16 // Set of known prefixes that produce bipolar values by default (e.g. "lfo", "random").
17 const std::set<std::string> kBipolarModulationSourcePrefixes = {
18 "lfo",
19 "stereo",
20 "random",
21 "pitch"
22 };
23
30 force_inline bool isConnectionAvailable(ModulationConnection* connection) {
31 return connection->source_name.empty() && connection->destination_name.empty();
32 }
33 }
34
35 ModulationConnection::ModulationConnection(int index, std::string from, std::string to) :
36 source_name(std::move(from)), destination_name(std::move(to)) {
37 // Create a ModulationConnectionProcessor for this connection index.
38 modulation_processor = std::make_unique<ModulationConnectionProcessor>(index);
39 }
40
42
44 // Determine if the source prefix indicates a bipolar output.
45 std::size_t pos = source.find(kModulationSourceDelimiter);
46 std::string prefix = source.substr(0, pos);
47 return kBipolarModulationSourcePrefixes.count(prefix) > 0;
48 }
49
51 // Pre-allocate kMaxModulationConnections with empty source/destination.
52 for (int i = 0; i < kMaxModulationConnections; ++i) {
53 std::unique_ptr<ModulationConnection> connection = std::make_unique<ModulationConnection>(i);
54 all_connections_.push_back(std::move(connection));
55 }
56 }
57
59
60 ModulationConnection* ModulationConnectionBank::createConnection(const std::string& from, const std::string& to) {
61 // Find an available connection slot and set up the connection.
62 int index = 1;
63 for (auto& connection : all_connections_) {
64 std::string invalid_connection = "modulation_" + std::to_string(index++) + "_amount";
65 if (to != invalid_connection && isConnectionAvailable(connection.get())) {
66 connection->resetConnection(from, to);
67 connection->modulation_processor->setBipolar(ModulationConnection::isModulationSourceDefaultBipolar(from));
68 return connection.get();
69 }
70 }
71
72 // No available slots.
73 return nullptr;
74 }
75} // namespace vital
~ModulationConnectionBank()
Destroys the ModulationConnectionBank and its connections.
Definition synth_types.cpp:58
ModulationConnection * createConnection(const std::string &from, const std::string &to)
Creates a new modulation connection by finding an empty slot and assigning source/destination.
Definition synth_types.cpp:60
ModulationConnectionBank()
Constructs the bank and pre-allocates all modulation connection slots.
Definition synth_types.cpp:50
#define force_inline
Definition common.h:23
Contains classes and functions used within the Vital synthesizer framework.
constexpr int kMaxModulationConnections
Maximum number of modulation connections allowed.
Definition synth_constants.h:49
A structure representing a single modulation connection between a modulation source and a destination...
Definition synth_types.h:30
ModulationConnection(int index)
Constructs a ModulationConnection with an index and empty source/destination.
Definition synth_types.h:36
std::unique_ptr< ModulationConnectionProcessor > modulation_processor
Processor applying scaling/mapping.
Definition synth_types.h:75
~ModulationConnection()
Destroys the ModulationConnection, cleaning up its processor.
Definition synth_types.cpp:41
static bool isModulationSourceDefaultBipolar(const std::string &source)
Checks if a given modulation source is bipolar by default.
Definition synth_types.cpp:43