Ignore:
Timestamp:
Jun 22, 2016, 1:00:10 PM (8 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, pitchshift, sampler, timestretch, yinfft+
Children:
4b9443c4
Parents:
60fc05b (diff), 6769586 (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 notes

File:
1 edited

Legend:

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

    r60fc05b rf264b17  
    11# -*- coding: utf-8 -*-
     2""" utilities to convert midi note number to and from note names """
     3
     4__all__ = ['note2midi', 'midi2note', 'freq2note']
     5
     6import sys
     7py3 = sys.version_info[0] == 3
     8if py3:
     9    str_instances = str
     10    int_instances = int
     11else:
     12    str_instances = (str, unicode)
     13    int_instances = (int, long)
    214
    315def note2midi(note):
    416    " convert note name to midi note number, e.g. [C-1, G9] -> [0, 127] "
    517    _valid_notenames = {'C': 0, 'D': 2, 'E': 4, 'F': 5, 'G': 7, 'A': 9, 'B': 11}
    6     _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2, 'b': -1, u'♭': -1, u'\ufffd': -2}
     18    _valid_modifiers = {None: 0, u'♮': 0, '#': +1, u'♯': +1, u'\udd2a': +2,
     19                        'b': -1, u'♭': -1, u'\ufffd': -2}
    720    _valid_octaves = range(-1, 10)
    8     if type(note) not in (str, unicode):
    9         raise TypeError, "a string is required, got %s" % note
    10     if not (1 < len(note) < 5):
    11         raise ValueError, "string of 2 to 4 characters expected, got %d (%s)" % (len(note), note)
     21    if not isinstance(note, str_instances):
     22        raise TypeError("a string is required, got %s (%s)" % (note, str(type(note))))
     23    if len(note) not in range(2, 5):
     24        raise ValueError("string of 2 to 4 characters expected, got %d (%s)" \
     25                         % (len(note), note))
    1226    notename, modifier, octave = [None]*3
    1327
     
    2741
    2842    if notename not in _valid_notenames:
    29         raise ValueError, "%s is not a valid note name" % notename
     43        raise ValueError("%s is not a valid note name" % notename)
    3044    if modifier not in _valid_modifiers:
    31         raise ValueError, "%s is not a valid modifier" % modifier
     45        raise ValueError("%s is not a valid modifier" % modifier)
    3246    if octave not in _valid_octaves:
    33         raise ValueError, "%s is not a valid octave" % octave
     47        raise ValueError("%s is not a valid octave" % octave)
    3448
    3549    midi = 12 + octave * 12 + _valid_notenames[notename] + _valid_modifiers[modifier]
    3650    if midi > 127:
    37         raise ValueError, "%s is outside of the range C-2 to G8" % note
     51        raise ValueError("%s is outside of the range C-2 to G8" % note)
    3852    return midi
    3953
    4054def midi2note(midi):
    4155    " convert midi note number to note name, e.g. [0, 127] -> [C-1, G9] "
    42     if type(midi) != int:
    43         raise TypeError, "an integer is required, got %s" % midi
    44     if not (-1 < midi < 128):
    45         raise ValueError, "an integer between 0 and 127 is excepted, got %d" % midi
    46     midi = int(midi)
     56    if not isinstance(midi, int_instances):
     57        raise TypeError("an integer is required, got %s" % midi)
     58    if midi not in range(0, 128):
     59        raise ValueError("an integer between 0 and 127 is excepted, got %d" % midi)
    4760    _valid_notenames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
    48     return _valid_notenames[midi % 12] + str( midi / 12 - 1)
     61    return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
    4962
    5063def freq2note(freq):
     64    " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] "
    5165    from aubio import freqtomidi
    5266    return midi2note(int(freqtomidi(freq)))
Note: See TracChangeset for help on using the changeset viewer.