Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generalize noteNumberToFrequency to use any periodic tuning #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions source/modules/soul_core/library/soul_library_audio_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ 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 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;
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)); }
/** 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-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. */
/** 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
Expand Down