Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def note(self):
"""
Return the (nearest) note to the tone's frequency. This will be a
string in the form accepted by :meth:`from_note`. If the frequency is
outside the range represented by this format ("A0" is approximately
27.5Hz, and "G9" is approximately 12.5Khz) a :exc:`ValueError`
exception will be raised.
"""
offset = self.midi - 60 # self.midi - A4_midi + Tone.tones.index('A')
index = offset % 12 # offset % len(Tone.tones)
octave = 4 + offset // 12
if 0 <= octave <= 9:
return (
Tone.tones[index] +
('#' if Tone.tones[index] == Tone.tones[index - 1] else '') +
str(octave)
)
raise ValueError('%f is outside the notation range' % self.frequency)
def test_tone_init(A4):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
assert Tone(440) == A4
assert Tone("A4") == A4
assert len(w) == 0
assert Tone(69) == A4
assert len(w) == 1
assert isinstance(w[0].message, AmbiguousTone)
assert Tone(frequency=440) == A4
assert Tone(note="A4") == A4
assert Tone(midi=69) == A4
with pytest.raises(TypeError):
Tone()
with pytest.raises(TypeError):
Tone(foo=1)
with pytest.raises(TypeError):
Tone(frequency=440, midi=69)
def test_tone_init(A4):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
assert Tone(440) == A4
assert Tone("A4") == A4
assert len(w) == 0
assert Tone(69) == A4
assert len(w) == 1
assert isinstance(w[0].message, AmbiguousTone)
assert Tone(frequency=440) == A4
assert Tone(note="A4") == A4
assert Tone(midi=69) == A4
with pytest.raises(TypeError):
Tone()
with pytest.raises(TypeError):
Tone(foo=1)
with pytest.raises(TypeError):
Tone(frequency=440, midi=69)
def test_tone_init(A4):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
assert Tone(440) == A4
assert Tone("A4") == A4
assert len(w) == 0
assert Tone(69) == A4
assert len(w) == 1
assert isinstance(w[0].message, AmbiguousTone)
assert Tone(frequency=440) == A4
assert Tone(note="A4") == A4
assert Tone(midi=69) == A4
with pytest.raises(TypeError):
Tone()
with pytest.raises(TypeError):
Tone(foo=1)
with pytest.raises(TypeError):
Tone(frequency=440, midi=69)
def test_tone_init(A4):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
assert Tone(440) == A4
assert Tone("A4") == A4
assert len(w) == 0
assert Tone(69) == A4
assert len(w) == 1
assert isinstance(w[0].message, AmbiguousTone)
assert Tone(frequency=440) == A4
assert Tone(note="A4") == A4
assert Tone(midi=69) == A4
with pytest.raises(TypeError):
Tone()
with pytest.raises(TypeError):
Tone(foo=1)
with pytest.raises(TypeError):
Tone(frequency=440, midi=69)
def test_tone_init(A4):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
assert Tone(440) == A4
assert Tone("A4") == A4
assert len(w) == 0
assert Tone(69) == A4
assert len(w) == 1
assert isinstance(w[0].message, AmbiguousTone)
assert Tone(frequency=440) == A4
assert Tone(note="A4") == A4
assert Tone(midi=69) == A4
with pytest.raises(TypeError):
Tone()
with pytest.raises(TypeError):
Tone(foo=1)
with pytest.raises(TypeError):
Tone(frequency=440, midi=69)
def test_tone_init(A4):
with warnings.catch_warnings(record=True) as w:
warnings.resetwarnings()
assert Tone(440) == A4
assert Tone("A4") == A4
assert len(w) == 0
assert Tone(69) == A4
assert len(w) == 1
assert isinstance(w[0].message, AmbiguousTone)
assert Tone(frequency=440) == A4
assert Tone(note="A4") == A4
assert Tone(midi=69) == A4
with pytest.raises(TypeError):
Tone()
with pytest.raises(TypeError):
Tone(foo=1)
with pytest.raises(TypeError):
Tone(frequency=440, midi=69)
def test_tone_from_note(A4):
assert Tone.from_note(b"A4") == A4
assert Tone.from_note("A4") == A4
with pytest.raises(ValueError):
Tone.from_note("a4")
with pytest.raises(ValueError):
Tone.from_note("foo")
with pytest.raises(ValueError):
Tone.from_note(0)
def __init__(self, pin=None, initial_value=None, mid_tone=Tone("A4"),
octaves=1, pin_factory=None):
self._mid_tone = None
super(TonalBuzzer, self).__init__(
pwm_device=PWMOutputDevice(
pin=pin, pin_factory=pin_factory
), pin_factory=pin_factory)
try:
self._mid_tone = Tone(mid_tone)
if not (0 < octaves <= 9):
raise ValueError('octaves must be between 1 and 9')
self._octaves = octaves
try:
self.min_tone.note
except ValueError:
raise ValueError(
'%r is too low for %d octaves' %