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!

  • $\begingroup$ Quit[] has no effect $\endgroup$ – M.R. Aug 22 '12 at 20:04
  • $\begingroup$ 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. $\endgroup$ – programming_historian Aug 22 '12 at 20:21
  • $\begingroup$ 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... $\endgroup$ – rm -rf Aug 22 '12 at 21:02
  • 11
    $\begingroup$ Have you tried ShutUp[]? $\endgroup$ – acl Aug 23 '12 at 17:44
  • $\begingroup$ Reminds me of the render queue, it too is uninterruptable. $\endgroup$ – rcollyer Aug 23 '12 at 20:36

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


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:

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.

  • 6
    $\begingroup$ Oy, first class spelunking! $\endgroup$ – Yves Klett Aug 23 '12 at 19:54
  • $\begingroup$ 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 $\endgroup$ – Mr.Wizard Aug 24 '12 at 8:02
  • $\begingroup$ @Mr.Wizard Is it a V7 issue or a Windows/Mac issue? (I'm running Mac OS X) $\endgroup$ – JxB Aug 24 '12 at 14:58
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard Aug 24 '12 at 15:07
  • 3
    $\begingroup$ @Mr.Wizard However, it is very useful. Try Speak[Array["I should migrate to v8"&, 10]] $\endgroup$ – Dr. belisarius Aug 28 '12 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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