Skip to content

volume #57

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

Closed
wants to merge 6 commits into from
Closed
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
31 changes: 13 additions & 18 deletions adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,24 +563,25 @@ def red_led(self, value):
self._led.value = value

@staticmethod
def _sine_sample(length):
tone_volume = (2 ** 15) - 1
def _sine_sample(length, volume=None):
if volume is None:
volume = 0
tone_volume = (2 ** 15) + volume
shift = 2 ** 15
for i in range(length):
yield int(tone_volume * math.sin(2*math.pi*(i / length)) + shift)

def _generate_sample(self, length=100):
def _generate_sample(self, length=100, volume=None):
Copy link
Contributor

Choose a reason for hiding this comment

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

As written, won't this cause the first volume used to apply to all samples until a restart?

if self._sample is not None:
return
self._sine_wave = array.array("H", Express._sine_sample(length))
self._sine_wave = array.array("H", Express._sine_sample(length, volume))
if sys.implementation.version[0] >= 3:
self._sample = audioio.AudioOut(board.SPEAKER)
self._sine_wave_sample = audioio.RawSample(self._sine_wave)
else:
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
raise NotImplementedError("Please use CircuitPython 3.0 or higher.")


def play_tone(self, frequency, duration):
def play_tone(self, frequency, duration, volume=None):
""" Produce a tone using the speaker. Try changing frequency to change
the pitch of the tone.

Expand All @@ -597,11 +598,11 @@ def play_tone(self, frequency, duration):
cpx.play_tone(440, 1)
"""
# Play a tone of the specified frequency (hz).
self.start_tone(frequency)
self.start_tone(frequency, volume)
time.sleep(duration)
self.stop_tone()

def start_tone(self, frequency):
def start_tone(self, frequency, volume=None):
""" Produce a tone using the speaker. Try changing frequency to change
the pitch of the tone.

Expand All @@ -626,16 +627,14 @@ def start_tone(self, frequency):
length = 100
if length * frequency > 350000:
length = 350000 // frequency
self._generate_sample(length)
self._generate_sample(length, volume)
# Start playing a tone of the specified frequency (hz).
if sys.implementation.version[0] >= 3:
self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency)
if not self._sample.playing:
self._sample.play(self._sine_wave_sample, loop=True)
else:
self._sample.frequency = int(len(self._sine_wave) * frequency)
if not self._sample.playing:
self._sample.play(loop=True)
raise NotImplementedError("Please use CircuitPython 3.0 or higher.")

def stop_tone(self):
""" Use with start_tone to stop the tone produced.
Expand Down Expand Up @@ -690,13 +689,9 @@ def play_file(self, file_name):
while audio.playing:
pass
else:
with audioio.AudioOut(board.SPEAKER, open(file_name, "rb")) as audio:
audio.play()
while audio.playing:
pass
raise NotImplementedError("Please use CircuitPython 3.0 or higher.")
self._speaker_enable.value = False


cpx = Express() # pylint: disable=invalid-name
"""Object that is automatically created on import.

Expand Down