|
31 | 31 | __version__ = "0.0.0-auto.0"
|
32 | 32 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_RTTTL"
|
33 | 33 |
|
| 34 | +import sys |
34 | 35 | import time
|
35 | 36 | import pulseio
|
36 | 37 |
|
@@ -128,8 +129,16 @@ def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo):
|
128 | 129 | base_tone.frequency = int(PIANO[piano_note])
|
129 | 130 | base_tone.duty_cycle = 2 ** 15
|
130 | 131 | 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) |
133 | 142 |
|
134 | 143 | time.sleep(4 / note_duration * 60 / tempo)
|
135 | 144 | if pwm:
|
@@ -167,7 +176,12 @@ def play(pin, rtttl, octave=None, duration=None, tempo=None):
|
167 | 176 | if AUDIOIO_AVAILABLE:
|
168 | 177 | wave, min_freq = _get_wave(tune, octave)
|
169 | 178 | 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) |
171 | 185 | except ValueError:
|
172 | 186 | # No DAC on the pin so use PWM.
|
173 | 187 | pass
|
|
0 commit comments