将音符转换为赫兹 (iOS)

Convert Notes to Hertz (iOS)

我尝试编写一个函数,以 MIDI 形式(C2、A4、Bb6)和 returns 接收它们各自的频率(以赫兹为单位)的音符。我不确定这样做的最佳方法应该是什么。我在两种方法之间左右为难。 1) 一个基于列表的列表,我可以在其中打开输入和 return 硬编码频率值,因为我可能只需要为 88 个音符(在三角钢琴的情况下)执行此操作。 2)一种简单的数学方法但是我的数学技能是一个限制以及将输入字符串转换为数值。最终我已经为此工作了一段时间并且可以使用一些方向。

您可以使用基于此公式的函数:

The basic formula for the frequencies of the notes of the equal tempered scale is given by

fn = f0 * (a)n

where

f0 = the frequency of one fixed note which must be defined. A common choice is setting the A above middle C (A4) at f0 = 440 Hz.

n = the number of half steps away from the fixed note you are. If you are at a higher note, n is positive. If you are on a lower note, n is negative.

fn = the frequency of the note n half steps away. a = (2)1/12 = the twelth root of 2 = the number which when multiplied by itself 12 times equals 2 = 1.059463094359...

http://www.phy.mtu.edu/~suits/NoteFreqCalcs.html

在 Objective-C 中,这将是:

+ (double)frequencyForNote:(Note)note withModifier:(Modifier)modifier inOctave:(int)octave {
    int halfStepsFromA4 = note - A;
    halfStepsFromA4 += 12 * (octave - 4);
    halfStepsFromA4 += modifier;

    double frequencyOfA4 = 440.0;
    double a = 1.059463094359;

    return frequencyOfA4 * pow(a, halfStepsFromA4);
}

定义了以下枚举:

typedef enum : int {
    C = 0,
    D = 2,
    E = 4,
    F = 5,
    G = 7,
    A = 9,
    B = 11,
} Note;

typedef enum : int {
    None = 0,
    Sharp = 1,
    Flat = -1,
} Modifier;

https://gist.github.com/NickEntin/32c37e3d31724b229696

你为什么不使用 MIDI pitch

其中 f 是频率,d 是 MIDI 数据。