Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Window problemes [ruby]
- - By grifdail Date 2011-11-23 19:13
Hi
I  still a begginer in ruby
But now it a more inportant request: it for a scool project!

I'm trying to make a very simple AI, for that I want to use espeak, but when I want to use

system('espeak "My sentence" ')

That work, espeak say the word but, while the TtS programe is speaking, the gosu window freeze which is very annoying.
I think (I can be wrong) that it's because, during the "speech", the window is considered as "Not visible" and so, it does not update.

So if you have a fix...
Thanks
Oh sorry for my english, I'm french!
Parent - - By Jwosty Date 2011-11-24 04:51
Could you please attach your full program? It would then be much easier to debug... xP
–––––
I might know why it's doing this. Is that system('espeak... thing the only line of code you use for the text to speech? If so, it might not exit until it is finished talking. There is a very simple fix for this, and I'll tell you if this turns out to be your problem. :)
Parent - - By grifdail Date 2011-11-24 13:24
require "Gosu"
require "Texplay"
load "Emotion cadrant.rb"

class Alpha < Gosu::Window
 
  def initialize
    super(1024,768,false)
  end
 
  def start
    $screen = Screen.new(0,448)
    @val = 0
    @back = Gosu::Image.new($window,"fond.png",false)
    show
  end
 
  def update
    $screen.speak("Bonjour") if @val == 50
    Kernel.system('espeak -vfr "bonjour"') if @val == 50 ########
    @val += 1
  end
 
  def draw
    @back.draw(0,0,0)
    $screen.draw
  end
end

$window = Alpha.new
$window.start

First you mays know that $screen.speak draw an animation of a face speaking and I use @val to follow the evolution of the code.
Yes it the only line taht I use. and yes you're right : it dont existe until it finished.
Parent - - By Jwosty Date 2011-11-24 16:34
Ah, then there's the problem. The app hangs because while espeak is talking, your program temporarily stops updating the app. To fix this, you could thread it like so:

def update
  thread = Thread.fork do
    # Changed this part a bit to remove a duplicate for you
    if @val == 50
      $screen.speak("Bonjour")
      Kernel.system('espeak -vfr "bonjour"')
    end
  end
  thread.run
  @val += 1
end


This posees on problem though: It might start speaking before it's finished. But I think you can figure that part out pretty easily. ;)

Hope this helps!
Parent - - By erisdiscord Date 2011-11-24 16:51
Whoops, you beat me to it.  I wrote up my post a while ago and then forgot to post!
Parent - By Jwosty Date 2011-11-24 16:55
lol xD
Parent - - By erisdiscord Date 2011-11-24 16:51
Yeah, system waits for the program being called to finish, so your program is hanging there because it's waiting for espeak. If you want to let espeak run in the background and not wait for it to finish, there are a couple of ways to do it, but here's one:

# create a new child process
pid = fork do
  # call espeak. exec never returns, so the child process effectively ends here.
  exec('espeak', 'bonjour')
end

# detach the child process; we aren't interested in waiting for it.
Process.detach pid


There is also an espeak gem. It looks like it can only record to a file, but you could create a temporary file and then load it as a Gosu::Sample. That's probably too much if you're just starting out, though. The solution above is far easier. :Ɔ
Parent - - By Spooner Date 2011-11-24 20:04
Using the gem to record a tmp file would work on Windoze though, since we windowed dolts can't fork for love nor money :D Bloody Microsoft :(
Parent - - By erisdiscord Date 2011-11-24 20:11
Fork doesn't work on Windows? How do you run a child process without waiting for it to return?

Oh, derp, Jwosty's solution would work on Windows wouldn't it.
Parent - - By Spooner Date 2011-11-24 20:21
Nope. The only way to fork is to delete Windows and install Linux (All fork-related things in Ruby just raise errors in our backwards OS because there is simply no equivalent on Windows OS). You can create an entirely new process from fresh, but you cannot fork one.

However, I don't think the original poster has to worry about Windows compatibility, so we don't need to worry either. Just throwing my own two-penneth in and a bit of a moan :D
Parent - - By erisdiscord Date 2011-11-24 20:24
Well, my impression is that system(…) works on Windows as do threads, which is why I assume Jwosty's answer would work there, but I could be mistaken. C:

I freely admit that I haven't used Windows in earnest since 2005, possibly even longer. That's when I got my first Mac, but I know I used Linux a lot off and on before then, so I dunno. I'm out of the loop!
Parent - By Spooner Date 2011-11-24 20:43
Ah, yes, I thought Thread.fork was just a side-ways entry to regular forking, not creating a thread in a normal way. Within Gosu, however, the thread created is almost certainly going to be starved by the Gosu thread, so I am doubtful that it will play the sound correctly, though if Gosu isn't being taxed it might work (do give it a try). That is why saving the file and loading it as a Sample is possibly the best plan for a GUI app/game.
Parent - - By grifdail Date 2011-11-24 21:56
Thanks to you guy.
I wander why I have'nt think about that?
I use the first solution with some change because I missunderstood the Jwosty question. no, this "bot" will say very various thing, in different part of the programe.

This how my tread look.
$espeak = Thread.fork {
    loop do
      if $ttstext != ""
        sentenc = 'espeak -vfr+m3 "' + $ttstext + '"'
        $ttstext.clear
        Kernel.system(sentenc)
      end
    end}

then I just have to switch $ttstext to make the string a speach

But now I have another strange probleme which is nearly unrelated...
When I launch my code from "Aptana" the whole thing work great (exepte for the espeak's window that open sudently over the Gosu::Window)
But when I want to launch it  by clicking on "main.rb" file (through the window's explorer), I can't hear any sound?
It happen a few time before You give me the answer so it can't came from it.
Thank !
Parent - - By RavensKrag Date 2011-11-25 15:53
It might be because when you launch through your IDE it automatically switches to the proper directory before starting execution, while clicking on the file does not.  I haven't used ruby on Windows very much though, so I can't say for sure.  I have had that problem on linux though.

Add this to the top of main.rb to switch directories.
Dir.chdir File.dirname(__FILE__)
Parent - - By grifdail Date 2011-11-25 19:14
Thank !
All Is working!
Parent - By Jwosty Date 2011-11-26 22:28
Awesome!
Up Topic Gosu / Gosu Exchange / Window problemes [ruby]

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill