Vital
Loading...
Searching...
No Matches
tuning.h
Go to the documentation of this file.
1/*
2Summary:
3The Tuning class provides a flexible microtonal tuning system for Vital. It can load multiple file formats, define custom scales, and set reference notes and frequencies. The code handles reading and interpreting different tuning standards (Scala, Tun), as well as mapping notes via keyboard mapping files. This allows Vital users to easily explore alternate tunings beyond standard equal temperament.
4 */
5
6#pragma once
7
8#include "JuceHeader.h"
9
10#include "common.h"
11#include "json/json.h"
12
13using json = nlohmann::json;
14
23class Tuning {
24public:
31 static constexpr int kTuningSize = 2 * vital::kMidiSize;
32
38 static constexpr int kTuningCenter = vital::kMidiSize;
39
49 static Tuning getTuningForFile(File file);
50
58 static String allFileExtensions();
59
66 static int noteToMidiKey(const String& note);
67
71 Tuning();
72
78 Tuning(File file);
79
85 void loadScale(std::vector<float> scale);
86
92 void loadFile(File file);
93
101 void setConstantTuning(float note);
102
106 void setDefaultTuning();
107
114 vital::mono_float convertMidiNote(int note) const;
115
121 void setStartMidiNote(int start_midi_note) { scale_start_midi_note_ = start_midi_note; }
122
128 void setReferenceNote(int reference_note) { reference_midi_note_ = reference_note; }
129
135 void setReferenceFrequency(float frequency);
136
143 void setReferenceNoteFrequency(int midi_note, float frequency);
144
150 void setReferenceRatio(float ratio);
151
157 std::string getName() const {
158 if (mapping_name_.empty())
159 return tuning_name_;
160 if (tuning_name_.empty())
161 return mapping_name_;
162 return tuning_name_ + " / " + mapping_name_;
163 }
164
170 void setName(const std::string& name) {
171 mapping_name_.clear();
172 tuning_name_ = name;
173 }
174
180 bool isDefault() const { return default_; }
181
187 json stateToJson() const;
188
194 void jsonToState(const json& data);
195
203 void loadScalaFile(const StringArray& scala_lines);
204
205private:
206 void loadScalaFile(File scala_file);
207 void loadKeyboardMapFile(File kbm_file);
208 void loadTunFile(File tun_file);
209
210 int scale_start_midi_note_;
211 float reference_midi_note_;
212 std::vector<float> scale_;
213 std::vector<int> keyboard_mapping_;
215 std::string tuning_name_;
216 std::string mapping_name_;
217 bool default_;
218
219 JUCE_LEAK_DETECTOR(Tuning)
220};
A class for managing microtonal tunings and custom pitch mappings in Vital.
Definition tuning.h:23
Tuning()
Constructs a Tuning object representing default (12-tone equal temperament) tuning.
Definition tuning.cpp:305
void setName(const std::string &name)
Sets a custom name for the current tuning, clearing any mapping name.
Definition tuning.h:170
void setReferenceNoteFrequency(int midi_note, float frequency)
Sets the reference note and frequency pair.
Definition tuning.cpp:379
static constexpr int kTuningCenter
The center index of the tuning table.
Definition tuning.h:38
void setReferenceRatio(float ratio)
Sets the reference ratio, defining a pitch offset in ratio form.
Definition tuning.cpp:383
static String allFileExtensions()
Returns a string listing all supported tuning file extensions.
Definition tuning.cpp:93
void setReferenceNote(int reference_note)
Sets the reference MIDI note number around which the tuning is centered.
Definition tuning.h:128
void setStartMidiNote(int start_midi_note)
Sets the starting MIDI note for the scale mapping.
Definition tuning.h:121
static constexpr int kTuningSize
The total size of the internal tuning table.
Definition tuning.h:31
void setConstantTuning(float note)
Sets a constant tuning, making all notes map to the same pitch offset.
Definition tuning.cpp:349
json stateToJson() const
Saves the current tuning state into a JSON object.
Definition tuning.cpp:387
void loadScalaFile(const StringArray &scala_lines)
Loads a Scala (.scl) file from a set of lines.
Definition tuning.cpp:160
static int noteToMidiKey(const String &note)
Converts a note name (e.g. "A4") to a MIDI key number.
Definition tuning.cpp:99
void loadFile(File file)
Loads a tuning from a given file, automatically detecting its format (.scl, .kbm, ....
Definition tuning.cpp:147
static Tuning getTuningForFile(File file)
Creates a Tuning object from a given file.
Definition tuning.cpp:143
void loadScale(std::vector< float > scale)
Loads a custom scale from a vector of offsets, specified in semitones or transposition units.
Definition tuning.cpp:316
std::string getName() const
Gets the name of the current tuning, combining tuning and mapping names if both exist.
Definition tuning.h:157
void setDefaultTuning()
Resets the tuning to the default 12-tone equal temperament with a standard reference pitch.
Definition tuning.cpp:354
vital::mono_float convertMidiNote(int note) const
Converts a MIDI note number to a pitch offset based on the current tuning.
Definition tuning.cpp:370
void setReferenceFrequency(float frequency)
Sets the reference frequency used for calculating pitches.
Definition tuning.cpp:375
bool isDefault() const
Checks if the tuning is the default equal temperament.
Definition tuning.h:180
void jsonToState(const json &data)
Restores the tuning state from a JSON object.
Definition tuning.cpp:410
nlohmann::json json
Definition line_generator.h:7
constexpr int kMidiSize
MIDI note count (0-127).
Definition common.h:44
float mono_float
Definition common.h:33