@@ -9,9 +9,9 @@ is located on the front of the board alongside a microphone activity LED,
99which is lit when the microphone is in use.
1010
1111.. image :: microphone.png
12- :width: 200 px
12+ :width: 300 px
1313 :align: center
14- :height: 200 px
14+ :height: 240 px
1515 :alt: micro:bit with microphone LED on
1616
1717Sound events
@@ -28,103 +28,56 @@ Classes
2828.. py :class :: MicrobitMicrophone
2929
3030 .. note ::
31- The `MicrobitMicroPhone ` class documented here can be accessed via the microbit module as
32- `microbit.microphone `. It cannot be initialised on it's own.
33-
34- Represents the microphone
35-
36- #: Value to represent loud sound events, like clapping or shouting
37- LOUD = "loud"
38- #: Value to represent quiet sound events, like speaking or background music
39- QUIET = "quiet"
40-
41- def current_sound(self) -> string:
42- """
43- :return: The name of the last recorded sound event, `loud ` or `quiet `.
44- """
45- pass
46-
47- def was_sound(self, sound: string) -> bool:
48- """
49- :param sound: A sound event, such as `microphone.LOUD ` or
50- `microphone.QUIET `.
51- :return: `true ` if sound was heard at least once since the last
52- call, otherwise `false `.
53- """
54- pass
55-
56- def get_sounds(self):
57- """
58- :return: A tuple of the event history. The most recent is listed last.
59- Also clears the sound event history before returning
60- """
61- pass
62-
63- def set_threshold(self, sound: string, level: int) -> None:
64- """
65- :param sound: A sound event, such as `microphone.LOUD ` or
66- `microphone.QUIET `.
67- :param value: The threshold level in the range 0-255. For example,
68- `set_threshold(microphone.LOUD, 250) ` will only trigger if the
69- sound is very loud (>= 250).
70- """
71- pass
72-
73- def sound_level(self) -> int:
74- """
75- :return: A representation of the sound pressure level in the range 0 to
76- 20000.
77- """
78- pass
31+ The ``MicrobitMicroPhone `` class documented here can be accessed via
32+ the microbit module as ``microbit.microphone ``. It cannot be
33+ initialised on it's own.
34+
35+ Represents the microphone.
36+
37+ .. py :class :: SoundEvent
38+
39+ Value to represent loud sound events, like clapping or shouting
40+ ``SoundEvent.LOUD `` = ``loud ``.
41+
42+ Value to represent quiet sound events, like speaking or background music
43+ ``SoundEvent.QUIET `` = ``quiet ``.
44+
45+ .. py :function :: current_event()
46+
47+ Returns the name of the last recorded sound event, ``loud `` or ``quiet ``.
48+
49+ .. py :function :: was_event(SoundEvent.LOUD )
50+
51+ Parameter: A sound event, such as ``SoundEvent.LOUD `` or ``SoundEvent.QUIET ``.
52+
53+ Returns ``true `` if sound was heard at least once since the last call,
54+ otherwise ``false ``.
55+
56+ .. py :function :: get_events():
57+
58+ Returns a tuple of the event history. The most recent is listed last.
59+
60+ Also clears the sound event history before returning.
61+
62+ .. py :function :: set_threshold()
63+
64+ Parameter: A sound event, such as ``SoundEvent.LOUD `` or ``SoundEvent.QUIET ``.
65+
66+ Parameter: The threshold level in the range 0-255.
67+
68+ For example, ``set_threshold(SoundEvent.LOUD, 250) `` will only trigger if
69+ the sound is very loud (>= 250).
70+
71+ .. py :function :: sound_level()
72+
73+ Returns a representation of the sound pressure level in the range 0 to
74+ 255.
75+
7976
8077Example
8178=======
8279
83- An example that runs through all the functions of the microphone API
84-
85- .. code ::
86-
87- # Basic test for microphone. This test should update the display when Button A is pressed and a loud or quiet sound *is* heard,
88- # printing the results. On Button B This test should update the display when a loud or quiet sound *was* heard,
89- # printing the results. On shake this should print the last sounds heard, you shoul try this test whilst
90- # making a loud sound and a quiet one before you shake.
91-
92- from microbit import *
93-
94- icons = {
95- microphone.LOUD: Image.SQUARE,
96- microphone.QUIET: Image.SQUARE_SMALL
97- }
98-
99- display.clear()
100- sound = microphone.current_sound()
101-
102- while True:
103- if button_a.is_pressed():
104- if microphone.current_sound() == microphone.LOUD:
105- display.show(Image.SQUARE)
106- uart.write('isLoud\n')
107- elif microphone.current_sound() == microphone.QUIET:
108- display.show(Image.SQUARE_SMALL)
109- uart.write('isQuiet\n')
110- sleep(500)
111- display.clear()
112- if button_b.is_pressed():
113- if microphone.was_sound(microphone.LOUD):
114- display.show(Image.SQUARE)
115- uart.write('wasLoud\n')
116- elif microphone.was_sound(microphone.QUIET):
117- display.show(Image.SQUARE_SMALL)
118- uart.write('wasQuiet\n')
119- else:
120- display.clear()
121- sleep(500)
122- display.clear()
123- if accelerometer.was_gesture('shake'):
124- sounds = microphone.get_sounds()
125- soundLevel = microphone.sound_level()
126- print(soundLevel)
127- for sound in sounds:
128- display.show(icons[sound])
129- print(sound)
130- sleep(500)
80+ An example that runs through some of the functions of the microphone API
81+
82+ .. include :: ../examples/microphone.py
83+ :code: python
0 commit comments