@@ -17,7 +17,7 @@ which is lit when the microphone is in use.
17
17
Sound events
18
18
============
19
19
The microphone can respond to a pre-defined set of sound events that are
20
- based on the amplitude and wavelength of the sound.
20
+ based on the amplitude and wavelength of the sound.
21
21
22
22
These sound events are represented by instances of the ``SoundEvent `` class,
23
23
accessible via variables in ``microbit.SoundEvent ``:
@@ -26,48 +26,48 @@ accessible via variables in ``microbit.SoundEvent``:
26
26
from ``loud `` to ``quiet `` like speaking or background music.
27
27
28
28
- ``microbit.SoundEvent.LOUD ``: Represents the transition of sound events,
29
- from ``quiet `` to ``loud `` like clapping or shouting.
29
+ from ``quiet `` to ``loud `` like shouting.
30
+
31
+ - ``microbit.SoundEvent.CLAP ``: Detects a loud event similar to a clap.
30
32
31
33
Functions
32
34
=========
33
35
34
36
.. py :function :: current_event()
35
37
36
- * ** return **: the name of the last recorded sound event,
37
- ``SoundEvent('loud') `` or ``SoundEvent('quiet') ``.
38
+ :returns: The name of the last recorded sound event,
39
+ ``SoundEvent('loud') ``, ``SoundEvent('quiet') ``, `` SoundEvent('clap ') ``.
38
40
39
41
.. py :function :: was_event(event)
40
42
41
- * ** event **: a sound event, such as ``SoundEvent.LOUD `` or
42
- ``SoundEvent.QUIET ``.
43
- * ** return ** : ``true `` if sound was heard at least once since the last
43
+ :param event: A sound event, such as ``SoundEvent.LOUD ``,
44
+ ``SoundEvent.QUIET ``, or `` SoundEvent.CLAP `` .
45
+ :returns : ``true `` if sound was heard at least once since the last
44
46
call, otherwise ``false ``. ``was_event() `` also clears the sound
45
47
event history before returning.
46
48
47
49
.. py :function :: is_event(event)
48
50
49
- * ** event **: a sound event, such as ``SoundEvent.LOUD `` or
50
- ``SoundEvent.QUIET ``.
51
- * ** return ** : ``true `` if sound event is the most recent since the last
51
+ :param event: A sound event, such as ``SoundEvent.LOUD `` or
52
+ ``SoundEvent.QUIET ``, or `` SoundEvent.CLAP `` .
53
+ :returns : ``true `` if sound event is the most recent since the last
52
54
call, otherwise ``false ``. It does not clear the sound event history.
53
55
54
56
.. py :function :: get_events()
55
57
56
- * ** return **: a tuple of the event history. The most recent is listed last.
58
+ :returns: A tuple of the event history. The most recent is listed last.
57
59
``get_events() `` also clears the sound event history before returning.
58
60
59
61
.. py :function :: set_threshold(event, value)
60
62
61
- * **event **: a sound event, such as ``SoundEvent.LOUD `` or
62
- ``SoundEvent.QUIET ``.
63
-
64
- * **value **: The threshold level in the range 0-255. For example,
63
+ :param event: A ``SoundEvent.LOUD `` or ``SoundEvent.QUIET `` sound event.
64
+ :param value: The threshold level in the range 0-255. For example,
65
65
``set_threshold(SoundEvent.LOUD, 250) `` will only trigger if the sound is
66
66
very loud (>= 250).
67
67
68
68
.. py :function :: sound_level()
69
69
70
- * ** return **: a representation of the sound pressure level in the range 0 to
70
+ :returns: A representation of the sound pressure level in the range 0 to
71
71
255.
72
72
73
73
@@ -80,7 +80,7 @@ An example that runs through some of the functions of the microphone API::
80
80
# Button A is pressed and a loud or quiet sound *is* heard, printing the
81
81
# results. On Button B this test should update the display when a loud or
82
82
# quiet sound *was* heard, printing the results. On shake this should print
83
- # the last sounds heard, you should try this test whilst making a loud sound
83
+ # the last sounds heard, you should try this test whilst making a loud sound
84
84
# and a quiet one before you shake.
85
85
86
86
from microbit import *
@@ -90,7 +90,10 @@ An example that runs through some of the functions of the microphone API::
90
90
91
91
while True:
92
92
if button_a.is_pressed():
93
- if microphone.current_event() == SoundEvent.LOUD:
93
+ if microphone.current_event() == SoundEvent.CLAP:
94
+ display.show(Image.DIAMOND)
95
+ uart.write('isClap\n')
96
+ elif microphone.current_event() == SoundEvent.LOUD:
94
97
display.show(Image.SQUARE)
95
98
uart.write('isLoud\n')
96
99
elif microphone.current_event() == SoundEvent.QUIET:
@@ -99,7 +102,10 @@ An example that runs through some of the functions of the microphone API::
99
102
sleep(500)
100
103
display.clear()
101
104
if button_b.is_pressed():
102
- if microphone.was_event(SoundEvent.LOUD):
105
+ if microphone.was_event(SoundEvent.CLAP):
106
+ display.show(Image.DIAMOND)
107
+ uart.write('wasClap\n')
108
+ elif microphone.was_event(SoundEvent.LOUD):
103
109
display.show(Image.SQUARE)
104
110
uart.write('wasLoud\n')
105
111
elif microphone.was_event(SoundEvent.QUIET):
@@ -114,7 +120,9 @@ An example that runs through some of the functions of the microphone API::
114
120
soundLevel = microphone.sound_level()
115
121
print(soundLevel)
116
122
for sound in sounds:
117
- if sound == SoundEvent.LOUD:
123
+ if sound == SoundEvent.CLAP:
124
+ display.show(Image.DIAMOND)
125
+ elif sound == SoundEvent.LOUD:
118
126
display.show(Image.SQUARE)
119
127
elif sound == SoundEvent.QUIET:
120
128
display.show(Image.SQUARE_SMALL)
0 commit comments