Microbit Python 課程介紹 --Speech
2017-07-22 20:17Warning
WARNING! THIS IS ALPHA CODE.
We reserve the right to change this API as development continues.
The quality of the speech is not great, merely “good enough”. Given the constraints of the device you may encounter memory errors and / or unexpected extra sounds during playback. It’s early days and we’re improving the code for the speech synthesiser all the time. Bug reports and pull requests are most welcome.
警告! 這是ALPHA代碼。
隨著開發的進行,我們保留更改此API的權利。
言論的質量不是很好,只是“夠好”。 鑑於設備的限制,您可能會在播放過程中遇到內存錯誤和/或意外的額外聲音。 這是早期的日子,我們正在改進語音合成器的代碼。 錯誤報告和拉請求是最受歡迎的。
Computers and robots that talk feel more “human”.
So often we learn about what a computer is up to through a graphical user interface (GUI). In the case of a BBC micro:bit the GUI is a 5x5 LED matrix, which leaves a lot to be desired.
談話的計算機和機器人感覺更“人性化”。
所以經常我們通過圖形用戶界面(GUI)了解計算機是什麼。 在BBC micro:bit的情況下,GUI是一個5x5 LED矩陣,留下了很多希望。
Getting the micro:bit talk to you is one way to express information in a fun, efficient and useful way. To this end, we have integrated a simple speech synthesiser based upon a reverse-engineered version of a synthesiser from the early 1980s. It sounds very cute, in an “all humans must die” sort of a way.
With this in mind, we’re going to use the speech synthesiser to create...
獲得微:與你談話是以有趣,高效和有用的方式表達信息的一種方式。 為此,我們已經集成了一個簡單的語音合成器,基於20世紀80年代初的合成器的反向工程版本。 聽起來很可愛,在一個“所有人類必須死”的方式。
考慮到這一點,我們將使用語音合成器來創建...
DALEK Poetry
It’s a little known fact that DALEKs enjoy poetry ~ especially limericks. They go wild for anapestic meter with a strict AABBA form. Who’d have thought?
(Actually, as we’ll learn below, it’s The Doctor’s fault DALEKs like limericks, much to the annoyance of Davros.)
In any case, we’re going to create a DALEK poetry recital on demand.
這是一個有點知道的事實,DALEKs享受詩歌〜特別是limericks。 他們用嚴格的AABBA形式野外用於無人電錶。 誰會想?
(實際上,正如我們將在下面學到的那樣,醫生的錯誤DALEK就像是混蛋,對Davros的煩惱很大)
無論如何,我們將按需製作DALEK詩歌獨奏。
Say Something
Before the device can talk you need to plug in a speaker like this:
在設備可以通話之前,您需要插入一個這樣的揚聲器:
The simplest way to get the device to speak is to import the speech module and use the sayfunction like this:
讓設備發言的最簡單的方式是導入語音模塊並使用這樣的說話功能:
import speech
speech.say("Hello, World")
While this is cute it’s certainly not DALEK enough for our taste, so we need to change some of the parameters that the speech synthesiser uses to produce the voice. Our speech synthesiser is quite powerful in this respect because we can change four parameters:
雖然這是可愛的,但肯定不是DALEK足夠我們的口味,所以我們需要改變一些語音合成器用於產生聲音的參數。 我們的語音合成器在這方面是相當強大的,因為我們可以改變四個參數:
· pitch - how high or low the voice sounds (0 = high, 255 = Barry White)
· speed - how quickly the device talks (0 = impossible, 255 = bedtime story)
· mouth - how tight-lipped or overtly enunciating the voice sounds (0 = ventriloquist’s dummy, 255 = Foghorn Leghorn)
· throat - how relaxed or tense is the tone of voice (0 = falling apart, 255 = totally chilled)
•音高 - 聲音有多高或低(0 =高,255 =巴里·懷特)
•速度 - 設備談話的速度(0 =不可能,255 =睡前故事)
•嘴巴 - 如何緊張或明顯地發出聲音(0 = ventriloquist的假人,255 = Foghorn Leghorn)
•喉嚨 - 聲音的音調如何輕鬆或緊張(0 =分崩離析,255 =完全冷卻)
Collectively, these parameters control the quality of sound - a.k.a. the timbre. To be honest, the best way to get the tone of voice you want is to experiment, use your judgement and adjust.
To adjust the settings you pass them in as arguments to the say function. More details can be found in the speech module’s API documentation.
After some experimentation we’ve worked out this sounds quite DALEK-esque:
總而言之,這些參數控制了音質的質量 - a.k.a.音色。 說實話,獲得你想要的聲音的最佳方式是試驗,用你的判斷力和調整。
要調整設置,將其作為參數傳遞給說話功能。 更多詳細信息,請參見語音模塊的API文檔。
經過一些實驗,我們已經弄清楚了,這聽起來很像DALEK-esque:
speech.say("I am a DALEK - EXTERMINATE", speed=120, pitch=100, throat=100, mouth=200)
Poetry on Demand
Being Cyborgs DALEKs use their robot capabilities to compose poetry and it turns out that the algorithm they use is written in Python like this:
由於Cyborgs DALEK使用他們的機器人功能來構成詩歌,並且證明他們使用的算法是用Python編寫的:
# DALEK poetry generator, by The Doctor
import speech
import random
from microbit import sleep
# Randomly select fragments to interpolate into the template.
location = random.choice(["brent", "trent", "kent", "tashkent"])
action = random.choice(["wrapped up", "covered", "sang to", "played games with"])
obj = random.choice(["head", "hand", "dog", "foot"])
prop = random.choice(["in a tent", "with cement", "with some scent",
"that was bent"])
result = random.choice(["it ran off", "it glowed", "it blew up",
"it turned blue"])
attitude = random.choice(["in the park", "like a shark", "for a lark",
"with a bark"])
conclusion = random.choice(["where it went", "its intent", "why it went",
"what it meant"])
# A template of the poem. The {} are replaced by the named fragments.
poem = [
"there was a young man from {}".format(location),
"who {} his {} {}".format(action, obj, prop),
"one night after dark",
"{} {}".format(result, attitude),
"and he never worked out {}".format(conclusion),
"EXTERMINATE",
]
# Loop over each line in the poem and use the speech module to recite it.
for line in poem:
speech.say(line, speed=120, pitch=100, throat=100, mouth=200)
sleep(500)
As the comments demonstrate, it’s a very simple in design:
· Named fragments (location, prop, attitude etc) are randomly generated from pre-defined lists of possible values. Note the use of random.choice to select a single item from a list.
· A template of a poem is defined as a list of stanzas with “holes” in them (denoted by {}) into which the named fragments will be put using the format method.
· Finally, Python loops over each item in the list of filled-in poetry stanzas and uses speech.saywith the settings for the DALEK voice to recite the poem. A pause of 500 milliseconds is inserted between each line because even DALEKs need to take a breath.
正如評論所示,設計非常簡單:
•從預定義的可能值列表中隨機生成命名碎片(位置,支柱,姿態等)。 注意使用random.choice從列表中選擇一個項目。
•一首詩的模板被定義為使用格式方法將命名片段放入其中的“孔”列表(由{}表示)。
•最後,Python循環填充詩歌節的列表中的每個項目,並使用speech.say和DALEK語音的設置來背誦這首詩。 每行之間插入500毫秒的暫停,因為即使DALEK需要呼吸。
Interestingly the original poetry related routines were written by Davros in FORTRAN (an appropriate language for DALEKS since you type it ALL IN CAPITAL LETTERS). However, The Doctor went back in time to precisely the point between Davros’s unit tests passing and the deployment pipeline kicking in. At this instant he was able to insert a MicroPython interpreter into the DALEK operating system and the code you see above into the DALEK memory banks as a sort of long hidden Time-Lord Easter Egg or Rickroll.
有趣的是,原始的詩歌相關例程由達夫斯在FORTRAN(DALEKS的一種適合的語言,因為你輸入所有的資本信件)寫的。 然而,醫生及時回到了達沃斯的單元測試通過和部署管道踢進之間的時間點。在這一瞬間,他能夠將一個MicroPython解釋器插入DALEK操作系統,並將上面的代碼插入到DALEK內存中 銀行是一種長期隱藏的時間主復活節彩蛋或Rickroll。
Phonemes
You’ll notice that sometimes, the say function doesn’t accurately translate from English words into the correct sound. To have fine grained control of the output, use phonemes: the building-block sounds of language.
The advantage of using phonemes is that you don’t have to know how to spell! Rather, you only have to know how to say the word in order to spell it phonetically.
你會注意到,有時候,說話功能不能準確地將英文單詞翻譯成正確的聲音。 要對輸出進行細粒度的控制,請使用音素:語言的構建塊聲音。
使用音素的優點是您不必知道如何拼寫! 相反,你只需要知道如何說出這個詞來拼寫它。
A full list of the phonemes the speech synthesiser understands can be found in the API documentation for speech. Alternatively, save yourself a lot of time by passing in English words to the translate function. It’ll return a first approximation of the phonemes it would use to generate the audio. This result can be hand-edited to improve the accuracy, inflection and emphasis (so it sounds more natural).
The pronounce function is used for phoneme output like this:
語音合成器理解的音素的完整列表可以在API文檔中找到。 或者,通過將英文單詞傳遞給翻譯功能,可以節省大量時間。 它將返回它將用於生成音頻的音素的第一個近似值。 這個結果可以手動編輯,以提高準確性,變化和強調(所以聽起來更自然)。
發音功能用於音素輸出,如下所示:
speech.pronounce("/HEH5EH4EH3EH2EH2EH3EH4EH5EHLP.”)
How could you improve on The Doctor’s code to make it use phonemes?
你如何改進醫生的代碼,使其使用音素?
Sing A Song of Micro:bit
By changing the pitch setting and calling the sing function it’s possible to make the device sing (although it’s not going to win Eurovision any time soon).
The mapping from pitch numbers to musical notes is shown below:
通過改變音調設置和調用唱歌功能,可以使設備發聲(儘管不久之後不會贏得Eurovision)。
從音調編號到音符的映射如下所示:
The sing function must take phonemes and pitch as input like this:
唱歌功能必須像這樣輸入音素和音調:
speech.sing("#115DOWWWW")
Notice how the pitch to be sung is prepended to the phoneme with a hash (#). The pitch will remain the same for subsequent phonemes until a new pitch is annotated.
The following example demonstrates how all three generative functions (say, pronounce and sing) can be used to produce speech like output:
請注意如何用音符(#)將音調放在音素之前。 對於後續音素,音高將保持不變,直到新音高被註釋為止。
以下示例演示瞭如何使用所有三個生成函數(例如,發音和唱歌)來產生語音,如輸出:
import speech
from microbit import sleep
# The say method attempts to convert English into phonemes.
speech.say("I can sing!")
sleep(1000)
speech.say("Listen to me!")
sleep(1000)
# Clearing the throat requires the use of phonemes. Changing
# the pitch and speed also helps create the right effect.
speech.pronounce("AEAE/HAEMM", pitch=200, speed=100) # Ahem
sleep(1000)
# Singing requires a phoneme with an annotated pitch for each syllable.
solfa = [
"#115DOWWWWWW", # Doh
"#103REYYYYYY", # Re
"#94MIYYYYYY", # Mi
"#88FAOAOAOAOR", # Fa
"#78SOHWWWWW", # Soh
"#70LAOAOAOAOR", # La
"#62TIYYYYYY", # Ti
"#58DOWWWWWW", # Doh
]
# Sing the scale ascending in pitch.
song = ''.join(solfa)
speech.sing(song, speed=100)
# Reverse the list of syllables.
solfa.reverse()
song = ''.join(solfa)
# Sing the scale descending in pitch.
speech.sing(song, speed=100)
Microbit 中文 課程 : Python , Javascript, 物聯網
標籤:
—————