-
Notifications
You must be signed in to change notification settings - Fork 71
start_tone / stop_tone implemented. #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ dist | |
_build/ | ||
*.mpy | ||
build/ | ||
|
||
.DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,9 @@ def __init__(self): | |
self.speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE) | ||
self.speaker_enable.switch_to_output(value=False) | ||
|
||
self.sample = None | ||
self.sine_wave = None | ||
|
||
@property | ||
def button_a(self): | ||
"""``True`` when Button A is pressed. ``False`` if not. | ||
|
@@ -191,6 +194,20 @@ def red_led(self): | |
def red_led(self, value): | ||
self._led.value = value | ||
|
||
@staticmethod | ||
def _sine_sample(length): | ||
TONE_VOLUME = (2 ** 15) - 1 | ||
shift = 2 ** 15 | ||
for i in range(length): | ||
yield int(TONE_VOLUME * math.sin(2*math.pi*(i / length)) + shift) | ||
|
||
def _generate_sample(self): | ||
if self.sample is not None: | ||
return | ||
length = 100 | ||
self.sine_wave = array.array("H", Express._sine_sample(length)) | ||
self.sample = audioio.AudioOut(board.SPEAKER, self.sine_wave) | ||
|
||
def play_tone(self, frequency, duration): | ||
""" Produce a tone using the speaker. Try changing frequency to change | ||
the pitch of the tone. | ||
|
@@ -206,19 +223,58 @@ def play_tone(self, frequency, duration): | |
|
||
circuit.play_tone(440, 1) | ||
""" | ||
length = 8000 // frequency | ||
sine_wave = array.array("H", [0] * length) | ||
for i in range(length): | ||
sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15) | ||
# Play a tone of the specified frequency (hz). | ||
self.start_tone(frequency) | ||
time.sleep(duration) | ||
self.stop_tone() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love it! Super clean |
||
|
||
def start_tone(self, frequency): | ||
""" Produce a tone using the speaker. Try changing frequency to change | ||
the pitch of the tone. | ||
|
||
:param int frequency: The frequency of the tone in Hz | ||
|
||
.. image :: /_static/speaker.jpg | ||
|
||
sample = audioio.AudioOut(board.SPEAKER, sine_wave) | ||
.. code-block:: python | ||
|
||
from adafruit_circuitplayground.express import circuit | ||
|
||
while True: | ||
if circuit.button_a: | ||
circuit.start_tone(262) | ||
elif circuit.button_b: | ||
circuit.start_tone(294) | ||
else: | ||
circuit.stop_tone() | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
self.speaker_enable.value = True | ||
self._generate_sample() | ||
# Start playing a tone of the specified frequency (hz). | ||
self.sample.frequency = int(len(self.sine_wave) * frequency) | ||
if not self.sample.playing: | ||
self.sample.play(loop=True) | ||
|
||
sample.play(loop=True) | ||
time.sleep(duration) | ||
sample.stop() | ||
def stop_tone(self): | ||
""" Use with start_tone to stop the tone produced. | ||
|
||
.. image :: /_static/speaker.jpg | ||
|
||
.. code-block:: python | ||
|
||
from adafruit_circuitplayground.express import circuit | ||
|
||
while True: | ||
if circuit.button_a: | ||
circuit.start_tone(262) | ||
elif circuit.button_b: | ||
circuit.start_tone(294) | ||
else: | ||
circuit.stop_tone() | ||
""" | ||
# Stop playing any tones. | ||
if self.sample is not None and self.sample.playing: | ||
self.sample.stop() | ||
self.speaker_enable.value = False | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.