Just for a laugh ...

All,
Found this while I was taking a break from a bit of heavy coding. Thought I’d post up - but, tried to correlate printing up the emoji images in time with the text - no joy. So if any of you know the trick - please add to the code.


-- FlySwallowing

function setup()
    --
    fontSize(64)
    fill(255, 255, 255, 255)
    poem()
end

function draw()
    --
    
end

function poem()
    --
    for sp = 0,7 do
        speech.say("There was an old lady who swallowed a "..animals[sp+1])
        if sp > 0 then speech.say(phrases[sp+1]) end
        if sp == 7 then break end
        if sp > 0 then
            for j=sp,1,-1 do
                speech.say("She swallowed the "..animals[j+1].." to catch the "..animals[j])    
                if j==2 then
                    speech.say(phrases[2])
                end
            end
        end 
        speech.say("I don't know why she swallowed a fly - Perhaps she'll die!")
    end
end

animals = {"fly", "spider", "bird", "cat","dog", "goat", "cow", "horse"}
phrases = {
    "",
    "That wriggled and jiggled and tickled inside her",
    "How absurd to swallow a bird",
    "Fancy that to swallow a cat",
    "What a hog, to swallow a dog",
    "She just opened her throat and swallowed a goat",
    "I don't know how she swallowed a cow",
    "  ...She's dead of course"
}

emojis = {}

Emojis didn’t show up but they are fly, spider, bird, cat, dog, goat, cow, horse- all accessible from Codea emojis (note bird is a parrot!!!).

@Bri_G Maybe this is the time for you to play around with co-routines. In order to show the emojis, you need to break out of the poem function and display the specific emoji in the draw function before continuing with the poem.

@dave1707 - thanks, was looking for a good reason to pursue co-routines. Not exactly a good example - will look over the one set up by @LoopSpace in my other thread.

@Bri_G Here’s a simple example of coroutines. Let this run for a second and then pause it and look at what prints. If we didn’t use coroutines, the function xx() would run the for loop 1,000,000 times before going back to draw. With coroutines, we break the for loop after every count of z and return to draw to print then it resumes the for loop in xx() before yielding again.

function setup()
    cr1=coroutine.create(xx)
end

function draw()
    background(40, 40, 50)
    print("draw function called")
    print("we can do draw stuff here")
    print("going to resume coroutine cr1")
    coroutine.resume(cr1)     
end

function xx()
    for z=1,1000000 do
        j=math.sqrt(z)
        print("continuing cr1 where we left off")
        print("sqrt of "..z.." is "..j)
        print("yield back to draw function")
        coroutine.yield()
    end
end

@Bri_G I don’t think coroutines are going to work on your program. I think the speech function buffers everything up and plays it back at the speech speed. When I played around with it, the poem function was done while the speech function was still saying the first sentence.

@Bri_G I think the only way you can sync the emojis with the speech is to modify the code so it only does one speech.say at a time. You can use speech.speaking to wait for the speech to end then go on to the next speech.

@Bri_G This isn’t perfect, but it’ll give you something to work on. The speech is overlapping at some points. Also, the emojis mess up the code, so replace the emoji words in the table with actual emoji pictures.

-- FlySwallowing

function setup()
    displayMode(FULLSCREEN)
    fontSize(200)
    fill(255)
    emojis = {"fly","spider","bird","cat","dog","goat","cow","horse"}
    cr1=coroutine.create(poem)
end

function draw()
    background(223, 201, 172, 255)
    if not speech.speaking then
        coroutine.resume(cr1)
    end
    text(emojis[sp1],WIDTH/2,HEIGHT/2)
end

function poem()
    for sp = 0,7 do
        sp1=sp+1
        speech.say("There was an old lady who swallowed a "..animals[sp+1])
        coroutine.yield()
        if sp > 0 then 
            sp1=sp+1
            speech.say(phrases[sp+1]) 
            coroutine.yield()
        end
        if sp == 7 then 
            break 
        end
        if sp > 0 then
            for j=sp,1,-1 do
                sp1=j+1
                speech.say("She swallowed the "..animals[j+1])
                coroutine.yield()
                sp1=j
                speech.say(" to catch the "..animals[j])
                coroutine.yield()
                if j==2 then
                    speech.say(phrases[2])
                    coroutine.yield()
                end
            end
        end 
        speech.say("I don't know why she swallowed a fly - Perhaps she'll die!")
        coroutine.yield()
    end
end

animals = {"fly", "spider", "bird", "cat","dog", "goat", "cow", "horse"}
phrases = {
    "",
    "That wriggled and jiggled and tickled inside her",
    "How absurd to swallow a bird",
    "Fancy that to swallow a cat",
    "What a hog, to swallow a dog",
    "She just opened her throat and swallowed a goat",
    "I don't know how she swallowed a cow",
    "  ...She's dead of course"
}

@dave1707 - superb, fiddled with it a little. I had tried to use speech.speaking before in my original code but couldn’t get it working - now learnt a bit more on coroutines thanks.


function setup()
    displayMode(FULLSCREEN)
    fontSize(200)
    fill(255)
    oldLady = oldLady"
    font("Arial-BoldMT")
    emojis = {“fly", "spider", "bird", "cat","dog", "goat", "cow", "horse"}
    animals = {"fly","spider","bird","cat","dog","goat","cow","horse"}
    cr1=coroutine.create(poem)
end

function draw()
    background(172, 192, 222, 255)
    fill(253, 253, 253, 255)
    text(oldLady,WIDTH//2,HEIGHT-180)
    if not speech.speaking then
        coroutine.resume(cr1)
    end
    text(emojis[sp1],WIDTH/2,HEIGHT/2)
    fill(134, 34, 34, 255)
    text(animals[sp1],WIDTH//2,200)
end

function poem()
    for sp = 0,7 do
        sp1=sp+1
        speech.say("There was an old lady who swallowed a "..animals[sp+1])
        coroutine.yield()
        if sp > 0 then 
            sp1=sp+1
            speech.say(phrases[sp+1]) 
            coroutine.yield()
        end
        if sp == 7 then 
            break 
        end
        if sp > 0 then
            for j=sp,1,-1 do
                sp1=j+1
                speech.say("She swallowed the "..animals[j+1])
                coroutine.yield()
                sp1=j
                speech.say(" to catch the "..animals[j])
                coroutine.yield()
                if j==2 then
                    speech.say(phrases[2])
                    coroutine.yield()
                end
            end
        end 
        speech.say("I don't know why she swallowed a fly - Perhaps she'll die!")
        coroutine.yield()
    end
end

phrases = {
    "",
    "That wriggled and jiggled and tickled inside her",
    "How absurd to swallow a bird",
    "Fancy that to swallow a cat",
    "What a hog, to swallow a dog",
    "Just opened her throat and swallowed a goat",
    "I don't know how she swallowed a cow",
    "  ...She's dead of course"
}

Made a few trivial changes, added a grandma emoji and text - hoping to make it a learning tool in reading for my grandkids.

Couple of things - doesn’t seem to be a simple learning font for reading in the Apple package - particularly with characters like ‘a’. Also do you know of a way to make the voice more lyrical?

Thanks again.

P.s. need to edit the emojis table again - text to image.

@dave1707 - take your point on the coroutines not being perfect. Thought you may be able to change a variable in the coroutine which could be picked up in the draw() function to print up the text. Queueing of the speech is obviously a problem - may be a call for another variable/trigger added to the speech commands to help sync it better.

I originally intended just putting up a ‘list’ of the animals accumulating vertically as the poem progresses but your approach is much better.

@Bri_G You can change the voice by setting different parameters. Look in the reference under speech.say for all the different settings you can make.

If you’re playing with the little coroutines example I show above, you can change when you yield by adding code such as

if z%10000 == 0 then
     coroutines.yield()
end

That will yield every 10,000 count instead of every count. So depending on the code, you can yield whenever you want.

@dave1707 - changed the speech.rate and speech.pitch variables and can improve it a little. Digging around to find a project where someone used it in different languages and I thought voices (probably the pitch and rate variations). Should be able to find it and may get a few hints. Thanks again.

@dave1707 etc - for all those interested added the following code to the setup() function to get a better voice (well … one I find better).


    speech.pitch = 1.30
    speech.rate = 0.52
    speech.language = "en-GB"
    speech.voice = speech.voices[20]