Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to use an event handler to stop speaking:

Speak[ExampleData[{"Text", "DonQuixoteISpanish"}]]

The only way I can stop this is by restarting mathematica!

share|improve this question
    
Quit[] has no effect –  M.R. Aug 22 '12 at 20:04
    
Fascinating - and if you execute something like Speak["test test test"]; Print["hi"], it executes the Print command before the first word is even spoken. –  programming_historian Aug 22 '12 at 20:21
    
Yeah, it looks like everything is pushed to a queue that is then inaccessible. Quit[] won't have any effect because all of this is done by the FE (which is why you have to quit Mathematica to make it stop). I wonder if there is some hidden/undocumented token that can be used with FrontEndExecute... –  rm -rf Aug 22 '12 at 21:02
9  
Have you tried ShutUp[]? –  acl Aug 23 '12 at 17:44
    
Reminds me of the render queue, it too is uninterruptable. –  rcollyer Aug 23 '12 at 20:36

1 Answer 1

up vote 17 down vote accepted

Running Trace[Speak["Hello"]] and Names["*Speak*"] revealed the following possibility:

MathLink`CallFrontEnd[CurrentlySpeakingPacket]

Using this with a text that is split into a list of shorter strings allows you to interrupt the audio at well-defined points, phrase breaks, say. Here is one way to do it:

Clear[interruptibleSpeak]; 
interruptibleSpeak[text_String] := Module[{phrases}, 
   phrases = StringSplit[
      StringReplace[text, (# -> # <> "\n") & /@ {".", ";", "!", "?"}], "\n"];
   Do[While[MathLink`CallFrontEnd[CurrentlySpeakingPacket], Pause[0.1]];
     Speak[phrases[[i]]], {i, Length@phrases}]
]

You can now interrupt the computer in the usual way (Command. on Mac, Alt. on PC):

interruptibleSpeak[ExampleData[{"Text", "DonQuixoteISpanish"}]]

Now, of course, you do not get asynchronous speaking, so subsequent evaluations have to wait for the last phrase to be queued to execute.

Note This solution was tested on version 8.04, and is reported not to work on version 7.

share|improve this answer
6  
Oy, first class spelunking! –  Yves Klett Aug 23 '12 at 19:54
    
Nuts! This doesn't work on version 7 and I have a session open I don't want to close out at the moment. Mute for however long DonQuixoteISpanish is. :-p –  Mr.Wizard Aug 24 '12 at 8:02
    
@Mr.Wizard Is it a V7 issue or a Windows/Mac issue? (I'm running Mac OS X) –  JxB Aug 24 '12 at 14:58
    
I don't know, I haven't fiddled with it again. I don't think I've ever had a practical application for Speak so this is not a high priority, but I'll try to return to this later to see if it can be mended for v7. –  Mr.Wizard Aug 24 '12 at 15:07
3  
@Mr.Wizard However, it is very useful. Try Speak[Array["I should migrate to v8"&, 10]] –  belisarius Aug 28 '12 at 3:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.