11
$\begingroup$

I made a series of interval workouts based on Speak[], where the voice tells me when the intervals start, when they stop and how much time I have left to prepare. For instance, this is a Tabata 8 x 20/10 interval:

rep[extra___String] :=
 {Speak["Go! " <> extra <> " interval"];
  Pause[15]; Speak["Five"]; Pause[5]; Speak["Stop"]; Pause[5]; Speak["Five"]; Pause[5];}

{rep["first"], rep["second"], rep["third"], rep["fourth"], rep["fifth"], rep["sixth"], rep["seventh"], rep["last"]};

I normally use them for push ups, burpees etc... at home with my laptop open. But I would like to convert them to mp3 to bring it to the gym as well. Is there any way of converting the output of Speak[] to mp3?

$\endgroup$
3
  • 4
    $\begingroup$ A New Kind of Fitness $\endgroup$ Commented Feb 24, 2015 at 18:16
  • 1
    $\begingroup$ What OS are you using? There are applications that can hijack application audio output and record it, or route it to another application that can record. Under OS X, for example, you can use rogueamoeba.com/audiohijack $\endgroup$ Commented Feb 24, 2015 at 23:50
  • $\begingroup$ I use OS X, I'm looking into the link you posted $\endgroup$
    – Ziofil
    Commented Feb 25, 2015 at 5:22

2 Answers 2

4
$\begingroup$

Update:

These are some interesting infos since my previous post:

  1. Contrary to what I thought and wrote in the first version of this answer, Speak can handle pauses inside a text and many other features to fine tune the speech (see this recent post for more infos).

  2. The commands to fine tune the speech (like pause, speed of the speech, ...) depend on your OS because Mathematica actually sends the text to your OS specific speech engine. It is probably why it is not documented in Mathematica (but it could be because there are some simple options which are in common).

  3. On OSX, I found the command to run directly the speech engine : this is the say command. It is particularly interesting as you can also choose the voice, ... and it can directly save the speech in a file and into different formats (mp4, wav, ... but however no mp3). Mathematica can run this external program to get exactly what the OP wanted (see below).

  4. On Windows OS, most proably you can do the same thing as there seems to be a similar text to speech command line system program or at least there are alternative programs (see this SO post), but I couldn't test.

  5. The text to speech internet API (http://tts-api.com/) that I used in my first answer and which i found in an example inside Mathematica documentation, seems clearly to handle only OSX type control commands to fine tune the speech ... Also, I noticed that it produces good quality and small size mp3 files.

Application to the OP`s problem

As previously :

rep[extra___String] := {speak["Go! " <> extra <> " interval"],
  pause[15], speak["Five"], pause[5], speak["Stop"], pause[5], 
  speak["Five"], pause[5]}

then

gym = {rep["first"], rep["second"]};

gymString = 
 gym /. {speak -> Sequence, 
     pause[t_] :> " [[slnc " <> ToString[t*1000] <> "]] "} // 
   Flatten // StringJoin @@ # &

To Play :

URLExecute["http://tts-api.com/tts.mp3", {"q" -> gymString}]

To Save directly into a mp3 :

Directory[]
URLSave["http://tts-api.com/tts.mp3", "gym.mp3", "Parameters" -> {"q" -> gymString}]

For OS X only ************** :

To Play :

Speak@gymString

Run["say -v Zarvox "<>gymString]

To Save :

Run["say -o gym.mp4 -v Zarvox "<>gymString]


Previous

Actually, the documentation almost tells you how to do it ;)

I just came across an interesting example in V10 documentation which shows how to play directly in the notebook a mp3 synthesized speech retrieved from an internet free API service.

Let's try that :

mysound = 
 URLExecute["http://tts-api.com/tts.mp3", {"q" -> "I like gym"}]

enter image description here

Then, you can check that it can be exported as an mp3 file like this Export["filename.mp3",mysound], but to prevent the import then export you could also directly write :

URLSave["http://tts-api.com/tts.mp3", "gymDirect.mp3", "Parameters" -> {"q" -> "I like gym"}]

By the way, you'll notice that Mathematica has strangely the same "voice" (at least on my Mac OSX)

Speak["I like gym"]

(it looks that on Mac, Mma uses a OSX local library (speech synthesis framework) to perform that.

Unfortunately, the internet synthesizer does not understand "pauses" (like Speak) otherwise you could have just sent a request with all your gym text.

This is not really a problem, to include the pauses we'll have to tweak a little bit.

Gym example

(Here I take your original code and just change Speak->speak, Pause->pause, so you can use it as before to create your sequences)

rep[extra___String] := {speak["Go! " <> extra <> " interval"],
  pause[15], speak["Five"], pause[5], speak["Stop"], pause[5], 
  speak["Five"], pause[5]};

Let's create a gym sequence example (shorter than yours for the purpose of the test) :

gym = {rep["first"], rep["second"]};

then some processing :

gymText = 
  Flatten[gym] /. speak[x_String] :> speak /@ StringSplit[x] // Flatten;

gymWord2Sound = 
  Rule[#, URLExecute["http://tts-api.com/tts.mp3", {"q" -> #}] // 
      First] & /@ Union[Cases[gymText, speak[str_] -> str]];

gymSound = 
 Sound[gymText /. {speak[str_] -> str, pause[t_] -> SoundNote[None, t]} /. gymWord2Sound]

enter image description here

Then to export the whole sequence (including the pauses) as a mp3 :

Directory[]
Export["gym.mp3", gymSound]

(You can also add mp3 specific export options in order to set the compression of the file )

Good luck !

$\endgroup$
4
  • $\begingroup$ Of course, this is not really an answer but a workaround. Instead of using synthesized speech, you could also record by yourself (with your own voice) each "keywords" ("Go","First",...) separately in files, then use them in the exact same way than i did here to construct a sound sequence. $\endgroup$
    – SquareOne
    Commented Mar 9, 2015 at 20:03
  • $\begingroup$ Evaluating URLExecute["http://tts-api.com/tts.mp3", {"q" -> "I like gym"}] gives me a 404 error. Is this service down or is there an issue using this from v10.1 under Windows? $\endgroup$
    – Mr.Wizard
    Commented May 22, 2015 at 12:31
  • $\begingroup$ @Mr.Wizard It seems there is a problem with the service: i checked directly the website (tts-api.com) and it gives the same 404 error ... $\endgroup$
    – SquareOne
    Commented May 22, 2015 at 12:55
  • 1
    $\begingroup$ @Mr.Wizard It seems the service is back. The URLExecute works OK for me with OSX. $\endgroup$
    – SquareOne
    Commented Jun 1, 2015 at 13:53
5
$\begingroup$

This is probably not the best idea, but there is a post by Szabolcs that describes some undocumented features of record sound. You can string these together to play the sound, record it back, then save to a file. First, here is a shortened version of your rep function, followed by the "start record", then the call to rep.

rep[extra___String] := Speak["Go! " <> extra <> " interval"]; 
FrontEndExecute[FrontEnd`RecordSound[1, 0, 0]]; (* start recording *)
rep["twenty"]

After the sound has stopped, you can end recording and save to the variable snd:

FrontEndExecute[FrontEnd`RecordSound[2]];       (* end recording *)
snd = FrontEndExecute[FrontEnd`RecordSound[3]]  (* save to sound format *)

Then export to an mp3 file

Export["test.mp3", snd]

If you wish to remove the room ambiance, don't use the computer's speaker and microphone: instead, connect to an I/O that let's you connect a wire from line out to line in. Then you'll get a much better recording.

Note the warning at the end of Szabolcs message: "use at your own risk."

$\endgroup$
6
  • 3
    $\begingroup$ ...and always consult a physician (or is that physicist?) before starting a Mathematica-based fitness program? $\endgroup$ Commented Feb 24, 2015 at 19:13
  • $\begingroup$ Well, I happen to be a physicist... ;) $\endgroup$
    – Ziofil
    Commented Feb 24, 2015 at 19:43
  • $\begingroup$ Okay, I tried this method. But it records from the built-in microphone, so all ambient sounds are captured too and I have to wait for the whole monologue to be uttered... $\endgroup$
    – Ziofil
    Commented Feb 24, 2015 at 19:57
  • $\begingroup$ That's right... Mathematica is off-loading the sound generation to the OS, so it can't just save it directly. In this sense, this is just a workaround. $\endgroup$
    – bill s
    Commented Feb 24, 2015 at 23:11
  • $\begingroup$ @bills, fails on OSX and 10.3.1: "CompiledFunction::cfne: Numerical error encountered; " $\endgroup$ Commented Jan 22, 2016 at 4:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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