Skip to content

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 4 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ dist
_build/
*.mpy
build/

.DS_Store
73 changes: 65 additions & 8 deletions adafruit_circuitplayground/express.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,18 @@ def red_led(self):
def red_led(self, value):
self._led.value = value

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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def _generate_sample(self):
    if self.sample != None:
        return
    length = 100
    sine_wave = array.array("H", sine_sample(length))
    self.sample = audioio.AudioOut(board.SPEAKER, sine_wave)

length = 100 # Amount of samples in the sine wave.
sine_wave = array.array("H", sine_sample(length))

# Initialize the audio output to play the generated sine wave.
sample = audioio.AudioOut(board.SPEAKER, sine_wave)

def play_tone(self, frequency, duration):
""" Produce a tone using the speaker. Try changing frequency to change
the pitch of the tone.
Expand All @@ -206,19 +218,64 @@ 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)

sample = audioio.AudioOut(board.SPEAKER, sine_wave)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.generate_sample()

self.speaker_enable.value = True

sample.play(loop=True)
# Play 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.start_tone(frequency)

time.sleep(duration)
sample.stop()
if self.sample.playing:
self.sample.stop()
self.speaker_enable.value = False

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

.. 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()
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self._generate_sample()

self.speaker_enable.value = True
# 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)

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.playing:
self.sample.stop()
self.speaker_enable.value = False


Expand Down