Merge remote-tracking branch 'OFW/dev' into dev

This commit is contained in:
MX
2025-09-29 23:11:36 +03:00
20 changed files with 776 additions and 32 deletions

View File

@@ -1,4 +1,5 @@
#include "notification.h"
#include <toolbox/strint.h>
/*
Python script for note messages generation
@@ -571,3 +572,35 @@ const NotificationMessage message_note_b8 = {
.data.sound.frequency = 7902.13f,
.data.sound.volume = 1.0f,
};
float notification_messages_notes_frequency_from_name(const char* note_name) {
const float base_note = 16.3515979f; // C0
const char* note_names[] = {"c", "cs", "d", "ds", "e", "f", "fs", "g", "gs", "a", "as", "b"};
const size_t notes_count = COUNT_OF(note_names);
char note_wo_octave[3] = {0};
for(size_t i = 0; i < sizeof(note_wo_octave) - 1; i++) {
char in = *note_name;
if(!in) break;
if(!isalpha(in)) break;
note_wo_octave[i] = in;
note_name++;
}
int note_index = -1;
for(size_t i = 0; i < notes_count; i++) {
if(strcasecmp(note_wo_octave, note_names[i]) == 0) note_index = i;
}
if(note_index < 0) return 0.0;
uint16_t octave;
StrintParseError error = strint_to_uint16(note_name, NULL, &octave, 10);
if(error != StrintParseNoError) return 0.0;
if(octave > 8) return 0.0;
int semitone_index = octave * notes_count + note_index;
float frequency = base_note * powf(2.0f, semitone_index / 12.0f);
return roundf(frequency * 100) / 100.0f;
}

View File

@@ -115,6 +115,17 @@ extern const NotificationMessage message_note_a8;
extern const NotificationMessage message_note_as8;
extern const NotificationMessage message_note_b8;
/**
* @brief Returns the frequency of the given note
*
* This function calculates and returns the frequency (in Hz) of the specified note.
* If the input note name is invalid, the function returns 0.0.
*
* @param [in] note_name The name of the note (e.g., "A4", cs5")
* @return The frequency of the note in Hz, or 0.0 if the note name is invalid
*/
extern float notification_messages_notes_frequency_from_name(const char* note_name);
#ifdef __cplusplus
}
#endif