|
| 1 | +Microphone **V2** |
| 2 | +***************** |
| 3 | + |
| 4 | +.. py:module:: microbit.microphone |
| 5 | +
|
| 6 | +This object lets you access the built-in microphone available on the |
| 7 | +micro:bit **V2**. It can be used to respond to sound. The microphone input |
| 8 | +is located on the front of the board alongside a microphone activity LED, |
| 9 | +which is lit when the microphone is in use. |
| 10 | + |
| 11 | +.. image:: microphone.png |
| 12 | + :width: 300px |
| 13 | + :align: center |
| 14 | + :height: 240px |
| 15 | + :alt: micro:bit with microphone LED on |
| 16 | + |
| 17 | +Sound events |
| 18 | +============ |
| 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. |
| 21 | + |
| 22 | +These sound events are represented by instances of the ``SoundEvent`` class, |
| 23 | +accessible via variables in ``microbit.SoundEvent``: |
| 24 | + |
| 25 | +- ``microbit.SoundEvent.QUIET``: Represents the transition of sound events, |
| 26 | + from ``loud`` to ``quiet`` like speaking or background music. |
| 27 | + |
| 28 | +- ``microbit.SoundEvent.LOUD``: Represents the transition of sound events, |
| 29 | + from ``quiet`` to ``loud`` like clapping or shouting. |
| 30 | + |
| 31 | +Functions |
| 32 | +========= |
| 33 | + |
| 34 | +.. py:function:: current_event() |
| 35 | +
|
| 36 | + * **return**: the name of the last recorded sound event, |
| 37 | + ``SoundEvent('loud')`` or ``SoundEvent('quiet')``. |
| 38 | + |
| 39 | +.. py:function:: was_event(event) |
| 40 | +
|
| 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 |
| 44 | + call, otherwise ``false``. ``was_event()`` also clears the sound |
| 45 | + event history before returning. |
| 46 | + |
| 47 | +.. py:function:: is_event(event) |
| 48 | +
|
| 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 |
| 52 | + call, otherwise ``false``. It does not clear the sound event history. |
| 53 | + |
| 54 | +.. py:function:: get_events() |
| 55 | +
|
| 56 | + * **return**: a tuple of the event history. The most recent is listed last. |
| 57 | + ``get_events()`` also clears the sound event history before returning. |
| 58 | + |
| 59 | +.. py:function:: set_threshold(event, value) |
| 60 | +
|
| 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, |
| 65 | + ``set_threshold(SoundEvent.LOUD, 250)`` will only trigger if the sound is |
| 66 | + very loud (>= 250). |
| 67 | + |
| 68 | +.. py:function:: sound_level() |
| 69 | +
|
| 70 | + * **return**: a representation of the sound pressure level in the range 0 to |
| 71 | + 255. |
| 72 | + |
| 73 | + |
| 74 | +Example |
| 75 | +======= |
| 76 | + |
| 77 | +An example that runs through some of the functions of the microphone API:: |
| 78 | + |
| 79 | + # Basic test for microphone. This test should update the display when |
| 80 | + # Button A is pressed and a loud or quiet sound *is* heard, printing the |
| 81 | + # results. On Button B this test should update the display when a loud or |
| 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 |
| 84 | + # and a quiet one before you shake. |
| 85 | + |
| 86 | + from microbit import * |
| 87 | + |
| 88 | + display.clear() |
| 89 | + sound = microphone.current_event() |
| 90 | + |
| 91 | + while True: |
| 92 | + if button_a.is_pressed(): |
| 93 | + if microphone.current_event() == SoundEvent.LOUD: |
| 94 | + display.show(Image.SQUARE) |
| 95 | + uart.write('isLoud\n') |
| 96 | + elif microphone.current_event() == SoundEvent.QUIET: |
| 97 | + display.show(Image.SQUARE_SMALL) |
| 98 | + uart.write('isQuiet\n') |
| 99 | + sleep(500) |
| 100 | + display.clear() |
| 101 | + if button_b.is_pressed(): |
| 102 | + if microphone.was_event(SoundEvent.LOUD): |
| 103 | + display.show(Image.SQUARE) |
| 104 | + uart.write('wasLoud\n') |
| 105 | + elif microphone.was_event(SoundEvent.QUIET): |
| 106 | + display.show(Image.SQUARE_SMALL) |
| 107 | + uart.write('wasQuiet\n') |
| 108 | + else: |
| 109 | + display.clear() |
| 110 | + sleep(500) |
| 111 | + display.clear() |
| 112 | + if accelerometer.was_gesture('shake'): |
| 113 | + sounds = microphone.get_events() |
| 114 | + soundLevel = microphone.sound_level() |
| 115 | + print(soundLevel) |
| 116 | + for sound in sounds: |
| 117 | + if sound == SoundEvent.LOUD: |
| 118 | + display.show(Image.SQUARE) |
| 119 | + elif sound == SoundEvent.QUIET: |
| 120 | + display.show(Image.SQUARE_SMALL) |
| 121 | + else: |
| 122 | + display.clear() |
| 123 | + print(sound) |
| 124 | + sleep(500) |
0 commit comments