31
31
except ImportError :
32
32
pass
33
33
34
+ try :
35
+ from typing import Optional , Union , Tuple , List
36
+ from audioio import AudioOut
37
+ except ImportError :
38
+ pass
39
+
34
40
PIANO = {
35
41
"4c" : 261.626 ,
36
42
"4c#" : 277.183 ,
73
79
}
74
80
75
81
76
- def _parse_note (note , duration = 2 , octave = "6" ) :
82
+ def _parse_note (note : str , duration : int = 2 , octave : int = 6 ) -> Tuple [ str , float ] :
77
83
note = note .strip ()
78
84
piano_note = None
79
85
note_duration = duration
@@ -89,14 +95,14 @@ def _parse_note(note, duration=2, octave="6"):
89
95
note_duration *= 1.5
90
96
if "#" in note :
91
97
piano_note += "#"
92
- note_octave = octave
98
+ note_octave = str ( octave )
93
99
if note [- 1 ].isdigit ():
94
100
note_octave = note [- 1 ]
95
101
piano_note = note_octave + piano_note
96
102
return piano_note , note_duration
97
103
98
104
99
- def _get_wave (tune , octave ) :
105
+ def _get_wave (tune : str , octave : int ) -> Tuple [ List [ int ], float ] :
100
106
"""Returns the proper waveform to play the song along with the minimum
101
107
frequency in the song.
102
108
"""
@@ -110,7 +116,14 @@ def _get_wave(tune, octave):
110
116
111
117
112
118
# pylint: disable-msg=too-many-arguments
113
- def _play_to_pin (tune , base_tone , min_freq , duration , octave , tempo ):
119
+ def _play_to_pin (
120
+ tune : str ,
121
+ base_tone : Union [pwmio .PWMOut , AudioOut ],
122
+ min_freq : float ,
123
+ duration : int ,
124
+ octave : int ,
125
+ tempo : int ,
126
+ ) -> None :
114
127
"""Using the prepared input send the notes to the pin"""
115
128
pwm = isinstance (base_tone , pwmio .PWMOut )
116
129
for note in tune .split ("," ):
@@ -139,10 +152,16 @@ def _play_to_pin(tune, base_tone, min_freq, duration, octave, tempo):
139
152
140
153
141
154
# pylint: disable-msg=too-many-arguments
142
- def play (pin , rtttl , octave = None , duration = None , tempo = None ):
155
+ def play (
156
+ pin ,
157
+ rtttl : str ,
158
+ octave : int = Optional [None ],
159
+ duration : Optional [int ] = None ,
160
+ tempo : Optional [int ] = None ,
161
+ ) -> None :
143
162
"""Play notes to a digialio pin using ring tone text transfer language (rtttl).
144
163
:param ~digitalio.DigitalInOut pin: the speaker pin
145
- :param rtttl: string containing rtttl
164
+ :param str rtttl: string containing rtttl
146
165
:param int octave: represents octave number (default 6 starts at middle c)
147
166
:param int duration: length of notes (default 4 quarter note)
148
167
:param int tempo: how fast (default 63 beats per minute)
0 commit comments