Skip to content

tone: Work with 4.x, 5.0; AudioOut and PWMAudioOut #46

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 7 commits into from
Sep 16, 2019
Merged
Changes from 6 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
39 changes: 20 additions & 19 deletions simpleio.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,23 @@
* Author(s): Scott Shawcroft
"""
import time
import sys
try:
import audioio
except ImportError:
pass # not always supported by every board!
import array
import digitalio
import pulseio
try:
import audiocore
except ImportError:
try:
import audioio as audiocore
except ImportError:
pass # not always supported by every board!
try:
from audioio import AudioOut
except ImportError:
try:
from audiopwmio import PWMAudioOut as AudioOut
except ImportError:
pass # not always supported by every board!

def tone(pin, frequency, duration=1, length=100):
"""
Expand All @@ -61,21 +70,13 @@ def tone(pin, frequency, duration=1, length=100):
square_wave = array.array("H", [0] * sample_length)
for i in range(sample_length / 2):
square_wave[i] = 0xFFFF
if sys.implementation.version[0] >= 3:
square_wave_sample = audioio.RawSample(square_wave)
square_wave_sample.sample_rate = int(len(square_wave) * frequency)
with audioio.AudioOut(pin) as dac:
if not dac.playing:
dac.play(square_wave_sample, loop=True)
time.sleep(duration)
dac.stop()
else:
sample_tone = audioio.AudioOut(pin, square_wave)
sample_tone.frequency = int(len(square_wave) * frequency)
if not sample_tone.playing:
sample_tone.play(loop=True)
square_wave_sample = audiocore.RawSample(square_wave)
square_wave_sample.sample_rate = int(len(square_wave) * frequency)
with AudioOut(pin) as dac:
if not dac.playing:
dac.play(square_wave_sample, loop=True)
time.sleep(duration)
sample_tone.stop()
dac.stop()



Expand Down