Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/lib/aubio/midiconv.py

    r3aad0b1 ra3e152a  
    22""" utilities to convert midi note number to and from note names """
    33
    4 __all__ = ['note2midi', 'midi2note', 'freq2note']
     4import sys
     5from ._aubio import freqtomidi, miditofreq
    56
    6 import sys
     7__all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
     8
    79py3 = sys.version_info[0] == 3
    810if py3:
     
    1315    int_instances = (int, long)
    1416
     17
    1518def note2midi(note):
    16     " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "
    17     _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
     19    """Convert note name to midi note number.
     20
     21    Input string `note` should be composed of one note root
     22    and one octave, with optionally one modifier in between.
     23
     24    List of valid components:
     25
     26    - note roots: `C`, `D`, `E`, `F`, `G`, `A`, `B`,
     27    - modifiers: `b`, `#`, as well as unicode characters
     28      `𝄫`, `♭`, `♮`, `♯` and `𝄪`,
     29    - octave numbers: `-1` -> `11`.
     30
     31    Parameters
     32    ----------
     33    note : str
     34        note name
     35
     36    Returns
     37    -------
     38    int
     39        corresponding midi note number
     40
     41    Examples
     42    --------
     43    >>> aubio.note2midi('C#4')
     44    61
     45    >>> aubio.note2midi('B♭5')
     46    82
     47
     48    Raises
     49    ------
     50    TypeError
     51        If `note` was not a string.
     52    ValueError
     53        If an error was found while converting `note`.
     54
     55    See Also
     56    --------
     57    midi2note, freqtomidi, miditofreq
     58    """
     59    _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7,
     60                        'A': 9, 'B': 11}
    1861    _valid_modifiers = {
    19             u'𝄫': -2,                        # double flat
    20             u'♭': -1, 'b': -1, '\u266d': -1, # simple flat
    21             u'♮': 0, '\u266e': 0, None: 0,   # natural
    22             '#': +1, u'♯': +1, '\u266f': +1, # sharp
    23             u'𝄪': +2,                        # double sharp
     62            u'𝄫': -2,                         # double flat
     63            u'♭': -1, 'b': -1, '\u266d': -1,  # simple flat
     64            u'♮': 0, '\u266e': 0, None: 0,    # natural
     65            '#': +1, u'♯': +1, '\u266f': +1,  # sharp
     66            u'𝄪': +2,                         # double sharp
    2467            }
    2568    _valid_octaves = range(-1, 10)
    2669    if not isinstance(note, str_instances):
    27         raise TypeError("a string is required, got %s (%s)" % (note, str(type(note))))
     70        msg = "a string is required, got {:s} ({:s})"
     71        raise TypeError(msg.format(str(type(note)), repr(note)))
    2872    if len(note) not in range(2, 5):
    29         raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \
    30                          % (len(note), note))
    31     notename, modifier, octave = [None]*3
     73        msg = "string of 2 to 4 characters expected, got {:d} ({:s})"
     74        raise ValueError(msg.format(len(note), note))
     75    notename, modifier, octave = [None] * 3
    3276
    3377    if len(note) == 4:
     
    5296        raise ValueError("%s is not a valid octave" % octave)
    5397
    54     midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier]
     98    midi = (octave + 1) * 12 + _valid_notenames[notename] \
     99                             + _valid_modifiers[modifier]
    55100    if midi > 127:
    56101        raise ValueError("%s is outside of the range C-2 to G8" % note)
    57102    return midi
    58103
     104
    59105def midi2note(midi):
    60     " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] "
     106    """Convert midi note number to note name.
     107
     108    Parameters
     109    ----------
     110    midi : int [0, 128]
     111        input midi note number
     112
     113    Returns
     114    -------
     115    str
     116        note name
     117
     118    Examples
     119    --------
     120    >>> aubio.midi2note(70)
     121    'A#4'
     122    >>> aubio.midi2note(59)
     123    'B3'
     124
     125    Raises
     126    ------
     127    TypeError
     128        If `midi` was not an integer.
     129    ValueError
     130        If `midi` is out of the range `[0, 128]`.
     131
     132    See Also
     133    --------
     134    note2midi, miditofreq, freqtomidi
     135    """
    61136    if not isinstance(midi, int_instances):
    62137        raise TypeError("an integer is required, got %s" % midi)
    63138    if midi not in range(0, 128):
    64         raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi)
    65     _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
     139        msg = "an integer between 0 and 127 is excepted, got {:d}"
     140        raise ValueError(msg.format(midi))
     141    _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',
     142                        'A', 'A#', 'B']
    66143    return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
    67144
     145
    68146def freq2note(freq):
    69     " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] "
    70     from aubio import freqtomidi
    71     return midi2note(int(freqtomidi(freq)))
     147    """Convert frequency in Hz to nearest note name.
     148
     149    Parameters
     150    ----------
     151    freq : float [0, 23000[
     152        input frequency, in Hz
     153
     154    Returns
     155    -------
     156    str
     157        name of the nearest note
     158
     159    Example
     160    -------
     161    >>> aubio.freq2note(440)
     162    'A4'
     163    >>> aubio.freq2note(220.1)
     164    'A3'
     165    """
     166    nearest_note = int(freqtomidi(freq) + .5)
     167    return midi2note(nearest_note)
     168
     169
     170def note2freq(note):
     171    """Convert note name to corresponding frequency, in Hz.
     172
     173    Parameters
     174    ----------
     175    note : str
     176        input note name
     177
     178    Returns
     179    -------
     180    freq : float [0, 23000[
     181        frequency, in Hz
     182
     183    Example
     184    -------
     185    >>> aubio.note2freq('A4')
     186    440
     187    >>> aubio.note2freq('A3')
     188    220.1
     189    """
     190    midi = note2midi(note)
     191    return miditofreq(midi)
Note: See TracChangeset for help on using the changeset viewer.