Skip to content

Commit 6d6d3f3

Browse files
authored
Merge pull request #10 from idcrook/fix_for_CP_RTTTL_issue8
add compatibility for AudioOut API changes in CP 3.x
2 parents a886b05 + d53e79d commit 6d6d3f3

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

adafruit_rtttl.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
__version__ = "0.0.0-auto.0"
3232
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RTTTL"
3333

34+
import sys
3435
import time
3536
import pulseio
3637

@@ -128,8 +129,16 @@ def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo):
128129
base_tone.frequency = int(PIANO[piano_note])
129130
base_tone.duty_cycle = 2 ** 15
130131
else:
131-
base_tone.frequency = int(16000 * (PIANO[piano_note] / min_freq))
132-
base_tone.play(loop=True)
132+
# AudioOut interface changed in CP 3.x
133+
if sys.implementation.version[0] >= 3:
134+
pitch = int(PIANO[piano_note])
135+
sine_wave = sine.sine_wave(16000, pitch)
136+
sine_wave_sample = audioio.RawSample(sine_wave)
137+
base_tone.play(sine_wave_sample, loop=True)
138+
else:
139+
base_tone.frequency = int(
140+
16000 * (PIANO[piano_note] / min_freq))
141+
base_tone.play(loop=True)
133142

134143
time.sleep(4 / note_duration * 60 / tempo)
135144
if pwm:
@@ -167,7 +176,12 @@ def play(pin, rtttl, octave=None, duration=None, tempo=None):
167176
if AUDIOIO_AVAILABLE:
168177
wave, min_freq = _get_wave(tune, octave)
169178
try:
170-
base_tone = audioio.AudioOut(pin, wave)
179+
# AudioOut interface changed in CP 3.x; a waveform if now pass
180+
# directly to .play(), generated for each note in _play_to_pin()
181+
if sys.implementation.version[0] >= 3:
182+
base_tone = audioio.AudioOut(pin)
183+
else:
184+
base_tone = audioio.AudioOut(pin, wave)
171185
except ValueError:
172186
# No DAC on the pin so use PWM.
173187
pass

0 commit comments

Comments
 (0)