Merge branch 'master' into feature/docstrings
authorPaul Brossier <piem@piem.org>
Tue, 30 Oct 2018 12:22:44 +0000 (13:22 +0100)
committerPaul Brossier <piem@piem.org>
Tue, 30 Oct 2018 12:22:44 +0000 (13:22 +0100)
1  2 
python/lib/aubio/midiconv.py

@@@ -134,24 -72,29 +140,47 @@@ def midi2note(midi)
      return _valid_notenames[midi % 12] + str(int(midi / 12) - 1)
  
  def freq2note(freq):
 -    " convert frequency in Hz to nearest note name, e.g. [0, 22050.] -> [C-1, G9] "
 +    """Convert frequency in Hz to nearest note name.
 +
 +    Parameters
 +    ----------
 +    freq : float [0, 23000[
 +        input frequency, in Hz
 +
 +    Returns
 +    -------
 +    str
 +        name of the nearest note
 +
 +    Example
 +    -------
 +    >>> aubio.freq2note(440)
 +    'A4'
 +    >>> aubio.freq2note(220.1)
 +    'A3'
 +    """
-     from aubio import freqtomidi
-     return midi2note(int(freqtomidi(freq)))
+     nearest_note = int(freqtomidi(freq) + .5)
+     return midi2note(nearest_note)
+ def note2freq(note):
+     """Convert note name to corresponding frequency, in Hz.
+     Parameters
+     ----------
+     note : str
+         input note name
+     Returns
+     -------
+     freq : float [0, 23000[
+         frequency, in Hz
+     Example
+     -------
+     >>> aubio.note2freq('A4')
+     440
+     >>> aubio.note2freq('A3')
+     220.1
+     """
+     midi = note2midi(note)
+     return miditofreq(midi)