-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DO NOT MERGE] Update for forthcoming beta MicroPython #97
base: main
Are you sure you want to change the base?
Changes from 19 commits
791689a
506c92f
9221520
12a25f1
d9b3ffb
44af467
5ff1eb7
6190ec7
9704523
e0a4593
b7f91ec
79b44b6
b90c486
609444f
7cb5277
f5ca4aa
936b172
7057837
f4c9898
263b689
3826bce
6dcc5ab
fc85377
69aaab7
91eb337
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
from typing import Optional, Tuple | ||
from ..microbit import SoundEvent | ||
from ..microbit.audio import AudioFrame | ||
|
||
def current_event() -> Optional[SoundEvent]: | ||
"""Get the last recorded sound event | ||
|
@@ -53,18 +54,107 @@ def set_threshold(event: SoundEvent, value: int) -> None: | |
|
||
Example: ``microphone.set_threshold(SoundEvent.LOUD, 250)`` | ||
|
||
A high threshold means the event will only trigger if the sound is very loud (>= 250 in the example). | ||
The ``SoundEvent.LOUD`` event is triggered when the sound level crosses the | ||
threshold from "quiet" to "loud", and the ``SoundEvent.QUIET`` event is | ||
triggered when the sound level crosses from "loud" to "quiet". | ||
|
||
If the ``SoundEvent.LOUD`` threshold is set lower than the | ||
``SoundEvent.QUIET`` threshold, then the ``SoundEvent.QUIET`` threshold | ||
will decrease by one unit below the ``SoundEvent.LOUD`` threshold. If the | ||
``SoundEvent.QUIET`` threshold is set higher than the ``SoundEvent.LOUD`` | ||
threshold, then the ``SoundEvent.LOUD`` threshold will increase by one unit | ||
above the ``SoundEvent.QUIET`` threshold. | ||
|
||
:param event: A sound event, such as ``SoundEvent.LOUD`` or ``SoundEvent.QUIET``. | ||
:param value: The threshold level in the range 0-255. | ||
:param value: The threshold level in the range 0-255. Values outside this range will be clamped. | ||
""" | ||
... | ||
|
||
def sound_level() -> int: | ||
"""Get the sound pressure level. | ||
"""Get the sound pressure level in the range 0 to 255. | ||
|
||
Example: ``microphone.sound_level()`` | ||
|
||
:return: A representation of the sound pressure level in the range 0 to 255. | ||
""" | ||
... | ||
|
||
def sound_level_db() -> int: | ||
"""Get the sound pressure level in decibels. | ||
|
||
Example: ``microphone.sound_level_db()`` | ||
|
||
:return: A representation of the sound pressure level in decibels (dB). | ||
""" | ||
... | ||
Comment on lines
+82
to
+89
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be subject to change following bbcmicrobit/micropython#803 PR |
||
|
||
def record(duration: int, rate: int = 7812) -> AudioFrame: | ||
"""Record sound into an ``AudioFrame`` for the amount of time indicated by | ||
``duration`` at the sampling rate indicated by ``rate``. | ||
|
||
Example: ``my_frame = microphone.record(3000)`` | ||
|
||
The amount of memory consumed is directly related to the length of the | ||
recording and the sampling rate. The higher these values, the more memory | ||
it will use. | ||
|
||
A lower sampling rate will reduce both memory consumption and sound | ||
quality. | ||
|
||
If there isn't enough memory available a ``MemoryError`` will be raised. | ||
|
||
:param duration: How long to record in milliseconds. | ||
:param rate: Number of samples to capture per second. | ||
:return: An ``AudioFrame`` with the sound samples. | ||
""" | ||
... | ||
|
||
def record_into(buffer: AudioFrame, rate: int = 7812, wait: bool = True) -> None: | ||
"""Record sound into an existing ``AudioFrame`` until it is filled, | ||
or the ``stop_recording()`` function is called. | ||
|
||
Example: ``microphone.record_into(my_frame)`` | ||
|
||
:param buffer: An ``AudioFrame`` to record sound. | ||
:param rate: Number of samples to capture per second. | ||
:param wait: When set to ``True`` it blocks until the recording is | ||
done, if it is set to ``False`` it will run in the background. | ||
""" | ||
... | ||
|
||
def is_recording() -> bool: | ||
"""Checks whether the microphone is currently recording. | ||
|
||
Example: ``is_recording = microphone.is_recording()`` | ||
|
||
:return: ``True`` if the microphone is currently recording sound, otherwise returns ``False``. | ||
""" | ||
... | ||
|
||
def stop_recording() -> None: | ||
"""Stops a recording running in the background. | ||
|
||
Example: ``microphone.stop_recording()`` | ||
""" | ||
... | ||
|
||
SENSITIVITY_LOW: float; | ||
"""Low microphone sensitivity.""" | ||
|
||
SENSITIVITY_MEDIUM: float; | ||
"""Medium microphone sensitivity.""" | ||
|
||
SENSITIVITY_HIGH: float; | ||
"""High microphone sensitivity.""" | ||
|
||
|
||
def set_sensitivity(gain: float) -> None: | ||
"""Configure the microphone sensitivity. | ||
|
||
Example: ``microphone.set_sensitivity(microphone.SENSITIVITY_HIGH)`` | ||
|
||
The default sensitivity is ``microphone.SENSITIVITY_MEDIUM``. | ||
|
||
:param gain: The microphone gain. Use ``microphone.SENSITIVITY_LOW``, ``microphone.SENSITIVITY_MEDIUM``, ``microphone.SENSITIVITY_HIGH``, or a value between these levels. | ||
""" | ||
... |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,16 @@ def ticks_us() -> int: | |
""" | ||
... | ||
|
||
def ticks_cpu() -> int: | ||
""" | ||
Similar to ticks_ms and ticks_us, but with higher resolution in CPU cycles. | ||
|
||
Example: ``time.ticks_cpu()`` | ||
|
||
:return: The counter value in CPU cycles. | ||
""" | ||
... | ||
|
||
Comment on lines
+59
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @microbit-carlos, this is the best guess we made for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Grace, I wasn't aware that the There is some upstream documentation here: https://docs.micropython.org/en/latest/library/time.html#time.ticks_cpu But it would be good to understand the exact implementation for micro:bit and to have that in the official micro:bit docs as well. I'll have a look. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upgraded here: microbit-foundation/micropython-microbit-v2@fdaf840 That diff contains the implementation that uses CYCCNT. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the moment we always return zero in the sim for ticks_cpu but we could return the same as ticks_us or a multiple. ticks_us is already a 1000 * multiple of ticks_ms which is our real resolution in the sim. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it was added with the update to MicroPython 1.22.0 in January: microbit-foundation/micropython-microbit-v2#166 The thing to keep in mind is that this is register is 32 bits and we run at 64 MHz, so the register overflows every minute ish ( 2^32 / 64_000_000 = 67 seconds) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @microbit-carlos ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction, it's likely 30-bits due to small int size in MicroPython. |
||
def ticks_add(ticks: int, delta: int) -> int: | ||
""" | ||
Offset ticks value by a given number, which can be either positive or | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an update based on recently merged micropython doc PR.
f5ca4aa - I copied
set_threshold
doc from the PR936b172 - my update of it to make the tenses match the rest of the docs and to make it clearer (maybe)?
Looks quite verbose, so I am tempted to cut it down, but am not sure how
![Screenshot 2024-04-12 at 17 17 21](https://private-user-images.githubusercontent.com/145345672/322052189-ab8eaf1c-372e-4e24-8c72-35f53b842fde.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkyNTM3MDQsIm5iZiI6MTczOTI1MzQwNCwicGF0aCI6Ii8xNDUzNDU2NzIvMzIyMDUyMTg5LWFiOGVhZjFjLTM3MmUtNGUyNC04YzcyLTM1ZjUzYjg0MmZkZS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjUwMjExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MDIxMVQwNTU2NDRaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1jMmI3MDcyMTVhZjQzMmY5Nzc5YmI0MzIxNzVlZDI4NzM1ODc2MGI3Yjk2ODc3ODg1MmI5MGVmZmZiNGVkN2YwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.OI5kOjBLc4xKOrzqVF8d8XQcbBPuJ4DI3fb4fLlfaHM)