maiacore 1.10.1
Music analisys library
helper.h
1#ifndef HELPERS_H
2#define HELPERS_H
3
4#include <math.h>
5#include <string>
6#include "maiacore/constants.h"
7#include "maiacore/note.h"
8#include "nlohmann/json.hpp"
9#include "pugi/pugixml.hpp"
10
11class Interval;
12
19class Helper {
20 public:
27 static std::vector<std::string> splitString(const std::string& s, char delimiter);
28
35 static std::string formatFloat(float floatValue, int digits);
36
43 static const std::string generateIdentation(int identPosition, int identSize = 2);
44
53 static std::pair<std::string, int> ticks2noteType(int durationTicks,
54 int divisionsPerQuarterNote,
55 int actualNotes = 1, int normalNotes = 1);
56
65 static std::pair<RhythmFigure, int> ticks2rhythmFigure(int durationTicks,
66 int divisionsPerQuarterNote,
67 int actualNotes, int normalNotes);
68
75 static int noteType2ticks(std::string noteType, const int divisionsPerQuarterNote = 256);
76
82 static float pitch2number(const std::string& pitch);
83
90 static const std::string number2pitch(const float number,
91 const std::string& accType = MUSIC_XML::ACCIDENT::SHARP);
92
99 static std::pair<int, int> freq2midiNote(const float freq,
100 std::function<int(float)> modelo = nullptr);
101
108 static float midiNote2freq(const int midiNote, const float freqA4 = 440.0f);
109
115 static int midiNote2octave(const int midiNote);
116
122 static int pitch2midiNote(const std::string& pitch);
123
130 static const std::string midiNote2pitch(const int midiNote, const std::string& accType = {});
131
137 static const std::vector<std::string> midiNote2pitches(const int midiNote);
138
145 static std::vector<Interval> notes2Intervals(const std::vector<Note>& notes,
146 const bool firstNoteAsReference = false);
147
154 static std::vector<Interval> notes2Intervals(const std::vector<std::string>& pitches,
155 const bool firstNoteAsReference = false);
156
163 static int semitonesBetweenPitches(const std::string& pitch_A, const std::string& pitch_B);
164
209 static float noteSimilarity(std::string& pitchClass_A, int octave_A, const float duration_A,
210 std::string& pitchClass_B, int octave_B, const float duration_B,
211 float& durRatio, float& pitRatio,
212 const bool enableEnharmonic = false);
213
219 static float pitch2freq(const std::string& pitch);
220
227 static int frequencies2cents(const float freq_A, const float freq_B);
228
235 static float freq2equalTemperament(const float freq, const float referenceFreq = 440.0f);
236
242 static std::string rhythmFigure2noteType(const RhythmFigure rhythmFigure);
243
250 static int rhythmFigure2Ticks(const RhythmFigure rhythmFigure,
251 const int divisionsPerQuarterNote = 256);
252
258 static RhythmFigure noteType2RhythmFigure(const std::string& noteType);
259
267 static const std::string transposePitch(
268 const std::string& pitch, const int semitones,
269 const std::string& accType = MUSIC_XML::ACCIDENT::SHARP);
270
277 static bool isEnharmonic(const std::string& pitch_A, const std::string& pitch_B);
278
288 static void splitPitch(const std::string& pitch, std::string& pitchClass,
289 std::string& pitchStep, int& octave, float& alterValue,
290 std::string& alterSymbol);
291
298 static float durationRatio(float duration_A, float duration_B);
299
300 // /**
301 // * @brief Converts an accidental RhythmFigure to a MusicXML note type string.
302 // * @param rhythmFigure RhythmFigure value.
303 // * @return Note type string.
304 // */
305 // static std::string rhythmFigure2noteType(const RhythmFigure rhythmFigure);
306
307 // /**
308 // * @brief Converts a MusicXML note type string to a RhythmFigure enum.
309 // * @param noteType Note type string.
310 // * @return RhythmFigure value.
311 // */
312 // static RhythmFigure noteType2RhythmFigure(const std::string& noteType);
313
319 static const std::string alterName2symbol(const std::string& alterName);
320
326 static float alterSymbol2Value(const std::string& alterSymbol);
327
333 static const std::string alterValue2symbol(const float alterValue);
334
340 static const std::string alterValue2Name(const float alterValue);
341
353 static void getNoteNodeData(const pugi::xml_node& node, std::string& pitch,
354 std::string& pitchClass, std::string& alterSymbol, int& alterValue,
355 int& octave, std::string& type, float& duration);
356
363 static const pugi::xpath_node_set getNodeSet(const pugi::xml_document& doc,
364 const std::string& xPath);
365
372 static const nlohmann::json getPercentiles(const nlohmann::json& table,
373 const std::vector<float>& desiredPercentiles);
374
375 // /**
376 // * @brief Converts a pitch string to its frequency in Hz.
377 // * @param pitch Pitch string.
378 // * @return Frequency in Hz.
379 // */
380 // static float pitch2freq(const std::string& pitch);
381
388 static std::pair<std::string, int> freq2pitch(
389 const float freq, const std::string& accType = MUSIC_XML::ACCIDENT::NONE);
390
397 static float pitchRatio(const std::string& pitch_A, const std::string& pitch_B);
398
404 static std::string toString(const RhythmFigure rhythmFigure);
405
449 static std::vector<float> getSemitonesDifferenceBetweenMelodies(
450 const std::vector<Note>& referenceMelody, const std::vector<Note>& otherMelody);
451
494 static float calculateMelodyEuclideanSimilarity(const std::vector<Note>& melodyPattern,
495 const std::vector<Note>& otherMelody);
496
502 static float calculateMelodyEuclideanSimilarity(const std::vector<float>& semitonesDifference);
503
510 static std::vector<float> getDurationDifferenceBetweenRhythms(
511 const std::vector<Note>& referenceRhythm, const std::vector<Note>& otherRhythm);
512
519 static float calculateRhythmicEuclideanSimilarity(const std::vector<Note>& rhythmPattern,
520 const std::vector<Note>& otherRhythm);
521
527 static float calculateRhythmicEuclideanSimilarity(const std::vector<float>& durationDifferences);
528};
529#endif // HELPERS_H
Helper class with static utility functions for music analysis, pitch/duration conversion,...
Definition: helper.h:19
static const std::string transposePitch(const std::string &pitch, const int semitones, const std::string &accType=MUSIC_XML::ACCIDENT::SHARP)
Transposes a pitch string by a number of semitones.
static const std::vector< std::string > midiNote2pitches(const int midiNote)
Returns all possible pitch spellings for a given MIDI note.
static int rhythmFigure2Ticks(const RhythmFigure rhythmFigure, const int divisionsPerQuarterNote=256)
Converts a RhythmFigure to a duration in ticks.
static int pitch2midiNote(const std::string &pitch)
Converts a pitch string (e.g., "C4") to a MIDI note number.
static std::pair< RhythmFigure, int > ticks2rhythmFigure(int durationTicks, int divisionsPerQuarterNote, int actualNotes, int normalNotes)
Converts a duration in ticks to a RhythmFigure and dot count.
static void getNoteNodeData(const pugi::xml_node &node, std::string &pitch, std::string &pitchClass, std::string &alterSymbol, int &alterValue, int &octave, std::string &type, float &duration)
Retrieves detailed information from a MusicXML note node.
static float alterSymbol2Value(const std::string &alterSymbol)
Converts an accidental symbol (e.g., "#") to its numeric value.
static std::string toString(const RhythmFigure rhythmFigure)
Converts a RhythmFigure to a string.
static int semitonesBetweenPitches(const std::string &pitch_A, const std::string &pitch_B)
Calculates the number of semitones between two pitch strings.
static const std::string alterName2symbol(const std::string &alterName)
Converts an accidental name (e.g., "sharp") to its symbol (e.g., "#").
static int frequencies2cents(const float freq_A, const float freq_B)
Converts a frequency ratio between two pitches to cents.
static float calculateMelodyEuclideanSimilarity(const std::vector< Note > &melodyPattern, const std::vector< Note > &otherMelody)
Calculates Euclidean distance-based melodic similarity from intervallic contour comparison.
static const nlohmann::json getPercentiles(const nlohmann::json &table, const std::vector< float > &desiredPercentiles)
Returns a JSON object with percentiles for a given table and desired percentile values.
static const std::string alterValue2symbol(const float alterValue)
Converts an accidental value to its symbol (e.g., 1.0 -> "#").
static float noteSimilarity(std::string &pitchClass_A, int octave_A, const float duration_A, std::string &pitchClass_B, int octave_B, const float duration_B, float &durRatio, float &pitRatio, const bool enableEnharmonic=false)
Computes multidimensional similarity between two notes using pitch-space and rhythmic metrics.
static const std::string number2pitch(const float number, const std::string &accType=MUSIC_XML::ACCIDENT::SHARP)
Converts a floating-point pitch number to a pitch string, with optional accidental type.
static float pitchRatio(const std::string &pitch_A, const std::string &pitch_B)
Computes the ratio between two pitch strings as a float.
static float calculateRhythmicEuclideanSimilarity(const std::vector< Note > &rhythmPattern, const std::vector< Note > &otherRhythm)
Calculates the Euclidean similarity between two rhythms based on duration differences.
static bool isEnharmonic(const std::string &pitch_A, const std::string &pitch_B)
Checks if two pitch strings are enharmonically equivalent.
static const std::string alterValue2Name(const float alterValue)
Converts an accidental value to its name (e.g., 1.0 -> "sharp").
static float calculateRhythmicEuclideanSimilarity(const std::vector< float > &durationDifferences)
Calculates the Euclidean similarity from a vector of duration differences.
static float calculateMelodyEuclideanSimilarity(const std::vector< float > &semitonesDifference)
Calculates the Euclidean similarity from a vector of semitone differences.
static void splitPitch(const std::string &pitch, std::string &pitchClass, std::string &pitchStep, int &octave, float &alterValue, std::string &alterSymbol)
Splits a pitch string into its components: pitch class, step, octave, accidental value,...
static const std::string generateIdentation(int identPosition, int identSize=2)
Generates a string of spaces for indentation, useful for pretty-printing MusicXML.
static std::vector< Interval > notes2Intervals(const std::vector< Note > &notes, const bool firstNoteAsReference=false)
Computes intervals between a sequence of notes.
static std::string formatFloat(float floatValue, int digits)
Formats a floating-point number as a string with a given number of decimal digits.
static float midiNote2freq(const int midiNote, const float freqA4=440.0f)
Converts a MIDI note number to frequency in Hz.
static float freq2equalTemperament(const float freq, const float referenceFreq=440.0f)
Converts a frequency to the nearest equal-tempered frequency.
static const pugi::xpath_node_set getNodeSet(const pugi::xml_document &doc, const std::string &xPath)
Selects nodes from a MusicXML document using an XPath expression.
static int noteType2ticks(std::string noteType, const int divisionsPerQuarterNote=256)
Converts a MusicXML note type string to a duration in ticks.
static float pitch2freq(const std::string &pitch)
Converts a pitch string to its frequency in Hz.
static float durationRatio(float duration_A, float duration_B)
Computes the ratio between two durations.
static const std::string midiNote2pitch(const int midiNote, const std::string &accType={})
Converts a MIDI note number to a pitch string, with optional accidental type.
static std::vector< float > getDurationDifferenceBetweenRhythms(const std::vector< Note > &referenceRhythm, const std::vector< Note > &otherRhythm)
Computes the vector of normalized duration differences between two rhythms.
static std::pair< int, int > freq2midiNote(const float freq, std::function< int(float)> modelo=nullptr)
Converts a frequency in Hz to the closest MIDI note and cents deviation.
static std::vector< float > getSemitonesDifferenceBetweenMelodies(const std::vector< Note > &referenceMelody, const std::vector< Note > &otherMelody)
Computes the intervallic contour difference vector between two melodic sequences.
static std::vector< Interval > notes2Intervals(const std::vector< std::string > &pitches, const bool firstNoteAsReference=false)
Computes intervals between a sequence of pitch strings.
static std::pair< std::string, int > freq2pitch(const float freq, const std::string &accType=MUSIC_XML::ACCIDENT::NONE)
Converts a frequency in Hz to the closest pitch and cents deviation.
static std::pair< std::string, int > ticks2noteType(int durationTicks, int divisionsPerQuarterNote, int actualNotes=1, int normalNotes=1)
Converts a duration in ticks to a MusicXML note type and dot count.
static RhythmFigure noteType2RhythmFigure(const std::string &noteType)
Converts a MusicXML note type string to a RhythmFigure enum.
static std::string rhythmFigure2noteType(const RhythmFigure rhythmFigure)
Converts a RhythmFigure enum to a MusicXML note type string.
static int midiNote2octave(const int midiNote)
Converts a MIDI note number to its octave number.
static float pitch2number(const std::string &pitch)
Converts a pitch string (e.g., "C4") to a floating-point number representation.
static std::vector< std::string > splitString(const std::string &s, char delimiter)
Splits a string into tokens using a specified delimiter.
Represents a musical interval between two notes, supporting tonal, diatonic, and chromatic analysis.
Definition: interval.h:14