Ignore:
Timestamp:
Oct 30, 2018, 1:22:44 PM (5 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:
152bf4f
Parents:
f9400d0 (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/docstrings

File:
1 edited

Legend:

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

    rf9400d0 red596f7  
    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:
     
    6466    _valid_octaves = range(-1, 10)
    6567    if not isinstance(note, str_instances):
    66         raise TypeError("a string is required, got %s (%s)" % (note, str(type(note))))
     68        msg = "a string is required, got {:s} ({:s})"
     69        raise TypeError(msg.format(str(type(note)), repr(note)))
    6770    if len(note) not in range(2, 5):
    68         raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \
    69                          % (len(note), note))
     71        msg = "string of 2 to 4 characters expected, got {:d} ({:s})"
     72        raise ValueError(msg.format(len(note), note))
    7073    notename, modifier, octave = [None]*3
    7174
     
    9194        raise ValueError("%s is not a valid octave" % octave)
    9295
    93     midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier]
     96    midi = 12 + octave * 12 + _valid_notenames[notename] \
     97            + _valid_modifiers[modifier]
    9498    if midi > 127:
    9599        raise ValueError("%s is outside of the range C-2 to G8" % note)
     
    130134        raise TypeError("an integer is required, got %s" % midi)
    131135    if midi not in range(0, 128):
    132         raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi)
    133     _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
     136        msg = "an integer between 0 and 127 is excepted, got {:d}"
     137        raise ValueError(msg.format(midi))
     138    _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',
     139            'A', 'A#', 'B']
    134140    return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
    135141
     
    154160    'A3'
    155161    """
    156     from aubio import freqtomidi
    157     return midi2note(int(freqtomidi(freq)))
     162    nearest_note = int(freqtomidi(freq) + .5)
     163    return midi2note(nearest_note)
     164
     165def note2freq(note):
     166    """Convert note name to corresponding frequency, in Hz.
     167
     168    Parameters
     169    ----------
     170    note : str
     171        input note name
     172
     173    Returns
     174    -------
     175    freq : float [0, 23000[
     176        frequency, in Hz
     177
     178    Example
     179    -------
     180    >>> aubio.note2freq('A4')
     181    440
     182    >>> aubio.note2freq('A3')
     183    220.1
     184    """
     185    midi = note2midi(note)
     186    return miditofreq(midi)
Note: See TracChangeset for help on using the changeset viewer.