© Pradeeka Seneviratne  2019
Pradeeka SeneviratneBBC micro:bit Recipeshttps://doi.org/10.1007/978-1-4842-4913-0_11

11. Playing Music

Pradeeka Seneviratne1 
(1)
Udumulla, Mulleriyawa, Sri Lanka
 

In this chapter, you will learn how to use the Music package of the MakeCode for micro:bit to build and play simple tunes. MakeCode allows you to build music by combining music tones, octaves, beats (duration), accidentals (flats and sharps), and so forth. You can also use the built-in melodies with your applications.

11-1. Connecting a Speaker to Pin 0

Problem

You want to connect a speaker to the micro:bit pin 0.

Solution

Connect the speaker with the micro:bit using alligator (crocodile) clips as explained below.
  • Connect one wire to pin0 and the other wire to ground pin. A speaker will work either way around.

  • Once completed, your hardware setup should look like that shown in Figure 11-1 .
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig1_HTML.jpg
    Figure 11-1.

    Wiring diagram

Now create the code as described below and flash it into your micro:bit.
  • In the Toolbox, click on the Music category. Then click and drag the ring tone (Hz) block over, and place it inside the on start block (Figure 11-2 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig2_HTML.jpg
    Figure 11-2.

    Placing the ring tone block inside the on start block

How It Works

By default, MakeCode expects the speaker to be connected through micro:bit’s pin 0 through the edge connector using alligator (crocodile) clips. A speaker has two wires: positive (usually red) and negative (usually black). Some speakers use different color codes for positive and negative leads. With some speakers, you must solder wires to the solder tabs before using them.

Pin 0 is the default pin used to generate music.

11-2. Connecting a Speaker to Other Pins

Problem

You want to connect a speaker to a micro:bit pin1.

Solution

Connect the speaker with the micro:bit using alligator (crocodile) clips as explained below.
  • Connect the positive lead of the speaker to the micro:bit pin 1.

  • Connect the negative lead of the speaker to the micro:bit pinging.

  • Once completed, your hardware setup should look like that shown in Figure 11-3 .
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig3_HTML.jpg
    Figure 11-3.

    Wiring diagram

Now create the code as described below and flash it into your micro:bit.
  • In the Toolbox, click on the Pins category. Next, click and drag the analog set pitch pin block over, and place it inside the on start block. Then select P1 from the drop-down menu.

  • In the Toolbox, click on the Music category. Then click and drag the ring tone (Hz) block over, and place it inside the on start block underneath the analog set pitch pin block (Figure 11-4 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig4_HTML.jpg
    Figure 11-4.

    Full code listing

How It Works

The analog set pitch pin block allows you to prepare some pins on the edge connector to output audio signals. Here is the list of pins you can use to connect a speaker.
  • P0

  • P1

  • P2

  • P3

  • P4

  • P10

11-3. Using Earphones

Problem

You want to connect an earphone with the micro:bit to listen to music.

Solution

You can use alligator (crocodile) clips to connect an earphone to the micro:bit without cutting off the jack. Figure 11-5 shows how you can make the wire connections. The steps below further explain the hack.
  • Take two alligator (crocodile) leads (black and red).

  • Connect one end of the black alligator lead to the micro:bit GND and the other end to the base of your earphone jack.

  • Connect one end of the red crocodile lead to the micro:bit pin 0 and the other end to the tip of the earphone jack.

../images/474604_1_En_11_Chapter/474604_1_En_11_Fig5_HTML.jpg
Figure 11-5.

Wiring diagram

How It Works

If you don’t have a speaker, you can still use your micro:bit with earphones. Earphones produce quiet music and are better for testing purposes. If you don’t have alligator leads, just cut off the earphone jack and connect the leads to the edge connector (tip to pin 0 and base to GND) of the micro:bit. However, pre-built audio cables are available to quickly connect earphone or headphones to the micro:bit. You can simply plug the earphone jack to the 3.5 mm socket of the audio cable and connect two crocodile clips to the edge connector of the micro:bit.

11-4. Using Amplifiers

Problem

You want to play tunes loudly with a micro:bit .

Solution

You will need MonkMakes Speaker for micro:bit ( https://www.monkmakes.com/mb_speaker/ ) to build this project. Table 11-1 lists the pin connection between two boards. You can use alligator (crocodile) clips to make connections.
Table 11-1

Wiring Between MonkMakes Speaker and micro:bit

Speaker

Micro:bit

IN

Pin 0

3V

3V

GND

GND

Figure 11-6 shows the wiring between the two boards.
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig6_HTML.jpg
Figure 11-6.

Wiring between the MonkMakes speaker module and the micro:bit (Image credits: MonkMakes at https://www.monkmakes.com/mb_speaker/ )

How It Works

Some vendors offer speakers with a built-in amplifier to make louder music. MonkMakes ( https://www.monkmakes.com/ ) manufactures a speaker breakout module with a built-in amplifier to produce loud music. It also has a built-in LED to indicate power. The MonkMakes speaker module uses three wires for connectivity and draws additional power from the micro:bit’s 3V pin.

11-5. Playing Built-In Melodies

Problem

You want to play a built-in melody .

Solution

  • In the Toolbox, click on the Music category. Next, click and drag the start melody block over, and place it inside the on start block. Then choose birthday from the drop-down menu (Figure 11-7 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig7_HTML.jpg
    Figure 11-7.

    Full code listing

How It Works

A melody also called a tune, voice, or line is a sequence of single notes that is musically satisfying. The start melody block provides you a set of melodies that can be easily integrated with the micro:bit applications. Here is the list:
  • dadadadum

  • entertainer

  • prelude

  • ode

  • nyan

  • ringtone

  • funk

  • blues

  • birthday

  • wedding

  • funeral

  • punchline

  • python

  • baddy

  • chase

  • ba ding

  • wawawawaa

  • jump up

  • jump down

  • power up

  • power down

The behavior of the melody can be changed with repeating options:
  • once - plays the melody in the foreground one time.

  • forever - plays the melody in the foreground and keeps repeating it.

  • once in background - plays the melody in the background one time.

  • forever in background - plays the melody in the background and keeps repeating it.

11-6. Playing a Tone or Note

Problem

You want to play the note Middle C when button A is pressed.

Solution

  • In the Toolbox, click on the Input category. Then click on the on button A pressed event block.

  • In the Toolbox, click on the Music category. Then click and drag the ring tone (Hz) and place it inside the on button A pressed event block (Figure 11-8 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig8_HTML.jpg
    Figure 11-8.

    Full code listing

    When you play a note or tone inside the forever block, you will hear crappy sound.

How It Works

The ring tone (Hz) block allows you to play a tone of specific frequency. The default frequency of the ring tone block is set to 262 Hz (tone), which is Middle C (note). When you click on the parameter box of the ring tone block, a 21-key visual piano keyboard (Figure 11-9 ) will display and allows you to choose a note.
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig9_HTML.jpg
Figure 11-9.

21-key visual piano keyboard

When you choose a note from the visual piano keyboard, the frequency of the note will display in the parameter box of the ring tone (Hz) block. If you know the frequency of the note you want to play, just type the frequency in the parameter box without choosing it from the visual piano keyboard. The precision of the frequency of a note is ± 1 Hz. As an example, for Middle C, the valid frequencies are 261, 262, and 263 Hz (Figure 11-10 ).
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig10_HTML.jpg
Figure 11-10.

Playing Middle C

If you type a frequency that does not belongs to a note, the ring tone (Hz) block will recognize it as a tone (Figure 11-11 ).
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig11_HTML.jpg
Figure 11-11.

Playing 264 Hz tone

You can also play tones not belonging to the music notes in human hearable range (20 Hz–20000 Hz). Figure 11-12 shows the code to play a 15,000 Hz tone for 4 beats. Can you hear?
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig12_HTML.jpg
Figure 11-12.

Playing 15000 Hz tone for 4 beat

All notes are tones but not all tones are notes. In other words, notes are taken from the frequency range (20 Hz to 20 kHz) that humans can hear.

Table 11-2 lists names of all the notes available to choose and their frequencies in Hertz in the 21-key piano keyboard .
Table 11-2.

Notes and Their Frequencies

Note

Frequency (Hz)

Low C

131

Low C#

139

Low D

147

Low D#

156

Low E

165

Low F

175

Low F#

185

Low G

196

Low G#

208

Low A

220

Low A#

233

Low B

247

Middle C

262

Middle C#

277

Middle D

294

Middle D#

311

Middle E

330

Middle F

349

Middle F#

370

Middle G

392

Middle G#

415

Middle A

440

Middle A#

466

Middle B

494

High C

523

High C#

554

High D

587

High D#

622

High E

659

High F

698

High F#

740

High G

784

High G#

831

High A

880

High A#

932

High B

988

In music, sharp (#) means higher in pitch. More specifically, in musical notation, sharp means "higher in pitch by one semitone (half step)." Sharp is the opposite of flat, which is a lowering of pitch. As an example, the Middle C# resides halfway between Middle C (262 Hz) and Middle D (294 Hz).

Musical notes can have flats and sharps known as accidentals. A flat can be written as b (lowercase), and a sharp can be written as # (hash).

Sharps and flats are not the black keys. All black keys are either a sharp or flat, but not all sharps and flats are black keys. Remember, an accidental (a sharp or flat) merely means to play the next higher or lower key on a piano, and that next key may be black or white (Figure 11-13 ).
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig13_HTML.jpg
Figure 11-13.

Sharp and flat keys in an octave (Image Credits: https://www.key-notes.com/blog/piano-key-chart )

Figure 11-14 shows how to play sharps and flats using MakeCode. Here are the sound of the notes that you can hear:
  • C# - C Sharp

  • Ab - A Flat

  • Cb - C Flat

../images/474604_1_En_11_Chapter/474604_1_En_11_Fig14_HTML.jpg
Figure 11-14.

Playing sharps and flats

11-7. Using Octaves

Problem

You want to play the musical note C in octave 3 .

Solution

  • In the Toolbox, click on the Arrays category. Next, click and drag the set text list to block over, and place it inside the on start block. Then replace each text box with c, c4, and c3, respectively.

  • In the Toolbox, click on the Music category. Then click and drag the start melody block over, and place it underneath the set text list to block.

  • In the Toolbox, click on the Variables category. Then click and drag the text list variable over, and place it on the melody list of the start melody block (Figure 11-15 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig15_HTML.jpg
    Figure 11-15.

    Full code listing

How It Works

In music, an octave or perfect octave is the interval between one musical pitch and another with half or double its frequency. Generally, a piano keyboard consists of keys spanning octaves. Figure 11-16 shows an octave of a piano keyboard.
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig16_HTML.jpg
Figure 11-16.

An octave on the piano keyboard (Image: Freepik.​com)

An octave has seven musical notes (C, D, E, F, G, A, B). A musical note can present with its octave (octave number) to indicate the position of the key on the keyboard. You can write a musical note with its octave as follows.

NOTE[octave]

As an example, the musical note C in octave 3 can be written as C3.

By default, micro:bit plays musical notes in octave 4 unless you explicitly mention it. As an example, the musical note C is equivalent to C4.

11-8. Playing a Note or Tone for Given Duration

Problem

You want to play a note for 4 beats.

Solution

  • In the Toolbox, click on the Music category. Next, click and drag the play tone for block over, and place it inside the on start block. Then select 4 from the drop-down menu (Figure 11-17 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig17_HTML.jpg
    Figure 11-17.

    Full code listing

How It Works

In music, a beat is the basic unit of time. You can play a musical note or tone for a number of beats. The play tone for block offers the following beats.
  • 1

  • 1/2

  • 1/4

  • 1/8

  • 1/16

  • 2

  • 4

By default, the duration of a beat is 500 milliseconds.

When you run the above code, the Middle C will play for 2 seconds.

The duration of a beat specifies the arbitrary length of time defined by tempo.

Alternatively, you can mention the duration as one of the following.
  • Write the musical note followed by a colon followed by the number of beats. Figure 11-18 shows an example to play three musical notes sequentially. Here are the musical notes you can hear.
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig18_HTML.jpg
    Figure 11-18.

    Playing three musical notes sequentially

    • C4#:3 - Plays the note C Sharp in octave 4 for 3 beats. If the duration of a beat is 500 milliseconds, the C4#:3 will play for 1.5 seconds.

    • Ab:4 - Plays the note A Flat IN octave 4 for 4 beats. If the duration of a beat is 500 milliseconds, the Ab:4 will play for 2 seconds.

    • D3b:2 - Plays the note D Flat in octave 3 for 2 beats. If the duration of a beat is 500 milliseconds, the D3b:2 will play for 1 second.

  • Use time as milliseconds instead of beat. Figure 11-19 shows an example to set 400 milliseconds duration for the musical note Middle C. Originally, the play tone block presents the duration in beats with a drop-down box. To type the value 400, first you should replace it with a value box. You can get an empty value box from the Math category.
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig19_HTML.jpg
    Figure 11-19.

    Setting a duration for a musical note

11-9. Setting the Tempo

Problem

You want to set the tempo to 400 for your music.

Solution

  • In the Toolbox, click on the Music category. Next, click and drag the set tempo to (bpm) block over, and place it inside the on start block. Then type 400 in the value box. Alternatively, you can use the slider to change the value.

  • In the Toolbox, click on the Music category again. Then click and drag the play tone for block over, and place it inside the on start block underneath the set tempo to (bpm) block (Figure 11-20 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig20_HTML.jpg
    Figure 11-20.

    Full code listing

How It Works

Tempo defines the speed of a piece of music. In your code, the default amount of the tempo is 120. Tempo can be expressed in bpm (beats per minute). You can set the tempo for the music using the set tempo to (bpm) block. You can type any positive value for the tempo, but MakeCode recommends 4 to 400.

When you change the tempo, the duration of a beat gets changed accordingly. By default, the duration of a beat is 500 milliseconds for the tempo, 120. You can calculate the duration of a beat in milliseconds for a given tempo as follows.

Duration of a beat in milliseconds = 60000 / Tempo (bps)
  • Example: Calculate the duration of a beat for tempo 120.

  • = 60,000 milliseconds / 120

  • = 500 milliseconds

In the above example, first the tempo is set to 400 using the set tempo to (bpm) block. Then it plays the Middle C for 1 beat using the play tone for block . The duration of the beat is,
  • = 60,000 milliseconds / 400

  • = 150 milliseconds

If you want to change the tempo to a different value during the music, use change tempo by (bpm) block. Figure 11-21 shows an example code to change the tempo from 400 to 300.
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig21_HTML.jpg
Figure 11-21.

Changing the tempo from 400 to 300

The larger the tempo value, the faster the notes (tunes) will play.

11-10. Getting the Tempo

Problem

You want to get the current tempo in beats per minute.

Solution

  • In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.

  • In the Toolbox, click on the Music category. Then click and drag the tempo (bpm) block over, and place it inside the placeholder of the show number block (Figure 11-22 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig22_HTML.jpg
    Figure 11-22.

    Full code listing

How It Works

The tempo (bpm) block returns the tempo in beats per minute.

11-11. Getting the Duration of a Beat

Problem

You want to get the duration of a beat in milliseconds.

Solution

  • In the Toolbox, click on the Basic category. Then click and drag the show number block over, and place it inside the on start block.

  • In the Toolbox, click on the Music category. Then click and drag the beat block over, and place it inside the placeholder of the show number block (Figure 11-23 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig23_HTML.jpg
    Figure 11-23.

    Full code listing

How It Works

By default, the beat block returns the duration of a beat in milliseconds. It also returns the duration of 1/2, 1/4, 1/8, 1/16, 2, and 4 beats.

11-12. Using Music Events

Problem

You want to display a happy icon once the happy birthday melody has ended.

Solution

  • In the Toolbox, click on the Music category. Next, click and drag the start melody block over, and place it inside the on start block. Then choose, birthday from the drop-down menu.

  • In the Toolbox, click on the Music category. Next, click and drag the music on event block . Then choose melody ended from the drop-down menu.

  • In the Toolbox, click on the Basic category. Next, click and drag the show icon block over, and place it inside the music on block. Then choose happy from the drop-down menu (Figure 11-24 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig24_HTML.jpg
    Figure 11-24.

    Full code listing

How It Works

The music on block raises actions for the following musical events.
  • melody note played

  • melody started

  • melody ended

  • melody repeated

  • background melody note played

  • background melody started

  • background melody ended

  • background melody repeated

  • background melody paused

  • background melody resumed

11-13. Adding Silence Between Notes and Tones

Problem

You want to add 2 seconds of silence between two notes.

Solution

  • In the Toolbox, click on the Music category. Then click and drag the play tone for block over, and place it inside the on start block.

  • In the Toolbox, click on the Music category. Next, click and drag the reset (ms) block over, and place it inside the on start block underneath the play tone for block. Then choose 4 for the beat from the drop-down menu.

  • Right-click on the play tone for block, and from the shortcut menu, click Duplicate. Next, click and drag the duplicated play tone for block and place it underneath the reset (ms) block . Then select the tone (note) Middle D from the visual piano keyboard (Figure 11-25 ).
    ../images/474604_1_En_11_Chapter/474604_1_En_11_Fig25_HTML.jpg
    Figure 11-25.

    Full code listing

How It Works

The reset(ms) block allows you to add silence between notes, tones, or melodies. The duration of a silence can be in beats or milliseconds. Figure 11-26 shows how to use 2000 milliseconds to add a duration for silence.
../images/474604_1_En_11_Chapter/474604_1_En_11_Fig26_HTML.jpg
Figure 11-26.

Using 2000 milliseconds to make silence

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.142.232