These exercises all involve sound, specifically the speech library that is available to MicroPython programmers. They have been separated from the other sound exercises since they require a speaker rather than a little piezo buzzer.
You can complete these tasks with,
- Alligator clip headphone socket accessory and a pair of headphones or external speaker.
- Headphone hack - alligator clips directly to the jack on your headphones.
- Accessory which has a speaker (eg Noise:bit)
Copy and test the following program. It makes use of the speech library.
from microbit import * import speech speech.say("Hello")
Write a new program that counts out loud from 0 to 9. Use a FOR loop, knowing that the variable used for the loop will keep count of the repetitions. The variable will be storing numbers. Look back at Task 10 from the Just A micro:bit tasks to remind yourself of how to perform the conversion.
Write a program to perform a countdown from 9 to 0, with the numbers being said out loud as well as displayed on the LED matrix. When the countdown is finished, get the micro:bit to say - "We have lift off." Use a for loop to do this efficiently.
The following program choose a random word to say out loud.
from microbit import * import speech import random results = ["loser", "winner", "outstanding"] while True: if button_a.was_pressed(): speech.say(random.choice(results))
Change the words in the list of results and add your own list of compliments.
Write a program that defines two lists of words, one called insults, one called compliments. When the user presses the A button, they should receive a random compliment, spoken out loud. When they press the B button, they should be insulted.
Write a program that makes use of the speech and random libraries to make a 6-sided speaking die. When the user presses the A button, your program should generate a random number from 1 to 6 and then speak the result. Remember that you can only say strings and numbers need to be converted to strings before they can be said out loud.
random.randint(a,b) generates a random integer from 1 to 6. Store this information in a variable before using.
You could vary the output depending on the value of the roll. For example, you could have some joy or disappointment depending on whether a 6 or a 1 were rolled.
The speech library makes a reasonable stab at pronouncing common words. It is not so good when it comes to names. Write a simple program to check how well the library pronounces your names (all of them).
Visit the following page in the MicroPython documentation,
Find the section on the TIMBRE of the voice being used and look at how to change the speed and pitch of the voice.
Write a program that speaks a sentence like a Year 8/9 student (ie with some voice-breaking moments in the middle of the sentence).
Read the section of the MicroPython speech documentation that explains how to use PHONEMES to pronounce words. Use phonemes to get the most accurate pronunciation of your full name.
Look back at Task 3. This code could be re-purposed for a different program. Instead of receiving a compliment, the user could receive a random piece of advice or a random answer to their question. Start with Yes,No, & Maybe as your words. Test the program out by asking questions and seeing how it answers. Then work out an extended list of suitable responses, like Not sure.