jstrout Thanks! Now i simplified dictionaries and made code cleaner
// Simple speech synthesizer v0.4
// by Al Rado
//
// Based on "The CMU Pronouncing Dictionary" http://www.speech.cs.cmu.edu/
// and sounds files from https://github.com/khaleeqahmad/phoneme-synthesiser
// Arphabet symbols
arphabet = ["AA", "AE", "AH", "AO", "AW", "AY", "B", "CH",
"D", "DH", "EH", "ER", "EY", "F", "G", "HH", "IH", "IY",
"JH", "K", "L", "M", "N", "NG", "OW", "OY", "P", "R", "S",
"SH", "T", "TH", "UH", "UW", "V", "W", "Y", "Z", "ZH"]
// symbols for clearing sentences from punctuation marks and others
excludedSymbols = [".", ",", "!", "?", ":", ";", "(", ")", "[", "]", """"]
print "Simple speech synthesizer v0.4"
// load monophones
monophones = {}
for symbol in arphabet
monophones[symbol] = file.loadSound("monophones/" + symbol + ".wav")
end for
namesMap = {}
// load CMU dictionary data
cmuDictNames = file.readLines("cmudict.names")
cmuDictValues = file.readLines("cmudict.values")
_initNamesMap = function()
print "Initialization, please wait..."
startTime = time
for i in range(0, cmuDictNames.len - 1)
namesMap[cmuDictNames[i]] = i
end for
print "Init time: " + (time - startTime)
end function
_initNamesMap
print cmuDictNames.len + " words is loaded!"
printHelp = function()
print ""
print "Type command 'say' and type the text in quotation marks."
print "You can optionally specify the speed of speech."
print "Examples:"
print ""
print "say ""Hello world"" "
print "say ""I am virtual computer my name is Mini Micro"" "
print "say ""I am Robocop"", 0.8"
print "say ""I am IRON MAN"", 0.75"
print "say ""Chip 'n Dale: Rescue Rangers"", 1.7"
end function
printHelp
// synthesizes speech
say = function(sentence, speed = 1)
words = sentence.split(" ")
for word in words
_sayWord word, speed
wait 0.1 / speed
end for
end function
// find word in dictionary and play phonemes
_sayWord = function(word, speed)
word = _getPreparedWord(word)
if namesMap.hasIndex(word) then
index = namesMap[word]
msgPhones = cmuDictValues[index].split(" ")
print word + " = " + msgPhones
_playPhonemes msgPhones, speed
else
print "Word [" + word + "] not found!"
end if
end function
// clearing sentences from punctuation marks and others
_getPreparedWord = function(word)
word = word.lower
word = word.replace("-", " ")
for symbol in excludedSymbols
word = word.remove(symbol)
end for
return word
end function
// play phonemes
_playPhonemes = function(phonemes, speed)
for sound in phonemes
phone = monophones[sound]
phone.play 1, 0, speed
// make sounds in the word more cohesive
wait phone.duration / speed * 0.9
end for
end function
Speech_04.minidisk