-
Notifications
You must be signed in to change notification settings - Fork 287
V2 docs #692
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
Merged
Merged
V2 docs #692
Changes from all commits
Commits
Show all changes
67 commits
Select commit
Hold shift + click to select a range
e267646
Add microphone API
microbit-mark f3407cc
include V2 on index
microbit-mark 656a2c2
update music and V2 pins
microbit-mark 97af5b3
update speech
microbit-mark 2e5b2c8
update audio
microbit-mark 9205f32
update i2c
microbit-mark 3b49c8a
add description to built-in sounds
microbit-mark 198831f
format sounds
microbit-mark f68e326
format pins
microbit-mark ad54991
format pins
microbit-mark 0067550
spelling
microbit-mark 11d1ab1
Update docs/audio.rst
microbit-mark f5e4aed
Update docs/audio.rst
microbit-mark 218ddd6
Update docs/i2c.rst
microbit-mark 10ab2ea
Update docs/microbit_micropython_api.rst
microbit-mark 529a165
Update docs/microbit_micropython_api.rst
microbit-mark 78bd241
changes based on initial feedback
microbit-mark 63647a3
update return pin
microbit-mark aa503f0
sound is in microbit module
microbit-mark 31a7722
Update docs/audio.rst
microbit-mark 35e1928
Update docs/microbit_micropython_api.rst
microbit-mark 099b4e3
Update docs/microbit_micropython_api.rst
microbit-mark 921d7ca
more feedback updates
microbit-mark 37da28e
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark 4239dad
spacing
microbit-mark dd714cd
Update docs/audio.rst
microbit-mark 4846113
does this comment work?
microbit-mark f30fa02
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark 946638b
Revert "does this comment work?"
microbit-mark 01331f3
update audio
microbit-mark cba5965
update audio
microbit-mark da4fd76
add Python Editor
microbit-mark d74c7a5
add references to sound
microbit-mark 09b04c0
update mic image
microbit-mark 3ae79a4
Update docs/microbit_micropython_api.rst
microbit-mark 67f6a97
multiple fixes
microbit-mark 8c06787
use Param in docs
microbit-mark 8e83d9e
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark 81ffe80
update SouneEvent
microbit-mark e1e8487
update microphone module
microbit-mark e22ce51
update param
microbit-mark cff8eee
update image list
microbit-mark 826e9f8
update parameters
microbit-mark 6d088b0
remove note
microbit-mark 0081158
update param
microbit-mark 5aa2626
add line break
microbit-mark 8ad485d
add mic and make clases a-z
microbit-mark 16146f7
format parameters
microbit-mark 33864c2
Update docs/microbit_micropython_api.rst
microbit-mark a1400f8
Update docs/microbit_micropython_api.rst
microbit-mark fadcb74
more updates
microbit-mark 690c4b9
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark b954d38
tidy up
microbit-mark 8f52c97
move SoundEvent
microbit-mark 849eb3d
Revert "move SoundEvent"
microbit-mark 6351791
move soundEvent
microbit-mark acd3c9d
update image list
microbit-mark b3c9957
remove param reference
microbit-mark f5faefa
more feedback updates
microbit-mark bbe4d05
move audio to modules
microbit-mark 1040971
Update docs/music.rst
microbit-mark 37acd15
Update docs/microphone.rst
microbit-mark b20d460
Update docs/microbit_micropython_api.rst
microbit-mark 9e7e33c
Update docs/microphone.rst
microbit-mark 54e0eda
Update docs/music.rst
microbit-mark bf9721d
Update docs/audio.rst
microbit-mark 8fe52d8
more updates
microbit-mark File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -3,29 +3,48 @@ Audio | |||||
|
|
||||||
| .. py:module:: audio | ||||||
|
|
||||||
| This module allows you play sounds from a speaker attached to the Microbit. | ||||||
| In order to use the audio module you will need to provide a sound source. | ||||||
| This module allows you to play your own sounds. If you are using a micro:bit | ||||||
| **V2**, ``audio`` is also part of the ``microbit`` module. | ||||||
|
|
||||||
| A sound source is an iterable (sequence, like list or tuple, or a generator) of | ||||||
| frames, each of 32 samples. | ||||||
| The ``audio`` modules plays samples at the rate of 7812.5 samples per second, | ||||||
| which means that it can reproduce frequencies up to 3.9kHz. | ||||||
| By default sound output will be via the edge connector on pin 0 and the | ||||||
| built-in speaker **V2**. You can connect a wired headphones or a speaker to | ||||||
| pin 0 and GND on the edge connector to hear the sounds. | ||||||
|
|
||||||
| Functions | ||||||
| ========= | ||||||
|
|
||||||
| .. py:function:: play(source, wait=True, pin=pin0, return_pin=None) | ||||||
| play(source, wait=True, pin=(pin_speaker, pin0), return_pin=None) | ||||||
|
|
||||||
| Play the source to completion. | ||||||
|
|
||||||
| ``source`` is an iterable, each element of which must be an ``AudioFrame``. | ||||||
| * **source**: ``Sound`` - The ``microbit`` module contains a list of | ||||||
| built-in sounds that your can pass to ``audio.play()``. | ||||||
|
|
||||||
| If ``wait`` is ``True``, this function will block until the source is exhausted. | ||||||
| * **source**: ``AudioFrame`` - The source agrument can also be an iterable | ||||||
| of ``AudioFrame`` elements as described below. | ||||||
|
|
||||||
| ``pin`` specifies which pin the speaker is connected to. | ||||||
| * **wait**: If ``wait`` is ``True``, this function will block until the | ||||||
| source is exhausted. | ||||||
|
|
||||||
| ``return_pin`` specifies a differential pin to connect to the speaker | ||||||
| instead of ground. | ||||||
| * **pin**: As with the music module, you can use the optional ``pin`` | ||||||
| argument to specify the output pin can be used to override the | ||||||
| default of ``microbit.pin0``. If you have the latest micro:bit **V2**, | ||||||
| you can use ``microbit.pin_speaker``. The pin argument can also take a | ||||||
| tuple of two pins, for example ``pin=(pin_speaker, pin0)`` which would | ||||||
| output sound on the built-in speaker and pin 0. | ||||||
|
|
||||||
| * **return_pin**: specifies a differential edge connector pin to connect | ||||||
|
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.
Suggested change
|
||||||
| to an external speaker instead of ground. This is ignored for the **V2** | ||||||
| revision. | ||||||
|
|
||||||
| .. py:function:: is_playing() | ||||||
|
|
||||||
| Return ``True`` if audio is playing, otherwise return ``False``. | ||||||
|
|
||||||
| .. py:function:: stop() | ||||||
|
|
||||||
| Stops all audio playback. | ||||||
|
|
||||||
| Classes | ||||||
| ======= | ||||||
|
|
@@ -41,9 +60,25 @@ Classes | |||||
| Using audio | ||||||
| =========== | ||||||
|
|
||||||
| You will need a sound source, as input to the ``play`` function. You can generate your own, like in | ||||||
| ``examples/waveforms.py``. | ||||||
| You will need a sound source, as input to the ``play`` function. You can use | ||||||
| the built-in sounds **V2** from the ``microbit`` module, ``microbit.Sound``, or | ||||||
| generate your own, like in ``examples/waveforms.py``. | ||||||
|
|
||||||
| Built-in sounds **V2** | ||||||
| ---------------------- | ||||||
|
|
||||||
| The built-in sounds can be called using ``audio.play(Sound.NAME)``. | ||||||
|
|
||||||
| * ``Sound.GIGGLE`` | ||||||
| * ``Sound.HAPPY`` | ||||||
| * ``Sound.HELLO`` | ||||||
| * ``Sound.MYSTERIOUS`` | ||||||
| * ``Sound.SAD`` | ||||||
| * ``Sound.SLIDE`` | ||||||
| * ``Sound.SOARING`` | ||||||
| * ``Sound.SPRING`` | ||||||
| * ``Sound.TWINKLE`` | ||||||
| * ``Sound.YAWN`` | ||||||
|
|
||||||
| Technical Details | ||||||
| ================= | ||||||
microbit-carlos marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
@@ -52,23 +87,24 @@ Technical Details | |||||
| You don't need to understand this section to use the ``audio`` module. | ||||||
| It is just here in case you wanted to know how it works. | ||||||
|
|
||||||
| The ``audio`` module consumes samples at 7812.5 Hz, and uses linear interpolation to | ||||||
| output a PWM signal at 32.5 kHz, which gives tolerable sound quality. | ||||||
| The ``audio`` module consumes ``AudioFrame`` samples at 7812.5 Hz, and uses | ||||||
| linear interpolation to output a PWM signal at 32.5 kHz, which gives tolerable | ||||||
| sound quality. | ||||||
|
|
||||||
| The function ``play`` fully copies all data from each ``AudioFrame`` before it | ||||||
| calls ``next()`` for the next frame, so a sound source can use the same ``AudioFrame`` | ||||||
| repeatedly. | ||||||
| calls ``next()`` for the next frame, so a sound source can use the same | ||||||
| ``AudioFrame`` repeatedly. | ||||||
|
|
||||||
| The ``audio`` module has an internal 64 sample buffer from which it reads samples. | ||||||
| When reading reaches the start or the mid-point of the buffer, it triggers a callback to | ||||||
| fetch the next ``AudioFrame`` which is then copied into the buffer. | ||||||
| This means that a sound source has under 4ms to compute the next ``AudioFrame``, | ||||||
| and for reliable operation needs to take less 2ms (which is 32000 cycles, so should be plenty). | ||||||
| The ``audio`` module has an internal 64 sample buffer from which it reads | ||||||
| samples. When reading reaches the start or the mid-point of the buffer, it | ||||||
| triggers a callback to fetch the next ``AudioFrame`` which is then copied into | ||||||
| the buffer. This means that a sound source has under 4ms to compute the next | ||||||
| ``AudioFrame``, and for reliable operation needs to take less 2ms (which is | ||||||
| 32000 cycles, so should be plenty). | ||||||
|
|
||||||
|
|
||||||
| Example | ||||||
| ======= | ||||||
|
|
||||||
| .. include:: ../examples/waveforms.py | ||||||
| :code: python | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.