# Música predefinida o crea tu música

##### <span style="color: rgb(22, 145, 121);">**SALIDAS DE AUDIO**</span>

La placa Microbit v2 tiene un altavoz incorporado que se puede anular o activar con la instrucción **speaker.on()** o **speaker.off()**

La salida de audio también sale por el **pin0,** de tal manera que si conectamos un altavoz o headphone, y tenemos speaker.on() se oirá por los sitios, si lo tienes en off sólo por el buzzer/headphones exterior:

[![435692553_1.png](https://libros.catedu.es/uploads/images/gallery/2024-09/scaled-1680-/435692553-1.png)](https://libros.catedu.es/uploads/images/gallery/2024-09/435692553-1.png)  
*[![headphones_1.png](https://libros.catedu.es/uploads/images/gallery/2024-09/scaled-1680-/headphones-1.png)](https://libros.catedu.es/uploads/images/gallery/2024-09/headphones-1.png)  
Fuente [https://www.teachwithict.com/microbit-music-python.html](https://www.teachwithict.com/microbit-music-python.html)*  
*BBC micro:bit "Magic 8-Ball" lesson is licenced under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/).*

<p class="callout info"><span style="color: rgb(0, 0, 0);">Ojo, hay que conectar un **buzzer pasivo,** es decir, que reproduce la señan analógica en sonido (o sea un altavoz normal y corriente) </span></p>

<p class="callout info"><span style="color: rgb(0, 0, 0);">si quieres conectar un **buzzer activo**, reproduce un tono (prederminado) al suministrarle un 1, el siguiente código sonaría una alarma:  
</span></p>

```
from microbit import *
while True:
    pin0.write_digital(1)
    sleep(500)
    pin0.write_digital(0)
    sleep(500)
```

<p class="callout info"><span style="color: rgb(0, 0, 0);">En los siguientes ejemplo usaremos siempre **buzzer pasivo.** Si no te queda claro lo que es un buzzer activo y un pasivo, mira [esta págin](https://libros.catedu.es/books/programa-arduino-mediante-codigo/page/actuadores-y-otras-salidas-GLB)a</span></p>

##### <span style="color: rgb(22, 145, 121);">**CREA TUS EFECTOS**</span>

Puedes crear los efectos utilizando rangos de frecuencias, aquí en el ejemplo la función pitch reproduce durante 6mseg las frecuencias de medio 880 , aguda 1760 y grave 16 y luego lo mismo pero en orden decreciente, y así sucesivamente para dar el efecto de sirena.

```
import music
from microbit import *
display.show(Image.GHOST)
while True:
    for freq in range(880, 1760, 16):
        music.pitch(freq, 6)
    for freq in range(1760, 880, -16):
        music.pitch(freq, 6)
```

Extraído de [https://microbit-micropython.readthedocs.io/en/v2-docs/tutorials/music.html#sound-effects](https://microbit-micropython.readthedocs.io/en/v2-docs/tutorials/music.html#sound-effects)

<span style="color: rgb(22, 145, 121);">**<iframe allowfullscreen="allowfullscreen" height="314" src="https://www.youtube.com/embed/tSkmgffox2A" width="560"></iframe>** </span>

##### <span style="color: rgb(22, 145, 121);">**MUSICA PREDEFINIDA**</span>

En Reference- Sound tienes muchos tonos predefinidos para experimentar:

[![2024-09-24 13_13_12-micro_bit Python Editor.png](https://libros.catedu.es/uploads/images/gallery/2024-09/scaled-1680-/2024-09-24-13-13-12-micro-bit-python-editor.png)](https://libros.catedu.es/uploads/images/gallery/2024-09/2024-09-24-13-13-12-micro-bit-python-editor.png)

Si tienes la microbit v2 tienes otros en "E**xpressive sounds**" como audio.play(Sound.TWINKLE)

También puede hablar, arrastra el código que tienes en Refernce-Sound- Speech, pero no lo intentes en español, no se entiende nada

```
import speech
from microbit import *
display.show(Image.FABULOUS)
speaker.on()
set_volume(255)
speech.say('Hello, How are you? Do you sign up in online course in CATEDU.ES ?')
audio.play(Sound.TWINKLE)
```

<iframe allowfullscreen="allowfullscreen" height="314" src="https://www.youtube.com/embed/EcV3aeYo6Vs" width="560"></iframe>

##### <span style="color: rgb(22, 145, 121);">**CONSTRUYENDO TU MÚSICA**</span>

Tienes que añadir la librería **music** y componer las notas según la notación americana :

[![2024-09-24 12_45_51-Making music with the micro_bit - MicroPython version - teachComputing.png](https://libros.catedu.es/uploads/images/gallery/2024-09/scaled-1680-/2024-09-24-12-45-51-making-music-with-the-micro-bit-micropython-version-teachcomputing.png)](https://libros.catedu.es/uploads/images/gallery/2024-09/2024-09-24-12-45-51-making-music-with-the-micro-bit-micropython-version-teachcomputing.png)  
*Fuente [https://www.teachwithict.com/microbit-music-python.html](https://www.teachwithict.com/microbit-music-python.html)*  
*BBC micro:bit "Magic 8-Ball" lesson is licenced under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/).*

La duración (si no se pone, sigue con la duración anterior)

[![2024-09-24 12_46_47-Making music with the micro_bit - MicroPython version - teachComputing.png](https://libros.catedu.es/uploads/images/gallery/2024-09/scaled-1680-/2024-09-24-12-46-47-making-music-with-the-micro-bit-micropython-version-teachcomputing.png)](https://libros.catedu.es/uploads/images/gallery/2024-09/2024-09-24-12-46-47-making-music-with-the-micro-bit-micropython-version-teachcomputing.png)  
*Fuente [https://www.teachwithict.com/microbit-music-python.html](https://www.teachwithict.com/microbit-music-python.html)*  
*BBC micro:bit "Magic 8-Ball" lesson is licenced under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/).*

Se puede poner incluso sostenidos, por ejemplo C#4:4 o f#5:4

Si quieres poner descansos es con la letra r seguido de su duración por ejemplo r:4 r:2

##### <span style="color: rgb(22, 145, 121);">**Un ejemplo**</span>

Tono Nokia (arriba la duración)  
[![image.png](https://libros.catedu.es/uploads/images/gallery/2024-09/scaled-1680-/fdKimage.png)](https://libros.catedu.es/uploads/images/gallery/2024-09/fdKimage.png)

El código sería:

```
from microbit import *
import music

tune = ["e5:2","d5","f#4:4","g#4","c#5:2","b4","d4:4","e4","b4:2","a4","c#4:4","e4","a4:12"]

music.play(tune)
```

Otro ejemplo, el código lo tienes en [https://microbit-micropython.readthedocs.io/en/v2-docs/music.html](https://microbit-micropython.readthedocs.io/en/v2-docs/music.html)

<iframe allowfullscreen="allowfullscreen" height="314" src="https://www.youtube.com/embed/0u3hjXXEZYg" width="560"></iframe>