Skip to content
Merged
Show file tree
Hide file tree
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 Nov 17, 2020
f3407cc
include V2 on index
microbit-mark Nov 17, 2020
656a2c2
update music and V2 pins
microbit-mark Nov 18, 2020
97af5b3
update speech
microbit-mark Nov 19, 2020
2e5b2c8
update audio
microbit-mark Nov 20, 2020
9205f32
update i2c
microbit-mark Nov 20, 2020
3b49c8a
add description to built-in sounds
microbit-mark Nov 25, 2020
198831f
format sounds
microbit-mark Nov 25, 2020
f68e326
format pins
microbit-mark Nov 25, 2020
ad54991
format pins
microbit-mark Nov 25, 2020
0067550
spelling
microbit-mark Nov 25, 2020
11d1ab1
Update docs/audio.rst
microbit-mark Nov 26, 2020
f5e4aed
Update docs/audio.rst
microbit-mark Nov 26, 2020
218ddd6
Update docs/i2c.rst
microbit-mark Nov 26, 2020
10ab2ea
Update docs/microbit_micropython_api.rst
microbit-mark Nov 26, 2020
529a165
Update docs/microbit_micropython_api.rst
microbit-mark Nov 26, 2020
78bd241
changes based on initial feedback
microbit-mark Nov 26, 2020
63647a3
update return pin
microbit-mark Nov 26, 2020
aa503f0
sound is in microbit module
microbit-mark Nov 26, 2020
31a7722
Update docs/audio.rst
microbit-mark Nov 26, 2020
35e1928
Update docs/microbit_micropython_api.rst
microbit-mark Nov 26, 2020
099b4e3
Update docs/microbit_micropython_api.rst
microbit-mark Nov 26, 2020
921d7ca
more feedback updates
microbit-mark Nov 26, 2020
37da28e
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark Nov 26, 2020
4239dad
spacing
microbit-mark Nov 26, 2020
dd714cd
Update docs/audio.rst
microbit-mark Nov 26, 2020
4846113
does this comment work?
microbit-mark Nov 26, 2020
f30fa02
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark Nov 26, 2020
946638b
Revert "does this comment work?"
microbit-mark Nov 26, 2020
01331f3
update audio
microbit-mark Nov 26, 2020
cba5965
update audio
microbit-mark Nov 26, 2020
da4fd76
add Python Editor
microbit-mark Nov 27, 2020
d74c7a5
add references to sound
microbit-mark Nov 27, 2020
09b04c0
update mic image
microbit-mark Nov 27, 2020
3ae79a4
Update docs/microbit_micropython_api.rst
microbit-mark Nov 27, 2020
67f6a97
multiple fixes
microbit-mark Nov 27, 2020
8c06787
use Param in docs
microbit-mark Nov 27, 2020
8e83d9e
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark Nov 27, 2020
81ffe80
update SouneEvent
microbit-mark Nov 27, 2020
e1e8487
update microphone module
microbit-mark Nov 27, 2020
e22ce51
update param
microbit-mark Nov 27, 2020
cff8eee
update image list
microbit-mark Nov 27, 2020
826e9f8
update parameters
microbit-mark Nov 27, 2020
6d088b0
remove note
microbit-mark Nov 27, 2020
0081158
update param
microbit-mark Nov 27, 2020
5aa2626
add line break
microbit-mark Nov 27, 2020
8ad485d
add mic and make clases a-z
microbit-mark Nov 27, 2020
16146f7
format parameters
microbit-mark Nov 27, 2020
33864c2
Update docs/microbit_micropython_api.rst
microbit-mark Nov 27, 2020
a1400f8
Update docs/microbit_micropython_api.rst
microbit-mark Nov 27, 2020
fadcb74
more updates
microbit-mark Nov 27, 2020
690c4b9
Merge branch 'v2-docs' of https://github.com/microbit-mark/micropytho…
microbit-mark Nov 27, 2020
b954d38
tidy up
microbit-mark Nov 28, 2020
8f52c97
move SoundEvent
microbit-mark Nov 30, 2020
849eb3d
Revert "move SoundEvent"
microbit-mark Dec 1, 2020
6351791
move soundEvent
microbit-mark Dec 1, 2020
acd3c9d
update image list
microbit-mark Dec 2, 2020
b3c9957
remove param reference
microbit-mark Dec 2, 2020
f5faefa
more feedback updates
microbit-mark Dec 2, 2020
bbe4d05
move audio to modules
microbit-mark Dec 2, 2020
1040971
Update docs/music.rst
microbit-mark Dec 3, 2020
37acd15
Update docs/microphone.rst
microbit-mark Dec 3, 2020
b20d460
Update docs/microbit_micropython_api.rst
microbit-mark Dec 3, 2020
9e7e33c
Update docs/microphone.rst
microbit-mark Dec 3, 2020
54e0eda
Update docs/music.rst
microbit-mark Dec 3, 2020
bf9721d
Update docs/audio.rst
microbit-mark Dec 3, 2020
8fe52d8
more updates
microbit-mark Dec 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 59 additions & 23 deletions docs/audio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* **return_pin**: specifies a differential edge connector pin to connect
* **return_pin**: Specifies a differential edge connector pin to connect

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
=======
Expand All @@ -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
=================
Expand All @@ -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

2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

# General information about the project.
project = u'BBC micro:bit MicroPython'
copyright = u'2015-2016, Multiple authors'
copyright = u'2015-2020, Multiple authors'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down
9 changes: 7 additions & 2 deletions docs/i2c.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ Functions

.. warning::

Changing the I²C pins from defaults will make the accelerometer and
compass stop working, as they are connected internally to those pins.
On a micro:bit V1 board, changing the I²C pins from defaults will make
the accelerometer and compass stop working, as they are connected
internally to those pins. This warning does not apply to the **V2**
revision of the micro:bit as this has `separate I²C lines
<https://tech.microbit.org/hardware/i2c/>`_
for the motion sensors and the edge connector.



.. py:function:: scan()
Expand Down
29 changes: 23 additions & 6 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@ BBC micro:bit MicroPython documentation

Welcome!

The BBC micro:bit is a small computing device for children. One of the
languages it understands is the popular Python programming language. The
version of Python that runs on the BBC micro:bit is called MicroPython.
The `BBC micro:bit <https://microbit.org>`_ is a small computing device for
children. One of the languages it understands is the popular Python programming
language. The version of Python that runs on the BBC micro:bit is called
MicroPython.

This documentation includes lessons for teachers
and API documentation for developers (check out the index on the left). We hope
you enjoy developing for the BBC micro:bit using MicroPython.

If you're a new programmer, teacher or unsure where to start, begin with the tutorials.
.. note::

The BBC micro:bit MicroPython documentation contains information for all
versions of the micro:bit board. Where functionality is applicable only
to the latest device, you will see a note or comment marking this as
**V2**.

If you're a new programmer, teacher or unsure where to start, begin with the
tutorials.

.. image:: comic.png

Expand All @@ -28,8 +37,15 @@ If you're a new programmer, teacher or unsure where to start, begin with the tut

Projects related to MicroPython on the BBC micro:bit include:

* `Mu <https://github.com/ntoll/mu>`_ - a simple code editor for kids, teachers and beginner programmers. Probably the easiest way for people to program MicroPython on the BBC micro:bit.
* `uFlash <https://uflash.readthedocs.io/en/latest/>`_ - a command line tool for flashing raw Python scripts onto a BBC micro:bit.
* `micro:bit Python Editor <https://python.microbit.org>`_ A simple
browser-based code editor, designed to help teachers and learners get the
most out of text-based programming on the micro:bit.

* `Mu <https://codewith.mu>`_ A simple offline code editor for kids, teachers
and beginner programmers.

* `uFlash <https://uflash.readthedocs.io/en/latest/>`_ A command line tool
for flashing raw Python scripts onto a BBC micro:bit.

.. toctree::
:maxdepth: 2
Expand Down Expand Up @@ -67,6 +83,7 @@ Projects related to MicroPython on the BBC micro:bit include:
i2c.rst
image.rst
machine.rst
microphone.rst
micropython.rst
music.rst
neopixel.rst
Expand Down
11 changes: 7 additions & 4 deletions docs/microbit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Classes
:maxdepth: 1

image.rst
Sound <audio.rst>


Modules
Expand All @@ -70,9 +71,11 @@ Modules
.. toctree::
:maxdepth: 1

display.rst
uart.rst
spi.rst
i2c.rst
accelerometer.rst
Audio V2 <audio.rst>
compass.rst
display.rst
i2c.rst
microphone.rst
spi.rst
uart.rst
Loading