Speech example

Run in landscape mode. Change str to whatever you want to be said. Select a language and a voice then press PLAY. This doesn’t seem to work right every time, so I’m not sure if I’m doing something wrong or it’s not working the way I think it should.

displayMode(FULLSCREEN)

function setup()
    textMode(CORNER)
    
    str="mary had a little lamb"
    
    tab1,tab2={},{}
    lang={"ar-SA","cs-CZ","da-DK","de-DE","el-GR","en-AU","en-GB",
            "en-IE","en-US","en-US","en-ZA","es-ES","es-MX","fi-FI",
            "fr-CA","fr-FR","he-IL","hi-IN","hu-HU","id-ID","it-IT",
            "ja-JP","ko-KR","nl-BE","nl-NL","no-NO","pl-PL","pt-BR",
            "pt-PT","ro-RO","ru-RU","sk-SK","sv-SE","th-TH","tr-TR",
            "zh-CN","zh-HK","zh-TW",""}
    cnt=0
    for x=1,3 do
        for y=1,13 do  
            cnt=cnt+1         
            table.insert(tab1,button(1,90*x,HEIGHT-y*45,lang[cnt],lang[cnt],""))
            vo=string.format("%s",speech.voices[cnt]) 
            table.insert(tab2,button(2,250+180*x,HEIGHT-y*45,string.sub(vo,18,35),"",speech.voices[cnt]))
        end
    end
    ll,vv="",""
end

function draw()
    background(0)
    for a,b in pairs(tab1) do
        b:draw()
    end
    for a,b in pairs(tab2) do
        b:draw()
    end  
    fill(255)
    text("Language",175,150)
    text("Voice",700,150)
    text(ll.."    "..vv,380,150)
    rect(400,50,100,50)
    fill(255, 0, 0, 255)
    text("PLAY",430,65)
    if play then
        play=false
        speech.say(str)
    end
end

function touched(t)
    background(0)
    if t.state==BEGAN then
        for a,b in pairs(tab1) do
            b:touched(t)
        end
        for a,b in pairs(tab2) do
            b:touched(t)
        end  
        if t.y<120 then
            play=true
        end
    end
end

button=class()

function button:init(type,x,y,t,l,v)
    self.type=type
    self.x=x
    self.y=y
    self.w1=80
    self.h1=35
    self.w2=175
    self.h2=35
    self.language=l
    self.voice=v
    self.text=t
    self.sel=false
end

function button:draw()
    if self.type==1 then
        if self.sel then
            fill(0, 225, 255, 255)
        else
            fill(255)
        end
        rect(self.x,self.y,self.w1,self.h1)
        fill(0)
        text(self.text,self.x+15,self.y+10)
    else
        if self.sel then
            fill(0, 225, 255, 255)
        else
            fill(255)
        end
        rect(self.x,self.y,self.w2,self.h2)
        fill(0)
        text(self.text,self.x+15,self.y+10)
    end
end

function button:touched(t)
    if self.type==1 then
        if t.x>self.x and t.x<self.x+self.w1 and
                t.y>self.y and t.y<self.y+self.h1 then
            for z=1,#tab1 do
                tab1[z].sel=false
            end
            self.sel=true
            ll=self.language
            speech.language=self.language
        end
    end
    if self.type==2 then
        if t.x>self.x and t.x<self.x+self.w2 and
                t.y>self.y and t.y<self.y+self.h1 then
            for z=1,#tab2 do
                tab2[z].sel=false
            end
            self.sel=true
            vv=self.text
            speech.voice=self.voice
        end
    end
end

This is neat. I think that setting the language and setting the voice seem to do the same thing. Perhaps language just picks the default voice for that language.
The example in the documentation says that setting speech.voice overrides the speech.language setting. In running this, it seems that setting language also overrides voice

@dave1707 - waoooh found it it was you !!!

@Bri_G I write so many examples that I forget what I do. Glad you found it.