Skip to content

Commit af19065

Browse files
Docs: Speaker update and play() pin arguments (#695)
* speaker pin changes * Docs: update speaker pin info * Add speaker page * grammar * Link to speaker page * add speaker to navigation * add speaker example * Update docs/speaker.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/speaker.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Update docs/music.rst Co-authored-by: Carlos Pereira Atencio <[email protected]> * Docs: Update speaker pin * remove build * gitignore _build directory * update music * Update docs/music.rst * Update docs/speech.rst * Update docs/speech.rst * Update docs/speech.rst * Update docs/music.rst Co-authored-by: Carlos Pereira Atencio <[email protected]>
1 parent d040ed5 commit af19065

File tree

9 files changed

+98
-40
lines changed

9 files changed

+98
-40
lines changed

docs/audio.rst

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ Audio
66
This module allows you play sounds with the micro:bit.
77

88
By default sound output will be via the edge connector on pin 0 and the
9-
built-in speaker **V2**. You can connect a wired headphones or a speaker to
10-
pin 0 and GND on the edge connector to hear the sounds.
9+
:doc:`built-in speaker <speaker>` **V2**. You can connect wired headphones or
10+
a speaker to pin 0 and GND on the edge connector to hear the sounds.
1111

1212
The ``audio`` module can be imported as ``import audio`` or accessed via
1313
the ``microbit`` module as ``microbit.audio``.
@@ -16,7 +16,6 @@ Functions
1616
=========
1717

1818
.. py:function:: play(source, wait=True, pin=pin0, return_pin=None)
19-
play(source, wait=True, pin=(pin_speaker, pin0), return_pin=None)
2019
2120
Play the source to completion.
2221

@@ -27,12 +26,9 @@ Functions
2726
of ``AudioFrame`` elements as described below.
2827
:param wait: If ``wait`` is ``True``, this function will block until the
2928
source is exhausted.
30-
:param pin: As with the music module, you can use the optional ``pin``
31-
argument to specify the output pin can be used to override the
32-
default of ``microbit.pin0``. If you have the latest micro:bit **V2**,
33-
you can use ``microbit.pin_speaker``. The pin argument can also take a
34-
tuple of two pins, for example ``pin=(pin_speaker, pin0)`` which would
35-
output sound on the built-in speaker and pin 0.
29+
:param pin: An optional argument to specify the output pin can be used to
30+
override the default of ``pin0``. If we do not want any sound to play
31+
we can use ``pin=None``.
3632
:param return_pin: specifies a differential edge connector pin to connect
3733
to an external speaker instead of ground. This is ignored for the **V2**
3834
revision.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Projects related to MicroPython on the BBC micro:bit include:
9292
pin.rst
9393
radio.rst
9494
random.rst
95+
speaker.rst
9596
speech.rst
9697
spi.rst
9798
uart.rst

docs/microbit.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,6 @@ Modules
105105
display.rst
106106
i2c.rst
107107
microphone.rst
108+
speaker.rst
108109
spi.rst
109110
uart.rst

docs/microbit_micropython_api.rst

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,7 @@ pin_logo::
158158
pin_logo.is_touched()
159159

160160
pin_speaker, as the above ``MicroBitPin`` class, but does not include
161-
``pin.is_touched()`` and includes::
162-
163-
# disable the built-in speaker
164-
pin_speaker.disable()
165-
# enable the built-in speaker
166-
pin_speaker.enable()
161+
``pin.is_touched()``.
167162

168163
Images
169164
------
@@ -348,6 +343,20 @@ accessed via the ``microbit`` module and played with the :doc:`audio <audio>` mo
348343
``Sound.TWINKLE``
349344
``Sound.YAWN``
350345

346+
Speaker **V2**
347+
--------------
348+
349+
The speaker is enabled by default and can be accessed using the ``speaker`` object. It
350+
can be turned off or on::
351+
352+
# disable the built-in speaker
353+
speaker.off()
354+
# enable the built-in speaker
355+
speaker.on()
356+
# returns True or False to indicate if the speaker is on or off
357+
speaker.is_on()
358+
359+
351360
UART
352361
----
353362

docs/music.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Music
55
66
This is the ``music`` module and you can use it to create and play melodies.
77
By default sound output will be via the edge connector on pin 0 and the
8-
built-in speaker **V2**. You can connect a wired headphones or a speaker to
9-
pin 0 and GND on the edge connector to hear the sound:
8+
:doc:`built-in speaker <speaker>` **V2**. You can connect wired headphones or
9+
a speaker to pin 0 and GND on the edge connector to hear the sound:
1010

1111
.. image:: music-pins.png
1212

@@ -80,7 +80,6 @@ Functions
8080
Gets the current tempo as a tuple of integers: ``(ticks, bpm)``.
8181

8282
.. py:function:: play(music, pin=pin0, wait=True, loop=False)
83-
play(music, pin=(pin_speaker, pin0) wait=True,loop=False)
8483
8584
Plays ``music`` containing the musical DSL defined above.
8685

@@ -95,36 +94,37 @@ Functions
9594
their defaults before the music (whatever it may be) is played.
9695

9796
An optional argument to specify the output pin can be used to override the
98-
default of ``microbit.pin0``. With micro:bit **V2** you can use
99-
``microbit.pin_speaker`` or a tuple of two pins, for example
100-
``pin=(pin_speaker, pin0)`` which would output sound on the built-in
101-
speaker and pin 0.
97+
default of ``microbit.pin0``. If we do not want any sound to play we can
98+
use ``pin=None``.
10299

103100
If ``wait`` is set to ``True``, this function is blocking.
104101

105102
If ``loop`` is set to ``True``, the tune repeats until ``stop`` is called
106103
(see below) or the blocking call is interrupted.
107104

108105
.. py:function:: pitch(frequency, duration=-1, pin=pin0, wait=True)
109-
pitch(frequency, duration=-1, pin=(pin_speaker, pin0), wait=True)
110106
111107
Plays a pitch at the integer frequency given for the specified number of
112108
milliseconds. For example, if the frequency is set to 440 and the length to
113109
1000 then we hear a standard concert A for one second.
114110

115111
Note that you can only play one pitch on one pin at any one time.
116112

113+
An optional argument to specify the output pin can be used to override the
114+
default of ``microbit.pin0``. If we do not want any sound to play out of
115+
the pins we can use ``pin=None``.
116+
117117
If ``wait`` is set to ``True``, this function is blocking.
118118

119119
If ``duration`` is negative the pitch is played continuously until either
120120
the blocking call is interrupted or, in the case of a background call, a
121121
new frequency is set or ``stop`` is called (see below).
122122

123123
.. py:function:: stop(pin=pin0)
124-
stop(pin=(pin_speaker, pin0))
125124
126-
Stops all music playback on a given pin, eg. ``music.stop(pin_speaker)``
127-
**V2**. If no pin is given, eg. ``music.stop()`` pin0 is assumed.
125+
Stops all music playback on the built-in speaker and any pin outputting
126+
sound. An optional argument can be provided to specify a pin, eg.
127+
``music.stop(pin1)``.
128128

129129
.. py:function:: reset()
130130

docs/pin.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ in MicroPython, but that are not available via the edge connector:
8282
* ``pin_logo`` - A touch sensitive logo pin on the front of the micro:bit,
8383
which by default is set to capacitive touch mode.
8484

85-
* ``pin_speaker`` - A pin to control the sound output of the micro:bit speaker.
85+
* ``pin_speaker`` - A pin to address the micro:bit speaker.
8686

8787

8888
Pulse-Width Modulation

docs/speaker.png

37.1 KB
Loading

docs/speaker.rst

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
Speaker **V2**
2+
**************
3+
4+
.. py:module:: microbit.speaker
5+
6+
The micro:bit **V2** has a built-in speaker located on the rear of the board.
7+
8+
.. image:: speaker.png
9+
:width: 300px
10+
:align: center
11+
:height: 240px
12+
:alt: speaker on rear of micro:bit V2
13+
14+
By default sound output will be via the edge connector on pin 0 and the
15+
built-in speaker **V2**. You can connect wired headphones or a speaker to
16+
pin 0 and GND on the edge connector to hear the sounds.
17+
18+
The speaker can be turned off or on using the functions listed here.
19+
20+
Functions
21+
=========
22+
23+
.. py:function:: off()
24+
25+
Use off() to turn off the speaker. This does not disable sound output, so
26+
you will still hear sound on Pin 0 if you have headphones or a speaker
27+
connected to that pin.
28+
29+
.. py:function:: on()
30+
31+
Use ``on()`` to turn on the speaker.
32+
33+
.. py:function:: is_on()
34+
35+
Returns ``True`` if the speaker is on, otherwise returns ``False``.
36+
37+
Example
38+
=======
39+
40+
An example that runs through some of the functions of the speaker API. ::
41+
42+
from microbit import *
43+
import audio
44+
45+
# Check that the speaker is on
46+
print(speaker.is_on())
47+
# Play a sound
48+
audio.play(Sound.HELLO)
49+
sleep(2000)
50+
# Disable the speaker
51+
speaker.off()
52+
# Check that the speaker is off
53+
print(speaker.is_on())
54+
# Play a sound. This will not be heard on the speaker, but will be heard
55+
# on Pin 0 if you have headphones or a speaker connected.
56+
audio.play(Sound.HELLO)

docs/speech.rst

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Speech
55
66
This module makes the micro:bit talk, sing and make other speech like sounds.
77
By default sound output will be via the edge connector on pin 0 and the
8-
built-in speaker **V2**. You can connect a wired headphones or a speaker to
9-
pin 0 and GND on the edge connector to hear the sound:
8+
:doc:`built-in speaker <speaker>` **V2**. You can connect wired headphones or
9+
a speaker to pin 0 and GND on the edge connector to hear the sound:
1010

1111
.. image:: speech.png
1212

@@ -46,32 +46,27 @@ Functions
4646
emphasis.
4747

4848
.. py:function:: pronounce(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128)
49-
pronounce(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=(pin_speaker, pin0))
49+
pronounce(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=pin0)
5050
5151
Pronounce the phonemes in the string ``phonemes``. See below for details of
5252
how to use phonemes to finely control the output of the speech synthesiser.
5353
Override the optional pitch, speed, mouth and throat settings to change the
5454
timbre (quality) of the voice.
5555

56-
As with the music module, you can use an optional argument to specify the
57-
output pin can be used to override the default of ``microbit.pin0``.
58-
If you have the latest micro:bit **V2**, you can use
59-
``microbit.pin_speaker``.
60-
61-
The pin argument can also take a tuple of two pins, for example
62-
``pin=(pin_speaker, pin0)`` which would output sound on the built-in
63-
speaker and pin 0.
56+
For micro:bit **V2** an optional argument to specify the output pin can be
57+
used to override the default of ``pin0``. If we do not want any sound to
58+
play out of the pins can use ``pin=None``.
6459

6560
.. py:function:: say(words, \*, pitch=64, speed=72, mouth=128, throat=128)
66-
say(words, \*, pitch=64, speed=72, mouth=128, throat=128, pin=(pin_speaker, pin0))
61+
say(words, \*, pitch=64, speed=72, mouth=128, throat=128, pin=pin0)
6762
6863
Say the English words in the string ``words``. The result is semi-accurate
6964
for English. Override the optional pitch, speed, mouth and throat
7065
settings to change the timbre (quality) of the voice. This is a short-hand
7166
equivalent of: ``speech.pronounce(speech.translate(words))``
7267

7368
.. py:function:: sing(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128)
74-
sing(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=(pin_speaker, pin0))
69+
sing(phonemes, \*, pitch=64, speed=72, mouth=128, throat=128, pin=pin0)
7570
7671
Sing the phonemes contained in the string ``phonemes``. Changing the pitch
7772
and duration of the note is described below. Override the optional pitch,

0 commit comments

Comments
 (0)