Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ sphinx
numpydoc
flake8
pytest
seaborn
30 changes: 13 additions & 17 deletions toymir/freq.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
import numpy as np
import seaborn # trap to make tests fail!



def midi_to_hz(notes):
"""Hello Part 6! You should add documentation to this function.

"""Adding some documentation to this function as part of Part 6.
"""

----------
notes : float or np.asanyarray(notes)
notes to convert
Returns
-------
return 440.0 * (2.0 ** ((np.asanyarray(notes) - 69.0) / 12.0))


def hz_to_midi(frequencies):
"""Get MIDI note number(s) for given frequencies

Parameters
----------
frequencies : float or np.ndarray [shape=(n,), dtype=float]
frequencies to convert

Returns
-------
note_nums : number or np.ndarray [shape=(n,), dtype=float]
MIDI notes to `frequencies`

Examples
--------
>>> hz_to_midi(60)
34.506
>>> hz_to_midi([110, 220, 440])
array([ 45., 57., 69.])

See Also
--------
midi_to_hz
Expand All @@ -38,35 +39,30 @@ def hz_to_midi(frequencies):
# Oh hey, it's Part 5! You could uncomment this implementation,
# and then the tests will pass!

# less_than_zero = (np.asanyarray(frequencies) <= 0).any()
less_than_zero = (np.asanyarray(frequencies) <= 0).any()

# if less_than_zero:
# raise ValueError('Cannot convert a hz of zero or less to a period.')
if less_than_zero:
raise ValueError('Cannot convert a hz of zero or less to a period.')

# return 12 * (np.log2(np.asanyarray(frequencies)) - np.log2(440.0)) + 69
return 12 * (np.log2(np.asanyarray(frequencies)) - np.log2(440.0)) + 69


def hz_to_period(frequencies):
"""Get the period of a frequency (Hz) in seconds.

Parameters
----------
frequencies : float or np.ndarray [shape=(n,), dtype=float]
frequencies to convert

Returns
-------
period : number or np.ndarray [shape=(n,), dtype=float]
period (periods) of `frequencies` in seconds.

Examples
--------
>>> hz_to_period(100)
0.01

>>> hz_to_period([110, 220, 440])
array([0.00909091, 0.00454545, 0.0030303 ])

See Also
--------
hz_to_midi
Expand All @@ -76,4 +72,4 @@ def hz_to_period(frequencies):
if less_than_zero:
raise ValueError('Cannot convert a hz of zero or less to a period.')

return 1 / np.asanyarray(frequencies)
return 1 / np.asanyarray(frequencies)
12 changes: 6 additions & 6 deletions toymir/tests/test_toymir.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ def test_midi_to_hz_array():
# These are the two tests you should uncomment!


# def test_hz_to_midi_float():
# expected = 69
# assert toymir.hz_to_midi(440.0) == expected
def test_hz_to_midi_float():
expected = 69
assert toymir.hz_to_midi(440.0) == expected


# def test_hz_to_midi_array():
# expected = [57, 69, 81]
# assert np.allclose(toymir.hz_to_midi([220.0, 440.0, 880.0]), expected)
def test_hz_to_midi_array():
expected = [57, 69, 81]
assert np.allclose(toymir.hz_to_midi([220.0, 440.0, 880.0]), expected)


# Hello! You could add the missing test for test_hz_to_midi here!
Expand Down