From fe3934170e41c1c50f2ab683a11d700b9583d109 Mon Sep 17 00:00:00 2001 From: Max Pollack Date: Fri, 15 May 2020 22:08:25 -0400 Subject: [PATCH 1/5] Generalize noteNumberToFrequency for any tuning - `scale` is an array containing the frequency interval of each scale degree, beginning with the first degree above the tonic (`1.` is implied) and ending with the octave. - `noteNumberToFrequency(rootNote, rootNote, rootFrequency, scale)==rootFrequency` --- .../soul_core/library/soul_library_audio_utils.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/modules/soul_core/library/soul_library_audio_utils.h b/source/modules/soul_core/library/soul_library_audio_utils.h index 9eeda9ad..b13aeed5 100644 --- a/source/modules/soul_core/library/soul_library_audio_utils.h +++ b/source/modules/soul_core/library/soul_library_audio_utils.h @@ -35,9 +35,15 @@ namespace soul float32 gainTodB (float32 gain) { return gain > 0 ? log10 (gain) * 20.0f : -100.0f; } float64 gainTodB (float64 gain) { return gain > 0 ? log10 (gain) * 20.0 : -100.0; } - /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz. */ + /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using a custom tuning. */ + float32 noteNumberToFrequency (int note, int rootNote, float32 rootFrequency, float32[] scale) + { + note -= rootNote + 1; + return scale[note] * rootFrequency * pow(scale[-1], float32((note-wrap(note, scale.size)) / scale.size)); + } + /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-EDO tuning. */ float32 noteNumberToFrequency (int note) { return 440.0f * pow (2.0f, (note - 69) * (1.0f / 12.0f)); } - /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz. */ + /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-EDO tuning. */ float32 noteNumberToFrequency (float32 note) { return 440.0f * pow (2.0f, (note - 69.0f) * (1.0f / 12.0f)); } /** Converts a frequency in Hz to an equivalent MIDI note number. */ float32 frequencyToNoteNumber (float32 frequency) { return 69.0f + (12.0f / log (2.0f)) * log (frequency * (1.0f / 440.0f)); } From 5e8de22b52570db0d720e4909f7f2390278d7639 Mon Sep 17 00:00:00 2001 From: Max Pollack Date: Fri, 15 May 2020 22:11:07 -0400 Subject: [PATCH 2/5] Fix comment --- source/modules/soul_core/library/soul_library_audio_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/modules/soul_core/library/soul_library_audio_utils.h b/source/modules/soul_core/library/soul_library_audio_utils.h index b13aeed5..9b9d96c2 100644 --- a/source/modules/soul_core/library/soul_library_audio_utils.h +++ b/source/modules/soul_core/library/soul_library_audio_utils.h @@ -45,7 +45,7 @@ namespace soul float32 noteNumberToFrequency (int note) { return 440.0f * pow (2.0f, (note - 69) * (1.0f / 12.0f)); } /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-EDO tuning. */ float32 noteNumberToFrequency (float32 note) { return 440.0f * pow (2.0f, (note - 69.0f) * (1.0f / 12.0f)); } - /** Converts a frequency in Hz to an equivalent MIDI note number. */ + /** Converts a frequency in Hz to an equivalent MIDI note number using A440 12-EDO tuning. */ float32 frequencyToNoteNumber (float32 frequency) { return 69.0f + (12.0f / log (2.0f)) * log (frequency * (1.0f / 440.0f)); } /** Returns the ratio by which a sample's playback must be sped-up in order to map From 922b9e3a1665dd3e88d791490fea4f910d76943f Mon Sep 17 00:00:00 2001 From: Max Pollack Date: Sat, 16 May 2020 07:55:45 -0400 Subject: [PATCH 3/5] EDO -> TET --- source/modules/soul_core/library/soul_library_audio_utils.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/modules/soul_core/library/soul_library_audio_utils.h b/source/modules/soul_core/library/soul_library_audio_utils.h index 9b9d96c2..a94f8649 100644 --- a/source/modules/soul_core/library/soul_library_audio_utils.h +++ b/source/modules/soul_core/library/soul_library_audio_utils.h @@ -41,11 +41,11 @@ namespace soul note -= rootNote + 1; return scale[note] * rootFrequency * pow(scale[-1], float32((note-wrap(note, scale.size)) / scale.size)); } - /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-EDO tuning. */ + /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-TET tuning. */ float32 noteNumberToFrequency (int note) { return 440.0f * pow (2.0f, (note - 69) * (1.0f / 12.0f)); } - /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-EDO tuning. */ + /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-TET tuning. */ float32 noteNumberToFrequency (float32 note) { return 440.0f * pow (2.0f, (note - 69.0f) * (1.0f / 12.0f)); } - /** Converts a frequency in Hz to an equivalent MIDI note number using A440 12-EDO tuning. */ + /** Converts a frequency in Hz to an equivalent MIDI note number using A440 12-TET tuning. */ float32 frequencyToNoteNumber (float32 frequency) { return 69.0f + (12.0f / log (2.0f)) * log (frequency * (1.0f / 440.0f)); } /** Returns the ratio by which a sample's playback must be sped-up in order to map From 355c41d8e42dc64b0a9de986bf270ad9e3def2c7 Mon Sep 17 00:00:00 2001 From: Max Pollack Date: Mon, 25 May 2020 22:59:48 -0400 Subject: [PATCH 4/5] Documentation --- .../modules/soul_core/library/soul_library_audio_utils.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/source/modules/soul_core/library/soul_library_audio_utils.h b/source/modules/soul_core/library/soul_library_audio_utils.h index a94f8649..3201a1f7 100644 --- a/source/modules/soul_core/library/soul_library_audio_utils.h +++ b/source/modules/soul_core/library/soul_library_audio_utils.h @@ -35,7 +35,13 @@ namespace soul float32 gainTodB (float32 gain) { return gain > 0 ? log10 (gain) * 20.0f : -100.0f; } float64 gainTodB (float64 gain) { return gain > 0 ? log10 (gain) * 20.0 : -100.0; } - /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using a custom tuning. */ + /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using a periodic tuning. + + @param note a note number + @param rootNote the note number at which the scale begins + @param rootFrequency the frequency at which the scale begins + @param scale the frequency interval of each scale degree (`scale[-1]` is used as the period) + */ float32 noteNumberToFrequency (int note, int rootNote, float32 rootFrequency, float32[] scale) { note -= rootNote + 1; From a9ffe4a5a41cea9e3af8fde99a70a66b22ce0562 Mon Sep 17 00:00:00 2001 From: Max Pollack Date: Thu, 25 Jun 2020 03:32:59 -0400 Subject: [PATCH 5/5] Fixed `noteNumberToFrequency` wrapping "bug" Strangely `scale[note]` appears to wrap `note` before indexing, despite the Syntax Guide saying that it shouldn't. Switching to `scale.at(note)` for consistency. --- source/modules/soul_core/library/soul_library_audio_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/modules/soul_core/library/soul_library_audio_utils.h b/source/modules/soul_core/library/soul_library_audio_utils.h index 3201a1f7..d69a2a6e 100644 --- a/source/modules/soul_core/library/soul_library_audio_utils.h +++ b/source/modules/soul_core/library/soul_library_audio_utils.h @@ -45,7 +45,7 @@ namespace soul float32 noteNumberToFrequency (int note, int rootNote, float32 rootFrequency, float32[] scale) { note -= rootNote + 1; - return scale[note] * rootFrequency * pow(scale[-1], float32((note-wrap(note, scale.size)) / scale.size)); + return scale.at(note) * rootFrequency * pow(scale[-1], float32((note-wrap(note, scale.size)) / scale.size)); } /** Converts a MIDI note (usually in the range 0-127) to a frequency in Hz using A440 12-TET tuning. */ float32 noteNumberToFrequency (int note) { return 440.0f * pow (2.0f, (note - 69) * (1.0f / 12.0f)); }