Ignore:
Timestamp:
Oct 30, 2018, 1:22:06 PM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master
Children:
8986239
Parents:
92948ab (diff), 4bc10e2 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' into feature/cdocstrings

File:
1 edited

Legend:

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

    r92948ab r81abf91  
    22""" utilities to convert midi note number to and from note names """
    33
    4 __all__ = ['note2midi', 'midi2note', 'freq2note']
     4__all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
    55
    66import sys
     7from ._aubio import freqtomidi, miditofreq
     8
    79py3 = sys.version_info[0] == 3
    810if py3:
     
    2527    _valid_octaves = range(-1, 10)
    2628    if not isinstance(note, str_instances):
    27         raise TypeError("a string is required, got %s (%s)" % (note, str(type(note))))
     29        msg = "a string is required, got {:s} ({:s})"
     30        raise TypeError(msg.format(str(type(note)), repr(note)))
    2831    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))
     32        msg = "string of 2 to 4 characters expected, got {:d} ({:s})"
     33        raise ValueError(msg.format(len(note), note))
    3134    notename, modifier, octave = [None]*3
    3235
     
    5255        raise ValueError("%s is not a valid octave" % octave)
    5356
    54     midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier]
     57    midi = 12 + octave * 12 + _valid_notenames[notename] \
     58            + _valid_modifiers[modifier]
    5559    if midi > 127:
    5660        raise ValueError("%s is outside of the range C-2 to G8" % note)
     
    6266        raise TypeError("an integer is required, got %s" % midi)
    6367    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']
     68        msg = "an integer between 0 and 127 is excepted, got {:d}"
     69        raise ValueError(msg.format(midi))
     70    _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',
     71            'A', 'A#', 'B']
    6672    return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
    6773
    6874def freq2note(freq):
    6975    " 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)))
     76    nearest_note = int(freqtomidi(freq) + .5)
     77    return midi2note(nearest_note)
     78
     79def note2freq(note):
     80    """Convert note name to corresponding frequency, in Hz.
     81
     82    Parameters
     83    ----------
     84    note : str
     85        input note name
     86
     87    Returns
     88    -------
     89    freq : float [0, 23000[
     90        frequency, in Hz
     91
     92    Example
     93    -------
     94    >>> aubio.note2freq('A4')
     95    440
     96    >>> aubio.note2freq('A3')
     97    220.1
     98    """
     99    midi = note2midi(note)
     100    return miditofreq(midi)
Note: See TracChangeset for help on using the changeset viewer.