Ignore:
Timestamp:
Nov 17, 2018, 10:19:27 PM (6 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/constantq
Children:
d1d4ad4
Parents:
088760e (diff), a114fe0 (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/constantq

File:
1 edited

Legend:

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

    r088760e rc03d191  
    22""" utilities to convert midi note number to and from note names """
    33
    4 __all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
    5 
    64import sys
    75from ._aubio import freqtomidi, miditofreq
     6
     7__all__ = ['note2midi', 'midi2note', 'freq2note', 'note2freq']
    88
    99py3 = sys.version_info[0] == 3
     
    1414    str_instances = (str, unicode)
    1515    int_instances = (int, long)
     16
    1617
    1718def note2midi(note):
     
    5657    midi2note, freqtomidi, miditofreq
    5758    """
    58     _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
     59    _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7,
     60                        'A': 9, 'B': 11}
    5961    _valid_modifiers = {
    60             u'𝄫': -2,                        # double flat
    61             u'♭': -1, 'b': -1, '\u266d': -1, # simple flat
    62             u'♮': 0, '\u266e': 0, None: 0,   # natural
    63             '#': +1, u'♯': +1, '\u266f': +1, # sharp
    64             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
    6567            }
    6668    _valid_octaves = range(-1, 10)
     
    7173        msg = "string of 2 to 4 characters expected, got {:d} ({:s})"
    7274        raise ValueError(msg.format(len(note), note))
    73     notename, modifier, octave = [None]*3
     75    notename, modifier, octave = [None] * 3
    7476
    7577    if len(note) == 4:
     
    9496        raise ValueError("%s is not a valid octave" % octave)
    9597
    96     midi = 12 + octave * 12 + _valid_notenames[notename] \
    97             + _valid_modifiers[modifier]
     98    midi = (octave + 1) * 12 + _valid_notenames[notename] \
     99                             + _valid_modifiers[modifier]
    98100    if midi > 127:
    99101        raise ValueError("%s is outside of the range C-2 to G8" % note)
    100102    return midi
     103
    101104
    102105def midi2note(midi):
     
    137140        raise ValueError(msg.format(midi))
    138141    _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#',
    139             'A', 'A#', 'B']
     142                        'A', 'A#', 'B']
    140143    return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
     144
    141145
    142146def freq2note(freq):
     
    163167    return midi2note(nearest_note)
    164168
     169
    165170def note2freq(note):
    166171    """Convert note name to corresponding frequency, in Hz.
Note: See TracChangeset for help on using the changeset viewer.